Consuming RSS Feed using ASP.NET

Posted by: Suprotim Agarwal , on 4/6/2008, in Category ASP.NET
Views: 102168
Abstract: 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.
Consuming RSS Feed using ASP.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. 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>
 
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by pankaj on Friday, May 2, 2008 2:20 AM
I m geting this error
proxy attentication is required plz help me out
Comment posted by Suprotim Agarwal on Saturday, May 3, 2008 7:14 AM
Hi..You could be behind a firewall or a proxy might be configured.

System.Net.WebProxy proxy = new System.Net.WebProxy("I.P address",8080);
//The DefaultCredentials automically get username and password.
proxy.Credentials = CredentialCache.DefaultCredentials;
Comment posted by umesh daiya on Wednesday, July 16, 2008 9:13 AM
its very good for me. and i do this with your help. thanks for this article.
Comment posted by nitin sharma on Wednesday, September 17, 2008 1:31 AM
It is a good article. It really helped me as i am beginner in RSS feed .
Thanks for this article.
Comment posted by Maziyar on Saturday, October 11, 2008 8:56 AM
hi, how to update xml datafile in each pageload.
Comment posted by Suprotim Agarwal on Monday, October 13, 2008 8:04 AM
Maziyar: Entirely depends on when and how your content updates. Remember that RSS is a format used to publish regularly updated web content. So when your content changes, the changes are automatically reflected in the xml.
Comment posted by SamsonJr2 on Friday, January 9, 2009 12:36 PM
Thanks...this helps a lot.  Great post!
Comment posted by punit on Wednesday, April 8, 2009 12:30 PM
good post. clear and simple. thanks.
Comment posted by sandeep on Sunday, May 17, 2009 1:51 AM
i want to get the no of feeds by user by entring ina textbox.
Comment posted by Amal on Sunday, May 17, 2009 7:11 AM
Hi,
thanks for your nice article , i tried it and works well , but what can i do if i need this feed to run as bar at the top moving with the News topics hyperlinks
Best regads
Amal
Comment posted by srdha on Monday, May 18, 2009 5:38 AM

i gust want to say some thing "great job"

Update your Twitter randomly according to your intrest Or, from Rss Feed Or, from your own tweet message list Or, Any combination of the above three http://feedmytwitter.com
Comment posted by Regi Varghese on Wednesday, July 15, 2009 1:59 PM
Hi,
Thank you for creating this article.It really helped to implement RSS feed to my website.

One question I have is on how  to provide a hyperlink or a selectable entity from the data list for  of all titles displayed.Upon clicking the title,a new window should be opened with the details content associated with that title.



Any help on this matter is greatly appreciated.

Thanks
Regi V
Comment posted by Suprotim Agarwal on Friday, July 17, 2009 2:37 AM
Regi: Are you referring to something like this: http://www.dotnetcurry.com/ShowArticle.aspx?ID=99

I created that for the GridView but you will get the idea.
Comment posted by Danish Jaffer on Wednesday, September 16, 2009 1:13 AM
Thank you so much. With your help and a simple, easy to follow step by step code for the xml integration in asp, I am finally able to surpass and understand how to include xml feeds.

Next step is to hunt for available feeds and save it in a database.

Thank you again.
DJ
Comment posted by jayan on Wednesday, December 30, 2009 4:46 AM
how to display flash chart from remote rss feed
Comment posted by Sudhakar on Sunday, January 10, 2010 2:32 AM
here i am getting this error

System.Xml.XmlException: Root element is missing.

Plz reply me it urgent  
Comment posted by Sudhakar on Sunday, January 10, 2010 2:38 AM
here i am getting this error

System.Xml.XmlException: Root element is missing.

Plz reply me it urgent  
Comment posted by Sudhakar on Sunday, January 10, 2010 3:23 AM
here i am getting this error

System.Xml.XmlException: Root element is missing.

Plz reply me it urgent  
Comment posted by Sudhakar on Sunday, January 10, 2010 3:24 AM
No need i made it
Thank u mr Suprotim Agarwal for this Article
Comment posted by Renah on Wednesday, March 3, 2010 2:32 AM
thanks for the detailed walkthrough..It's helped me. :)
Comment posted by siva L on Monday, July 26, 2010 6:09 AM
your website contents are neat and look pretty good. you have done a good job

Thanks
Siva
www.dotnetanalyst.com
Comment posted by Ron Jones on Monday, August 9, 2010 9:33 AM
I tried with this url but it did not seem to work?
http://feeds.feedburner.com/michigan-news
Thanks,
Ron
Comment posted by 3nigma on Thursday, August 26, 2010 5:21 PM
how can i save the rss feed in database im working in mvc2 c#
Comment posted by Somnia on Wednesday, November 17, 2010 1:24 PM
I believe Regi's question above was about how to get the link to an item

Cheers,
<a href="http://www.somnia.ca">www.somnia.ca</a>
Comment posted by santosh kumar pachauri on Friday, February 18, 2011 3:42 AM
it is very good article and easily understandable.
Comment posted by Deepak Porwal on Thursday, May 19, 2011 1:05 AM
Thanks dude...gr8 work
Comment posted by Tiny Bubbles on Thursday, August 11, 2011 9:21 AM
simple and precise example. good job!
Comment posted by smacleod on Friday, December 9, 2011 7:10 AM
Thank you - the most straight forward approach I've seen.
Comment posted by RameshS on Wednesday, February 22, 2012 4:06 AM
Hi,
I tried it in my asp.net webapplication and it does not work.I am getting System.Net.WebException: The operation has timed out.Kindly Help me.I just opened a new webproject and implemented ur code.is there any authentication problem
Comment posted by sanjay on Sunday, April 1, 2012 1:48 AM
excellent job, works great. i now need to display the last 4 posts so they show up in an LI.. float left ?? also how can i show all this in my index page?
Comment posted by Jim on Friday, August 17, 2012 8:58 AM
Thanks for the great article.  I went from no experience to pulling in RSS feeds on a demo website in less than an hour.  Thanks!
Comment posted by Will on Tuesday, October 23, 2012 10:30 AM
Great article - I got your example running on my page in very little time; thank you. However, could you provide sample code showing how to consume the link to a particular news item so a user can click on it and be redirected to the source? Thanks!
Comment posted by Will on Tuesday, October 23, 2012 10:58 AM
I looked around and found a solution that works:
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%#XPath("link")%>' NavigateUrl='<%#XPath("link")%>' Target="_blank" Font-Names="Arial" Font-Size="Small"></asp:HyperLink>
Comment posted by Vaishali Patil on Saturday, November 17, 2012 4:57 AM
This is good artical. I have used this, and taking the <%#XPath("title")%> in hyper link. and i am trying to click and going to the details of the news. and if i use DataFile="http://www.gatesfoundation.org/FileReturn.aspx?returntype=rss&feed=all". how can we seperate the images and the detailed news
Comment posted by Priyanka88 on Thursday, February 28, 2013 1:42 AM
i am getting this erroe  This document already has a 'DocumentElement' node.
when im added .xsl file to trasform file..please help
Comment posted by Priyanka88 on Thursday, February 28, 2013 2:59 AM
how can i add diffrent link and find that link has xml data..
Comment posted by basit on Friday, June 14, 2013 5:10 AM
thanks
Comment posted by Raj on Friday, October 4, 2013 2:45 AM
Sir,
Nice Article. But i want to display image also.
How can i achieve so.
Comment posted by phurba on Sunday, July 13, 2014 6:50 AM
thanks

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook