Friday 18 September 2009

Read RSS and Atom feed in C#

I had a requirement to read RSS/Atom feeds from blogs and store in SharePoint List.
I remember we had to go for separate framework to read the feeds and parse it. In that case we need to refer the framework in our project and install that assembly in Bin or GAC.

Now with .Net 3.5, life has become much easier.

Yes, with the namespace 'System.ServiceModel.Syndication' it is just few lines of code to read and parse the feeds either RSS or Atom.

Just add reference to 'System.ServiceModel.Web' to your project and include above namespace in your class, thats it.

Below is the code i used.
XmlReader reader = XmlReader.Create(curUrl);
SyndicationFeed feed = SyndicationFeed.Load(reader);
string title = String.Empty;
string link = String.Emplty;
DateTime dateTime = null;
foreach (var feeditem in feed.Items)
{
title = feeditem.Title.Text;
dateTime = feeditem.PublishDate.LocalDateTime;
link = feeditem.Links[feeditem.Links.Count -1].Uri.OriginalString;

//Do your tasks here
}

Easy to deploy and maintain too.
Happy Coding
வாழ்க வளமுடன் :)

2 comments:

challa said...

Thanks for the good sample, but is working for rss version 2.0 but not working for version 0.92

Unknown said...

Works beautifully. Thanks so much.