Tag: rss

rss 2.0 feed format tutorial for .NET

Posted by – August 22, 2007

I have been creating custom blog software using csharp.net. One task that I accomplished today was getting both an RSS and an ATOM feed setup. This will allow people to read the blog on sites like netvibes, my yahoo, or google IG. So heres how I did it:

  1. Change the content type of the page to text/xml

    This is needed so that other computers and browsers will know that it is an XML page they are reading, and not the standard HTML. Luckily this is simple to do, simply add ContentType=”text/xml” to the Page directive, like so:

    
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="rss.aspx.cs" Inherits="rss" ContentType="text/xml"%>
    
  2. Setup RSS 2.0 XML Document

    Now in our code behind file, we can write directly to the output stream, as the proper header is already set. You may think about using Response.Write to create the RSS XML tags, but there is a much easier way. You can use an XmlTextWriter to achieve the same result, without needing to escape all characters that can’t normally be in an xml file

    
            // Create a new XmlTextWriter instance
            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
    
            // Create Document
            writer.WriteStartDocument();
                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                    //Set Channel Information
                    writer.WriteStartElement("channel");
                        writer.WriteElementString("title","[SITE_TITLE]");
                        writer.WriteElementString("link",[SITE_URL]);
                        writer.WriteElementString("description",[SITE DESCRIPTION]);
                        writer.WriteElementString("lastBuildDate",[RFC-822 DATE]);
    

    Here we setup the start of the XML document with all required fields. The lastBuildDate element should be the last time the xml feed has changed, which should also be the last time something new was posted.

  3. Output Post information

    The standard is to display them in reverse chronological order. Most likely you will be creating these in a loop, like so:

    
    for (int i = 0; i < postList.Count; i++)
    {
    
        writer.WriteStartElement("item");
            writer.WriteElementString("title",postList[i].pageTitle);
            writer.WriteElementString("link", postList[i].url);
            writer.WriteElementString("description", postList[i].content);
            writer.WriteElementString("pubDate", postList[i].postDate);
            writer.WriteElementString("guid", postList[i].url);
        writer.WriteEndElement();
    }
    

    Here I am assuming postList is a list of object containing post information. The pubDate tag is the date that the post was published, and guid is a globally unique identifier for the post. If you have a GUID setup in the database for the post you can use it, otherwise it's easiest to just use the permalink (url).

  4. Close Document

    Now that we have displayed all relevant information, we simply need to close open tags, and end the xml document

    
                    writer.WriteEndElement();
                writer.WriteEndElement();
            writer.WriteEndDocument();
    writer.Close();    
    

    Thats all there is to it. You can now use this page to submit to sites such as netvibes, google ig, my yahoo, or any other services that will parse rss 2.0 xml feeds.