Generating an RSS feed with XmlSerializer, ASP.NET and C#


I wanted to play around with creating a simple RSS feed from an ASP.NET page. I looked around for existing solutions. Any searches involving RSS are a bit hopeless, but I found Jason Salas’ RSS server control. It’s nice because you can easily hook it up to a SQL source. I didn’t need that though, and I didn’t really like the idea of creating the XML by hand, especially since there’s such a nice XmlSerializer. So, I put together a simple solution using an XmlSerializer. As a disclaimer, I haven’t really dug into the Rss specifications in detail, and this probably isn’t valid RSS. But, it may serve as a good starting point for someone else.

 

To start with, we define classes for each of the different elements with the attributes necessary for XmlSerializer to spit out the right XML:

 

[XmlRoot("rss")]

public class Rss

{

    [XmlAttribute]

    public string version = "2.0";

    public Channel channel = new Channel();

}

 

[XmlRoot("channel")]

public class Channel

{

    public string title;

    public string link;

    public string description;

    [XmlElement]

    public List<Item> item = new List<Item>();

}

 

public class Item

{

    public string title;

    public string link;

    public string guid;

    public string description;

}

 

I only included a few of the possible properties for channels and items. Adding the others is pretty straightforward. If you add a field and don’t set a value, it won’t generate any XML. The only slightly complex one would be the publication date fields because they are supposed to be in RFC822 format, and I don’t know of a built in function to format DateTimes to that format, although I see there is one here.

 

To use this from an ASP.Net page, you’ll first want to create a basically blank page (note: this is the latest Whidbey syntax):

 

<%@ Page Language="C#" CompileWith="Default.aspx.cs" ClassName="Default_aspx" %>

 

Then, in the codebehind file:

 

[Insert all the usings you need here]

 

public partial class Default_aspx

{

 

    protected void Page_Load(object sender, EventArgs e)

    {

        Response.ContentType = "text/xml;charset=utf-8";

 

        Rss feed = new Rss();

 

        // Set up channel parameters

        feed.channel.title = "Test feed";

        feed.channel.link = "http://blogs.msdn.com/dancre";

        feed.channel.description = "Simple feed using XmlSerialization";

 

        // Add some items

        Item item1 = new Item();

        item1.title = "item 1";

        item1.description = "item 1 description";

        item1.guid = "something unique 1";

        feed.channel.item.Add(item1);

 

        Item item2 = new Item();

        item2.title = "item 2";

        item2.description = "item 2 description";

        item2.guid = "something unique 2";

        feed.channel.item.Add(item1);

 

        // Serialize Xml to output stream

        XmlSerializer serializer = new XmlSerializer(typeof(Rss));

        serializer.Serialize(Response.OutputStream, feed);

    }

}

 

Updated to not use memory stream, but just output to response. Thanks, Matt!

 


Comments (8)

  1. Phil Weber says:

    Hi, Dan: Guess you didn’t Google for "rss vb.net" or you would have found this: http://www.philweber.com/articles/easy_rss_in_vbnet.htm 😉

  2. Matt Berther says:

    XmlSerializer serializer = new XmlSerializer(typeof(Rss));

    serializer.Serialize(Response.OutputStream, feed);

  3. Kevin says:

    ms.Position = 0; //I think you need to reset the start position of the MemoryStream

    byte[] buffer = new byte[1024];

    int count;

    do

    {

    count = xmlOutput.Read(buffer,0,buffer.Length);

    Response.Output.Write(buffer,0,count);

    }while(count > 0);

    I must have written that code one too many times. I remember the right order of the parameters.

    Don’t forget to Dispose of the MemoryStream.

  4. Dan Crevier says:

    Thanks Matt, that seems so obvious now. As you can guess, I’m pretty new to ASP.Net 🙂 I’ve updated the code with your suggestion. So, the MemoryStream comments may no longer make sense to people reading this for the first time – basically I had a gross hack using MemoryStream to output the Xml before.

  5. Quite a cool code. it helped me a lot. i can serilize the objects, now i want to Deserlize the data i receive at the other end

Skip to main content