RSS stands for Really Simple Syndication. It is a format used to publish regularly updated web content such as blogs, articles, news sites etc. In this article we will explore how to consume a RSS feed of a site.
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 use any RSS reader (like the one we will be building) which checks for updated content frequently and displays it to you. 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 my RSS feed over here:
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 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:
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.
Tip: If you wish to limit the number of RSS items to a limited item, use XPath as shown below.
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://feeds.feedburner.com/netCurryRecentArticles"
XPath="rss/channel/item [position()<=10]"></asp:XmlDataSource>
XPath="rss/channel/item [position()<=10]"></asp:XmlDataSource>
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 <ItemTemplate> within the <asp:DataList> 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 Feed</title>
</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 Feed</title>
</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.
No comments:
Post a Comment