[英]how to get(read) data from xmldocument in windows phone, c#
我從電話應用程序中的Web服務獲取數據,並獲得對xmldocument
的響應,如下所示。
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(newx2);
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>
我嘗試了這個。
XmlNodeList xnList = XmlDoc.SelectNodes("/root[@*]");
但結果為空。 計數為0
。 我如何從this.thanx中讀取數據。
您可以獲取特定元素的值,例如,
var fareIndex = XmlDoc.SelectSingleNode("/root/itinerary/FareIndex").InnerText;
如果您想獲取root/itinerary
目錄下所有元素的列表,
XmlNodeList xnList = XmlDoc.SelectNodes("/root/itinerary/*");
您可以使用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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.