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


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. (You can download the entire article from here ).

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(“https://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<SyndicationItem> items = new List<SyndicationItem>();
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<Blog> 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)

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”rss.aspx.cs” Inherits=”rss” %>

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)

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”atom.aspx.cs” Inherits=”atom” %>

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

how to consume an Atom RSS Feed using Asp.Net C# with LINQ


This article describes how to consume an Atom RSS Feed using Asp.Net C# with LINQ.
There are many ways to achieve this. But I am using LINQ because LINQ has lot of features to customize with minimum code.

XDocument doc = XDocument.Load(“http://www.asp.net/News/rss.ashx&#8221;); //configure your Url here.

var query =from feed in doc.Descendants(“item”)
orderby DateTime.Parse(feed.Element(“pubDate”).Value) descending
select new
{
Title = feed.Element(“title”).Value,
Description = feed.Element(“description”).Value,
Date = DateTime.Parse(feed.Element(“pubDate”).Value)
};

Using Xdocument object in LINQ will help you to read an xml file from the server.
Once you have the document object you can just take advantage of  linq to xml for customize,
So the above code just read all the rss reader from the given url sort by last publishing date.
If you want more customize to the above code to show only top few items from the list you can try with Take() method.

XDocument doc = XDocument.Load(“http://www.asp.net/News/rss.ashx&#8221;);
var query = (from feed in doc.Descendants(“item”)
orderby DateTime.Parse(feed.Element(“pubDate”).Value) descending
select new
{
Title = feed.Element(“title”).Value,
Description = feed.Element(“description”).Value,
Date = DateTime.Parse(feed.Element(“pubDate”).Value)
}).Take(
10).ToList();

Here I am taking Top 10 Feed items from the list and sorting by last publishing date and simply binding to a DataGrid

<asp:DataGrid ID=”dg” runat=”server”></asp:DataGrid>

dg.DataSource = query.ToList();
dg.DataBind();

Note: You may need to replace the attribute name inside the Element tag with your XML node name.

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

Thanks
Deepu