2

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

为了从中提取数据,我们得到了以下 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,它创建以下 XML 文件

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

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

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

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

但这似乎行不通。

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

谢谢你,托马斯

4

1 回答 1

1

Start with this:

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

You can learn more about parsing XML documents with LINQ to XML. Of particular use to you is probably the XElement documentation, which shows all the methods and properties defined for each "element" (tag) in the XML document.

于 2011-10-21T00:55:36.537 回答