Hector Correa

Expose your data as an RSS Feed with .NET 3.5

In talking with my one of my friends a couple of months ago I found that .NET 3.5 now has built-in classes to generate RSS Feeds. This was great news to me since both my personal blog (this site) and the DotWiki expose data as RSS Feeds but for the longest time I've been using an unsupported library that I downloaded from the web years ago.

Last month I updated the DotWiki to use these new .NET classes (instead of the old unsupported library) to expose recent changes to Wiki topics as an RSS Feed. Although I found the .NET classes to generate feeds rather comprehensive I had a hard time finding good practical examples on MSDN on how to use them[1]. Lucky for me Bipin Joshi posted two great examples on how to generate RSS feeds and consume RSS feeds with these .NET classes.

Below is how the code in the DotWiki looks like to expose as an RSS Feed the list of changes in the last 24 hours. The complete code is available, follow the source code links on this page.

using System.ServiceModel.Syndication;

// Define the feed 
SyndicationFeed feed = new SyndicationFeed();
feed.Title = new TextSyndicationContent("DotWiki");
feed.Copyright = new TextSyndicationContent("Copyright (C) 2008. All rights reserved.");
feed.Description = new TextSyndicationContent("DotWiki RSS Feed");

// ... and the link for the site the feed represents
SyndicationLink link = new SyndicationLink();
link.Title = "DotWiki";
link.Uri = new Uri("http://code.hectorcorrea.com/DotWiki/");
feed.Links.Add(link);

// Load items that we want on the feed (more on this below)
feed.Items = LoadBlogItems();

// Prepare the response with the proper encoding and content type  
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";

// Dump the content of the feed as an RSS 2.0 feed into the response
XmlWriter rssWriter = XmlWriter.Create(Response.Output);
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(rssWriter);
rssWriter.Flush();
rssWriter.Close();
Response.End();

The code for LoadBlogItems which is the one that adds individual items to the feed is shown below. This method returns a generic list of SyndicationItems. Each element in the list contains the information about a topic that has been changed in the last 24 hours. Classes Topic and TopicList in this example are DotWiki specific classes but the rest is plain .NET code.

private List<SyndicationItem> LoadBlogItems()
{
    List<SyndicationItem> items = new List<SyndicationItem>();

    TopicList topics = TopicList.GetRecentChanges(GetLatest.Last24Hours);
    foreach (TopicInfo topicInfo in topics)
    {
        Topic topic = Topic.GetTopic(topicInfo.Id);

        // Define basic element about this topic
        SyndicationItem item = new SyndicationItem();
        item.Id = topic.Id.ToString();
        item.Title = new TextSyndicationContent(topic.Name);
        item.Content = new TextSyndicationContent(topic.Content);
        item.LastUpdatedTime = topic.UpdatedOn;
        string url = string.Format("http://code.hectorcorrea.com/DotWiki/?topic={0}", HttpUtility.UrlEncode(topic.Name));
        item.AddPermalink(new Uri(url));

        // ...and the author of the topic
        SyndicationPerson author = new SyndicationPerson();
        author.Email = "somebody@somesite.com";
        item.Authors.Add(author);

        items.Add(item);
    }
    return items;
}

The RSS Feed generated by the .NET classes seems to be fully compatible with popular feed readers like Bloglines and Google Reader. From reading the MSDN documentation on the System.ServiceModel.Syndication namespace it seems that there is support for Atom 1.0 in addition to RSS 2.0. I have only tested the RSS 2.0 formatter, though.

[1] Update: Actually there is a rather nice MSDN code sample under the SyndicationFeed class documentation that shows how to expose the same feed as an Atom or RSS feed.