Categories: Blog

Silverlight for Windows Phone Programming Tip #2

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.

Jeff Prosise

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

2 weeks ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

3 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

1 month ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

2 months ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

2 months ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago