【问题标题】:Parse XML using XDocument in Windows Phone 8在 Windows Phone 8 中使用 XDocument 解析 XML
【发布时间】:2014-05-19 19:24:54
【问题描述】:

谁能告诉我如何在 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);
}

提前致谢

【问题讨论】:

    标签: c# .net xml windows-phone-8 linq-to-xml


    【解决方案1】:

    Elements 仅返回当前元素的直接子元素(如果提供了匹配的名称)。因为&lt;makes&gt; 不是根元素的直接子元素,所以xdoc.Root.Elements("makes") 将返回空集合。

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2011-10-19
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      相关资源
      最近更新 更多