Count Items in a RSS/Atom Feed using .NET

Given a RSS or Atom Feed URL, here’s a simple way to count the number of items in the feed. We will use the DevCurry.com RSS Feed and a sample Atom Feed for our example.

using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string urlRss = "http://feeds.feedburner.com/devcurry";
Response.Write("Number of Items in RSS Feed " + urlRss +
" is " + numberOfItems(urlRss) + "<br /><br />");

string urlAtom = "http://alexharden.org/blog/atom.xml";
Response.Write("Number of Items in Atom Feed " + urlAtom +
" is " + numberOfItems(urlAtom));
}

private int numberOfItems(string feedUrl)
{
using (XmlReader reader = XmlReader.Create(feedUrl))
{
return(SyndicationFeed.Load(reader).Items.Count());
}
}
}

The SyndicationFeed class available in .NET 3.5 and .NET 4.0 is very useful in such scenarios since it can represent both Atom 1.0 and RSS 2.0.

OUTPUT

image



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: