ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Fetch and Display RSS Feeds using ASP.Net
Author Name: Mudassar Khan Published Date: March 22, 2009
Filed Under :
ASP.Net
 |
C#.Net
 |
VB.Net
Views: 7818

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)


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Daud said:
Great br Thanks
January 18, 2010  

Mani said:
Its super.I would like to host my webpage.So what ia the procedure and whom to contact
January 21, 2010  

NIKHIL said:
hi thanks for this artical br But i have problem while using this. the xmltextreader return none when i use your example br br Can you please help me out.br br thanks and regards br nikhilbr
January 24, 2010  

Mudassar Khan said:
Reply To: NIKHIL
This can happen in 2 scenarios

1. server is not returning the response
2. You are not connected to internet

January 24, 2010  

Nikhil said:
i am using the same url as you written br br the same code as you written br br nothing other then that br br an also my internet connection is on fullybr br but still i got the same response br also i am using asp.net 2.0
January 24, 2010  

Mudassar Khan said:
Reply To: Nikhil
I don't know what issue you are facing but just now I tested the sample and it is working for me
check whether the Url you are accessing works for you by placing it in IE
January 24, 2010  

Mudassar Khan said:
Reply To: Nikhil
I don't know what issue you are facing but just now I tested the sample and it is working for me
check whether the Url you are accessing works for you by placing it in IE
January 24, 2010  

anoop said:
how image is included
February 16, 2010  

Mudassar Khan said:
Reply To: anoop
For images your RSS should have image Urls. If it has then bind the image urls to image control
February 16, 2010  

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.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code