简体   繁体   English

如何从Windows Phone,C#中的xmldocument中获取(读取)数据

[英]how to get(read) data from xmldocument in windows phone, c#

I'm getting data from a web service in my phone application and get the response to xmldocument like below. 我从电话应用程序中的Web服务获取数据,并获得对xmldocument的响应,如下所示。

XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.LoadXml(newx2);

Ther result of XmlDoc is like below.now I want to get the values from this. XmlDoc结果如下。现在我想从中获取值。

<root>
    <itinerary>
    <FareIndex>0</FareIndex>
    <AdultBaseFare>4719</AdultBaseFare>
    <AdultTax>566.1</AdultTax>
    <ChildBaseFare>0</ChildBaseFare>
    <ChildTax>0</ChildTax>
    <InfantBaseFare>0</InfantBaseFare>
    <InfantTax>0</InfantTax>
    <Adult>1</Adult>
    <Child>0</Child>
    <Infant>0</Infant>
    <TotalFare>5285.1</TotalFare>
    <Airline>AI</Airline>
    <AirlineName>Air India</AirlineName>
    <FliCount>4</FliCount>
    <Seats>9</Seats>
    <MajorCabin>Y</MajorCabin>
    <InfoVia>P</InfoVia>
    <sectors xmlns:json="http://james.newtonking.com/projects/json">
</itinerary>
</root>

I tried with this. 我尝试了这个。

XmlNodeList xnList = XmlDoc.SelectNodes("/root[@*]");

but it gives null result. 但结果为空。 the count is 0 . 计数为0 how can I read the data from this.hope your help with this.thanx. 我如何从this.thanx中读取数据。

You can get the value of a particular element like, 您可以获取特定元素的值,例如,

 var fareIndex = XmlDoc.SelectSingleNode("/root/itinerary/FareIndex").InnerText;

If you want to get the list of all elements that come under root/itinerary - 如果您想获取root/itinerary目录下所有元素的列表,

  XmlNodeList xnList = XmlDoc.SelectNodes("/root/itinerary/*");

This link might help you. 此链接可能会对您有所帮助。

You can use System.Xml.Linq.XElement to parse an xml: 您可以使用System.Xml.Linq.XElement来解析xml:

XElement xRoot = XElement.Parse(xmlText);
XElement xItinerary = xRoot.Elements().First();
// or xItinerary = xRoot.Element("itinerary");

foreach (XElement node in xItinerary.Elements())
{
    // Read node here: node.Name, node.Value and node.Attributes()
}

If you want to use XmlDocument you can do like this: 如果要使用XmlDocument,可以执行以下操作:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlText);

XmlNode itinerary = xmlDoc.FirstChild;
foreach (XmlNode node in itinerary.ChildNodes)
{
    string name = node.Name;
    string value = node.Value;

    // you can also read node.Attributes
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中获取XmlDocument的数据 - Get data for XmlDocument in C# 如何使用C#Windows Phone 8.1从URL读取XML数据 - How to read XML data from a URL by using C# Windows Phone 8.1 如何从 Windows 7 上的 C# 程序读取 Android 手机上的文件? - How to read files on Android phone from C# program on Windows 7? 使用Windows Phone C从HTML页面获取数据 - get data from html page with windows phone c# 如何使用 XMLDocument 类从 C# 中的 XML 文件中获取数据? - How to get data from an XML File in C# using XMLDocument class? 在C#中如何从XMLDocument中选择特定数据? - in C# how to select specific Data from an XMLDocument? LongListSelector C#-如何从分组的数据列表中以字符串形式获取SelectedItem-Windows Phone Silverlight 8.1 - LongListSelector C# - How to get SelectedItem as string from grouped data list - Windows Phone Silverlight 8.1 如何通过Windows Phone的Webbrowser C#登录获取用户ID? - How to get userID from login on webbrowser c# for windows phone? 如何在C#Windows Phone中从JSON查询数据 - How to query data from json in c# windows phone 如何在 C# Windows 应用程序中从 xml 文件中获取/读取数据 - How to get/read data from the xml file in C# Windows application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM