AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Create RSS Feeds to your website and Display it in browser friendly design
Posted by Moderator1 on 7/7/2007 11:35:49 AM Category: Asp.Net 2.0
Total Views : 232897
Add to my favorites
Email to friend
  
Introduction
This article explains the concept of creating RSS feeds to the dynamically updating and releasing contents in any Asp.Net website and display the RSS feeds in a browser friendly design with the help of XSLT.
Many of the web application now days update their website contents frequently to attract the users at the maximum extent. Even web publishers needs to share their content without re-creating it on any form. But there is no way to inform the users about the new contents in their website. Either they have send newsletters to the users or the users have to bookmark the website URL in their browser’s favorites, which had its own drawbacks. To overcome this difficulty, Netscape introduced the concept of RSS in late 90s. It serves the information consumers and publishers to stay up to date with their desired website's content.

What is RSS?

RSS, can be expanded as Rich Site Summary or Resource Description Framework Site Summary or Really Simple Syndication. RSS is a lightweight XML format for distributing regularly changed web contents among different web sites and users. A Web site can allow the users or other sites to publish some of its content by creating an RSS feeds.

Benefits of RSS Feeds

RSS allows the users to easily stay informed by retrieving the latest content from the sites they are interested in. It saves time by not needing to visit each site individually. It also ensures the privacy of the users by not needing to join the site's email newsletter. The number of sites offering RSS feeds is growing rapidly and includes big names like Microsoft and Asp.Net websites.
Basic Structure of RSS Feeds

As we explained earlier, RSS is nothing but creation of a well-formed XML file in a pre-defined format. The main tags are shown below without which you cannot say it as RSS.
<rss version="2.0">
  <channel>
   ......
  </channel>
</rss>
<rss> tag is the global container of the xml element, openly said it is the root element. <channel> tag contains one or many description tags such as <title>, <description>, <link> and mainly <item> tag which is very important. With all these tags the RSS will look like
<rss version="2.0">
<channel>
     <title>AspdotnetCodes</title>
     <link>http://www.aspdotnetcodes.com/</link>
     <description>Latest Articles List in Asp.Net</description>
     <item>
         ........
     </item>
</channel>
</rss>
The above tag details are

<title> provides the title of the channel or the website.

<link>
provides the url of the website that provided the RSS.

<description>
provides some content from the RSS provider.

<item>
very important tag to keep in mind, every RSS must contain atleast one <item> tag to provide content. This <item> tag must contain some child tags such as <title>, <description>, <link>, and <pubDate>, etc. With these tags a full RSS format will look like
<rss version="2.0">
<channel>
     <title>AspdotnetCodes</title>
     <link>http://www.aspdotnetcodes.com/</link>
     <description>Latest Articles List in Asp.Net</description>
     <item>
         <title>Create RSS to your website</title>
         <link>http://www.yourwebsite.com/createrss.html</link>
         <description>... some text... </description>     
     </item>
</channel>
</rss>
The <item> tag's child nodes are
<title> provides the Title of the article.
<link> provides the URL of the page.
<description> provides the summary of the article.

There are some optional tags, which we cannot explain here since it is out of scope of the article.

This article helps you to create your own RSS feeds to your website for your desired content in Asp.Net 2.0 with the help of XMLTextWriter class and to embed the XML with XSLT to display your feed in a browser friendly design and layouts. We have explained it as simple as possible, just follow the steps below
Step 1: Create a new XSL file in you application and name it as rss.xsl, design it on your own way if you are familiar with XSL or you can use our sample rss.xsl, we created for you.

Step 2: Create an Aspx Page with separate code-behind file and name it as RSS.aspx.

Step 3: Open the RSS.aspx, remove all the html and asp tags you found, except the page directive. So your aspx page will look like
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="Rss" Title="My First RSS Feeds" %>

Step 4:
Open your code-behind file RSS.aspx.cs, add the following namespaces
using System.Data.SqlClient;
using System.Xml;
using System.Text;
Step 5: In the Page_Load event, start to write the code by clearing the server response and specify the response content type should be in XML format as follows

Response.Clear();
Response.ContentType = "text/xml"; 

Step 6: Next we are going to create XMLTextWriter object to create the XML document to output the XML as an OutputStream with encoding utf-8.

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

Step 7: Now we can start to build the XML document with the following line of code

xtw.WriteStartDocument();

Step 8: Next we have to create the XML processing instructions tag. This step helps you to bind your XML with the XSLT file you created in Step 1.

string processtext = "type=\"text/xsl\" href=\"rss.xsl\"";
xtw.WriteProcessingInstruction("xml-stylesheet", processtext);

Step 9: Now you can create the neccessary tags of the RSS as follows

xtw.WriteStartElement("rss");
xtw.WriteAttributeString("version", "2.0");
xtw.WriteStartElement("channel");
xtw.WriteElementString("title","Latest Articles List");
xtw.WriteElementString("link", "http://www.yourwebsite.com/");
xtw.WriteElementString("description", "some description here");

Step 10: In this step you are going to create the main tag and its elements. As explained earlier, these tags, are responsible for delivering the content informtion to the users. Here you have to fetch all the records from the database that you want to include in your feed and loop through them to create the elements. Look at the code below

string sql = "Select * from Articles Order by Created_Date Desc";
SqlDataAdapter da = new SqlDataAdapter(sql,”Your Connection String”);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
 xtw.WriteStartElement("item");
 xtw.WriteElementString("title", dt.Rows[i]["Title"].ToString());
 xtw.WriteElementString("description",dt.Rows[i]["summary"].ToString());
 xtw.WriteElementString("link",dt.Rows[i]["Url"].ToString());
 xtw.WriteElementString("pubDate",
  XmlConvert.ToString(Convert.ToDateTime(dt.Rows[i]["Created_Date "].ToString())));
 xtw.WriteEndElement();
}


Step 11: Close all the opened elements in the above steps and close XMLTextWriter class as follows

xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();

xtw.Flush();
xtw.Close();

Done. Save your code and view this page in your browser. You must say “Yes, I’ve done it.”. For explanation purpose, we have to separate the source code section by section, you can view the full source code without break in the next page for free.

Click here to view Sample Source Code

Click here to view or add our articles RSS Feed
Viewer's Comments
Posted by Catia Sofia on 7/18/2007 10:27:31 AM
Great article. I've already set up RSS feeds for my website. Thanks! :)
 
Posted by younger on 8/16/2007 2:27:31 AM
site. me! pleasant to It Excellent was ,
 
Posted by Rathikumar.V on 9/20/2007 3:59:50 AM
This article rally is very usefull for me. Can u Expalin About Rss Feed how its work?.why should we use rss feed?. Give simple example
 
Posted by Paul N. on 10/19/2007 9:56:44 AM
Hi, I am new to asp.net. I am creating a rss for a blog. If I want to strip the description (body of the blog) of the item to 200 characters. How would i got about doing that?
 
Posted by verdana on 11/20/2007 3:29:51 PM
Good site! lacostelulu.com/hide-my-ip/index.html
 
Posted by Vandy on 11/23/2007 4:32:09 AM
i created a rss page URL of my RSS page is "http://10.238.122.88/Client/wtvrss.aspx". But when i try adding this to a reader,Reader throws an error saying "this feed cannot be added.Please try again later" while checking for autodiscover tag.Can you help me on this?
 
Posted by dipesh on 12/5/2007 1:01:10 PM
Good site, i liked it. Keep up the good work.
 
Posted by ib on 4/22/2008 12:57:55 AM
Doesn't work in IE!!!
 
Posted by sylvia on 5/19/2008 5:22:04 PM
nice work man 10x
 
Posted by tadalafil on 5/28/2008 2:15:18 AM
tadalafil
 
Posted by Abhilash on 6/17/2008 2:25:41 AM
Thats Yummy!
 
Posted by Giri on 7/19/2008 5:15:24 AM
Very Good Article
 
Posted by Giri on 7/19/2008 5:15:42 AM
very very good
 
Posted by ben on 9/6/2008 5:08:30 PM
interesting post thx
 
Posted by Fabiano on 9/13/2008 10:22:56 AM
very very good and easy ! thank you
 
Posted by Gayathri on 9/17/2008 3:00:25 AM
Grt work and was easy to implement Thank you
 
Posted by john2025 on 10/23/2008 1:48:42 AM
HiT8KS hi webmastters
 
Posted by Bhumireddy on 10/30/2008 4:39:28 AM
It was very helpful for me...Thanks alot
 
Posted by web programmer on 11/5/2008 2:22:29 PM
Do you have this article in vb version? thanks.
 
Posted by mebmasters on 11/10/2008 4:05:35 PM
L8LOlN hi mebmasters
 
Posted by Smruti Cool on 11/14/2008 5:19:07 AM
Good but not excellent bcz it is not working in IE
 
Posted by chirag on 11/26/2008 6:18:26 AM
I have problem when i used it in IE6. I need to run on it.
 
Posted by parag on 11/26/2008 11:59:36 PM
i have problem with IE 6.
 
Posted by parag on 11/27/2008 12:00:34 AM
In IE 6.0 i got error. The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML documen...
 
Posted by Lilli on 1/18/2009 2:09:42 PM
Very good, I was so suprised when I came upon this site. I would love to find a chat for installers to help and give opinions and compare prices in differant areas Thank you and keep up the good work. I am from Japan and now teach English, please tell me whether I wrote the following sentence: "Fast weight lose and tips for fat loose secrets that lose weight quickly advices that work." Thank :( Lilli.
 
Posted by Robert Woltz on 2/9/2009 4:00:28 PM
I have been trying to convert your code to VB. Having some problems with the display. Here is the code I hope you can help me. I have tried different ideas nothing seems to help. Lots of commented out lines. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sqlCon As String = System.Configuration.ConfigurationManager.AppSettings("MSIdbConnectionString") 'Dim spName As String = System.Configuration.ConfigurationMana
 
Posted by Very nice site! on 3/20/2009 1:17:42 PM
Very nice site!
 
Posted by Narges on 4/8/2009 3:58:07 AM
Thank you so much for your greate article.... It really helped me ....
 
Posted by Very nice site! on 4/14/2009 8:04:38 AM
Very nice site!
 
Posted by Mahadev on 7/28/2009 2:03:21 AM
Nice article but I have some queries. Query: I am running the same code what you have provided and changed the table name other stuffs. It displays all the links which are there in the DB but I am not able to see the "Subscribe to this feed" option in page. Can you please help me to solve this problem? Thanks
 
Posted by Niren Prajapati on 9/11/2009 2:47:43 PM
Hi everybody! I have created RSS feed. now I want to use that RSS feed in my website and in perfect format. If anybody know then please send the answer as early as possible on my Email-ID.
 
Posted by Purathani on 9/30/2009 6:01:59 AM
Hai, It is not working in IE. It is displaying as xml content in IE. Please give a solution. Thanks.
 
Posted by Ace on 11/16/2009 10:57:09 PM
Useful article & I got it working after small modifications, I needed to add "Response.End();" line at the end of the code, then it worked. Any way thanks for sharing 2/5 for the imperfect code
 
Posted by Jimmy Hoffa on 12/11/2009 2:09:37 PM
Terrible, Nothing Worked. DEAD code.
 
Posted by kanchan singh on 2/2/2010 12:06:29 AM
It was very helpful for me...Thanks
 
Posted by Sachin on 2/2/2010 1:39:46 AM
Very good article provided this code is open source
 
Posted by prasant on 9/28/2010 7:31:57 AM
article is nice but it doesn't work in IE and Chrome. Just working in mozila
 
Posted by Kaung on 11/10/2011 2:47:01 AM
could you add and example to include image tag also please?
 
Posted by Muhammad Waseem on 5/3/2012 3:13:48 AM
Hi sir I am using this code for rss feed.its work great with book mark. but when i subscribe rss feed with outlook express. it show url http://https:/www.abc.com my site use https url instead of http. https://www.abc.com I search google but did not find anything good. Please help me.
 
Posted by Muhammad Waseem on 5/3/2012 3:16:41 AM
(sorry my email is emc_waseem@hotmail.com) Hi sir I am using this code for rss feed.its work great with book mark. but when i subscribe rss feed with outlook express. it show url http://https:/www.abc.com my site use https url instead of http. https://www.abc.com I search google but did not find anything good. Please help me.
 
Posted by gsgsdgsd on 1/31/2017 4:57:57 AM
sdgsdgsgdgs
 
 Rating & Comments
A word 'Excellent' means lot to the author of this article. You can give comments about this article but not the author.
Rate this Article:
Name:
Email Id:  
We never display your email id anywhere.
Comment/Question: Max. 500 letters
Thirteen minus One Equals To
 
Sponsored by
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
SearchSAP
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview