Click here to Skip to main content
Click here to Skip to main content

Using serialization to produce RSS feeds from C# classes

By , 2 Sep 2008
Rate this:
Please Sign up or sign in to vote.

Introduction

I've seen a few APIs based around RSS feeds, and decided to take a different approach. The idea behind mine is to model the feed in C# classes. If done correctly, the classes serialization could produce an RSS feed, and the deserialization would handle all the XML parsing.

Background

An RSS feed is an XML based format for distributing site content. They have three basic elements: the RSS feed, a channel within the feed, and different Item elements within the channel.

Many Microsoft products support RSS feeds, including Outlook and Internet Explorer.

Using the code

The program contains three main classes to model the feed. Rss, Channel, and Item.

The key to ridding us of XML parsing duties is the XMLSerializer class. It serializes a class for .NET, and allows for classes and properties to specify attributes that override the default serialization.

Basic serialization will work as is for the Item and Rss class. They contain properties that when serialized will produce the correct elements for the feed. We will, however, override the element name in Rss so we can name the class with an uppercase (the class name is otherwise used as the element name).

[XmlRootAttribute(ElementName = "rss"]    //change the name to rss, not Rss
public class Rss
{

Also, the version of RSS you are using needs to be represented as an attribute instead of as an element. We use the XmlAttribute attribute for this.

[XmlAttribute("version")]
public string Version
{
  ...

The Channel class has to override the default serialization. It needs a container of Item objects, but by default, the collection variable name will become the element under Channel. We need to use the XmlElement attribute to override the way the array is represented in the feed.

We specify a name for an "item" with a type of our Item class:

[XmlElement(ElementName="item", Type=typeof(Item))]
public List<Item> items
{
     ...

This does the trick. Now, instead of an <items> element, we get a list of <item>s, each containing our serialized Item class.

Now, all we have is the rather trivial code of using the XmlSerializer class to load or save our feed. I accomplish this with two, three line functions in the Rss class.

Load:

XmlSerializer serializer = new XmlSerializer(typeof(Rss));
Rss feed = (Rss)serializer.Deserialize(fileStream);
return feed;

Save:

XmlSerializer serializer = new XmlSerializer(typeof(Rss));
FileStream stream = File.Create(filename);
serializer.Serialize(stream, this);

Points of interest

I added an example with a simple aggregator displaying how the class works. The <enclosure> element isn't supported due to the lack of feeds I have found which contain it. I've tested quite a few feeds, and it works great with them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Derek Burnett
Software Developer MedAssets
United States United States
No Biography provided

Comments and Discussions

 
GeneralAwesome ! PinmemberMazen el Senih19-May-13 10:57 
QuestionAn RSS reader Pinmemberv-man200017-Dec-08 8:00 
AnswerRe: An RSS reader PinmemberDerek Burnett17-Dec-08 8:04 
General99% Ok PinmemberHenrik Carlsen8-Sep-08 12:49 
GeneralRe: 99% Ok PinmemberDerek Burnett10-Sep-08 12:07 
GeneralGenerating RSS Web feeds PinmemberclintonG8-Sep-08 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Sep 2008
Article Copyright 2008 by Derek Burnett
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid