Working with NSort and RSS.NET - Creating an RSS feed
(Page 4 of 4 )
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’sProcessRequest()method is where you’ll be doing all your work. The first step is to create a newRssFeed object. This is where you actually write your RSS out to the response:
RssFeed feed = new RssFeed();
Next, create a newRssChannelobject and tell it what you want to publish. TheRssChannel acts as a heading for the feed and contains the actual news items in itsItemscollection (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 anRssItem, 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 newRssGuid(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 theRssGuidobject, set a couple of properties, and then assign it to theGuidproperty of theRssItem:
RssGuid rssGuid = new RssGuid();
rssGuid.PermaLink = true;
rssGuid.Name = http://www.windevpowertools.com/sampleItem;
rssItem.Guid = rssGuid;
One important property to note is thePermaLink property. SettingPermaLinktotruetells 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 itsLastBuildDateproperty:
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 theRssFeedobject that you created earlier:
rssFeed.Channels.Add(rssChannel);
Then, set the response type using the HTTP context’sContentTypeproperty, 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
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);
TheRssFeed 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.
Getting Support
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.
RSS.NET in a Nutshell
RSS.NET is a great little library that makes working with RSS a little bit easier and cleaner. You don’t need it to use RSS, but it helps and encourages good object-oriented programming. The project has been sitting in beta status for some time, but even in its
current state, it is still very useful.
Please check back tomorrow for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|