Parse XML using XDocument in Windows Phone 8

南笙酒味 提交于 2019-12-11 20:44:00

问题


Can any one tell me how to parse XML which is in this format using XDocument in Windows phone 8

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

Right now i am using this code but unable to get the data out. I need image and name from this 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);
}

Thanks In Advance


回答1:


Elements returns only direct children of current element (with matching name, when provided). Because <makes> is not direct child of root element, xdoc.Root.Elements("makes") will return empty collection.

Add another Element("domain") call on xdoc.Root before calling Element("makes").

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);
}


来源:https://stackoverflow.com/questions/23745665/parse-xml-using-xdocument-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!