Geekpedia Programming Tutorials






Create an RSS feed using ASP.NET 2.0

This tutorial will show you how to create a dynamic XML RSS 2.0 feed using ASP.NET 2.0 (C#). The headlines will be retrieved from a SQL database.

On Wednesday, January 4th 2006 at 09:58 AM
By Andrei Pociu (View Profile)
*****   (Rated 4.7 with 47 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
I don't think there's a need for explaining what RSS is, or what RSS does. Most of the time, the headlines of an RSS feed are retrieved from a database, such as SQL server. An example of this is Feedpedia.com which creates feeds containing the latest headlines retrieved from the website's database. One of these feeds is located at http://www.feedpedia.com/RSS/TodaysHeadlines.aspx?Group=13.



In this tutorial we're going to create a similar RSS feed, using ASP.NET 2.0.



In the ASP.NET 2.0 project, create a new file which will be the RSS feed. We used "TodaysHeadlines.aspx".

Now open the markup of the file and replace everything inside it with the following two lines:



Don't forget to change the CodeFile and Inherits attributes of the Page tag in case you gave your RSS file a different name than TodaysHeadlines.aspx.

If you're wondering what the second page does: it tells the ASP.NET server to keep the page in cache for 120 seconds (2 minutes) so that the SQL server doesn't have to be accessed every time someone opens your RSS feed. You can change the duration depending on how often your content updates.





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TodaysHeadlines.aspx.cs" Inherits="TodaysHeadlines" %>
<%@ OutputCache Duration="120" %>



Now since we are already discussing the OutputCache, I want to bring your attention towards something. If you're going to create a dynamic RSS feed file to which you're going to pass a parameter which changes the content of the feed (such as the one on Feedpedia: http://www.feedpedia.com/RSS/TodaysHeadlines.aspx?Group=13), the cache you will create you some problems: when you open the RSS feed with a different attribute (in Feedpedia's case, a different Group ID), the content will not change, but the cached one will be shown. In that case you will need to tell the cache that the content varies by that parameter, and it should build a different cache based on the parameter passed. Here's how you can do that:





<%@ OutputCache Duration="120" VaryByParam="Group" %>



Now if you didn't understand any of my mumbling above, don't worry, as long as you don't plan to pass parameters to the RSS feed, everything will work fine for you without the VaryByParam attribute.



Believe it or not, we're done with the
markup, so switch to code view. Add the following using statements at the top:





using System.Data.SqlClient;

using System.Text;

using System.Xml;



Now what's left to do for your RSS feed to work, is add this code in the Page_Load event:





// Clear any previous output from the buffer

Response.Clear();

Response.ContentType = "text/xml";

XmlTextWriter xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xtwFeed.WriteStartDocument();

// The mandatory rss tag

xtwFeed.WriteStartElement("rss");

xtwFeed.WriteAttributeString("version", "2.0");

// The channel tag contains RSS feed details

xtwFeed.WriteStartElement("channel");

xtwFeed.WriteElementString("title", "Feedpedia Today's World News");

xtwFeed.WriteElementString("link", "http://www.feedpedia.com");

xtwFeed.WriteElementString("description", "The latest news and journals from all over the world.");

xtwFeed.WriteElementString("copyright", "Copyright 2005 - 2006 Feedpedia.com. All rights reserved.");

// Objects needed for connecting to the SQL database

SqlConnection SqlCon;

SqlCommand SqlCom;

SqlDataReader SqlDR;

// Edit to match your connection string

SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

// Edit to match your stored procedure or SQL command

SqlCom = new SqlCommand("GetTodaysHeadlines", SqlCon);

SqlCom.CommandType = CommandType.StoredProcedure;


if (SqlCon.State == ConnectionState.Closed)

{

   SqlCon.Open();

}

SqlDR = SqlCom.ExecuteReader();

// Loop through the content of the database and add them to the RSS feed

while (SqlDR.Read())

{

   xtwFeed.WriteStartElement("item");

   xtwFeed.WriteElementString("title", SqlDR["Title"].ToString());

   xtwFeed.WriteElementString("description", SqlDR["Description"].ToString());

   xtwFeed.WriteElementString("link", "http://www.feedpedia.com/View.aspx?View=" + SqlDR["ID"]);

   xtwFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());

   xtwFeed.WriteEndElement();

}

SqlDR.Close();

SqlCon.Close();


// Close all tags

xtwFeed.WriteEndElement();

xtwFeed.WriteEndElement();

xtwFeed.WriteEndDocument();

xtwFeed.Flush();

xtwFeed.Close();

Response.End();




Of course, you'll need to do the necessary changes so that the code matches your SQL database table. If you have any questions or doubts about the code, don't hesitate to place a comment for this tutorial. I will answer ASAP.



If you did everything right, your RSS feed will be valid and should have a structure similar to the one below:



XML RSS Feed




There are several tags which you can add to the RSS feed, such as the author of the headline, the category or an unique ID. You can find more information about this at the RSS 2.0 specification page.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Chris on Friday, January 20th 2006 at 06:39 AM

Excellent informative article.

by Tim on Sunday, February 19th 2006 at 03:30 AM

pubDate is not in the correct format.
E.g. Tue, 10 Jan 2006 08:00:00 GMT

by Stan on Thursday, April 13th 2006 at 11:03 AM

Hi,
A handy tutorial there but i'm new to the asp.net and its working! Could it be a possibility that this tut can have some screenshots and some easier to folllow instructions

sorry to be a pain

by Ron on Saturday, April 22nd 2006 at 08:23 PM

I have been looking for something like this for months! Thanks a lot, it looks great!

by Jason James on Friday, May 12th 2006 at 06:06 AM

This is just what I was looking for. Everywhere I went I saw references to the 4Guys solution, which started off OK, but my ISP doesn't all access to system.io, which 4Guys use to stream the feed. Using the XMLTextWriter and streaming directly to the response.outputstream go me around the problem.

Once again many thanks, just a shame is wasn't higher up the search engine order.

Regards,

Jason.

by jYUm on Friday, June 30th 2006 at 05:23 AM

Keep it up. You have a good job..

by siva on Wednesday, August 23rd 2006 at 12:48 AM

good this site.ok

by joe vivona on Wednesday, September 6th 2006 at 01:47 PM

to get this to work with IE7 and Outlook 2007, you need to remove the content type of text/xml

by Dinesh Kmar Mandal on Monday, September 11th 2006 at 04:00 AM

Really nice!

by Jeff on Monday, October 16th 2006 at 06:35 PM

How would one use CDATA in the for the item/description element.

by Joshua on Thursday, January 18th 2007 at 09:07 PM

I am in the same boat jeff, i am pulling info from the database and using an editor to put the info into the database so when it is returned, i am getting an error. any help with be greatly appreciated

by Kajol on Friday, June 1st 2007 at 07:23 AM

You can try Feedity - http://www.feedity.com to create custom RSS web feeds from non-syndicated webpages.

by admin on Wednesday, June 13th 2007 at 06:43 AM

kbdhuigsdfkvxclvc vki jodf.gdf klfdnmbdfbfdb

by admin on Wednesday, June 13th 2007 at 06:45 AM

kbdhuigsdfkvxclvc vki jodf.gdf klfdnmbdfbfdb

by Brian on Thursday, June 14th 2007 at 08:43 PM

I am new to asp.net. how can I create a feed that will do the above but pull information from a table that has multiple rows with the same id. The table I have has multiple rows with the same id, has a field xpath listing the category of the data then has a field with data I'd like to display that within the category. ex

row 1: id=1, xpath=name, name=jim
row 2: id=1, xpath=location, location=123 street
row 3: id=1, xpath=date, date=2.3.05

So, How can I get the rss feed to display all the records in the table but group the items by id, lable subcategories in the xml tag then insert the data from the category in the brackets.

by rahul on Tuesday, July 3rd 2007 at 03:51 AM

i am not able to see the xml file but it is showing google reader with my application link.How to get the xml file output?

by ravi on Monday, July 9th 2007 at 02:18 AM

hi,
thank you very much.
but i have still problem with it.
i want to know that how to use RSS in our site.
what to do in our site.

regards...

ravi

by Hemang on Wednesday, July 11th 2007 at 03:36 AM

hi,
its a nice article and its all about what i was looking for.

by mediaguy on Thursday, July 26th 2007 at 12:35 PM

great article , do you have a vb.net code example you can send me

by Craig on Tuesday, August 14th 2007 at 04:33 AM

Hi,
For a start I'm using vb.net not c#.
I dont know why, but I am getting errors with the aspx code. The errors are:
Validation (): Element 'html' occurs too few times.
'Context' is not a member of 'top10Feed'
'Context' is not a member of 'top10Feed'
Thats not a typo, the error is appearing twice. The pages are top10Feed.aspx, and obviously top10Feed.aspx.vb
It seems strange as everyone else seems to have just gone straight through with the code without any problems.
Any help would be greatly appreciated.

by Rajni Padhiyar on Thursday, October 4th 2007 at 08:49 AM

Hello friend,
This is very interesting and useful topic.
But let me know how can i add style sheet using this xmltextwriter.

by shobha on Wednesday, October 17th 2007 at 08:03 AM

Hi,
Useful article.

How can i use RSS reader with respect to RSS feed?

Thanks
Shobha

by mehdi on Wednesday, October 24th 2007 at 01:10 AM

excellent job thank you

by Rajni Padhiyar on Wednesday, November 21st 2007 at 10:27 AM

<b>How can i use RSS reader with respect to RSS feed? </b>
Hello shobha,
There are many ways to use rss feed in our application. example using javascript or in .net programming etc...

in .net we have xmltextreader we can use that reader object to read rss feed. we have to past here just rss url of feed.

one more thing you can get rssreader.dll from net you can use that assembly to read rss feed.

you can find it from net.

Thanks
Regards
Rajni Padhiyar
Sr. Software Engineer.
www.Ojeez.com/rajni


by FM on Wednesday, November 21st 2007 at 07:51 PM

Beautiful.

by Noopur on Thursday, December 13th 2007 at 01:33 AM

Plz tel me wat is the SP \"GetTodaysHeadlines\"... can u plz give me the procedure

by utsav on Tuesday, December 18th 2007 at 07:55 AM

this is excellent article for beginer

by Fayaz on Sunday, December 30th 2007 at 09:25 AM

Good and Simple article for understanding.

But I want to show the above data generated in a GridView.
I know we can read XML file using the XMLTextReader.
We have to pass the Path of the XMl file to the XMLTextReader Object.
If I give Aspx page instead of the XML.
It is giving me Error.
How Can I show above Data in A Gridview.

by Kyle on Tuesday, January 1st 2008 at 12:47 AM

Article really helped me out. Thanks a lot.

by Srini on Monday, January 14th 2008 at 02:49 PM

I google for this today and got another way in asp.net 2.0 here. http://weblogs.asp.net/skillet/archive/2006/08/29/RSS-in-.NET-Made-Easy-with-XML-Serialization.aspx

by Pushkar on Monday, February 25th 2008 at 01:37 PM

Hey man thanks for the Nice Article...Can u please tell how can i make RSS Feed for my Blog Entry/Article...& that should be redirect other site....please Guide me if U can....Thanks in advance.

by Sayitfast on Tuesday, April 22nd 2008 at 06:56 PM

Great... struggled with different tutorials. This was perfect. Worked like a charm.

by mmahmoud on Thursday, April 24th 2008 at 03:42 PM

good article......
but i did not understand VaryByParam="Group"
and i think it is related with my problem .
my prob. is when i update in the database the changes does not reflect on the rss feed????

by Jake on Thursday, May 1st 2008 at 05:25 AM

this is fantastic, saved me a couple hours - thanks!

Jake

by Ad on Tuesday, May 20th 2008 at 01:37 AM

It seems to me very convincing

by sudhakar on Tuesday, May 20th 2008 at 08:02 AM

Thanks for information,
But i have one doubt.how to sbscribe news.

please inform me.

by rajiv on Tuesday, June 17th 2008 at 01:27 AM

I want to search for RSS field, can u help me.

by dotnetguts on Monday, August 25th 2008 at 09:38 AM

Informative and simple article.

Appreciate your hard work.
Thanks.
DotNetGuts
http://dotnetguts.blogspot.com

by Aditi on Monday, August 25th 2008 at 10:16 AM

Thanks it helped me.

by Testing on Monday, August 25th 2008 at 10:20 AM

This Test for subscibe me.

Thank

by qattwa on Monday, September 29th 2008 at 04:06 AM

I have tried this tutorial, and everything worked fine, except for one thing.

When I delete a record from the database, it doesn't reflect on the rss feed. The old records are still there. I am pretty sure its to do with the catch.

Can anybody help?


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons