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 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 = "";
Response.Write("Number of Items in RSS Feed " + urlRss +
" is " + numberOfItems(urlRss) + "<br /><br />");

string urlAtom = "";
Response.Write("Number of Items in Atom Feed " + urlAtom +
" is " + numberOfItems(urlAtom));

private int numberOfItems(string feedUrl)
using (XmlReader reader = XmlReader.Create(feedUrl))

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.



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

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: