ASP.NET RSS/Atom FeedReader with just one line of code


By mv ark
Printer Friendly Version
  

Using the much improved Version 2.0 of the ASP.Net RssToolkit building our own Feed reader to consume RSS/ATOM feeds is a cinch & requires effectively just one line of code!



Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.  [Chinese Proverb]

If a one-time avid Internet surfer abducted by outer space aliens were to return back after a decade to planet Earth & to his old ways, he will have a real culture shock! The Web landscape has changed phenomenally & its a Wild Wierd Web out there. Driven by a multitude of concepts including the currently  hyped "Web 2.0", the kind of things you can do on the Internet has grown beyond what was originally envisaged.

One cool idea from among the plethora of ideas around Web 2.0 that makes information from websites better accessible is RSS. Wikipedia defines RSS thus - "An RSS document, which is called a "feed", "web feed", or "channel", contains either a summary of content from an associated web site or the full text. RSS makes it possible for people to keep up with their favorite web sites in an automated manner that's easier than checking them manually."

Nowadays many websites (including EggHeadCafe) syndicate or publish their website content alternatively using RSS. You can subscribe to RSS content using software called a "feed reader" or an "aggregator". There are many Feed readers freely available & many of these are desktop applications. As you are restricted to check your subscribed feeds only from the computer on which you have installed it, it may not appeal to people who are constantly on the move. Therefore online Feed readers like Google Reader are a blessing to those folks.

Now being developers wouldn't it be cool & geeky if we could build a Feed reader ourselves & customize it as we like? Using the much improved Version 2.0 of the ASP.Net RssToolkit building our own Feed reader to consume RSS/ATOM feeds is a cinch & you need to write effectively just one line of code! VS.NET will write all the remaining infrastructure code when you drag & drop controls.

We shall see how to fetch the RSS feed of EggHeadCafe articles or wait a second....considering there is so much we can learn by participating in developer Messageboards, how about writing a program that will get the EggHeadCafe ASP.NET 2.0 Messageboard Posts at pre-set intervals using a bit of AJAX? Without much ado, let's check out the steps to implement a frill-free feed aggregator that refreshes automatically at intervals & plunge into the code.

1) Presuming you have VS.NET 2005 with the latest ASP.NET AJAX Extensions installed, choose the ASP.NET AJAX-enabled Web site template.

2) Move to Design mode & drop a ASP.NET AJAX UpdatePanel & Timer controls

3) Into the UpdatePanel, drop the RssDataSource control. You can get the ASP.Net RssToolkit Version 2.0 which contains the RssDataSource control from Codeplex

4) Clicking on its Smart Tag prompter, you can provide the EggHeadCafe ASP.NET 2.0 Messageboard Posts feed URL - http://feeds.feedburner.com/EggheadcafecomAspnet20MessageboardPosts in the dialog box that opens from  "RssDataSource Tasks" > "Configure Data Source". If you need a more detailed & graphical account on doing this, check ASPNET RssToolkit team member Piyush Shah's blog.

5) Now drop a GridView control from the Data Controls section of the VS.NET toolbox.

6) Clicking on the Smart Tag prompter for GridView shows us the commonly used "GridView Tasks". In the "Choose Data Source" dropdown the RssDataSource control is already there ready to be bound.

7) Pick the columns that are of our interest to reduce clutter. I choose the title, link, description & pubDate columns. I merged the title & link columns into a hyperlink field.

8) To refresh the grid with the latest data, I just had to add a single line of code to the Timer's Tick event.

        protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataBind();
Label1.Text = "Page refreshed at: " +
DateTime.Now.ToLongTimeString();
}


I did not disturb the Timer control's default Interval value of 5 minutes but set the RssDataSource control's MaxItems value to 10 to reduce the time it takes to return the feed.

The final code to build this bare-bones aggregator looks like this:

<%@ Page Language="C#" %>

<%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Page refreshed at: " +
DateTime.Now.ToLongTimeString();
GridView1.DataBind();
}
void Page_Load(object sender, EventArgs e)
{
Label2.Text = "Page created at: " +
DateTime.Now.ToLongTimeString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My frill-free EggHeadCafe ASP.NET 2.0 Messageboard Posts feed aggregator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick">
</asp:Timer>
<cc1:RssDataSource ID="RssDataSource1" runat="server" MaxItems="10" Url="http://feeds.feedburner.com/EggheadcafecomAspnet20MessageboardPosts">
</cc1:RssDataSource>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RssDataSource1">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="link" DataTextField="title" HeaderText="Title" />
<asp:BoundField DataField="description" HeaderText="description" SortExpression="description"
HtmlEncode="False" />
<asp:BoundField DataField="pubDate" HeaderText="pubDate"
SortExpression="pubDate" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

When you run the code above, this is what you will see:



You can style & format the Gridview before you show it your friends.

If the gridview gets too wide due to the content returned by the feed, you can add scrollbars by enclosing it in within div tags and setting its style property to OVERFLOW:auto, something like this:

<div style="OVERFLOW:auto;WIDTH:800px;HEIGHT:500px">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="RssDataSource1">
...
</asp:GridView>
</div>

Summary: The new improved ASP.Net RssToolkit Version 2.0 simplifies the process of publishing and consuming RSS feeds. Using the RssDataSource control that is part of the RssToolkit, we can easily build our RSS/Atom Feed reader & customize it.



Biography
M.V. 'Anil' Radhakrishna is a seasoned developer and a Microsoft MVP (ASP/ASP.NET). He blogs his little discoveries and Web development tips, tricks and trivia quite regularly. You can find some of his unusual code samples & snippets at his Code Gallery.

button

 
Article Discussion: D.I.Y. : Build your own RSS/Atom FeedReader with one line of code
mv ark posted at 10-Aug-07 05:10
Original Article