Consuming RSS feeds on your web site
Introduction
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
www.asp.net in a DataGrid control.
How to read that XML?
RSS feed is nothing but an XML markup with following structure:
<rss version="2.0">
<channel>
<title>DotNetBips.com Latest Articles</title>
<link>www.dotnetbips.com</link>
<description>DotNetBips.com Latest Articles</description>
<copyright>Copyright (C) DotNetBips.com. All rights reserved.</copyright>
<generator>www.dotnetbips.com RSS Generator</generator>
<item>
<title>Using WebRequest and WebResponse</title>
<link>http://www.dotnetbips.com/displayarticle.aspx?id=239</link>
<description>Description here</description>
<pubDate>Sun, 25 Jan 2004 12:00:00 AM GMT</pubDate>
</item>
</channel>
</rss>
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
ds.ReadXml("http://www.asp.net/modules/articleRss.aspx?count=7&mid=64")
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
Channel
- title
- link
- description
- language
- generator
- channel_Id
- rss_Id
Item
- 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
DataGrid1.DataBind()
Once you call this code on the Page_Load event you should see a DataGrid
similar to following screen shot.

Conclusion
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.