Consuming an RSS Feed with the .NET Framework

Introduction

Welcome to this installment of the .NET Nuts & Bolts column! This particular column harkens back to the style of some of my early columns where I took a problem faced in my every day project work and shared the solution with the masses. In this case, the problem at hand is how to consume an RSS feed programmatically with .NET framework code. This article will cover the use of the System.Net.WebRequest and System.Xml.XmlDocument to do just that.

Really Simple Syndication

I’ll keep the introduction brief. I’m going to assume that if you’re reading this that you’ve at least heard of RSS before otherwise there are probably other things you’d be doing with your time. Really Simple Syndication, or RSS, is a series of formats used to publish works on the Internet in a fairly standardized format. An RSS document, which is often referred to as a feed or channel, contains full or summarized text along with metadata about the work such as author and/or publication date. It is basically a structured XML file that is updated on regular intervals. For example, CodeGuru.com has a number of RSS feeds available such as the one that has the latest stories for the .NET/C# channel. We’ll use one of the many CodeGuru.com feeds in our example.

Object Model

We’ll start by defining a simple object model that represents an RSS channel and its contained publication. There will be three parts to our basic model. The first part is an object that represents the channel. Another is an object that represents the document contained within the channel. The third and final object is an item representing content within the publication. This is an area where there is plenty of theory, but no real right or wrong answer. It all depends on what you’re trying to accomplish.


using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;

public class RssChannel
{
 public string Title { get; set; }
 public System.Uri Link { get; set; }
 public string Description { get; set; }
 public RssDocument Rss { get; set; }

 public RssChannel()
 {
this.Rss = new RssDocument();
 }
}

public class RssDocument
{
 public List<Item> Items { get; set; }

 public RssDocument()
 {
this.Items = new List<Item>();
 }
}

public class Item
{
 public string Title { get; set; }
 public string Link { get; set; }
 public string Description { get; set; }
 public string PubDate { get; set; }
}


System.Net.WebRequest

An RSS feed is not much more than combining HTTP requests and XML parsing. To perform the HTTP parsing we use the System.Net.WebRequest and then use the System.Xml.XmlDocument to parse the resulting output. The example code below adds a static method to the RssChannel class to create a new channel instance and populate it with the content.


public static RssChannel Read(System.Uri url)
{
 WebRequest request = WebRequest.Create(url.AbsoluteUri);

 WebResponse response = request.GetResponse();
 XmlDocument xmlDoc = new XmlDocument();
 try
 {
xmlDoc.Load(response.GetResponseStream());
XmlElement rssElement = xmlDoc[“rss”];
if (rssElement == null)
{
 return null;
}

XmlElement channelElement = rssElement[“channel”];

if (channelElement != null)
{
   // Create the channel and set attributes
 RssChannel rssChannel = new RssChannel();
 rssChannel.Title = channelElement[“title”].InnerText;
 rssChannel.Link = new Uri(channelElement[“link”].InnerText);
 rssChannel.Description = channelElement[“description”].InnerText;

 // Read the content
 XmlNodeList itemElements =
channelElement.GetElementsByTagName(“item”);
 
 foreach (XmlElement itemElement in itemElements)
 {
Item item = new Item()
{
 Title = itemElement[“title”].InnerText,
 Link = itemElement[“link”].InnerText,
 Description = itemElement[“description”].InnerText,
 PubDate = itemElement[“pubDate”].InnerText
};

rssChannel.Rss.Items.Add(item);
 }
 return rssChannel;
}
else
{
 return null;
}
 }
 catch
 {
return null;
 }
}


More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read