Publish Rss feed using the Syndication namespace

Here’s an easy way to expose feeds using the new System.ServiceModel.Syndication namespace.

Instead of dealing with XmlDocument the Rss20FeedFormatter class will be responsible to format the correct xml content for our feed.

   1: SyndicationFeed feed = GetSyndicationFeed();
   2: Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
   3: using (XmlWriter writer = XmlWriter.Create(context.Response.OutputStream))
   4: {
   5:    rssFormatter.WriteTo(writer);
   6:    writer.Flush();
   7: }

The feed object is composed by properties like the author and the language but, more important, by the list of the feed categories and the items (posts, articles etc).

   1: SyndicationFeed newsFeed = new SyndicationFeed("Tryingtocode", "Sample RSS Feed", new Uri(""));
   2: newsFeed.Authors.Add(new SyndicationPerson("mail", "author name", "http://link"));
   3: newsFeed.Language = "en-US";
   4: newsFeed.Generator = "Custom RSS generator";
   5: newsFeed.Copyright = new TextSyndicationContent("© Copyright 2010");
   6: // Add categories
   7: List<SyndicationCategory> categories = GetAllSyndicationCategories();
   8: foreach(SyndicationCategory category in categories)
   9: {
  10:    newsFeed.Categories.Add(category);
  11: }
  12: newsFeed.Items = GetAllSyndicationItems();
  14: return newsFeed;

Here’s how the page will look:


Source code: RssGenerator sample


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s