XML to LINQ with Digg API

150 views Asked by At

Continuining from my previous question (http://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight, which I want to thank you for answering) I now have a follow up less errory question.

To extract the data from this we got the following XML to LINQ code.

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

This works as intended, but seeing as the Digg API is outdated I would prefer to be the new api, which creates the following XML file.

Now I was wondering how do I adjust the XML to LINQ code to utilize this new XML file? I know the problem is in

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

But I do not know what I need to change it to, because the new XML file has more levels. I was thinking something in the line of

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

But that doesn't seem work.

I want to thank you again for helping me with this problem and any further problems, this is truly a great website!

Thank you, Thomas

1

There are 1 answers

0
Matt Bridges On BEST ANSWER

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.