ASP Snippets

Categories






Alerts

Free Alerts

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




Follow us on twitter.




Zoom In | Zoom Out


Author is awarded Most Valuable Professional award by Microsoft ASP/ASP.Net

Display RSS Feeds using ASP.Net

Author:Mudassar Khan

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)

Posted: Mar 22 2009, 04:46 by Mudassar Khan | Comments (37) RSS comment feed |
Filed under: ASP.Net | C# | VB.Net

Views: 7832
Page copy protected against web site content infringement by Copyscape


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share





Comments









Community News





Web Hosting SpotLight


Consulting


For consulting and work related queries click here.



Advertise


Advertise with us. For more details click here.


Suggestions


Please provide your valuable suggesstions here.

This Site is hosted on

Lunarpages.com Web Hosting