Creating an RSS feed from your database

Your dynamic site is likely to have many pages built from data in your application database. Your clients may wish to be kept aware of the latest details and will be used to using RSS feeds on other sites. Fortunately it is relatively easy to provide an RSS feed from your site. The only problem is deciding what sort of information you wish to provide. Download an example project based on this mini-tutorial.

RSS feed structure

An RSS feed is simply an XML data file, formatted to a defined schema. The root element is <rss> and may contain several news channels in <channel> elements. Each <channel> element can have many news items in <item> elements. The code below shows a simple RSS feed file for my tutorial site:

<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0">
 <channel>
  <title>Scotty's Tutorials</title>
  <link>http://www.johnpscott.co.uk</link>
  <description>Visit Scotty's site for web 
               application development tutorials</description>
  <language>en-gb</language>
  <lastBuildDate>Mon, 02 Feb 2009 09:01:15 GMT</lastBuildDate>
  <copyright>Copyright: (C) John P Scott</copyright>
  <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
  <ttl>15</ttl>
  <image>
   <title>Scotty's Tutorials</title>
   <url>http://www.johnpscott.co.uk/images/scotty.jpg</url>
   <link>http://www.johnpscott.co.uk</link>
  </image>
  <item>
   <title>XHTML</title> 
   <description>XHTML is a standard for representing the 
     structure and content of a web page. Its predecessor
     HTML not only represented the content and structure, 
     but also included aspects such as the presentation
     of the information on the screen. However, attempting
     to change the design and presentation of the information
     using XHTML is now frowned upon and all page styling
     should now be done using a technique/standard called
     Cascading Style Sheets (CSS).</description>
   <link>http://www/johnpscott.co.uk/xhtmltutorial</link> 
   <guid isPermaLink="true">
      http://www/johnpscott.co.uk/xhtmltutorial</guid> 
   <pubDate>Mon, 02 Feb 2009 08:58:05 GMT</pubDate>
  </item>
  <item> 
   <title>CSS</title>
   <description>Cascading Style Sheets (CSS) define how to 
     display XHTML. The XHTML tutorial stressed the importance
     of separating content from style. XHTML simply codes the
     structure and content of the web page. CSS provide a
     powerful technique for determining the presentation of a
     webpage on a display and/or device.</description>
   <link>http://www/johnpscott.co.uk/csstutorial</link>
   <guid isPermaLink="true">
     http://www/johnpscott.co.uk/csstutorial</guid>
   <pubDate>Mon, 02 Feb 2009 08:59:05 GMT</pubDate>
  </item>
 </channel>
</rss>  

Analysis of the structure

For a single channel RSS feed we can break this structure up into sections and identify static and variable elements:

RSS Header section

The only variable bit of this will be the <lastBuildDate> element.

<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
 <channel>
  <title>Scotty's Tutorials</title>
  <link>http://www.johnpscott.co.uk</link>
  <description>Visit Scotty's site for web application
     development tutorials</description>
  <language>en-gb</language>
  <lastBuildDate>Mon, 02 Feb 2009 09:01:15 GMT</lastBuildDate>
  <copyright>Copyright: (C) John P Scott</copyright>
  <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
  <ttl>15</ttl>
  <image>
   <title>Scotty's Tutorials</title>
   <url>http://www.johnpscott.co.uk/images/scotty.jpg</url>
   <link>http://www.johnpscott.co.uk</link>
  </image>
News Items

Each RSS feed item will need building with values, most or all of which may come from your database.

   <item>
   <title>XHTML</title> 
   <description>XHTML is a standard for representing the 
     structure and content of a web page. Its predecessor
     HTML not only represented the content and structure, 
     but also included aspects such as the presentation
     of the information on the screen. However, attempting
     to change the design and presentation of the information
     using XHTML is now frowned upon and all page styling
     should now be done using a technique/standard called
     Cascading Style Sheets (CSS).</description>
   <link>http://www/johnpscott.co.uk/xhtmltutorial</link> 
   <guid isPermaLink="true">
      http://www/johnpscott.co.uk/xhtmltutorial</guid> 
   <pubDate>Mon, 02 Feb 2009 08:58:05 GMT</pubDate>
  </item>
Footer

We just need to close the <channel> and <rss> elements.

  </channel>
</rss>  

Building the RSS structure dynamically

The simplest way to build the RSS feed structure is to use an ASP.NET Repeater, linked to a table or view in your database. For this example I have a database table (tblTutorials) containing the fields: 'title', 'description', 'Date' and 'url', but your own application may use any fields and names you like.

The asp:Repeater code for this is:

<asp:Repeater runat="server" ID="repeater1"
              DataSourceID="SqlDataSource1">
 <HeaderTemplate><?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0">
  <channel>
    <title>Scotty's Tutorials</title>
    <link>http://www.johnpscott.co.uk</link>
    <description>This page is an RSS feed generated 
       from an SQL database table.</description>
    <language>en-gb</language>
    <lastBuildDate>date goes here</lastBuildDate>
    <copyright>Copyright: (C) John P Scott</copyright>
    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
    <ttl>15</ttl>
    <image>
     <title>Scotty's Tutorials</title>
     <url>http://www.johnpscott.co.uk/images/scotty.jpg</url>
     <link>http://www.johnpscott.co.uk</link>
    </image>
  </HeaderTemplate>
  <ItemTemplate>    <item>
     <title><%# Eval("title") %></title>
     <description><%# Eval("description") %></description>
     <link><%# Eval("url") %></link>
     <guid isPermaLink="true"><%# Eval("url") %></guid>
     <pubDate><%# Eval("Date", "{0:R}") %></pubDate>
    </item>
  </ItemTemplate>
  <FooterTemplate>  </channel>
</rss>
  </FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
     SelectCommand="SELECT * FROM [tblTutorials]">
</asp:SqlDataSource> 

Note how the header template uses some dummy text to fill the <lastBuildDate> element. This value should be replaced by the date and time of the latest item to be changed in the database - see below. For more information about the RSS v2.0 specification go to the RSS Specification website.

The only other issue of note is the way the <?xml ?> declaration follows immediately after the <HeaderTemplate> tag, as it must start in the first character of the first line of the resulting XML document.

Making it work

We now have a repeater which will render a full RSS feed in XML data file format, however, as it stands this will be embedded inside a plain old XHTML file. What we must now do is override the XHTML container and the 'text/html' content type and deliver just the Repeater's content with content type 'application/rss+xml'. We do this in the Page_Load event handler for our page.

The stages are:

  • Clear out the generated content and change the content type to 'application/rss+xml'.
  • Force the repeater to render it's contents into a text string.
  • Output the text string from the repeater.
  • Replace the dummy text for the repeater with the pubDate of the latest item.
  • Deliver the content.

We can use the System.Xml namespace to provide functions for copying the last pubDate value into the lastBuildDate element.

The code to do this is:

protected void Page_Load(object sender, EventArgs e)
{
  Response.Clear(); //clear page
  Response.ContentType = "application/rss+xml";
  Response.Flush(); // flush page buffer
  StringWriter tw = new StringWriter(); // container for rss
  HtmlTextWriter hw =
               new HtmlTextWriter(tw); // pipe to the container
  repeater1.DataBind(); // force Repeater to load it's data
  repeater1.RenderControl(hw); // render it to the container
  XmlDocument feedXML =
               new XmlDocument(); // get an XmlDocument object
  feedXML.LoadXml(tw.ToString()); // fill it with the text
                                  // from the Repeater
  string lastBuildDate =
    feedXML.GetElementsByTagName
      ("pubDate")[0].InnerText; // get the pubDate from the
                                // first entry
  feedXML.GetElementsByTagName
    ("lastBuildDate")[0].InnerText =
          lastBuildDate; // put it into lastBuildDate element
  Response.Write(feedXML.InnerXml); // write out the rss xml
  Response.End(); // complete the page
} 

Finishing touch

Don't forget to put an RSS icon on your home page linked to your RSS feed page, otherwise no one will know about it! You should also consider adding a link element in the <head> element of all your pages, informing browsers of the availability of your feed.

<link href="http://www.domain.com/rsspagename.aspx"
      rel="alternate" type="application/rss+xml"
      title="Put feed title here" />
Valid XHTML 1.0! | Valid CSS! | WCAG Approved AA
Page design by: John P Scott - Hosting with: Netcetera