This content is not currently approved and is visible here for review only.

Library code snippets

RSS Feeds with C# and asp.net in 6 easy steps.

RssFeeds

What Is RSS?

( R eally S imple S yndication) A syndication format that was developed by Netscape in 1999 and became very popular for aggregating updates to blogs and the news sites. RSS has also stood for "Rich Site Summary" and "RDF Site Summary

RSS (Really Simple Syndication) provides a convenient way to syndicate information from a variety of sources, including news stories, updates to a web site or even source code check-ins for a development project. Regardless of the purpose for which the RSS file is being used, by watching this XML file, you can quickly and easily see whenever an update has occurred. Of course, viewing the RSS feed in Internet Explorer and hitting F5 every few minutes is not the most efficient use of your time, so most people take advantage of some form of client software to read and monitor RSS feeds.

 

On Web pages, web feeds (RSS or Atom) are typically linked with the word "Subscribe", an orange rectangle or with the letters "XML" or "RSS". 

 

RSS in Action with ASP.NET 1.x

 

In order to make RSS Feeds for some website and make it visible to your page follow these steps.

 

  • Make a new Visual C#’s ASP.net web Application Project.
  • Add a new aspx File name it e.g. RSSFeeds.aspx
  • Drag the dataGrid control to your form and rename it like feedsDataGrid.
  • Drag a button control to your form and label it like “Fetch RSS Feeds”.
  • Add a label with the text “RSS Feeds Demo”.

 

Now the user interface is really simple with just a datagrid control, a button for fetching feeds, a label making top of the page a heading.

You can always change the appearance of dataGrid by changing properties.

 

Locate RSS Feeds XML files.

A lot of website are providing support for RSS feeds now a days e.g.

www.msdn.microsoft.com

www.news.com

www.bbc.co.uk

To find the url of rss feed xml file.Just go to any site which support rss feeds, click on rss feeds icon and a new page will be opened , just save the url of that new page that will latter be used.

Lets suppose you want to be updated for recent code samples on developerfusion.co.uk, For dat you just have to save the url of link of RSS feed which is somewhat like this.

http://www.developerfusion.co.uk/Services/Rss/Content.aspx?type=Code#

 

You will use this url latter in code.

 

1-Add System.xml name space.

First step is to add the System.xml name space in code behind file

using System.Xml;

Next step is to double click on button and code behind Button1_Click function do these steps.

2-Get the Remote XML Data Into a DataGrid

In order to display the data from the xyz.com RSS feed, the first thing is to retrieve the RSS feed data (the XML content) from the any web site. This, can be accomplished with one line of code:

XmlTextReader reader = new XmlTextReader("xml file URL");

This line of code creates a new XmlTextReader object that reads from the XML data at the URL specified by the input parameter URL to RSS feed.

3-Create a dataset Object

After Xml, next step is to put the xml into the dataset so we can bind it to the datagrid..

DataSet dataset = new DataSet();

4-Read the XML into dataset

Now the next step is to read the xml into dataset by using statement like

dataset.ReadXml(xmltextreader);

5-Assign the dataset to the datagrid

You can assign dataset to the datagrid in the following way.

feedsDataGrid.DataSource = dataset.Tables[2];

XML is put in dataset in layers.  first row containts the first layer/level of xml nesting and so on. 3rd tag at index '2' is always the item tag which contains the information   

6-Bind the datagrid

Now the final step is to bind the datagrid.

feedsDataGrid.DataBind();

 The Code

RssFeeds.aspx

<%@ Page language="c#" Codebehind="RssFeeds.aspx.cs" AutoEventWireup="false" Inherits="CustomControls.RssFeed.RssFeeds" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

  <HEAD>

            <title>WebForm1</title>

            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

            <meta content="C#" name="CODE_LANGUAGE">

            <meta content="JavaScript" name="vs_defaultClientScript">

            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

  </HEAD>

      <body MS_POSITIONING="GridLayout">

            <form id="Form1" method="post" runat="server">

                  <asp:datagrid id="feedsDataGrid" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 56px"

                        runat="server" Width="344px" Height="208px" BorderColor="#DEBA84" BorderWidth="1px" BackColor="#DEBA84"

                        CellPadding="3" BorderStyle="None" CellSpacing="2">

                        <FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>

                        <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C"></SelectedItemStyle>

                        <EditItemStyle BackColor="#C0C0FF"></EditItemStyle>

                        <AlternatingItemStyle BorderColor="#FFFF80"></AlternatingItemStyle>

                        <ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>

                        <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#A55129"></HeaderStyle>

                        <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510" Mode="NumericPages"></PagerStyle>

                  </asp:datagrid>

                  <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 8px" runat="server"

                        Width="176px" Height="24px" Text="Fetch RSS Feeds"></asp:Button>

                  <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 8px" runat="server"

                        Width="256px" Height="32px">RSS FEEDS</asp:Label></form>

      </body>

</HTML>

 The Code Behind RssFeeds.aspx.cs

usingSystem;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Web;

usingSystem.Web.SessionState;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.HtmlControls;

usingSystem.Xml;

 

namespaceCustomControls.RssFeed

{

      ///<summary>

      ///Summary description for RssFeeds.

      ///</summary>

      publicclass RssFeeds : System.Web.UI.Page

      {

            protected System.Web.UI.WebControls.DataGrid feedsDataGrid;

            protected System.Web.UI.WebControls.Button Button1;

            protected System.Web.UI.WebControls.Label Label1;

            DataSet ds = new DataSet();

     

            privatevoid Page_Load(object sender, System.EventArgs e)

            {

                  // Put user code to initialize the page here

            }

 

            #regionWeb Form Designer generated code

            overrideprotectedvoid OnInit(EventArgs e)

            {

                  //

                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.

                  //

                  InitializeComponent();

                  base.OnInit(e);

            }

           

            ///<summary>

            ///Required method for Designer support - do not modify

            ///the contents of this method with the code editor.

            ///</summary>

            privatevoid InitializeComponent()

            {   

                  this.Button1.Click += new System.EventHandler(this.Button1_Click);

                  this.Load += new System.EventHandler(this.Page_Load);

 

            }

            #endregion

 

            privatevoid Button1_Click(object sender, System.EventArgs e)

            {

                  

                  XmlTextReader xmltextreader = new XmlTextReader("http://www.developerfusion.co.uk/Services/Rss/Content.aspx?type=Code#");

                          

 

                  DataSet dataset = new DataSet();

               

 

                  dataset.ReadXml(xmltextreader);

 

                 

                  feedsDataGrid.DataSource = dataset.Tables[2];

 

                  

                  feedsDataGrid.DataBind();

 

            }

      }

}

References

http://msdn2.microsoft.com/en-us/aboutmsdn/aa973533.aspx

http://www.answers.com/RSS

http://www.sitepoint.com/article/rss-datalist-control-asp-net

http://aspnet.4guysfromrolla.com/articles/031903-1.aspx

 

 

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of RSS Feeds with asp.net in 6 easy steps..

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media

    CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...

Related jobs

Events coming up

  • Mar 23

    DevWeek 2009

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET Framework 4.0, Silverlight 2, WCF 4.0, Visual Studio 2010, RESTful services, Windows Workflow, ASP.NET AJAX 4.0, SQL Server 2008, LINQ, C# 3, .NET Patterns, Ruby, and more.