I just started my initiation to Silverlight and noticed while following the workshops of the MSDN Coachs program that a class XDocument was used to parse some Xml. So I thought at first “what’s that new class ? Why didn’t they use XmlDocument ?”.

Well, simply because it’s gone ! The reason of that, found on the forums of the official Silverlight website, is that because Linq to Xml is the recommended way to work with Xml in Silverlight, so System.Xml.XmlDocument was removed and is now succeeded by System.Xml.Linq.XDocument.

For those who’re new to Linq to Xml, here’s a quick example of what you can perform with that powerful technique :

1. The class, using auto-implemented properties

publicclassDiggStory

{

publicint Id { get; set; }

publicstring Title { get; set; }

publicstring Description { get; set; }

publicstring HrefLink { get; set; }

publicint NumDiggs { get; set; }

publicstring Thumbnail { get; set; }

}

2. Using Linq to Xml

XDocument xmlStories = XDocument.Parse(xmlContent);

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

where story.Element("thumbnail") != null

selectnewDiggStory

{

Id = (int) story.Attribute("id"),

Title = (string) story.Attribute("title"),

Description = (string) story.Attribute("description"),

Thumbnail = (string) story.Attribute("thumbnail"),

HrefLink = (string) story.Attribute("link"),

NumDiggs = (int) story.Attribute("diggs")

};

3. Conclusion

Advantages of this method is that :

It’s way more elegant than classic XPath strings

You get a collection of strongly typed items

It’s very easy to perform databinding on the basis of the item properties