I wrote this RssReader to use in a dot net nuke site, but quickly found uses for it in a lot of other sites. This reader parses the rss stream to an xml document, then reads the nodes specified and returns a collection of rss items. This oen is easily customizable with whatever you want to bring back from the rss feed. Names definitely change per rss feed, so custimzation is likely, however the properties used here are extremely common so that much should be available.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Xml;
using System.Linq;
namespace RssTabDisplay
{
public static class RssReader
{
/// <summary>
/// Retrieves the remote RSS feed and parses it.
/// </summary>
public static IEnumerable<RssItem> GetFeed(string feedUrl)
{
//check to see if the FeedURL is empty
if (String.IsNullOrEmpty(feedUrl))
return new Collection<RssItem>();
// Add try catch block here in case the url
// goes down. Log and Handle the error in the method preferred.
// Return the exception, null or an empty collection
//start the parsing process
using (XmlReader reader = XmlReader.Create(feedUrl))
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
//parse the items of the feed
return ParseRssItems(xmlDoc);
}
}
/// <summary>
/// Retrieves the remote RSS feed and parses it.
/// </summary>
public static IEnumerable<RssItem> GetFeed(string feedUrl, int itemCount)
{
return GetFeed(feedUrl, 0, itemCount);
}
/// <summary>
/// Retrieves the remote RSS feed and parses it.
/// </summary>
public static IEnumerable<RssItem> GetFeed(string feedUrl, int startAt, int itemCount)
{
return GetFeed(feedUrl).Skip(startAt).Take(itemCount);
}
/// <summary>
/// Parses the xml document in order to retrieve the RSS items.
/// </summary>
private static IEnumerable<RssItem> ParseRssItems(XmlDocument xmlDoc)
{
Collection<RssItem> rssItems = new Collection<RssItem>();
XmlNodeList nodes = xmlDoc.SelectNodes("rss/channel/item");
var items = from XmlNode node in nodes
select new RssItem()
{
Title =node.SelectSingleNode("title") != null ? node.SelectSingleNode("title").InnerText : "(No title)",
Description = node.SelectSingleNode("description") != null ? node.SelectSingleNode("description").InnerText : "(none)",
Link =node.SelectSingleNode("link") != null ? node.SelectSingleNode("link").InnerText : "N/A",
Date = node.SelectSingleNode("pubDate") != null ? node.SelectSingleNode("pubDate").InnerText : "N/A",
Thumbnail = node.SelectSingleNode("thumb") != null ? node.SelectSingleNode("thumb").InnerText : "",
// To add more properties, another another node call here
};
return items;
}
/// <summary>
/// A structure to hold the RSS Feed items
/// </summary>
[Serializable]
public class RssItem
{
/// <summary>
/// The publishing date.
/// </summary>
public string Date { get; set; }
/// <summary>
/// The title of the feed
/// </summary>
public string Title { get; set; }
/// <summary>
/// A description of the content (or the feed itself)
/// </summary>
public string Description { get; set; }
/// <summary>
/// The link to the feed
/// </summary>
public string Link { get; set; }
public string Thumbnail { get; set; }
// Add more properties here if necessary
}
}
}
I left the try catch block off the when instantiating the xml document. It does have the ability to throw an exception if the url is invalid. You may want the exception, or you may want to simply return an empty collection. Depends on your implementation. Be careful, you don’t want a faulty rss feed to throw an exception and ruin other pages.

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 