Geeks With Blogs
Scott Van Vliet Once a Developer, Always a Developer

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:

 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 on Wednesday, August 30, 2006 1:12 AM ASP.NET , C# , Technology | Back to top


Comments on this post: RSS in .NET Made Easy with XML Serialization

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
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!
Left by skillet on Aug 31, 2006 2:35 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
Dude: http://www.philweber.com/articles/easy_rss_in_vbnet.htm
Left by Phil Weber on Sep 11, 2006 3:40 AM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
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
Left by skillet on Sep 11, 2006 3:33 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
Sorry :(
Left by Alexis on Jun 09, 2007 5:54 AM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
Interesting...
Left by Stratis on Jun 10, 2007 5:48 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
Cool.
Left by Carolos on Jun 11, 2007 5:27 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
Sorry :(
Left by Sotiris on Jun 13, 2007 8:52 AM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
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
Left by Tommy T on Jul 23, 2008 12:41 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
There are better ways to do this in .NET 3.5. I blogged about it: http://jclaes.blogspot.com/2009/10/rss-in-net.html
Left by Nemesis116 on Oct 05, 2009 6:44 PM

# re: RSS in .NET Made Easy with XML Serialization
Requesting Gravatar...
These steps are easy to follow and really works good. - Antiquities of California
Left by Alena Anderson on Dec 16, 2016 10:23 PM

Your comment:
 (will show your gravatar)


Copyright © svanvliet | Powered by: GeeksWithBlogs.net