Click here to Skip to main content
15,867,991 members
Articles / Programming Languages / XML

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 79.9K   777   27   6
Creating classes that model an RSS, in C#

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).

C#
[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.

C#
[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:

C#
[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:

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

Save:

C#
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)


Written By
Software Developer MedAssets
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

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
v-man200017-Dec-08 8:00 
AnswerRe: An RSS reader Pin
Derek Burnett17-Dec-08 8:04
Derek Burnett17-Dec-08 8:04 
General99% Ok Pin
Henrik Carlsen8-Sep-08 12:49
Henrik Carlsen8-Sep-08 12:49 
GeneralRe: 99% Ok Pin
Derek Burnett10-Sep-08 12:07
Derek Burnett10-Sep-08 12:07 
GeneralGenerating RSS Web feeds Pin
Clinton Gallagher8-Sep-08 8:21
professionalClinton Gallagher8-Sep-08 8:21 

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.