Click here to Skip to main content
15,393,946 members
Articles / Programming Languages / XML
Posted 2 Sep 2008


27 bookmarked

Using Serialization to Produce RSS Feeds from C# Classes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
2 Sep 2008CPOL2 min read
Creating classes that model an RSS, in C#


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.


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.

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.


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


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.


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 ! Pin
Mazen el Senih19-May-13 10:57
professionalMazen el Senih19-May-13 10:57 
QuestionAn RSS reader Pin
v-man200017-Dec-08 8:00
Memberv-man200017-Dec-08 8:00 
AnswerRe: An RSS reader Pin
Derek Burnett17-Dec-08 8:04
MemberDerek Burnett17-Dec-08 8:04 
General99% Ok Pin
Henrik Carlsen8-Sep-08 12:49
MemberHenrik Carlsen8-Sep-08 12:49 
GeneralRe: 99% Ok Pin
Derek Burnett10-Sep-08 12:07
MemberDerek Burnett10-Sep-08 12:07 
GeneralGenerating RSS Web feeds Pin
Clinton Gallagher8-Sep-08 8:21
professionalClinton Gallagher8-Sep-08 8:21 
Very interesting Derek. I'm developing a FEEDgenerator and this methodology you've devised is very interesting. Given the time to evaluate your source further (i.e. beat myself up to make the time) I hope to get more insight how to use the approach.

Your comments regarding enclosures warrants further discussion as does how other developers have decided to map the contents of a Web feed to a relational db or decided to keep the file in the file system; criteria which must be determined but remain obscure and rarely discussed in the context of RSS web publishing.


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

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