Consuming RSS feeds on your web site


In my last article I explained how to generate RSS feed for your site articles and other content. This part of the article explains how to consume RSS feeds exposed by others on your web site. As an example we will build a web form that displays latest articles listed on in a DataGrid control.

How to read that XML?

RSS feed is nothing but an XML markup with following structure:

<rss version="2.0">
  <title> Latest Articles</title> 
  <description> Latest Articles</description> 
  <copyright>Copyright (C) All rights reserved.</copyright> 
  <generator> RSS Generator</generator> 
  <title>Using WebRequest and WebResponse</title> 
  <description>Description here</description> 
  <pubDate>Sun, 25 Jan 2004 12:00:00 AM GMT</pubDate> 

I won't explain various tags here because they have already been explained in the previous article.

In order to read this XML data from a URL one can certainly use WebRequest and WebResponse objects (refer my article titled Using WebRequest and WebResponse). However, there is an easy way to do that - DataSet.

The DataSet class has a method called ReadXml() that can read XML data from a physical file or URL. This method reads that data and automatically generate required DataTables for us.

Dim ds As New DataSet

Here, we create a new instance of DataSet and called the ReadXml() method by passing the URL. You can replace the URL with that of your own choice.

Tables generated by ReadXml()

If you expect that the ReadXml() will generate a single DataTable with list of links you will be surprised to see that there are actually three DataTables generated. XML markup shown above is a nested data and DataSet automatically creates related tables while reading it. It also adds ID columns to each DataTable so that they can be linked with each other.

In this case you will get DataTables with following schema:

  • rss_Id
  • version
  • title
  • link
  • description
  • language
  • generator
  • channel_Id
  • rss_Id
  • creator
  • title
  • link
  • pubDate
  • guid
  • description
  • channel_Id

Note that some fields such as creator and guid are not mandetory in RSS markup. Also note how DataSet added columns such rss_Id and channel_Id to relate the DataTables.

Now that we know the table structure let us write code to display the data in a DataGrid.

Displaying the data in DataGrid

Out of the details obtained from RSS the third DataTable is most important because it contains the actual listing of links. Here, we will bind our DataGrid with this third DataTable.

DataGrid1.DataSource = ds.Tables(2).DefaultView

Once you call this code on the Page_Load event you should see a DataGrid similar to following screen shot.



In this article we saw how a DataSet class can be used to consume RSS feeds. The RSS feed being a nested XML markup, DataSet automatically creates corresponding DataTables that are related to each other. The third table (item) contains the core data of the RSS feed.


Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 22 February 2004

Tags : ASP.NET Server Controls XML Data Controls