【问题标题】:XML to LINQ with Digg API使用 Digg API 的 XML 到 LINQ
【发布时间】:2011-10-19 16:09:29
【问题描述】:

继续我之前的问题(http://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight,感谢您的回答)我现在有一个错误较少的后续问题。

为了从this 中提取数据,我们得到了以下 XML to LINQ 代码。

    var stories = from story in document.Descendants("story")
                  //where story.Element("thumbnail") != null
                  select new DiggStory
                  {
                      Id = (string)story.Attribute("story_id"),
                      Title = (string)story.Element("title"),
                      Description = (string)story.Element("description"),
                      ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
                      //HrefLink = (string)story.Attribute("permalink"),
                      NumDiggs = (int)story.Attribute("diggs")
                  };

这按预期工作,但鉴于 Digg API 已过时,我更愿意成为新的 api,它创建 the following XML file

现在我想知道如何调整 XML 到 LINQ 代码以利用这个新的 XML 文件? 我知道问题出在

   var stories = from story in document.Descendants("story")

但我不知道我需要将其更改为什么,因为new XML file 有更多级别。我在想一些事情

var stories = from item in document.Descendants("stories")

但这似乎行不通。

再次感谢您帮助我解决这个问题和任何其他问题,这真是一个很棒的网站!

谢谢你,托马斯

【问题讨论】:

    标签: xml linq api digg


    【解决方案1】:

    从这里开始:

    var stories = from item in document.RootElement.Element("stories").Descendants("item")
                  select new DiggStory
                  {
                      Id = item.Element("story_id").Value
                      // ...etc
                  }
    

    您可以通过LINQ to XML 了解有关解析 XML 文档的更多信息。对您特别有用的可能是XElement documentation,它显示了为 XML 文档中的每个“元素”(标记)定义的所有方法和属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多