Consuming RSS Feeds using ASP.NET

Published 04 May 07 04:54 AM | estoychev 

Introduction

Really Simple Syndication (RSS) is a great way to stay in touch with latest news and blogs. RSS delivers its information in simple XML document called "RSS feed", "RSS channel", "webfeed" etc. More about RSS format can be learned from Wikipedia.

ASP.NET 2.0 provides a great way to consume RSS feeds by using the new XmlDataSource. In ASP.NET 1.1 you can use XmlTextReader to achieve almost the same functionality. Also, awesome tool for both consuming and exposing RSS from ASP.NET 2.0 applications is the RSS Toolkit provided by CodePlex.

     

Consuming a Syndication Feed in an ASP.NET Web Page

The easiest way to consume a feed in ASP.NET 2.0 is to use the XmlDataSource. The only thing you need to do is to add XmlDataSource to your page and set its Data File to the URL of your RSS Feed path. Important is to notice that the XPath expression should be set to rss/channel/item. This is the XPath to the items of the feed according to the RSS 2.0 Specification. Then you can bind any appropriate data control (such as DataList) to display the feeds.

It is almost the same to use ASP.NET 1.1 but instead of using XmlDataSource, you can use the XmlTextReader to get the feed. Your code may look like that:

XmlTextReader feedReader = new XmlTextReader( "http://completit.com/communityserver/blogs/MainFeed.aspx" );

DataSet aggregatedData = new DataSet( "RSSFeeds" );
aggregatedData.ReadXml( feedReader );

dataGrid.DataSource = aggregatedData.Tables[ 2 ];
dataGrid.DataBind();

Notice the DataSource of the DataGrid. It is bounded to the third table in the data set. That is because the data set (aggregatedData) contains tables of each level of XML nesting. The first table contains only a single row that represents the <rss> element. The second table represents the <channel> element. As described above we need the <item> element which actually represents the RSS feed. It is represented in the third table of the data set.

Example for the data grid implementation:

<asp:DataGrid ID="dataGrid" runat = "server">
    <
Columns>
        <
asp:TemplateColumn>
            <
ItemTemplate>
                <
a href = "<%# DataBinder.Eval(Container.DataItem, "link")%>"><%# DataBinder.Eval(Container.DataItem, "title") %></a>
            </
ItemTemplate>
        </
asp:TemplateColumn>
    </
Columns>
</
asp:DataGrid>

List of fields to which you can bind is available in the RSS 2.0 Specification.

My preferred way of consuming RSS Feeds is to use the RSS Toolkit. It is a ready-to-use tool that provides a lot of ways to consume RSS feeds - using RSSDataSource control, ObjectDataSource control, strongly typed classes, late bound classes, and expose feeds - using strongly typed classes and late bound classes. ScottGu has a great article about its usage.

     

References

RSS 2.0 Specification

Wikipedia: RSS File format

MSDN Library: Creating an Online RSS News Aggregator with ASP.NET by Scott Mitchell

ScottGu: Awesome ASP.NET 2.0 RSS Tool-Kit Released

RSS Feed using ASP.NET 2.0 by mssoni

Filed under: ,

Comments

No Comments
Anonymous comments are disabled