Fetch and Display RSS Feeds using ASP.Net
 
Author:
Filed Under: ASP.Net  |  C#.Net  |  VB.Net
Published Date: Mar 22, 2009
Views: 16165
 

Abstract: Here Mudassar Ahmed Khan has explained how to pull RSS from a website and display the same on the web page using ASP.Net

Comments:  15

 

Many times it is asked how to display pull RSS Feeds from a website and display it on the Web Page

Many websites pull RSS feeds from various websites and display on their web pages. In this article I am explaining how to pull RSS feeds from website and display it on your Website using ASP.Net

For this tutorial I am pulling the Community News RSS feeds from asp.net website. You can use the one you want to display by changing the URL

 

Namespaces

First Import the following Namespaces

C#

using System;

using System.Net;

using System.Xml;

using System.Data; 

 

VB.Net

Imports System

Imports System.Net

Imports System.Xml

Imports System.Data

 

GetRSS Function

The GetRSS function is responsible to pull the RSS feed and display it on the web page. The whole idea is to create a Web Request and Web Proxy to the website from where the RSS needs to be pulled. The received Web Response is then read using the XmlTextReader and finally the contents of the XmlTextReader into the DataSet using the ReadXml property.

Refer the code below

       

C#

private void GetRSS()

{

    //Create a WebRequest

    WebRequest rssReq =

    WebRequest.Create("http://www.asp.net/news/rss.ashx");

   

    //Create a Proxy

    WebProxy px = new WebProxy("http://www.asp.net/news/rss.ashx", true);

 

    //Assign the proxy to the WebRequest

    rssReq.Proxy = px;

 

    //Set the timeout in Seconds for the WebRequest

    rssReq.Timeout = 5000;

    try

    {

        //Get the WebResponse

        WebResponse rep = rssReq.GetResponse();

 

        //Read the Response in a XMLTextReader

        XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

 

        //Create a new DataSet

        DataSet ds = new DataSet();

 

        //Read the Response into the DataSet

        ds.ReadXml(xtr);

 

        //Bind the Results to the Repeater

        rssRepeater.DataSource = ds.Tables[2];

        rssRepeater.DataBind();

    }

    catch(Exception ex)

    {

        throw ex;

    }

}

 

  VB.Net

      

Private Sub GetRSS()

 'Create a WebRequest

 Dim rssReq As WebRequest =

 WebRequest.Create("http://www.asp.net/news/rss.ashx")

 

 'Create a Proxy

 Dim px As New WebProxy("http://www.asp.net/news/rss.ashx", True)

 

 'Assign the proxy to the WebRequest

 rssReq.Proxy = px

 

 'Set the timeout in Seconds for the WebRequest

 rssReq.Timeout = 5000

 Try

   'Get the WebResponse

   Dim rep As WebResponse = rssReq.GetResponse()

 

   'Read the Response in a XMLTextReader

   Dim xtr As New XmlTextReader(rep.GetResponseStream())

 

   'Create a new DataSet

   Dim ds As New DataSet()

 

   'Read the Response into the DataSet

   ds.ReadXml(xtr)

 

   'Bind the Results to the Repeater

   rssRepeater.DataSource = ds.Tables(2)

   rssRepeater.DataBind()

  Catch ex As Exception

            Throw ex

  End Try

End Sub

 

        

 

Display the retrieved RSS Feeds

To display the data I am using ASP.Net Repeater Control Refer below

<asp:Repeater ID = "rssRepeater" runat = "server">

<ItemTemplate>

  <table style="border:solid 1px black;width:500px;font-family:Arial">

        <tr>

            <td style=" font-weight:bold">

                <asp:HyperLink ID="HyperLink1" runat="server"

                NavigateUrl = '<%#Eval("link")%>'

                Text = '<%#Eval("title")%>'></asp:HyperLink>

            </td>

        </tr>

        <tr>

            <td><hr /></td>

        </tr>

        <tr>

            <td style="background-color:#C2D69B">

                <asp:Label ID="Label1" runat="server"

                 Text='<%#Eval("description")%>'></asp:Label>

            </td>

        </tr>

  </table>

  <br />

</ItemTemplate>

</asp:Repeater>

 

The figure below displays how the output is displayed on the page



RSS Feeds displayed on the page



The source code is available to download in C# and VB.Net


DispalyRSSFeeds.zip (4.35 kb)









Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit