Syndicating Feeds Using C#

How many of you still use RSS?

I’m willing to bet the answer is “Not Many”, but the reality is, you use it without realizing.

Take your blog.

Does it run on WordPress? Well, take a close look at it, then, because it’ll be using RSS. Your browser, too, has an RSS Feed reader built into it, and let’s not forget many of the Google APIs, such as the YouTube search API, that returns its results as an Atom feed, which is a newer type of the original RSS Protocol. Even this blog you’re reading now has an RSS feed if you look at the .NET Nuts and Bolts index page.

The fact is that XML-based RSS & Atom feeds are still used in quite a lot of places, and although they may not be as popular as they once were, they do still have their uses.

I have a client that uses them for workplace notifications. A custom plug-in has been written for the Chrome browser all his staff use. this-plug in monitors an XML end point on a central Windows service, and when there’s a short message to send out to all staff, it’s sent to this service, which sends it out as an RSS feed, which is then rendered in the user’s browser.

I have a similar setup on the PC I’m writing this on. It tells me when my phone’s ringing. (I often don’t hear it because I have headphones on.)

What many folks don’t know, however, is that .NET has built-in classes for creating syndication feeds, and these can be found in the “System.ServiceModel.Syndication” namespace.

Reading a Feed

Reading a feed is simple. The RSS feed for this blog can be found at We can easily read this and display its contents by using the following code.

Rss20FeedFormatter rssFormatter;

using(var xmlReader = XmlReader.Create
   rssFormatter = new Rss20FeedFormatter();


var title = rssFormatter.Feed.Title.Text;

foreach (var syndicationItem in rssFormatter.Feed.Items)
   Console.WriteLine("Article: {0}",
   Console.WriteLine("URL: {0}",
   Console.WriteLine("Summary: {0}",

When added to a console mode program, the output looks like this:

Figure 1: The console mode output

It’s up to you how you deal with it, but you can see just how easy it is to grab the XML from a feed, pass it to the Rss Version 2 formatter, and then iterate over the collection of items available.

If the feed specifies it, each item also has a list of authors, tags, and various other bits of meta information that could be used to decide how to present an item. You could, for example, in the case of an internal notification system, decide to show items with an “Urgent” tag immediately.

Producing RSS Feeds

Reading them is one thing, but the syndication classes also can be used to produce RSS feed data just as easily. Let’s imagine we have the following object definition:

public class ArticleSummary
   public string Title { get; set; }
   public string Url { get; set; }
   public string Summary { get; set; }

We can produce a feed similar to this blog’s, by using the formatter in the reverse direction.

First, let’s create some data to add to the feed.

List<ArticleSummary> articles = new List<ArticleSummary>
   new ArticleSummary {Title = "Article 1",
      Url = "",
      Summary = "This is article one"},
   new ArticleSummary {Title = "Article 2",
      Url = "",
      Summary = "This is article two"},
   new ArticleSummary {Title = "Article 3",
      Url = "",
      Summary = "This is article three"},
   new ArticleSummary {Title = "Article 4",
      Url = "",
      Summary = "This is article four"},
   new ArticleSummary {Title = "Article 5",
      Url = "",
      Summary = "This is article five"}

Once you have some data, you can easily construct the feed by using the following code:

var newFeed = new SyndicationFeed();
newFeed.Generator = "My .NET Program";
newFeed.Language = "en-gb";
newFeed.LastUpdatedTime = DateTime.UtcNow;
newFeed.Title = SyndicationContent.
   CreatePlaintextContent("My Article Feed");
newFeed.Items = articles.Select(article =>
   new SyndicationItem
   Title = SyndicationContent.
   Content = SyndicationContent.
   BaseUri = new Uri(article.Url)

To format it as an Rss version 2 feed, use the following:

SyndicationFeedFormatter formatter =
   new Rss20FeedFormatter(newFeed);

or for an Atom feed, this:

SyndicationFeedFormatter formatter =
   new Atom10FeedFormatter(newFeed);

At this point, you have a streamable XML object, so you can send it to an MVC Return type, an XML Output stream, a Linq to XML query, and all sorts of other things.

To close this post, let’s push it out as a formatted XML string to the console:

var output = new StringBuilder();
using(var writer = XmlWriter.Create(output,
   new XmlWriterSettings{Indent = true}))


If everything has worked okay, you should see the following:

Figure 2: Formatted XML is now visible

Discovered a strange .NET object you didn’t know existed, or just have an idea for a subject this column should cover? Ping me on Twitter as @shawty_ds and let me know. You might see if featured in a future post.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read