Phone developers sometimes bemoan the fact that Silverlight for Windows Phone lacks syndication classes such as SyndicationFeed and SyndicationItem. In the desktop versions of Silverlight, these classes simplify the task of consuming RSS feeds, as well as other types of feeds such as ATOM.
It’s a well-documented fact that you can work around this by adding a reference to Silverlight 3’s System.ServiceModel.Syndication assembly to a phone project and use the syndication classes contained therein the same way you’d use them in a desktop Silverlight application. However, such tricks aren’t necessary. It’s just as easy to use LINQ to XML to parse syndication feeds, and doing so gives you more control over how the content is interpreted than the Silverlight syndication classes do. After all, every feed has its own quirks and nuances.
One of the RSS feeds I often use in the classroom is one that serves up top news stories from ABC News (http://feeds.abcnews.com/abcnews/topstories). Here is a code snippet that uses LINQ to XML to parse that feed and generate a collection of RssItems that can be bound to a ListBox or other items control:
using (StreamReader reader = new StreamReader(e.Result))
{
// Use LINQ to XML to extract RSS items
XDocument doc = XDocument.Parse(reader.ReadToEnd());
var items = from results in doc.Descendants("item")
select new RssItem
{
Title = results.Element("title").Value.ToString(),
Link = results.Element("link").Value.ToString(),
Description = results.Element("description").Value.ToString()
};
}
And here’s my RssItem class:
public class RssItem
{
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
}
Remember to add a reference to System.Xml.Linq to your project so LINQ to XML is available. Then let LINQ do the dirty work.