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 http://www.codeguru.com/rss/csharp/. We can easily read this and display its contents by using the following code.

Rss20FeedFormatter rssFormatter;

using(var xmlReader = XmlReader.Create
   ("http://www.codeguru.com/rss/csharp/"))
{
   rssFormatter = new Rss20FeedFormatter();
   rssFormatter.ReadFrom(xmlReader);

}

var title = rssFormatter.Feed.Title.Text;

foreach (var syndicationItem in rssFormatter.Feed.Items)
{
   Console.WriteLine("Article: {0}",
      syndicationItem.Title.Text);
   Console.WriteLine("URL: {0}",
      syndicationItem.Links[0].Uri);
   Console.WriteLine("Summary: {0}",
      syndicationItem.Summary.Text);
   Console.WriteLine();
}

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

Feeds1
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 = "http://article1.com",
      Summary = "This is article one"},
   new ArticleSummary {Title = "Article 2",
      Url = "http://article2.com",
      Summary = "This is article two"},
   new ArticleSummary {Title = "Article 3",
      Url = "http://article3.com",
      Summary = "This is article three"},
   new ArticleSummary {Title = "Article 4",
      Url = "http://article4.com",
      Summary = "This is article four"},
   new ArticleSummary {Title = "Article 5",
      Url = "http://article5.com",
      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.
      CreatePlaintextContent(article.Title),
   Content = SyndicationContent.
      CreatePlaintextContent(article.Summary),
   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}))
{
   formatter.WriteTo(writer);
   writer.Flush();
}

Console.Write(output.ToString());

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

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



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date