December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

Untitled 1

Using Syndication Classes to Generate RSS Feeds

Introduction

In the previous article I discussed how to consume RSS feeds exposed by other web sites onto your site. You may also want to expose your own site content as RSS or ATOM feed. The syndication classes also allow you to accomplish this easily. This article shows how.

Example

One of the advantage of using .NET syndication classes is that you need not bother about the intricacies of the feed format (RSS, ATOM etc.). You build your feed in a format neutral fashion and while serializing it decide which format to be used viz. RSS or ATOM.

In order to expose your site content as a feed you need to perform the following steps:

  • Create a SyndicationFeed object and specify its properties. These properties are applicable to the feed as a whole.
  • Create one or more SyndicationItem objects. Each instance represents a single feed item. A feed item can also have one or more SyndicationPerson and SyndicationLink associated with it.
  • Once created add SyndicationItem to the SyndicationFeed
  • Decide the feed format viz. RSS or ATOM
  • Serialize the entire SyndicationFeed to the response stream

Let's put all the above steps to work by developing a simple web form. For the sake of simplicity we will not focus on database interactions. We will assume that your code has, by some means, access to the data to be exposed.

Begin by creating a new web site. Make sure to refer required assemblies as mentioned in the previous article.

Create SyndicationFeed object

The first step to expose your content is to create a SyndicationFeed object that represents your feed.

SyndicationFeed feed = new SyndicationFeed();
feed.Title = new TextSyndicationContent
("DotNetBips.com RSS Feed");
feed.Copyright = new TextSyndicationContent
("Copyright (C) 2008. All rights reserved.");
feed.Description = new TextSyndicationContent
("Rss Feed Generated Via .NET 3.5 Syndication Classes");
feed.Generator = "DotNetBips.com RSS Feed Generator";

SyndicationLink link = new SyndicationLink();
link.Title = "DotNetBips.com";
link.Uri = new Uri("http://www.dotnetbips.com");
feed.Links.Add(link);

The SyndicationFeed class has several properties. The Title property specifies the title of the feed as it appears in any feed reader. The Title property is of type TextSyndicationContent. The TextSyndicationContent class represents any content that is in plain text format. The Copyright, Description and Generator properties represent the copyright holder of the content, description of the feed and name of the feed generator application respectively. You can also add a URL to the feed provider with the help of SyndicationLink class. Though the Links property of the SyndicationFeed is a collection property in many cases you will have just one link in it.

Creating SyndicationItem

The next step in to create one or more SyndicationItem instances. Normally you will run a for loop against your database to fetch data of the individual feed item.

SyndicationItem item = new SyndicationItem();
item.Id = Guid.NewGuid().ToString();
item.Title = new TextSyndicationContent
("Sample feed item");
item.Summary = new TextSyndicationContent
("Sample summary");
item.Content = new TextSyndicationContent
("This is sample content");

Here, we created an instance of SyndicationItem class and set its Id, Title, Summary and Content properties. The Id property is an indicator of a unique identifier (generally a GUID or URL) for the current feed item. The Summary property normally holds a small description of the item whereas the Content property holds the entire content to be exposed.

Creating SyndicationLink and SyndicationPerson

Every feed item can have a collection of SyndicationLink objects (exposed as Links property) and a collection of SyndicationPerson objects (exposed as Authors property) associated with it.

SyndicationLink itemlink = new SyndicationLink();
itemlink.Title = "DotNetBips.com";
itemlink.Uri = new Uri("http://www.dotnetbips.com/" 
+ item.Id.ToString() + ".aspx");
item.Links.Add(itemlink);

SyndicationPerson person = new SyndicationPerson();
person.Name = "Tom & Jerry";
person.Email = "tomnjerry@somedomain.com";
item.Authors.Add(person);

Adding feed items to the feed

Once you create one or more feed items you can then add them to the SyndicationFeed.

List<SyndicationItem> items = 
new List<SyndicationItem>();
items.Add(item);
feed.Items = items;

Here, we created a generic List of SyndicationItem objects and added individual SyndicationItem objects to it. Then Items property of the SyndicationFeed class is set to the generic List we just created.

Serializing the feed

In the final step you need to serialize the feed to the response stream. Note that nowhere in the above code you specified whether the feed will be an RSS feed or ATOM feed. You will do that in this final step.

Response.Clear();
Response.ContentEncoding = 
System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";

XmlWriter rssWriter = XmlWriter.Create
(Response.Output);
Rss20FeedFormatter rssFormatter = 
new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();

Response.End();

Here, we first clear the response buffer to ensure that our feed content alone is serialized. Then we set ContentEncoding and ContentType properties to appropriate MIME types.

The Rss20FeedFormatter class allows you to serialize a SyndicationFeed in RSS format. If you want to expose your feed as an ATOM feed then you would have used Atom10FeedFormatter class instead. The constructor of the Rss20FeedFormatter class accepts an instance of XmlWriter class that wraps the stream to write. In our case we pass the stream obtained from Response.Output property. The feed content is serialized using WriteTo() method of the Rss20FeedFormatter class. The XmlWriter is then closed and response is ended.

If you put all the above code in the Page_Load event handler of a web form and view the web form in the browser you should see something like this:

 


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 13 April 2008


Tags : ASP.NET Web Forms .NET Framework Components XML