Update 2010-03-23 – Sorry, but the images and downloads were hosted on a server that is no longer available. I will try to find the sample code bits and images on a backup sometime!
Based on my previous rants, here’s a stereotype for this post:
<<Casual_English_Used>>
So I was looking for a real quick way to publish an RSS feed from a Web application I created for a friend and found some great articles and components (RSS Toolkit for ASP.NET 2.0.) However, I didn’t find anything that used XML Serialization (specifically) to work with the RSS 2.0 Specification (maybe my Google skills are truly weak today.) I use XmlSerializer religiously, and thought it would be quite trivial to build a set of classes to quickly create and publish an RSS feed. Thus, as any geek would do, I did just that.
Take a gander at the following model:

These classes are adorned with System.Xml.Serialization attributes, which comply with the RSS 2.0 Specification. To create an RSS document, it’s no more difficult than creating and populating a PONO (Plain Ole .NET Object.)
RssDocument rss = new RssDocument();
rss.Channel.Title = "My Feed";
rss.Channel.Link = "http://www.example.com/";
rss.Channel.Description = "A very cool feed on fun stuff.";
rss.Channel.Items.Add(new RssChannelItem());
rss.Channel.Items[0].Title = "Post #1";
rss.Channel.Items[0].Link = "http://www.example.com/content/post1.html";
rss.Channel.Items[0].Description = "The details for Post #1...";
rss.Channel.Items[0].Guid = "00000000-0000-0000-0000-000000000000";
To generate the XML for this feed, simply call the ToString() method:
rss.ToString();
It’s that simple! Who’d have thought it’d be this easy :) What’s more, I created a method to fetch an RSS feed and deserialize it into an instance of RssDocument (listed below.)
/// <summary>
/// Creates an RssDocument instance based on the RSS feed at
/// the specified URI.
/// </summary>
/// <param name="uri">The URI of the RSS feed to be loaded.</param>
/// <returns>
/// An instance of RssDocument based on the RSS feed
/// at the specified URI.
/// </returns>
public static RssDocument GetFeed(string uri)
{
RssDocument feedDocument = null;
WebClient client = null;
byte[] feedData = null;
MemoryStream ms = null;
XmlSerializer xs = null;
try
{
client = new WebClient();
feedData = client.DownloadData(uri);
ms = new MemoryStream(feedData);
xs = new XmlSerializer(typeof(RssDocument));
feedDocument = xs.Deserialize(ms) as RssDocument;
}
catch (Exception ex)
{
//
// TODO: Handle Exception.
//
throw;
}
finally
{
ms.Close();
ms.Dispose();
client.Dispose();
}
return feedDocument;
}
It’s not bulletproof, but gets the job done rather elegantly. If you’d like a copy, grab it from the link below.
http://www.scottvanvliet.com/downloads/Boardworks.Rss.zip
Enjoy!