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
www.asp.net in a DataGrid control.
How to read that XML?
RSS feed is nothing but an XML markup with following structure:
<title>DotNetBips.com Latest Articles</title>
<description>DotNetBips.com Latest Articles</description>
<copyright>Copyright (C) DotNetBips.com. All rights reserved.</copyright>
<generator>www.dotnetbips.com RSS Generator</generator>
<title>Using WebRequest and WebResponse</title>
<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:
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
Now that we know the table structure let us write code to display the data in
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.