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

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”); //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”);
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


About these ads
Categories: Asp.Net, Atom Feed, C#, LINQ, RSS Tags: ,
  1. Anil
    February 24, 2010 at 5:12 am

    Nice…will try this in test project this weekend…

    • February 25, 2010 at 4:55 pm

      Cool try it out and please let me know if you are facing any issues

  2. March 6, 2010 at 3:23 pm

    nice tuto in here , good introduction , look at mine in my website righ here http://help.uk.cm.

    hear from you soon thanks

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: