Consume a WordPress RSS Feed in C# and cache it.

Posted On: April 1st, 2009

I couldn’t find a simple example of reading a WordPress Blog Feed in C# and caching it. So I decided to write out my own.

Here is an example to get you started. I wrote it in two parts:

A Class that Consumes a Feed (FeedReader)
An Object representing a Feed Item (FeedViewModel).

And so we begin:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace FeedExample
{

	public class FeedReader
	{
		public static IList<FeedViewModel> GetBlogFeed(string feedUrl, int feedCount)
		{
			//Load feed via a feedUrl.
			var doc = XDocument.Load(feedUrl); 

			//Get all the "items" in the feed.
			var feeds = doc.Descendants("item").Select(x =>
					new FeedViewModel
					{
					     //Get title, pubished date, and link elements.
						Title = x.Element("title").Value, //3
						PublishedDate = DateTime.Parse(x.Element("pubDate").Value),
						Url = x.Element("link").Value
					} //  Put them into an object (FeedViewModel)
					)
					// Order them by the pubDate (FeedViewModel.PublishedDate).
					.OrderByDescending(x=> x.PublishedDate)
					//Only get the amount specified, the top (1, 2, 3, etc.) via feedCount.
					.Take(feedCount);

 			//Convert the feeds to a List and return them.
			return feeds.ToList();
		}
	}

	public class FeedViewModel
	{
		public string Title { get; set; }
		public string Url { get; set; }
		public DateTime PublishedDate { get; set; }
	}

}

You can get some more items from the feed like: title, link, comments, pubDate, dc:creator, category, guid, description, content:encoded, etc…

To prevent your website from slowing down you can cache your feed like this:

//Check if feed exists
if (HttpRuntime.Cache["Feed"] == null)
				{
					//If it is, insert it into the cache, cache for 10 minutes
					HttpRuntime.Cache.Insert("Feed",
						FeedReader.GetBlogFeed("url", 5), null, SystemTime.Now().AddMinutes(10), Cache.NoSlidingExpiration);
				}

//retrieve cached feeds
var cachedFeeds = (List<FeedItemViewModel>) HttpRuntime.Cache["Feed"]

HttpRuntime.Cache is part of the System.Web namespace.

, , , , , ,

4 Comments »

  1. avatar

    Thanks alot this was helpful. I really love your site, the design is very cool. I have came here a few times but have never left a commented, just wanted to let you know…

    Moises Myrum (September 3rd, 2010)

  2. avatar

    thanks for your sharing, i’m art designer, i still don’t know how to use it, is there any example or demo here? thank you.

    tky (May 6th, 2011)

  3. avatar

    This is great, but you should point out there is a “trick” to getting the content:encoded.

    // Declare this
    XNamespace content = XNamespace.Get(“http://purl.org/rss/1.0/modules/content/”);

    // Then later:
    Content = x.Element(content + “encoded”).Value

    Jason M (November 21st, 2011)

  4. avatar

    Here is a function to get the 5 fields of the feed:

    static IList GetBlogFeed(string feedUrl, int feedCount)
    {
    XNamespace content = XNamespace.Get(“http://purl.org/rss/1.0/modules/content/”);

    var doc = XDocument.Load(feedUrl);
    var feeds = doc.Descendants(“item”).Select(x =>
    new FeedViewModel
    {
    Title = x.Element(“title”).Value,
    PublishedDate = DateTime.Parse(x.Element(“pubDate”).Value),
    Url = x.Element(“link”).Value,
    Description = x.Element(“description”).Value,
    Content = x.Element(content + “encoded”).Value
    }).OrderByDescending(x => x.PublishedDate).Take(feedCount);
    return feeds.ToList();
    }

    Jason M (November 21st, 2011)

TrackBack URL

Leave a comment