CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 11, 2006

Consuming an RSS Feed with ASP.NET

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:

Continue Consuming an RSS Feed with ASP.NET

Post a Comment