RSS has changed the world of technology in only a few short years. As a company, you can make an incredible outreach to your customers by adding RSS feeds to your site. Your customers are able to subscribe to those feeds and keep up to date on your company’s product lines and services, marketing announcements, news flashes, or even scores for the company volleyball team.
Tip
See Robert Scoble and Shel Israel’s great book Naked Conversations: How Blogs are Changing the Way Businesses Talk with Customers (Wiley) for a very insightful look into companies and blogs.
Of course, it’s not only companies who can benefit from RSS feeds; individuals can also add RSS capabilities to their web sites to vastly increase their reach and readership. Look no further than the explosion of the “blogosphere” where sites enabled with RSS spread everything from political opinions to recipes across the globe.
If your site includes any sort of frequently changing data, you should implement an RSS feed. RSS isn’t a particularly tough technology to work with, but there is a moderate amount of XML parsing involved.
The RSS Toolkit, a free download that is part of the “Sandbox” Projects on http://www.asp.net, includes a number of RSS-related features to make working with RSS even easier:
An RSS data source
An HTTP handler
Object model generation tools
Tip
A general-purpose library for working with RSS called RSS.NET is covered in Chapter 4, but the RSS Toolkit is built specifically for ASP.NET. Depending on your technology, you can choose the best option for your project.
Tool | RSS Toolkit |
Version covered | 1.2.0 |
Home page | http://www.asp.net/downloads/teamprojects/default.aspx?tabid=62#rss |
Power Tools page | |
Summary | Includes a plethora of RSS-related features, including an RSS data source, an HTTP handler, and an object model |
License type | Shared Source License for ASP.NET Source Projects |
Online resources | Samples |
Supported Frameworks | .NET 2.0 |
Related tools in this book | RSS.NET |
To get started working with the RSS Toolkit, download the .zip archive from the home page and extract its contents to your local drive. Next, copy RssToolkit.dll to your web site’s bin directory or add a reference to it if you are using the Web Application Project model (see the Appendix).
The RSS Toolkit includes a novel approach to generating RSS feeds. Unlike RSS.NET (discussed in Chapter 4), which simply provides a general API to work with the common RSS objects, the RSS Toolkit lets you generate the necessary classes based on a supplied XML file.
Tip
You can avoid these steps if you want to use late-bound classes, but unless you have good reason, it is best to stick with the strongly typed classes.
Before you can create an RSS feed, you need to determine the format of the RSS file you would like to create. Here is a simple RSS 2.0 example:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>Windows Developer Power Tools</title> <link>~/wdptrss.aspx</link> <description>Channel for Windows Developer Power Tools updates and news</description> <ttl>10</ttl> <name></name> <user></user> <item> <title></title> <description></description> <link></link> </item> </channel> </rss>
Add this example to an empty text file, name it wdpt.rss, and save it in your App_Code folder. The RSS Toolkit includes a custom build provider to build the required object model and HTTP handler based on your .rss file. You just need to add the required buildProviders
element to your web.config:
<configuration> <system.web> <compilation> <buildProviders> <add extension=".rss" type="RssToolkit.RssBuildProvider,RssToolkit, Version=1.0.0.1,Culture=neutral,PublicKeyToken=02e47a85b237026a"/> </buildProviders> </compilation> </system.web> </configuration>
You can now build the project. The object model and a base HTTP handler will be created for you based on the .rss file.
Next, you need to add a new generic handler to the project (Add New Item → Generic Handler). In this example, the name of the new handler base is wdptHttpHandlerBase
. This name is based on the name of the original .rss file, which in this case was wdpt.rss. You’ll need to change the handler to inherit from the new generated handler base:
<%@ WebHandler Language="C#" Class="RssHandler" %> using System; using System.Web; public class RssHandler : wdptHttpHandlerBase { protected override void PopulateChannel(string channelName, string userName) { } }
You also need to override the PopulateChannel( )
method, where you will actually create the content of the RSS feed:
protected override void PopulateChannel(string channelName, string userName) { Channel.Name = channelName; wdptItem item = new wdptItem("First Post!", "Here is my first post to this feed", "http://www.oreilly.com"); Channel.Items.Add(item); wdptItem item2 = new wdptItem("Second Post!", "Here is my second post to this feed", "http://www.oreilly.com"); Channel.Items.Add(item2); }
In this sample, I’ve added a couple of arbitrary items to the channel. In real life, you will most likely pull this information from a database, then create the items and add them to the channel in a for
loop.
Now that the handler is complete, you can access it like a normal page. The following RSS will be generated:
<?xml version="1.0" encoding="utf-8" ?> <rss version="2.0"> <channel> <title>Windows Developer Power Tools</title> <link>http://localhost:2264/wdpt/wdtprss.aspx</link> <description>Channel for Windows Developer Power Tools updates and news</description> <ttl>10</ttl> <name /> <user /> <image /> <item> <title>First Post!</title> <description>Here is my first post to this feed</description> <link>http://www.oreilly.com</link> </item> <item> <title>Second Post!</title> <description>Here is my second post to this feed</description> <link>http://www.oreilly.com</link> </item> </channel> </rss>
Instead of linking directly to the handler, you can use the RssHyperlink
control, which makes it easier to supply the channel name to the RSS feed. Add the control to your page using the following tag:
<cc1:rsshyperlink id="RssHyperLink1" runat="server" height="15px" includeusername="False" width="91px" ChannelName="General" NavigateUrl="~/RssHandler.ashx">My RSS Feed</cc1:rsshyperlink>
In the tag declaration, specify the name of the channel as well as the URL to use. The control will then render as a normal hyperlink that points directly to your RSS feed (but you have to supply the orange icon).
The RSS Toolkit provides a couple of other ways to generate strongly typed classes, including a command-line tool and another build provider for .rssdl files (.rssdl is a simple file format pointing to RSS URLs on the Web). You can explore these options on your own. Now, let’s look at how to consume a feed using the RSS Toolkit.
The RSS Toolkit doesn’t just make it easy to generate RSS feeds; it also makes it easy to consume them. Many sites expose RSS feeds that can be used to syndicate news or posts on your site.
Without the RSS Toolkit, you’re stuck manually requesting the RSS feed and parsing the XML returned. With this tool, you can simply drag a data source to your ASP.NET form and wire up a Gridview
, and you’re ready to go.
First you need to add a reference to the RssToolkit.dll assembly in your application. Next, either add the control to your toolbox (see the Appendix) or manually add the data source to your page using the following registration:
<%@ Register Assembly="RssToolkit" Namespace="RssToolkit" TagPrefix="cc1" %>
Then add this tag:
<cc1:RssDataSource ID="RssDataSource1" runat="server"> </cc1:RssDataSource>
Start by configuring the data source. Click on the SmartTag and select “Configure Data Source,” as shown in Figure 1-19.
This will launch the data source configuration dialog shown in Figure 1-20.
Specify the URL of the RSS feed and click OK. Now that the data source is configured, you can attach a Gridview
and view the results in your browser, as shown in Figure 1-21.
The RSS Toolkit makes it very easy to read from an existing RSS feed and display its contents in your application.
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.