Scott Van Vliet

Less Talk, More Rock

RSS in .NET Made Easy with XML Serialization

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:

 RSS Object 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!

Posted: Aug 29 2006, 06:12 PM by skillet | with 15 comment(s)
Filed under: , ,

Comments

RJ Dudley said:

# August 30, 2006 3:55 PM

Scott Hanselman said:

The source code for DasBlog uses XmlSerializer for RSS and Atom. We also support extensions to RSS like comments and things in other namespaces...something to think about.

# August 31, 2006 12:29 AM

skillet said:

Scott,

Thanks for the tip.  I figured someone else had to have thought of this too :)  To your point, extending this model with RSS extensions is, again, quite trivial.  I literally knocked out this code in 30 minutes, so I’m sure there’s much more value to be added to such components.

Cheers!

# August 31, 2006 10:35 AM

.Net Discoveries said:

Prologue Not long ago, I discovered how cool RSS can be. I was having trouble with my Peerflix account,

# August 31, 2006 12:32 PM

Phil Weber said:

# September 10, 2006 11:40 PM

skillet said:

Phil,

Thanks for the heads-up.  Your post didn't come up in my Google attempt (again, I might have been sucking at that time.)  Although, for my purposes, I would've had to rewrite in C# anyway ;)

Best,

Scott

# September 11, 2006 11:33 AM

Brian Barnett said:

Here's the scenerio.  I'm contemplating using RSS as a data exchange mechanism.  Some of the data can be sensitive therefore the RSS feed generator needs to be aware of the user submitting the URL.  The URL will contain parameters indicating the requested data.  The potential clients could be any RSS reader or an app that's automatically pulling the data using techniques Scott mentions above.  

So, is there a way to securely pass username/pswd info or security tokens to an RSS feed generator?

Has anyone attempted this?  Any ideas?  

Thanks,

Brian

# October 24, 2006 10:21 AM

Alexis said:

Sorry :(

# June 9, 2007 1:54 AM

Stratis said:

Interesting...

# June 10, 2007 1:48 PM

Carolos said:

Cool.

# June 11, 2007 1:27 PM

Sotiris said:

Sorry :(

# June 13, 2007 4:52 AM

A few good links | Aaron Lerch said:

Pingback from  A few good links | Aaron Lerch

# August 26, 2007 2:51 PM

Rajesh said:

Is Discription at Chanel level is working?  channel title is displayed with link but not discription & publish dates

# November 4, 2007 8:42 PM

Tommy T said:

I was trying to download your file attached to this page, but it was pointing most likely to your old site.

Is it possible for you to mail me the code so I can explore it, or just  update the link above ?

Thank you in advance

/tommyt

# July 23, 2008 8:41 AM

Deepak.sharma.softwares said:

dsf

# May 1, 2009 2:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)