9/17/2010

Create RSS 2.0 and Atom 1.0 in Asp.Net 3.5 C#

March 14, 2010
Deepu MI's Blog


In this article I am going to explain how we can create web syndications like RSS 2.0 and Atom 1.0 in Asp.Net and C# with very minimal code.

RSS 2.0
Really Simple Syndication (RSS) is one of the syndication feed formats which can get the frequently updated content from the web site. (Refer: http://en.wikipedia.org/wiki/RSS).
The specification of RSS format http://cyber.law.harvard.edu/rss/rss.html. RSS is most widely used syndication format.

Atom 1.0
Atom is a syndication format which is more flexible than RSS. Atom came into existence out of a need to improve RSS. (Refer: http://en.wikipedia.org/wiki/Atom_standardard)

In Asp.Net 3.5 frame work we can create subscription feeds with very minimal code using System.ServiceModel.Syndication namespace which contains all of the classes that make up the Syndication Object Model. For example below is a sample Blog class I am defining a public method and some properties to retrieve the blog items (I have hardcoded two items you can replace this from your database logic).

The next step I am going to create another class called Syndication Helper which converts our web content to syndication format.

Code Explanation

Uri uri = new Uri(“http://deepumi.wordpress.com”);

Configure your site url ( blog or news).

SyndicationFeed syndicationFeed = new SyndicationFeed();
Syndicaiton Feed class represent a top level of feed object, (you can add your blog name / site name with description and the last blog/site updated time).

List items = new List();
Syndication Item class represent a individual feed atom/rss.item object like item url, item description, item id, last updated etc. Here I am creating a syndicaiton item collection object which mapping from MyBlogList() method.

List oBlogList = Blog.GetMyBlogList();
foreach (Blog oBlog in oBlogList)
{
SyndicationItem oItem = new SyndicationItem(oBlog.Title,
SyndicationContent.CreateHtmlContent(oBlog.Description),
new Uri(oBlog.Url),
oBlog.BlogId.ToString(),
oBlog.LastUpdated);
items.Add(oItem);
}

Finally you are return the SyndicationFeed object to the aspx pages.
Now we need to render the atom and rss content in the aspx pages.

Create a new aspx page called rss.aspx and make sure there is no html markup in the page(just a blank page)

Code behind (RSS page)

Create a new aspx page called rss.aspx and make sure there is no html markup in the page(just a blank page)

Code behind (Atom page)

Hope this help and If you have any comments, please feel free to write your feedback.

You can download the entire article from here or copy paste this URL

http://www.4shared.com/file/240764190/b37e055d/Feeds.html

Thanks
Deepu