Simple RSS reader class in C#

Feb 8, 2007

Update February 11th. The class now updates every feed in the cache asynchronously and automatically.

On the login screen of Headlight, we are soon adding news updates so that our customers can see what is going on with the product. The content is delivered from our company blog via RSS and we are probably going to use FeedBurner’s JavaScript to display the latest items. Then I started thinking about how easy it would be to write a simple RSS feed parser in C# instead.

It should support caching so it doesn’t parse the feed at every page request. I know there are some very good RSS libraries such as RSS.NET, but I wanted to build it myself. Now it is one hour later and this is the result.

Examples of use

By using the Create method of the RssReader class, you specify a TimeSpan of when the feed should expire from the cache. In this example it expires after two hours. Remember not to dispose or use the "using" claus when you use the CreateAndCache method. Otherwise you dispose the cached instance.

RssReader reader = RssReader.CreateAndCache("http://feeds.feedburner.com/netslave", new TimeSpan(2, 0, 0));
foreach (RssItem item in reader.Items)
{
 Response.Write(item.Title + "<br />");
}

You can also use the class directly without caching.

using (RssReader rss = new RssReader())
{
 rss.FeedUrl = "http://feeds.feedburner.com/netslave";
 foreach (RssItem item in rss.Execute())
 {
  Response.Write(item.Title + "<br />");
 }
}

It doesn’t parse all the different XML tags of a RSS feed, just the basic ones. However, it is very easy to add more yourself.

Download

RssReader.zip (1,5 KB)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (4) -

 Brian Kuhn
Brian Kuhn
2/8/2007 9:43:03 PM #

I know all about wanting to write your own syndication class library, (see oppositionallydefiant.com/files/10/default.aspx), mine does the full RSS 2.0 and ATOM 1.0 specifications and I just added support for RSS extensions. The link provided is for binaries, documentation, and full source code if you interested in leveraging it.

I  haven't really thought about the caching issue before, but after reading your post I will ponder how to do something similar in my class library. Great work for just an hour of coding, good job.

 Michal Talaga
Michal Talaga
2/12/2007 8:10:22 AM #

I have looked through your code and I think there is no need for the class to be IDisposable. Otherwise a good job!

Mads Kristensen
Mads Kristensen
2/12/2007 8:23:23 AM #

No, it doesn't need to be disposable, because there is little to dispose. On the other side, it doesn't hurt either.

 Michal Talaga
Michal Talaga
2/12/2007 9:25:42 AM #

True, with your current implementation of IDisposable it doesn't hurt but it may confuse.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.