Creating RSS feeds for your web site


Rss is a standard way to share your web site content with others. Rss stands for Really Simple Syndication. It is nothing but a standardized XML markup that describes the content you want to share. Since Rss is a widely accepted format immediately your content becomes ready to be consumed by others. Some examples of Rss feeds are, and exposes the list of latest additions through Rss which may be used by other webmasters to put on their sites or directories.

Rss format

As I mentioned earlier, Rss is nothing but an XML markup with special tags. Following markup illustrates one such document

<rss version="2.0">
  <title> Latest Articles</title> 
  <description> Latest Articles</description> 
  <copyright>Copyright (C) All rights reserved.</copyright> 
  <generator> RSS Generator</generator> 
  <title>Using WebRequest and WebResponse</title> 
  <description>Description here</description> 
  <pubDate>Sun, 25 Jan 2004 12:00:00 AM GMT</pubDate> 

Let's look at each markup tag closely.

  • <rss> : Forms the root tag and has version attribute. Latest version is 2.0
  • <channel> : rss root node can further contain <channel> tags. This tag further contains  tags such as <tile>,<link> and <item> nodes
  • <title> : represents title of this Rss feed
  • <link> : this represents URL of the web site providing the rss feed.
  • <description> : details more information about this feed
  • <copyright> : Specifies copyright information
  • <generator> : Specifies the application that generated this feed.

In addition to above tags there can be one or more <item> tags. Item tag represents actual item that you want to share e.g. Article, web log entry. Each <item> tag further contains following sub-nodes.

  • <title> : This tag represents title of this item e.g. article title
  • <link> : This tag represents the URL of this item e.g. article URL
  • <description> : This tag contains description of the item e.g. summary of article
  • <pubDate> : This tag contains publication date of the item. Typical date format is Sun 28 Dec 2003 12:00:00 AM GMT.

Approach taken

Ok. We have  familiarize ourselves with Rss format but how to generate such Rss feed in .NET? >NET has plenty of XML related classes. Out of these we will use XML Text writer class to generate our Rss feed. But we want to develop a generic solution that can work with any web site. That means our code should be independent of a particular database field, or table. To achieve this we will create a class library in VS.NET. The source of our Rss <item> tags will be a Dataset usually populated from database table. The class will have following properties and methods.


  • Outputstream : A stream object where the feed is to be thrown
  • RssTitle : Specifies value for <title> tag under <channel>
  • PublisherUrl : represents <link> tag under <channel> tag
  • Description : Represents <description> tag under <channel> tag
  • Copyright : Specifies <copyright> tag under <channel> tag
  • Generator : Specifies <generator> tag under <channel> tag
  • ItemSource : Specifies a Dataset object that contains item rows
  • ItemTitleField : Datacolumn name that represents <title> tag of <item> tag
  • ItemURLField : Datacolumn that represents <link> tag of <item>
  • ItemDescriptionField : Datacolumn that represents <description> tag of <item>
  • ItemPublicationDateField : Datacolumn that represents <pubDate> tag of <item>


  • PublishRss : This shared (static in C# ) method actually writes the Rss markup to the outputstream.

Hear, is the complete list of above properties and methods. Note that for the sake of simplicity and quick illustration I have created public variables instead of properties. In real life application you should use properties instead.

Public Class Rss
    Public OutputStream As Stream
    Public RssTitle As String
    Public PublisherUrl As String
    Public Description As String
    Public Copyright As String
    Public Generator As String
    Public ItemSource As DataSet
    Public ItemTitleField As String
    Public ItemUrlField As String
    Public ItemDescriptionField As String
    Public ItemPublicationDateField As String
   Public Shared Function PublishRss(ByVal r As Rss)
        Dim writer As New XmlTextWriter(r.OutputStream, 
        writer.WriteAttributeString("version", "2.0")
        writer.WriteElementString("title", r.RssTitle)
        writer.WriteElementString("link", r.PublisherUrl)
        writer.WriteElementString("description", r.Description)
        writer.WriteElementString("copyright", r.Copyright)
        writer.WriteElementString("generator", r.Generator
        For Each row As DataRow In r.ItemSource.Tables(0).Rows
            writer.WriteElementString("title", row(r.ItemTitleField))
            writer.WriteElementString("link", row(r.ItemUrlField))
	    DateTime).ToString("ddd, dd MMM yyyy 12:00:00 tt G\MT"))
     End Function
End Class

Creating properties is that way easy. The PublishRss() method  is of our interest as core things happen there. We created an instance of system.XML. XmlTextwriter class. This class is a fast way of writing XML documents. We pass the OutputStream and specify the encoding (ASCII) in our case. We then start writing various parts of the document. We used following methods of XmlTextwriter  class.

  • WritestartElement : This method writes open tag for the specified tag.
  • WriteAttributeString : This method writes an attribute for currently open tag.
  • WriteElementString : This method writes the start tag followed by text value inside it and closing tag.
  • WriteEndElement : This method writes end tag of recently opened tag. You need not specify tag name here as it internally assumes that as per nesting.
  • Flush : This method flushes all the buffered output to the destination.

Note that you must properly call the WriteStartElement() and WriteEndElement() methods to generate well formed XML document.

Creating ASP.NET Web Form

Now that we have created a generic class, we can put it to use in our web form. I assume that our data to be published as Rss feed resides in a table with following structure.

  • Article_ title - Varchar (255)
  • Article_ Description -Varchar (1000)
  • Article_ url  - Varchar (255)
  • Article_ pubdate - DateTime        

Gating the table content as DataSet

We will create a function in ASP.NET web called GetDataSet (). This function uses a DataAdapter to fill a Dataset.

Function GetDataSet() as DataSet
Dim cnn as New SqlConnection("connection string here")
sql = "select * from sometable order by Article_pubdate desc"
Dim da As New SqlDataAdapter(sql,cnn)
Dim ds as New DataSet()
Return ds
End Function

We will then create an instance of our Rss class and set its various properties we then call GetDataSet () method to get our DataSet.

    Private Sub Page_Load(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim r As New Rss
        Dim ds As DataSet = GetDataSet()
        r.OutputStream = Response.OutputStream
        r.RssTitle = " Latest Articles"
        r.PublisherUrl = Request.Url.Host
        r.Description = " - Applying .NET"
        r.Copyright = "Copyright (C)"
        r.Generator = " RSS Generator"
        r.ItemSource = ds
        r.ItemTitleField = "Article_title"
        r.ItemDescriptionField = "Article_Description"
        r.ItemPublicationDateField = "Article_pubdate"
        r.ItemUrlField = "Article_url"
        Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.ContentType = "text/xml"
    End Sub

Once we get the DataSet we simply set ItemSource to this DataSet. In addition we set two properties of response object ContentEncoding and ContentType. Then we call the PublishRss () method passing instance of Rss class.


In this article we saw what is RSS and how to generate RSS feed for your web site. We used XmlTextwriter class to generate RSS markup. We created our class as generic one so that it can be used with any other web application also. In the next article I will show how to consume RSS feeds exposed by other sites in your applications.


Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Kriya and Meditation online course are available here.

Posted On : 08 February 2004

Tags : ASP.NET Web Forms XML