2016-06-14 9 views
0

私の電話アプリケーションのWebサービスからデータを取得しており、応答をxmldocumentに以下のように取得します。 XmlDocWindows Phoneのxmldocumentからデータを取得(読み込み)する方法C#

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

THER結果は、私がこれから値を取得したい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[@*]"); 

ただし、結果はnullです。カウントは0です。どうすればthis.hopeからのデータを読むことができますか.thanx。

+0

あなたが選択することが好きですか? 'Xpath'は少なくとも1つの属性を持つ要素を探しますが、xml要素には属性がありません。 –

答えて

0

あなたを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 
} 
0

あなたはroot/itinerary下に来るすべての要素のリストを取得したい場合は、のような

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

を特定の要素の値を取得することができます -

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

This link might help you.

関連する問題