Consuming Feeds with the .NET Syndication API

One of the nicest (and very under-advertised) features to make it into .NET framework 3.5 was the new Syndication API (https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.aspx). The Syndication API provides .NET applications a great way to consume and publish RSS and Atom feeds. If you've been paying attention to Azure lately you will have realised that Atom forms a major part of Azure and Live services in the cloud. Syndication feeds (Atom/RSS) are fast emerging as the de facto way for applications to update interested parties about changes in state, news and any other updates. There's a good reason for this. Feeds allow consumers (that's you and me) to control when, where and how often to receive updates.

Without further ado, let me present some sample code to consume an RSS feed using the Syndication API:

XmlReader reader = XmlReader.Create("https://blogs.msdn.com/knowledgecast/rss.xml");
Rss20FeedFormatter rss = new Rss20FeedFormatter();
if (rss.CanRead(reader))

  rss.ReadFrom(reader); 
  var items = from item in rss.Feed.Items.OfType<SyndicationItem>() 
                   select new 
                   { 
                      Title = item.Title.Text, 
                      Url = item.Links.First<SyndicationLink>().Uri.AbsoluteUri 
                   };

  lstFeedTitles.DataSource = items; 
  lstFeedTitles.DataBind();
}

reader.Close();

We open an XmlReader with the RSS feed for this blog as URL. We then create an Rss20FeedFormatter (System.ServiceModel.Syndication.Rss20FeedFormatter) and use the CanRead method to verify that the feed loaded in our XmlReader is an RSS feed. The ReadFrom method reads the feed into the feed formatter.

Now things start to get really interesting. I use a clever LINQ query to slurp out the Title and Url of the post into a collection. Note that the objects in this collection all use an anonymouse type that is defined inside the LINQ query. We then bind the resulting collection (which should be an IEnumerable) to a DataList we have sitting in our ASPX page. Here's what our ASPX form looks like:

<form id="form1" runat="server">
    <div>
    <ul>
    <asp:DataList ID="lstFeedTitles" runat="server">
        <ItemTemplate>
         <li>
            <asp:LinkButton ID="lnkTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Title")%>' PostBackUrl='<%# DataBinder.Eval(Container.DataItem, "Url")%>' />
         </li>  
        </ItemTemplate>
    </asp:DataList>
    </ul>
    </div>
    </form>

And voila! You have your very own super simple RSS feed reader. It's almost too easy :) Here's what the rendered page looks like:

Silverlight 2: So Close I Can Taste It

Phew! It's Been a Busy Month

Encrypting Configuration Settings in .NET 2.0

Using VisualTransition with a Silverlight Content Control

Assigning a Name to a Silverlight Element

Writing a Silverlight Content Control

Microsoft SQL Server 2005 Express Edition with Advanced Services

Quick Guide to Silverlight 2 Beta 2

Quick Guide to Silverlight 2 Beta 1

Are you an aspiring architect?

Windows Mobile Power Savers

Windows XP Service Pack 3 released

Biztalk Server 2006 R3 announced

Paste As Text 1.0

TechReady 5

To get this code to work with a proxy server, you can run the XmlReader off a WebClient instead of a direct URL. Here's how you'd do that:

WebClient wc = new WebClient();
WebProxy proxy = new WebProxy("myproxy.corp.com", true);
wc.Proxy = proxy;
wc.Credentials = CredentialCache.DefaultCredentials;
Stream s = wc.OpenRead("https://blogs.msdn.com/knowledgecast/rss.xml");

XmlReader reader = XmlReader.Create(s);

//Syndication API code here