简体   繁体   中英

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.

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

Ther result of XmlDoc is like below.now I want to get the values from this.

<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 . how can I read the data from this.hope your help with 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 -

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

This link might help you.

You can use System.Xml.Linq.XElement to parse an 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 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM