Friday, September 23, 2011

Creating RSS feed using Asp.Net 2.0 / 3.5


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:
1<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CreateRSS.aspx.cs" Inherits="_Default" %>
2<%@ OutputCache Duration="120" VaryByParam="*" %>
Now go to code behind & write the code under page load event to dynamically create RSS feed using Asp.Net:
01using System;
02using System.Data;
03using System.Configuration;
04using System.Data.SqlClient;
05using System.Text;
06using System.Xml;
07 
08public partial class _Default : System.Web.UI.Page
09{
10    protected void Page_Load(object sender, EventArgs e)
11    {
12        string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
13        DataTable dt = new DataTable();
14        SqlConnection conn = new SqlConnection(connectionString);
15        using (conn)
16        {
17            SqlDataAdapter ad = new SqlDataAdapter("SELECT * from tblRSS", conn);
18            ad.Fill(dt);
19        }
20 
21        Response.Clear();
22        Response.ContentType = "text/xml";
23        XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
24        TextWriter.WriteStartDocument();
25         
26        //Below tags are mandatory rss tag
27        TextWriter.WriteStartElement("rss");
28        TextWriter.WriteAttributeString("version""2.0");
29 
30        // Channel tag will contain RSS feed details
31        TextWriter.WriteStartElement("channel");
32        TextWriter.WriteElementString("title"".Net Mixer Free Articles");
33        TextWriter.WriteElementString("link""http://shawpnendu.blogspot.com");
34        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");
35        TextWriter.WriteElementString("copyright""Copyright 2009 - 2010 shawpnendu.blogspot.com. All rights reserved.");
36 
37        foreach (DataRow oFeedItem in dt.Rows)
38        {
39            TextWriter.WriteStartElement("item");
40            TextWriter.WriteElementString("title", oFeedItem["Title"].ToString());
41            TextWriter.WriteElementString("description", oFeedItem["Description"].ToString());
42            TextWriter.WriteElementString("link", oFeedItem["URL"].ToString());
43            TextWriter.WriteEndElement();
44        }
45        TextWriter.WriteEndElement();
46        TextWriter.WriteEndElement();
47        TextWriter.WriteEndDocument();
48        TextWriter.Flush();
49        TextWriter.Close();
50        Response.End();
51    }
52}
Now build the project & run it hope you will get an output like below:
Create_RSS_output
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.

No comments :

Post a Comment