RSS stands for Real Simple Syndication (or Rich Site Summary, depending on which version you are using). It is a simple XML format that is used to publish content for consumption by various applications. RSS has taken the technology world by storm, rapidly going from being a fringe technology to being supported by almost every major Internet site, including Yahoo!, Google, and even NYTimes.com.
RSS is a relatively simple XML format, so implementing and exposing RSS feeds from your application is fairly straightforward. RSS.NET is an open source library that makes it even easier to publish RSS feeds by giving you a simple object model so you don’t have to actually work with XML. (Who wants to work with XML, anyway?) RSS.NET also handles the small differences between the various versions of RSS, including 0.90, 0.91, 0.92, and 2.0.1.
Tool | RSS.NET |
Version covered | .86 |
Home page | |
Power Tools page | |
Summary | A simple library written in C# to make publishing and consuming RSS feeds even easier |
License type | MIT License |
Online resources | Bug and feature request trackers |
Supported Frameworks | .NET 1.1 natively; convert and recompile for .NET 2.0 |
Related tools in this book | RSS Toolkit |
RSS.NET is still a work in progress, so binaries have not yet been released. This means you will need to download and compile the source yourself. The best way to do so is to select the Nightly Build link from the tool’s home page, which points to a tar archive of the most recent source. You can also connect directly to the CVS repository to get the latest code if that link disappears.
The existing code is for .NET 1.1, but you can recompile it in .NET 2.0 without so much as a compiler warning by simply converting the project to Visual Studio 2005.
Once you have successfully built the project in your Framework of choice, you just need to add a reference to the compiled assembly to your application. You can then start writing code to publish and consume RSS feeds.
RSS.NET provides an easy-to-work-with object model for creating RSS feeds. To get started, first create a new ASP.NET project in Visual Studio 2005. Then add a new HTTP handler, using the Add New Item → Generic Handler menu command.
The handler’s ProcessRequest( )
method is where you’ll be doing all your work. The first step is to create a new RssFeed
object. This is where you actually write your RSS out to the response:
RssFeed feed = new RssFeed( );
Next, create a new RssChannel
object and tell it what you want to publish. The RssChannel
acts as a heading for the feed and contains the actual news items in its Items
collection (you’ll populate it shortly):
RssChannel rssChannel = new RssChannel( ); rssChannel.Description = "Sample RSS Feed for WDPT"; rssChannel.Link = new Uri("http://www.windevpowertools.com"); rssChannel.Title = "Windows Developer Power Tools Feed";
The consuming application uses the channel description, link, and title to let readers know a little bit about your feed and what it contains.
Now you can create an RssItem
, which is the actual meat of the RSS feed:
RssItem rssItem = new RssItem( ); rssItem.Title = "First Item Title"; rssItem.Description = "Hello World from RSS"; rssItem.Link = new Uri("http://www.windevpowertools.com"); rssItem.PubDate = new DateTime(2006, 4, 23);
The title identifies the item to your consumer’s application. The description is the actual body of the item, which could be a news item, blog post, etc. You also need to set the link and publication date for the item.
Next, create a new RssGuid
(globally unique identifier) object, which will be used to uniquely identify the item. The main use of this GUID is to provide consuming applications with a way to keep track of what posts they have already seen. For instance, an aggregator will want to keep track of what posts the user has read and not show the posts as new each time the feed is updated. Simply create the RssGuid
object, set a couple of properties, and then assign it to the Guid
property of the RssItem
:
RssGuid rssGuid = new RssGuid( ); rssGuid.PermaLink = true; rssGuid.Name = "http://www.windevpowertools.com/sampleItem"; rssItem.Guid = rssGuid;
One important property to note is the PermaLink
property. Setting PermaLink
to true
tells the consumer that the GUID you are using is an actual URL to the item, as opposed to just a globally unique identifier.
Once you’ve done all that, add the item to the channel and set its LastBuildDate
property:
rssChannel.Items.Add(rssItem); rssChannel.LastBuildDate = rssChannel.Items.LatestPubDate( );
Some consumers check this property before parsing your entire feed to see whether any new updates have been added, so it’s important to avoid setting this to the current date and time.
You can now add the channel to the RssFeed
object that you created earlier:
rssFeed.Channels.Add(rssChannel);
Then, set the response type using the HTTP context’s ContentType
property, write the RSS out to the response, and close the response:
rssFeed.Channels.Add(rssChannel);
context.Response.ContentType = "text/xml";
rssFeed.Write(context.Response.OutputStream);
context.Response.End( );
You can now point your browser to your test page, and the following RSS will be served up:
<?xml version="1.0" encoding="iso-8859-1" ?> <rss version="2.0"> <channel> <title>Windows Developer Power Tools Feed</title> <description>Sample RSS Feed for WDPT</description> <link>http://www.windevpowertools.com/</link> <lastBuildDate>Sun, 23 Apr 2006 00:00:00 GMT</lastBuildDate> <docs>http://backend.userland.com/rss</docs> <generator>RSS.NET: http://www.rssdotnet.com/</generator> <item> <title>First Item</title> <description>Hello World from RSS</description> <link>http://www.windevpowertools.com/</link> <guid isPermaLink="true">http://www.windevpowertools.com/sampleItem</guid> <pubDate>Sun, 23 Apr 2006 00:00:00 GMT</pubDate> </item> </channel> </rss>
RSS.NET makes it very simple to publish RSS feeds without writing XML. It also lets you avoid having to deal with escaping text or with minor differences between RSS versions.
Reading RSS feeds with RSS.NET is even easier than writing them. The object model is exactly the same, just reversed. To read an RSS feed, simply call the static Read( )
method on the RssFeed
object:
RssFeed rssFeed = RssFeed.Read("http://www.dotavery.com/blog/rss.aspx");
The RssFeed
object is now populated with the channels and items from the supplied RSS feed. You can display the items in a grid for users, save them to a database, or even modify an item and rewrite the entire feed back out to the response.
Support is limited to filing bug reports at the tool’s SourceForge site (http://sourceforge.net/projects/rss-net/). However, with the source available, you can fix any bugs you happen to come across.
Get Windows Developer Power Tools now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.