Skip to content
15/06/2011 / Roberto Gimenez

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&gt;

  <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&gt;

  <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.  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#” %>

 

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&#8221;);

 

        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;

        }

    }

}

Font:  http://www.codedigest.com/Articles/ASPNET/86_Generate_RSS_Feed_in_ASPNet.aspx 

*********************************************

Soluções em desenvolvimento de sistemas com a DANRESA Consultoria.

Anúncios

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s

%d blogueiros gostam disto: