Tuesday, January 5, 2010

Creating RSS feed using Asp.Net 2.0 / 3.5

Please visit my new Web Site WWW.Codedisplay.com

RSS means Really Simple Syndication which is a Web content syndication format.

If you are looking for "Read/Consume RSS feed" article then click here.

Now let’s try to find out what RSS is about. Basically the website owner should return a RSS feed - and that's simply an XML document following a certain standard, describing new or latest articles on your site. When one copy your feeds & read it by a reader like goggle reader then reader get an overview of your latest articles. If reader wants to read details lets "How to make or create RSS Feed using Asp.net" then the reader will click on your Creating RSS Feed link which will redirect the user to your site.

So i hope now you can understand why Creating RSS feed is necessary for website or blog owner. In blog we will get RSS feed by default but for website you must need to create or make RSS feed for your regular readers so that all times they won’t visit your website to read your latest articles.

In most cases developers Create RSS feed from database. That’s why I will show you the way how we can achieve it. Since you are making or creating RSS feed from database so that you can Dynamically or Runtime create RSS feed from your aspx page. To do that first create the below table:

Fig: Table Structure

Enter some data like:

Fig: Sample data

Now create a new web site. Rename the default.aspx page to CreateRSS.aspx. Now open the CreateRSS.aspx page and add the below line just after the first line of the page:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CreateRSS.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="120" VaryByParam="*" %>
Now go to code behind & write the code under page load event to dynamically create RSS feed using Asp.Net:
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(connectionString);
        using (conn)
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * from tblRSS", conn);

        Response.ContentType = "text/xml";
        XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        //Below tags are mandatory rss tag
        TextWriter.WriteAttributeString("version", "2.0");

        // Channel tag will contain RSS feed details
        TextWriter.WriteElementString("title", ".Net Mixer Free Articles");
        TextWriter.WriteElementString("link", "http://shawpnendu.blogspot.com");
        TextWriter.WriteElementString("description", "Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML,GridView Articles and code examples -- by Shawpnendu Bikash");
        TextWriter.WriteElementString("copyright", "Copyright 2009 - 2010 shawpnendu.blogspot.com. All rights reserved.");

        foreach (DataRow oFeedItem in dt.Rows)
            TextWriter.WriteElementString("title", oFeedItem["Title"].ToString());
            TextWriter.WriteElementString("description", oFeedItem["Description"].ToString());
            TextWriter.WriteElementString("link", oFeedItem["URL"].ToString());
Now build the project & run it hope you will get an output like below:
So now i hope that you can runtime create RSS feed in asp.net application without help of others. In my next article i will show you how you can Read/Consume RSS feed in your asp.net aspx page.

There are more useful tags which you can use to create the RSS feed, such as the author, category or an unique ID. You found more information on Creating RSS Feed in the RSS 2.0 specification page.


Jenny said...

hi great blog...
visit my blog also asp.net example

Anonymous said...

Thanks for posting such a usefull info. I was having some trouble with inserting data, but you helped, thanks :)


Anonymous said...

Thanks for This Nice Article.....

Anonymous said...

hi,i have started to learn abt RSS Feed n have gone thru many sites.but not getting how to start the project for tryel.just to understand how to use RSS Feeds and create it on our website.could u plz guide me from the first step.in database URL feild whose url i have to save.u can just tell me the steps if u cant define it in breif.i will try it myself.
thanks in advance.

Anonymous said...

I used this example and eveything works great except the following. All the items appear as long as the user has not subscribed to the feed. After a user subscribes only the most recent item appears. This happens both in the browser and in any external rss viewer. Any thoughts/solutions?

krishna said...

thnx for ur article i used this example but i'm gettig error:
" Error 1 The name 'Encoding' does not exist in the current context"
what is this? i'm not found any name reference for "Encoding". where is located. r u using any external file.?
pls tell me ........

Shawpnendu said...

Hi Krishna,
The error "The name 'Encoding' does not exist in the current context" comes from below XmlTextWriter object:
XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

Check my code seriously you will get the above object & replace by the below code as solution:
XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);

I.E. You missed the namespace System.Text;

Hope your problem will be resolved.

Anonymous said...

Thanks for the post...it cleared all my doubts about the RSS...

html5 music player said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
html5 media player

Anonymous said...

Awesome information, nicely written. Thanks!

Roger Douglass said...

Thanks for the great post. Clear, concise, and a straight-forward working example. Helped me a lot.

Admin said...

Its very hard to understand from where should i start how rss work can anyone help me

aruneshdeep srivastava said...

I have done for my rss project what you have writen in this tutorial but it not getting from the data base why

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash