Online courses in ASP.NET MVC / Core, Angular, and Design Patterns. Read more...
Know the inner dimension of personal growth and well-being. A practical yogic approach to success, happiness, and inner transformation. A unique sequence of Mantra, Pranayama, Hand Mudras and Meditation. Conducted by Bipin Joshi in Thane. See more details here.

Untitled 1

Using Syndication Classes to Read RSS Feeds

Introduction

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:

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

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
("http://localhost/MyWebSite/rssfeed.aspx");
Rss20FeedFormatter formatter = 
new Rss20FeedFormatter();
formatter.ReadFrom(reader);
reader.Close();
Label3.Text = formatter.Feed.Title.Text;
Label5.Text = formatter.Feed.Copyright.Text;
DataList1.DataSource = 
formatter.Feed.Items.Single().lin;
DataList1.DataBind();

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 a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 06 Apr 2008



Tags : ASP.NET Web Forms .NET Framework Components XML