Partager via


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 = "https://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

  • Anonymous
    December 13, 2004
    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 ;-)
  • Anonymous
    December 13, 2004
    Oh well :-)
  • Anonymous
    December 13, 2004
    XmlSerializer serializer = new XmlSerializer(typeof(Rss));
    serializer.Serialize(Response.OutputStream, feed);
  • Anonymous
    December 13, 2004
    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.
  • Anonymous
    December 14, 2004
    The comment has been removed
  • Anonymous
    January 05, 2005
    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