Sunday, March 2, 2008

How to get the RSS feeds in to webpages in .net

RSS stands for Really Simple Syndication. It is a format used to publish regularly updated web content such as blogs, articles, news sites etc. Now if you happen to visit a website regularly and have to often to go that website physically, to check if there is some new content available, then RSS feed is what you are looking out for. If that website provides RSS feed, all you need to do is subscribe to it. You can then either use any RSS reader which checks for updated content frequently and displays it to you or consume RSS feed in your own application. Handy, isn’t it!

For eg: I assume some of you like visiting dotnetcurry.com to check for new articles. Instead what you can do is subscribe to our RSS feed over here:

http://feeds.feedburner.com/netCurryRecentArticles

Then by using a RSS reader, you can view the latest updated content of this site. Without further ado, let us see how to consume a RSS feed of any website in your application. For demonstration purposes, we will be consuming the RSS feed of dotnetcurry.com in our application.

Note: Once you visit the url given above, right click on the page and View Source. You will find an XML file, since RSS is nothing but an XML file.

Step 1: Create a new website (Open VS 2005 > File > New Website) called ‘RSSFeed’.

Step 2: Drag and drop a XMLDataSource from the toolbox on to the Default.aspx. If the ‘Configure Data Source’ option is not visible on the XMLDataSource, click the smart tag (arrow just above the XMLDataSource) to make it visible and click on the link.

Step 3: The ‘Configure Data Source’ dialog appears. Add the following entries:

Data File - http://feeds.feedburner.com/netCurryRecentArticles

Transform File – Specify an .XSL file if you have one. This is used to beautify or change the layout of the XML file.

XPath Expression - rss/channel/item

Once the entries has been added, click ok to close the dialog.

Step 4: Now drag and drop a ‘DataList’ control from the toolbox on to Default.aspx. Click the smart tag to display the ‘Choose Data Source’. Choose ‘XMLDataSource1’ option from the dropdown.

Step 5: Now go to the ‘Source’ view of Default.aspx. Create an within the element as shown below:

<ItemTemplate>

<%#XPath("title")%><br />

<%#XPath("pubDate")%><br />

<%#XPath("author")%><br />

<%#XPath("description")%>

ItemTemplate>

After adding some UI look and feel to the DataList, the entire source code will look similar to the following:

C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>RSS Feedtitle>

head>

<body>

<form id="form1" runat="server">

<div>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://feeds.feedburner.com/netCurryRecentArticles"

XPath="rss/channel/item">asp:XmlDataSource>

div>

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" BackColor="White" BorderColor="#404040" BorderStyle="Solid" GridLines="Vertical">

<ItemTemplate>

<%#XPath("title")%><br />

<%#XPath("pubDate")%><br />

<%#XPath("author")%><br />

<%#XPath("description")%>

ItemTemplate>

<AlternatingItemStyle BackColor="CadetBlue" />

<ItemStyle BackColor="AliceBlue" ForeColor="Black" />

<HeaderStyle BackColor="#804040" ForeColor="White" Font-Bold="true" />

<asp:DataList/>

form>

body>

html>

VB.NET

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>RSS Feedtitle>

head>

<body>

<form id="form1" runat="server">

<div>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://feeds.feedburner.com/netCurryRecentArticles"

XPath="rss/channel/item">asp:XmlDataSource>

div>

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" BackColor="White" BorderColor="#404040" BorderStyle="Solid" GridLines="Vertical">

<ItemTemplate>

<%#XPath("title")%><br />

<%#XPath("pubDate")%><br />

<%#XPath("author")%><br />

<%#XPath("description")%>

ItemTemplate>

<AlternatingItemStyle BackColor="CadetBlue" />

<ItemStyle BackColor="AliceBlue" ForeColor="Black" />

<HeaderStyle BackColor="#804040" ForeColor="White" Font-Bold="true" />

asp:DataList>

form>

body>

html>

That’s it. Run the application. You will see that the top 5 recently published articles are shown in your webpage. You can verify that by going to the home page of www.dotnetcurry.com and viewing the top 5 articles over there. You can use this code to consume RSS feed of other websites as well. All you would need to do is specify the correct XPath expression based on the XML structure of the RSS.

I hope this article was useful and I thank you for viewing it.

No comments :

Post a Comment