2011-10-19 37 views
2

继续从我以前的问题(http://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight,我想感谢你回答)我现在有一个跟进较少的错误问题。XML to LINQ与Digg API

要从this中提取数据,我们将以下XML转换为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") 

但这似乎不工作。

我想再次感谢你帮助我解决这个问题和任何进一步的问题,这真是一个很棒的网站!

谢谢,托马斯

回答

1

开始与此:

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

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