CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Generate RSS Feed in ASP.Net

By Satheesh babu
Posted On Jun 23,2008
Article Rating:
Be first to rate
this article.
No of Comments: 1
Category: ASP.Net
Print this article.

Generate RSS Feed in ASP.Net

 

RSS feed is an xml formatted document that gives user the capability to read frequently updated contents on our websites without visiting our site. RSS feed has become one of the most sophisticated mechanism to share information on the internet world. It is one of the web 2.0 features which allows users to read their relevant information’s available on the internet. All we need to do is, get the RSS feed URL from different sites of our interest and subscribe it on our RSS reader. Before moving to actual implementation of RSS feed we will understand the format of RSS feeds that a RSS reader can consume.

For example, CodeDigest Rss feed will look like,

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

  <channel>

  <title>CodeDigest.com Latest Articles</title>

  <link>http://www.codedigest.com</link>

  <description>Latest articles hosted on CodeDigest.com.</description>

  <item>

  <title>Useful Datagrid Tips</title>

  <description>There are some frequent queries that are being asked in most of the forums and UG’s regarding datagrid. I have compiled a list of useful tips from my previous posts and posted it here.</description>

  <link>http://www.codedigest.com/Articles/ASPNET/77_Useful_Datagrid_Tips.aspx</link>

  <pubDate>4/29/2008 9:47:12 AM</pubDate>

  </item>

</channel>

  </rss>

 

As we can see the above XML, the whole contents are packed inside <rss> tag. The actual information about the posts is kept inside <item> tag which in turn is packed inside a <channel> tag. Read the history and specification of RSS here. Moving forward this article will help us understand constructing an RSS feeds for our asp.net sites.

 

What we need to do?

Generating RSS feed for our website is nothing but emitting the frequently updated data in the above specified XML format. It can be done by constructing the XML document and performing a Response.Write of the constructed XML document.

 

What should we use?

Our requirement can be achieved by performing a Response.Write on an ASPX page. But, in this article we will generate our RSS feed using HttpHandler instead of an ASPX page. There are some advantages in using HttpHandler over an ASPX pages, an ASPX page will have a series of events like OnInit, Page load, etc  which is not at all required in our case. These page level events are over head since we just want to get the data from database and construct a XML document and output the raw xml.

 

Implementation

With the RSS feed XML format in mind, we can construct XML using XmlTextWriter class. Include an HttpHandler to our project by right clicking solution and clicking “Add New Item”. Refer the below figure.

 

 




RSS.ashx

<%@ WebHandler Language="C#" Class="Rss" %>

 

using System;

using System.Web;

using System.Data;

using System.Text;

using System.Xml;

using DataAccess;

 

 

public class Rss : IHttpHandler {

 

    ArticleDAO articleDAO = new ArticleDAO();

 

    public void ProcessRequest (HttpContext context) {

 

        // Clear any previous output from the buffer

 

        context.Response.Clear();

     

        context.Response.ContentType = "text/xml";

 

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

 

        cdRSS.WriteStartDocument();      

 

        cdRSS.WriteStartElement("rss");

 

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

 

        cdRSS.WriteStartElement("channel");

 

        cdRSS.WriteElementString("title", "CodeDigest.com Latest Articles");

 

        cdRSS.WriteElementString("link", "http://www.codedigest.com");

 

        cdRSS.WriteElementString("description", "Latest articles hosted on CodeDigest.com.");

 

        cdRSS.WriteElementString("copyright", "Copyright 2008 - 2009 CodeDigest.com. All rights reserved.");

 

       //Connect database to get the data

        DataTable dtArticles = articleDAO.GetArticlesForRss();

 

        for (int i = 0; i < dtArticles.Rows.Count; i++)

        {

       //Build Item tags with the data from database

            cdRSS.WriteStartElement("item");

 

            cdRSS.WriteElementString("title", dtArticles.Rows[i]["Title"].ToString());

 

            cdRSS.WriteElementString("description", dtArticles.Rows[i]["description"].ToString());

 

            cdRSS.WriteElementString("link", "http://"+ context.Request.Url.Host + dtArticles.Rows[i]["URL"].ToString());

 

            cdRSS.WriteElementString("pubDate", dtArticles.Rows[i]["ApprovedOn"].ToString());

 

            cdRSS.WriteEndElement();

        }

 

        cdRSS.WriteEndElement();

 

        cdRSS.WriteEndElement();

 

        cdRSS.WriteEndDocument();

 

        cdRSS.Flush();

 

        cdRSS.Close();

 

        context.Response.End();     

 

    }

 

    public bool IsReusable {

        get {

            return false;

        }

    }

}

 

You can read more about HttpHandler from the articles in the reference section.

 

Reference

HttpHandler – Part 1

HttpHandler – Part 2

 

Conclusion

Thus, we have understood implementation of one of the very famous and extremely useful Web 2.0 feature. To consume the RSS feed in our ASP.Net sites you can follow the article here.

Happy Coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
articleDAO
Hi - what is articleDAO?