Setting up an RSS Feed in ASP.Net

4/9/2008

Out of all the "Web 2.0" technologies that we've been given, the two that everyone wants on their site are AJAX and an RSS Feed (sadly even if the content is static). Both of which are extremely easy to setup and use. For ASP.Net programmers, Microsoft has added AJAX to 3.5 (no longer an external DLL) and there are about 10,000 versions of RSS middleware out there that plugs in just fine. However many programmers have no idea what is going on under the hood or even how to implement their own version of these technologies. The one that we're interested in today though are RSS feeds.

What is an RSS Feed?

RSS stands for Really Simple Syndication. As far as what it is; have you ever dealt with an XML document before? That's all that an RSS feed is, an XML document with a specific format. In fact the format is freely available here. Note though that there are multiple formats running around for syndication. ATOM, RSS .9, .91, 1.0, 2.0, etc. The version I'm going to go over here though is RSS 2.0.

How can I create an RSS Feed?

As always, I'm going to give you some code for this:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Xml; 
   5:  
   6: namespace Site
   7: {
   8:     /// <summary>
   9:     /// WebPage: RSSFeed.aspx
  10:     /// Class: RSSFeed
  11:     /// Creator: James Craig
  12:     /// Description: Implements a simple RSS feed.
  13:     /// </summary>
  14:     public partial class RSSFeed : System.Web.UI.Page
  15:     {
  16:         /// <summary>
  17:         /// Called at page loading
  18:         /// </summary>
  19:         /// <param name="sender"></param>
  20:         /// <param name="e"></param>
  21:         protected void Page_Load(object sender, EventArgs e)
  22:         {
  23:             //Now make the response an xml file
  24:             Response.Clear();
  25:             Response.ContentType = "text/xml";
  26:             XmlTextWriter RSSOutput = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
  27:             //initialize the xml document for RSS using version 2.0
  28:             RSSOutput.WriteStartDocument();
  29:             RSSOutput.WriteStartElement("rss");
  30:             RSSOutput.WriteAttributeString("version", "2.0");
  31:             LoadItems(RSSOutput); 
  32:  
  33:             //Close the document
  34:             RSSOutput.WriteEndElement();
  35:             RSSOutput.WriteEndDocument();
  36:             RSSOutput.Flush();
  37:             RSSOutput.Close();
  38:         } 
  39:  
  40:         /// <summary>
  41:         /// Loads Items and adds them to the feed.
  42:         /// </summary>
  43:         /// <param name="RSSOutput"></param>
  44:         private void LoadItems(XmlTextWriter RSSOutput)
  45:         {
  46:             //Load the rss items (database call, etc. would go here) 
  47:  
  48:             //Create a default channel
  49:             RSSOutput.WriteStartElement("channel");
  50:             RSSOutput.WriteElementString("title", "Channel Title");
  51:             RSSOutput.WriteElementString("link", "Your website");
  52:             RSSOutput.WriteElementString("description", "Description of the channel");
  53:             RSSOutput.WriteElementString("copyright", "Copyright " + DateTime.Now.Year.ToString() + ". All rights reserved."); 
  54:  
  55:             //Now output each of the announcements.
  56:             foreach (Item in your list of items)
  57:             {
  58:                 RSSOutput.WriteStartElement("item");
  59:                 RSSOutput.WriteElementString("title", "Item's Title");
  60:                 RSSOutput.WriteElementString("description", "Actual text of the item");
  61:                 RSSOutput.WriteElementString("pubDate", "Item's date (use ToString("R") for the date output)";
  62:                 RSSOutput.WriteElementString("link", "Link to the item");
  63:                 RSSOutput.WriteEndElement();
  64:             }
  65:             //Close everything up.
  66:             RSSOutput.WriteEndElement();
  67:         }
  68:     }
  69: }

You might have noticed that the XmlTextWriter is used rather extensively. As I stated before, we're dealing with an XML document and we really don't want to deal with the brackets, etc. so we'll use what Microsoft gives us. Anyway, lets go over the parts of the RSS File:

  • Channel - This is just a used as a way to break up your feed into sections. For example if there were two or more authors for this blog, I could set up a channel for each one allowing services and people to search for the specific author easily. We can declare multiple channels if we want, the example above just has one though.
    • Title - The title of the channel
    • Link - A link back to the source of the channel (your website most likely)
    • Description - The description of the channel
    • Copyright - Get the feeling that these are a bit self explanatory?
  • Item - A channel is made up of a collection of items. These items are basically the news posts, pods, etc. that you're feed contains.
    • Title - The title of the item
    • Description - The actual item content.
    • pubDate - The date the item was published.
    • Link - A link back to this particular item on your website.

There are, of course, other fields that we can fill out (for example the language the item is in, webmaster email address, the amount of time the feed can be cached, etc.) but the ones above are probably the most basic. Now if you want something a bit more robust, you can always use Microsoft's built in classes for handling RSS Feeds (SyndicationFeed is the class you're interested in) or you can even use the one in my utility library (or the tens of thousands of other libraries for it out there). Anyway, try out the code, leave feedback, and as always happy coding.



Comments