Tap the power of breath, mantra, mudra, and dhyana.
Online course in Advanced Ajapa Japa and Shambhavi Mudra Meditation by Bipin Joshi.

Untitled 1

Using Syndication Classes to Read RSS Feeds


RSS and ATOM feeds dominate the syndication systems today. If you run a web site you probably use RSS and ATOM for two purposes:

  • Expose your site content for syndication using these formats
  • Consume content exposed by other web sites and aggregate it on your web site

Whatever be the case until recently developers either coded their custom solution or made use of some third party component. Luckily .NET framework 3.5 introduced a set of classes that can simplify your job. This article explains how.

The Syndication Classes

The syndication classes of .NET 3.5 reside in System.ServiceModel.Syndication namespace. The System.ServiceModel.Syndication is physically contained in System.ServiceModel.Web assembly and you need to refer it before proceeding any further.

The System.ServiceModel.Syndication namespaces supplies the following main classes related to syndication:

  • SyndicationFeed
  • SyndicationItem
  • SyndicationContent
  • SyndicationLink
  • SyndicationPerson
  • SyndicationCategory

The SyndicationFeed class represents the entire feed from a particular URL. A feed typically consists of one or more items. Each feed item is represented by SyndicationItem class. Feed as well as feed items expose details such as description. These details are represented by SyndicationContent class. The links from a feed are represented by SyndicationLink class and authors and contributors by SyndicationPerson class. Finally, the items of a feed can be grouped using SyndicationCategory class.

The classes mentioned above are independent of feed format (RSS or ATOM). The format specific classes render or read feeds in a specific format. These classes are:

  • Rss20FeedFormatter
  • Rss20ItemFormatter
  • Atom10FeedFormatter
  • Atom10ItemFormatter

The first two classes above deal with RSS feeds and the later two classes deal with ATOM feeds respectively.

Reading RSS feeds

Now that you have some understanding of syndication classes let's develop a simple web form that reads an RSS feed. The web form after development looks as shown below:

It reads RSS feeds from a web site in this example and displays the feed items in a DataList.

Begin by creating a new web site in Visual Studio. Drag and drop two Label controls at the top of the default web form for displaying title of the RSS feed and copyright message as it appears in the feed. Also place a DataList for displaying the feed items.

The ItemTemplate of the DataList consists of a HyperLink control and two Label controls. The markup for this template is shown below:

<asp:HyperLink ID="HyperLink1" runat="server" 
Text='<%# Eval("Title.Text") %>' 
NavigateUrl='<%# Eval("Links[0].Uri.AbsoluteUri") %>'>
<br />
<asp:Label ID="Label1" runat="server" 
Text='<%# Eval("Summary.Text") %>'>
<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True" 
Text='<%# Eval("PublishDate.DateTime","Published On :{0}") %>'>

Notice the markup shown in bold letters. The DataList will be bound with a collection of SyndicationItem objects. Recollect that the SyndicationItem class represents a feed item from a feed. The Title property of SyndicationItem class is of type SyndicationContent. The Text property of SyndicationContent class returns the text representation of the content. The Links property of SyndicationItem class is a collection SyndicationLink instances. Each instance represents a link from the feed item. We used 0th element of the Links collection. The Uri property of the SyndicationLink class represents a URI (URL in most of the cases) and we use its absolute URI address. The Summary property of the SyndicationItem represents summary of a feed item. Similarly, PublishDate property represents date of publication of an item. Note that PublishDate property is of type DateTimeOffset and to display the actual date and time we use its DateTime property.

Before you write any code make sure that you have added a reference to System.ServiceModel.Web assembly and have imported System.Xml and System.ServiceModel.Syndication namespaces.

using System.Xml;
using System.ServiceModel.Syndication;

Now write the following code in the Load event of the web form.

XmlReader reader = XmlReader.Create
Rss20FeedFormatter formatter = 
new Rss20FeedFormatter();
Label3.Text = formatter.Feed.Title.Text;
Label5.Text = formatter.Feed.Copyright.Text;
DataList1.DataSource = 

Here, we created an instance of XmlReader class that reads XML data from a specified URL. Replace the above URL with appropriate URL at your end. Then we instantiate Rss20FeedFormatter class. The ReadFrom() method of Rss20FeedFormatter class accepts an XmlReader and reads the XML data. The underlying XmlReader is then closed. The Feed property of Rss20FeedFormatter class is of type SyndicationFeed and represents the feed being read. The Title and Copyright properties of SyndicationFeed class return title and copyright mesage of the current feed respectively. The Items property of the SyndicationFeed class returns a collection of SyndicationItem objects representing feed items. This collection acts as a DataSource to the DataList.

That's it! If you run the web form you should see something similar to the figure above.

The syndication features of .NET framework are not limited to reading existing feeds. They also allow you to expose your site content as an RSS or ATOM feed. The next article will explain how.

Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 06 April 2008