在Windows Phone 8中使用XDocument解析XML [英] Parse XML using XDocument in Windows Phone 8

查看:67
本文介绍了在Windows Phone 8中使用XDocument解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何使用Windows Phone 8中的XDocument解析这种格式的XML

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>

现在我正在使用此代码,但无法获取数据.我需要此XML中的图片和名称.

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

预先感谢

推荐答案

Elements仅返回当前元素的直接子元素(如果提供,则具有匹配的名称).因为<makes>不是根元素的直接子代,所以xdoc.Root.Elements("makes")将返回空集合.

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.

在调用Element("makes")之前在xdoc.Root上添加另一个Element("domain")调用.

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

这篇关于在Windows Phone 8中使用XDocument解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆