Consume an RSS Feed with .NET 3.5

.NET 3.5 added some new classes that really help when dealing with RSS/ATOM feeds.  These classes are located in the System.ServiceModel.Syndication namespace in the System.ServiceModel.Web.dll library.  The SyndicationFeed class in this namespace can be used to both expose and consume a feed.

Today I was attempting to create a clone of my feed on a website that I am toying around with.  After doing some research on the System.ServiceModel.Syndication namespace, I found that this is extremely simple to do in 3.5.  The following code illustrates just how simple it can be by using the static Load method of SyndicationFeed:

    1: // Blog feed
    2: SyndicationFeed blogFeed;
    3: try
    4: {
    5:     // Read the feed using an XmlReader
    6:     using (XmlReader reader = XmlReader.Create("https://someblog/feed.xml");
    7:     {
    8:         // Load the feed into a SyndicationFeed
    9:         blogFeed = SyndicationFeed.Load(reader);
   10:     }
   11: }
   12: catch (Exception ex)
   13: {
   14:     if (ex is WebException || ex is XmlException)
   15:     {
   16:         // Handle bad url, timeout or xml error here.
   17:     }
   18:     else
   19:         throw;
   20: }
   21:  
   22: // Use the feed
   23: foreach (SyndicationItem item in blogFeed.Items)
   24: {
   25:     Console.WriteLine(item.Title.Text);
   26: }