如何从 windows phone 中的 xmldocument 获取(读取)数据,c#

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

我正在从我的 phone 应用程序中的 Web 服务获取数据,并收到对 xmldocument 的响应,如下所示。

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

XmlDoc 的结果类似于 below.now 我想从中获取值。

<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>

我试过这个。

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

但它给出了空结果。计数是 0。我如何从 this.hope 中读取数据 this.thanx。

您可以获得特定元素的值,例如,

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

如果你想得到属于root/itinerary的所有元素的列表 -

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

This link might help you.

您可以使用 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()
}

如果你想使用 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
}