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
Tweet
No comments:
Post a Comment