WEBSITE

Consume an RSS Feed

9/26/2010 7:47:47 PM
An RSS feed is merely an XML file generated at regular intervals, consumed by an application that knows what to do with it. Therefore, it’s really just putting together pieces you already know: web download and XML parsing.

The accompanying source code for this article contains two projects: RssLib and RssReader. The following sample is from RssLib and contains some simple feed parsing code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Globalization;

namespace RssLib
{
public class Channel
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
public CultureInfo Culture { get; set; }
public List<Item> Items { get; set; }
}

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

public class Feed
{
public Channel Read(string url)
{
WebRequest request = WebRequest.Create(url);

WebResponse response = request.GetResponse();
XmlDocument doc = new XmlDocument();
try
{
doc.Load(response.GetResponseStream());
Channel channel = new Channel();
XmlElement rssElem = doc["rss"];
if (rssElem == null) return null;
XmlElement chanElem = rssElem["channel"];

if (chanElem != null)
{
//only read a few of the many possible fields
channel.Title = chanElem["title"].InnerText;
channel.Link = chanElem["link"].InnerText;
channel.Description =
chanElem["description"].InnerText;
channel.Culture = CultureInfo.CreateSpecificCulture(chanElem["language"].InnerText);
channel.Items = new List<Item>();
XmlNodeList itemElems =
chanElem.GetElementsByTagName("item");
foreach (XmlElement itemElem in itemElems)
{
Item item = new Item();
item.Title = itemElem["title"].InnerText;
item.Link = itemElem["link"].InnerText;
item.Description =
itemElem["description"].InnerText;
item.PubDate = itemElem["pubDate"].InnerText;
item.Comments = itemElem["comments"].InnerText;
channel.Items.Add(item);
}
}
return channel;
}
catch (XmlException)
{
return null;
}
}

public void Write(Stream stream, Channel channel)
{
XmlWriter writer = XmlTextWriter.Create(stream);
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", channel.Title);
writer.WriteElementString("link", channel.Link);
writer.WriteElementString("description",
channel.Description);
writer.WriteElementString("language",
channel.Culture.ToString());
foreach (Item item in channel.Items)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", item.Title);
writer.WriteElementString("link", item.Link);
writer.WriteElementString("description",
item.Description);
writer.WriteElementString("pubDate", item.PubDate);
writer.WriteEndElement();
}

writer.WriteEndElement();
writer.WriteEndElement();

writer.Flush();
}
}
}


Here’s a short sample of how this library is used:

private void buttonLoad_Click(object sender, EventArgs e)
{
LoadFeed(textBoxFeed.Text);
}

private void LoadFeed(string url)
{
listViewEntries.Items.Clear();

RssLib.Channel channel = _feed.Read(url);
this.Text = "RSS Reader - " + channel.Title;

foreach (RssLib.Item item in channel.Items)
{
ListViewItem listViewItem = new
ListViewItem(item.PubDate.ToString());
listViewItem.SubItems.Add(item.Title);
listViewItem.SubItems.Add(item.Link);
listViewItem.Tag = item;
listViewEntries.Items.Add(listViewItem);
}
}

In a complete implementation, RSS can have many more fields than are presented here. See http://www.rssboard.org/rss-specification for the full specification of required and optional elements.

Other  
 
Most View
Fitness Gadget Shootout : Fitness Gadgets (Part 1) - Apple Ipod Nano, Fitbit Ultra
Windows 7 : Programming KMDF Hardware Driver - Mapping Resources - Code to Map Resources
6 Best New Things To Do With Raspberry Pi (Part 2)
Fujifilm X-E1 - A Retro Camera That Inspires (Part 14)
Lenovo Thinkpad Xl Carbon – That’s What We’ve Been Waiting For
Windows 8 : Managing User Access and Security - Managing Local User Accounts and Groups (part 3)
Elgato Game Capture HD
Cloud Computing Reconsidered (Part 2)
Sharepoint 2013 : Creating team sites (part 1) - Customizing team sites, Working with document libraries
Are Your Passwords Safe? (Part 1)
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered