Consuming RSS Feed using XmlTextReader and DataSet

Waqas Anwar
27 May 2008
7420 Views

RSS Feed is a very popular web feed format to publish frequently updated contents such as blog entries, articles, news headlines. In the following tutorial, I will show you how you can read RSS feed from its URL using XmlTextReader class in System.Xml namespace. I will also show you how you can bind and display feed contents using ASP.NET DataList control.

First, I am creating a simple ASP.NET web form which ask you to type and check different RSS Feed URLs from different websites. You can also use hard coded string URL in your application, or it can be stored in database if you have large website, and you have many different RSS Feed URLs to display. The following figure shows the preview of the web form with the RSS feed results. 


RSS Feed Preview

Following code shows how to read RSS feed from the URL in XmlTextReader and DataSet object.

protected void Button1_Click(object sender, EventArgs e)
{
    XmlTextReader reader = new XmlTextReader(TextBox1.Text);
    DataSet ds = new DataSet();
    ds.ReadXml(reader);
    DataList1.DataSource = ds.Tables[2];
    DataList1.DataBind();    
}


Following is the html source code to bind the DataSet table fields title and link with a DataList control.

<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px">

   <AlternatingItemStyle BackColor="#CCCCFF" />
   <HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" />

   <HeaderTemplate>
      Latest Tutorials
   </HeaderTemplate>

   <ItemTemplate>
      <asp:HyperLink ID="link" runat="server" Text='<%# Eval("title") %>' NavigateUrl='<%# Eval("link") %>' />                
   </ItemTemplate>
           
</asp:DataList>