65.9K
CodeProject is changing. Read more.
Home

Using Serialization to Produce RSS Feeds from C# Classes

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6 votes)

Sep 2, 2008

CPOL

2 min read

viewsIcon

80984

downloadIcon

781

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

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