Consuming an RSS Feed with the .NET Framework | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 3, 2010
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Advertisement

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;
  }
}

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.