Consuming an RSS Feed with ASP.NETBy 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.|
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:
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:
Note that for each syndicated item there is an
<item> element, which is a child of
<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
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
which a row for each top-level element. That is,
ds.Tables(0) contains a single row that
ds.Tables(1) will get the
DataTable, which also contains a single. This single row, however,
represents the sole
<channel> element. Finally,
ds.Tables(2) will get the
DataTable, which contains a row for each of the
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
DataTable, we can accomplish this with the following code:
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.
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.)
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
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:
That's it! What this code does is check to see if a cache item named
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
aspNetNews cache item (which, by this point, is guaranteed to exist in the cache), and
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.
This article examined how to consume the contents of a remote RSS feed using ASP.NET. Specifically, we saw how to use the
XmlTextReaderobject 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
DataTablecould 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.