簡體   English   中英

在Windows Phone 8中使用XDocument解析XML

[英]Parse XML using XDocument in Windows Phone 8

誰能告訴我如何使用Windows Phone 8中的XDocument解析這種格式的XML。

 <search total=""  totalpages="">
 <domain>
 <makes filter="">
 <make cnt="374" image="abc.png">One</make>
 <make cnt="588" image="bca">Two</make>
 <make cnt="105" image="tley.png">Three</make>
 <make cnt="458" image="mw.png">Four</make>
 </makes>
 </domain>
 </search>

現在,我正在使用此代碼,但無法獲取數據。 我需要此XML中的圖像和名稱。

XDocument xdoc = XDocument.Parse(flickRes);
var rootCategory = xdoc.Root.Elements("makes");
List<string> list = new List<string>();

foreach (XElement book in rootCategory.Elements("make"))
{
    string id = (string)book.Attribute("image");
    string name = (string)book;
    Debug.WriteLine(id);
    //list.Add(data);
}

提前致謝

Elements僅返回當前元素的直接子元素(具有匹配名稱,如果提供)。 由於<makes>不是根元素的直接子代,因此xdoc.Root.Elements("makes")將返回空集合。

在調用Element("makes")之前,在xdoc.Root上添加另一個Element("domain")調用。

XDocument xdoc = XDocument.Parse(flickRes);
var rootCategory = xdoc.Root.Element("domain").Element("makes");
List<string> list = new List<string>();

foreach (XElement book in rootCategory.Elements("make"))
{
    string id = (string)book.Attribute("image");
    string name = (string)book;
    Debug.WriteLine(id);
    //list.Add(data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM