To read the article online, visit http://www.4GuysFromRolla.com/articles/031903-1.aspx

Consuming an RSS Feed with ASP.NET

By Scott Mitchell


This Article has Been Deprecated...
This article has been deprecated. A more recent article - A Custom ASP.NET Server Control for Displaying RSS Feeds - examines an easier way to display an RSS feed in an ASP.NET Web page. Specifically, the updated article looks at how to use an open-source custom server control to display an RSS feed. Using the control is painfully simple, requiring just two lines of code. Furthermore, the look and feel of the server control is highly customizable, much like the look and feel of the DataGrid.

For More Information
A more robust RSS news aggregator can be found in another article of mine: Creating an Online RSS News Aggregator with ASP.NET. Specifically, Creating an Online RSS News Aggregator with ASP.NET shows how to use a three-paned interface, a DataGrid, the ASP.NET Xml Web control, and XSLT to create a fully operational, online news aggregator.

Introduction


Last week I (Scott Mitchell), authored an article titled Syndicating Your Web Site's Content with RSS, which examined using RSS to syndicate Web content. RSS is a syndication standard that specifies the syndication format. As discussed in this previous article, we saw that RSS feeds must be XML-formatted files, with an <item> tag for each content item that you want to syndicate.

In addition to examining the fundamentals of RSS, the Syndicating Your Web Site's Content with RSS also looked at creating an actual RSS feed for ASPMessageboard.com. Specifically, we stepped through the process of creating an RSS feed that syndicates the messageboard's 20 most recent posts. (You can check out the RSS feed directly at http://www.aspmessageboard.com/scripts/rss.asp.)

In this article we will examine how to consume an RSS feed via an ASP.NET Web page. Specifically, we'll display the data from the ASPMessageboard.com RSS feed using a DataGrid. To improve performance, we'll look at how to use caching to cache the RSS feed results for a 20 minute period. When I sat down to create this code example, I had no idea it would be as easy as it turned out to be. Read on, and see how ASP.NET only requires four or five lines of code to display nicely formatted, remote XML data on a Web page. Truly amazing!

Getting the Remote XML Data Into a DataGrid


In order to display the data from the ASPMessageboard.com RSS feed, the first thing we need to do is retrieve the RSS feed data (the XML content) from the ASPMessageboard.com Web site. This, believe it or not, can be accomplished with one line of code:

Dim reader as XmlTextReader = New XmlTextReader(URL to RSS feed)

This line of code creates a new XmlTextReader object that reads from the XML data at the URL specified by the input parameter URL to RSS feed. Now, what we're after is having the RSS feed's data displayed in a DataGrid. We can accomplish this by loading the XML data into a DataSet, and then binding this DataSet to a DataGrid. Before we examine how to do this, though, let's examine the XML format returned by an RSS feed:

<rss version="2.0">
  <channel>
    <title>Channel title</title>
    <link>URL</link>
    ...
    
    <item>
      <title>Title for Syndicating Content Item #1</title>
      <link>Link to Content Item #1</link>
      ...
    </item>
    <item>
      <title>Title for Syndicating Content Item #2</title>
      <link>Link to Content Item #2</link>
      ...
    </item>
    ...
    <item>
      <title>Title for Syndicating Content Item #N</title>
      <link>Link to Content Item #N</link>
      ...
    </item>
  </channel>
</rss>

Note that for each syndicated item there is an <item> element, which is a child of the <channel> element. As discussed in an earlier article of mine, XML, the DataSet, and a DataGrid, XML content can be loaded into a DataSet using the DataSet's ReadXml() method. There are many overloaded forms of this method, one of them accepting an XmlReader as input. This means we can just pass in the XmlTextReader object that we created earlier into the DataSet's ReadXml() method like so:

Dim ds as DataSet = New DataSet()
ds.ReadXml(reader)

When reading in XML, the DataSet will create a number of DataTables, one for each "level" of XML nesting. Each of these DataTable instances are accessible through the DataSet's Tables property. That is, ds.Tables(0) will get the first DataTable, which a row for each top-level element. That is, ds.Tables(0) contains a single row that represents the <rss> element. ds.Tables(1) will get the second DataTable, which also contains a single. This single row, however, represents the sole <channel> element. Finally, ds.Tables(2) will get the third DataTable, which contains a row for each of the <item> elements. This is the DataTable we want to bind to the DataGrid.

When binding a DataSet to a DataGrid, the DataSet's first DataTable is used by default. In order to bind an alternate DataTable, rather than binding the DataSet itself to the DataGrid, we simply need to bind the appropriate DataTable. Since we are interested in the third DataTable, we can accomplish this with the following code:

DataGridID.DataSource = ds.Tables(2) DataGridID.DataBind()

The following code shows a complete ASP.NET Web page that will display the contents of an RSS feed in a DataGrid. Note that the code to load the RSS feed's XML content into a DataSet has been moved into a function, GetRSSFeed(), which returns the appropriate DataTable to bind to the DataGrid.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)    
    recentPosts.DataSource = GetRSSFeed(RSS URL)
    recentPosts.DataBind()      
  End Sub


  Function GetRSSFeed(strURL as String) as DataTable
    'Get the XML data
    Dim reader as XmlTextReader = New XmlTextReader(strURL)
    
    'return a new DataSet
    Dim ds as DataSet = New DataSet()
    ds.ReadXml(reader)    
    Return ds.Tables(2)
  End Function
</script>

<asp:DataGrid runat="server" id="recentPosts" />
[View a Live Demo!]

Improving the Appearance of the DataGrid


The RSS contents displayed in the live demo are quite unsightly. However, by just setting a few of the DataGrid's aesthetic properties, we can quickly make the live demo much more spiffy. The following DataGrid declaration improves the appearance of the live demo considerably. (For an in-depth examination of the DataGrid Web control, be sure to read: An Extensive Examination of the DataGrid Web Control.)

<asp:DataGrid runat="server" id="recentPosts" AutoGenerateColumns="False"
     Font-Name="Arial" Font-Size="10pt"
     HeaderStyle-Font-Bold="True"
     HeaderStyle-HorizontalAlign="Center"
     HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
     HeaderStyle-Font-Size="15pt"
     AlternatingItemStyle-BackColor="#eeeeee">
  <Columns>
    <asp:TemplateColumn HeaderText="20 Most Recent ASPMessageboard.com Posts">
      <ItemTemplate>
        <a href="<%# DataBinder.Eval(Container.DataItem, "link")%>">
          <%# DataBinder.Eval(Container.DataItem, "title") %>
        </a> (<i><%# PrintDateOnly(DataBinder.Eval(Container.DataItem, "pubDate")) %></i>)
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>
[View a Live Demo!]

Adding Caching Support


If you want to display data from an RSS feed on your Web site, it is often not the best idea to request the RSS feed from the remote Web site on every single page load. Not only will your own users experience a degradation in performance, but the Web site that's syndicating its content might not very well appreciate it either! However, syndicated content becomes stale at some point, and therefore needs to be periodically updated.

A simple solution is to cache the RSS content locally for a duration of time. Once that time period has expired, the contents can be rerequested from the remote RSS feed. This is a breeze to accomplish with ASP.NET's caching capabilities. All we need to do is cache the DataTable returned by the GetRSSFeed() function. This can be inserted into the cache for a specific period of time, say 20 minutes. During this time interval, any visitors that visit the page, will be displayed the content from the cache.

All we need to do to implement paging is add a few lines to the Page_Load event handler:

Sub Page_Load(sender as Object, e as EventArgs)
    'Cache the feed for 20 minutes
    Dim dt as DataTable = CType(Cache("aspNetNews"), DataTable)
    If dt Is Nothing then
        'Item not in cache, get it manually    
        dt = GetRSSFeed("http://www.asp.net/news/rss.ashx")
        Cache.Insert("aspNetNews", _
                     dt, _
                     Nothing, _
                     DateTime.Now.AddMinutes(20), _
                     TimeSpan.Zero)
    End If
    
    recentPosts.DataSource = Cache("ASPMBPosts")
    recentPosts.DataBind()      
End Sub
[View a Live Demo!]

That's it! What this code does is check to see if a cache item named aspNetNews exists in the cache. If it does not, then a call to the GetRSSFeed() function occurs, which grabs the RSS content from the remote RSS feed, loads it into a DataSet, and returns the appropriate DataTable. This returned DataTable is then inserted into the cache with instructions to expire after 20 minutes. Next, the DataGrid's DataSource is assigned to the aspNetNews cache item (which, by this point, is guaranteed to exist in the cache), and then the DataBind() method is called.

That's all that needs to be added to provide caching support of the RSS content! For more information on caching, check out Scott McFarland's article Caching with ASP.NET.

Conclusion


This article examined how to consume the contents of a remote RSS feed using ASP.NET. Specifically, we saw how to use the XmlTextReader object to read the contents of a remote XML source. Once we had an XmlTextReader, we could then populate the contents of a DataSet from this data. Finally, the DataSet's appropriate DataTable could then be bound to a DataGrid.

In addition to examining displaying RSS content, we also saw how to take advantage of .NET's caching capabilities. By caching the RSS content, the content doesn't need to be reretrieved from the remote source every single page view. Through caching, both the performance on your Web server and on the RSS feed Web server will be improved.

Happy Programming!

  • By Scott Mitchell

  • Article Information
    Article Title: ASP.NET.Consuming an RSS Feed with ASP.NET
    Article Author: Scott Mitchell
    Published Date: March 19, 2003
    Article URL: http://www.4GuysFromRolla.com/articles/031903-1.aspx


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers