A Simple RSS Reader

In this post I thought of publishing a code snippet regarding how to build a simple RSS reader. Here I explained how to build a RSS reader as a user control so that you can re-use it among your web applications.

Follow these steps…

  1. Open VS 2005 & create a new project.
  2. Right-click the project title & select Add New Item. Then from the templates select Web User Control. Name that as ‘RSSList’.
  3. Go to the source view of the user control and put the following code.

  4. <%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”RSSList.ascx.cs” Inherits=”RSSList” %>
    <%@ OutputCache Duration=”3600″ VaryByParam=”None” %>

    <asp:Repeater ID=”Repeater1″ runat=”server”>
    <HeaderTemplate>
    <table border=0 style=”width: 240px; font-size: x-small; color: black; font-family: Verdana;”>
    <thead>
    <tr style=”font-weight: bold;”>
    <td><%#this.Title%></td>
    </tr>
    <tr style=”font-style: italic;”>
    <td><%#this.Description%></td>
    </tr>
    </thead>
    </table>
    </HeaderTemplate>

    <ItemTemplate>
    <table>
    <tr bgcolor=”LightBlue” >
    <td>
    <a target=”article” style=”text-decoration: none; color: black;” href=<%# DataBinder.Eval(Container.DataItem, “link”) %>>
    <%# DataBinder.Eval(Container.DataItem, “title”) %> </a>
    </td>
    </tr>
    <tr bgcolor=”Ivory”>
    <td style=”color: CornflowerBlue;”>
    <%# DataBinder.Eval(Container.DataItem, “description”) %>
    </td>
    </tr>
    <tr style=”font-size: x-small; color: black; font-family: Verdana;”>
    <td>Published
    <%#DataBinder.Eval(Container.DataItem, “pubDate”)%>
    </td>
    </tr>
    </table>
    </ItemTemplate>

    </asp:Repeater>

  5. Now open the RSSList.ascx.cs file & put the follwing code..

  6. using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Net;
    using System.Data;

    public partial class RSSList : System.Web.UI.UserControl
    {

    public string Title;
    public string Description;

    protected void Page_Load(object sender, EventArgs e)
    {
    DataSet _rssData = RefreshFeed();

    object[] channelItems = _rssData.Tables[1].Rows[0].ItemArray;
    int titleColumn = _rssData.Tables[1].Columns[“title”].Ordinal;
    int descriptionColumn = _rssData.Tables[1].Columns[“description”].Ordinal;
    Title = channelItems.GetValue(titleColumn).ToString();
    Description = channelItems.GetValue(descriptionColumn).ToString();

    Repeater1.DataSource = _rssData.Tables[2];
    Repeater1.DataBind();

    }

    private DataSet RefreshFeed()
    {
    HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(“http://localhost/Search1/Rss.xml&#8221;);
    DataSet rssData = new DataSet();
    rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());

    return rssData;

    }
    }

  7. Now here you can give any web address of a rss news channel, but for this example I used my own xml file called ‘Rss.xml’ which is inside my IIS virtual directory. For your convenience I’ll publish my Rss.xml file at the end.
  8. Now our user control is ok, we need to test it. Therefore create a new aspx page and simply drag & drop the RSSList.acsx from the solution explorer to the design view of the page.
  9. Now you can see a new control in the design view of the page. Now our work is done. Simply run the web application.
  10. If you use my Rss.xml file you have to put it inside same virtual directory structure that I have used as in this example.
  11. When you run the application you ‘ll be able to see the News items from the news chanel that you’ve provided. (In my example it is Rss.xml)
  12. Enjoy…!!

This is my Rss.xml file…

<rss version=”2.0″>
<channel>
<title>Liftoff News</title> </channel></rss>
<link />http://liftoff.msfc.nasa.gov/ <description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubdate>Tue, 10 Jun 2003 04:00:00 GMT</pubdate> <lastbuilddate>Tue, 10 Jun 2003 09:41:01 GMT</lastbuilddate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs&gt;
<generator>Weblog Editor 2.0</generator>
<managingeditor>editor@example.com</managingeditor>
<webmaster>webmaster@example.com</webmaster>
<item>
<title>Second post</title>
<description>This is a sample description for the second post..</description>
<time>10/16/2006 10:26:25 AM</time>
<month>October/2006</month>
</item>
<item>
<title>First post</title>
<description>here goes some sample description for the first post. I updated this news.</description>
<time>10/16/2006 10:27:34 AM</time>
<month>October/2006</month>
</item>
<item>
<title>Star City</title> </item>
<link />http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp <description>How do Americans get ready to work with Russians aboard the
International Space Station? They take a crash course in culture, language
and protocol at Russia’s Star City.</description>
<pubdate>Tue, 03 Jun 2003 09:39:21 GMT</pubdate> <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid&gt;
<time>10/10/2006 1:22:27 PM</time>
<month>November/2006</month>

<item>
<title>Space Exploration</title> </item>
<link />http://liftoff.msfc.nasa.gov/ <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada
will experience a partial eclipse of the Sun on Saturday, May 31st.</description>
<pubdate>Fri, 30 May 2003 11:06:42 GMT</pubdate> <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid&gt;
<time>10/10/2006 1:11:27 PM</time>
<month>December/2006</month>

<item>
<title>The Engine That Does More</title> </item>
<link />http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp <description>Before man travels to Mars, NASA hopes to design new engines
that will let us fly through the Solar System more quickly. The proposed
VASIMR engine would do that.</description>
<pubdate>Tue, 27 May 2003 08:37:32 GMT</pubdate> <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid&gt;
<time>10/10/2006 1:44:27 PM</time>
<month>November/2006</month>

<item>
<title>Astronauts’ Dirty Laundry</title> </item>
<link />http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp <description>Compared to earlier spacecraft, the International Space
Station has many luxuries, but laundry facilities are not one of them.
Instead, astronauts have other options.</description>
<pubdate>Tue, 20 May 2003 08:56:02 GMT</pubdate> <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid&gt;
<time>10/10/2006 1:31:27 PM</time>
<month>July/2006</month>

Advertisements

One thought on “A Simple RSS Reader

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s