A Custom ASP.NET Server Control for Displaying RSS FeedsBy Scott Mitchell
Recently I decided to list recommended blogs for ASP.NET developers here on 4Guys. If you're not familiar with blogs, they are personal Web sites that are frequently updated. Most blogs are rather personal in nature, with their founders using them as a sort of "online diary." They'll talk about their day, what they're plans are for the weekend, their relationships, their views on world events and politics, and so on. More interestingly, there are a number of blogs that are highly focused on a particular subject: law, politics, a specific computer technology, or a particular profession. The recommended blogs page lists blogs I personally recommend that are heavily .NET- or ASP.NET focused.
When creating this recommended blogs page, I decided rather than merely listing links to the various blogs, I'd like to also show the title of each recommended blog's most recent entries. This task is not too difficult to accomplish because blogs typically provide an RSS syndication feed. RSS is a standard that spells out a format for syndicated content in an XML format. (For the specifics of the format, see the RSS 2.0 specification.) There are a number of programs, called aggregators that are designed specifically to organize syndicated content. Some free aggregators build using the .NET Framework include SharpReader and RssBandit. You can also easily build your own aggregator, as I described in this article: Creating an Online RSS News Aggregator with ASP.NET.
Initially I decided to just use the techniques described in an earlier 4Guys article, Consuming an RSS Feed with ASP.NET, which demonstrates how to display the items in an RSS feed in a data Web control. However, the code to implement this technique, while only a few lines of code long, seemed overly complex for the simple task I was after. Furthermore, the code, as-is, does not employ any caching of the RSS data, which not only leads to longer load times on my end (since the content must be grabbed from a remote Web server on each page load), but is also bad netiquette since it greatly increases the bandwidth sucked down from the server hosting the blog. What I wanted to be able to do was the following: add a Web control to my ASP.NET Web page and with one or two lines of code, at most, have the control display the items from an RSS feed. Too, the control should cache the downloaded RSS feed for a developer-specified number of minutes.
To solve this problem I decided to create a custom ASP.NET server control, which I call RssFeed. RssFeed is an open-source control, with complete source code available for download and the RssFeed project home. This article explains how to obtain, install, and use RssFeed in an ASP.NET Web page. For technical information about RssFeed and its source code, be sure to consult the RssFeed Project Page.
You can download the code and/or a compiled assembly for the RssFeed server control by visiting the RssFeed project home. You can download the entire source code or a pre-compiled assembly (
.dllfile). (Note: the compiled assembly was compiled using Visual Studio .NET 2003, and is therefore targeted for the .NET Framework Version 1.1. If you need to use RssFeed for .NET Framework 1.0, you will need to download the source code and compile it through Visual Studio .NET 2002.)
If you download the source code, before using RssFeed in an ASP.NET Web application you'll need to compile the source
code. If you have Visual Studio .NET, this task is simple - simply double-click the
skmRss.csproj file in
the downloaded code files, which will open Visual Studio .NET. Next, go to the Build menu and choose Build Solution.
That's it! This will create the compiled assembly -
skmRss.dll. (If you are going to be using
RssFeed in an ASP.NET Web application on a Web server running the .NET Framework Version 1.1, you can just download
the pre-compiled assembly instead, if you want to skip the step of compilation yourself...)
Using RssFeed in an ASP.NET Web Application
The first step to using RssFeed in an ASP.NET Web application is to add the assembly,
skmRss.dll, to the Web application. If you are building your ASP.NET Web application using Visual Studio .NET you can accomplish this by adding the assembly to the References folder. From the Solution Explorer, right-click on the References folder, Browse to the
skmRss.dllfile, and click OK. If you are not using Visual Studio .NET, simply copy the
skmRss.dllfile to the Web application's
If you are using Visual Studio .NET you can, at this point, add RssFeed to the Toolbox. To accomplish this, right-click
on the Toolbox in Visual Studio .NET and select Add/Remove Toolbox Items (the exact wording differs between Visual Studio .NET
2002 and 2003...). Browse to the
skmRss.dll file and click OK. This will add a control called RssFeed
to the Toolbox. To add the RssFeed control to an ASP.NET Web page simply drag the control from the Toolbox onto an
ASP.NET Web Form's Design tab. As you can see in the screenshot below, RssFeed provides a rich design-time experience
with Visual Studio .NET.
To add the control to an ASP.NET Web page without using Visual Studio .NET you will need to perform the following
two steps. First, add the following
@Register directive to the top of the ASP.NET Web page:
Then, to add the control to your ASP.NET page, add the following markup where you want the feed to appear:
Once the RssFeed control has been added to an ASP.NET Web page, you need to add a couple lines of source code to
bind the control to an RSS feed. Specifically, in the
Page_Load event handler add the following
lines of code:
It's that easy! Take a moment to review the live demo so that you can see what the RssFeed control looks like in action. In that live demo it looks pretty bland. However, as we'll see next, there are a number of properties that can be set to specify the look and feel. The most pertinent ones are:
HeaderStyle- dictates the style for the header. (The top row.)
ShowHeader- determines if the header row is shown (defaults to True).
HeaderText- the text that is displayed in the header.
MaxItems- the maximum number of RSS items to show (defaults to 10).
ItemStyle- dictates the style for the displayed RSS items.
AlternatingItemStyle- dictates the style for the alternating displayed RSS items.
ShowCategory- specifies if the Category column is displayed. (Some blog engines use the
<category>element of the RSS 2.0 specification to indicate what category the blog entry belongs to.)
ShowPubDate- specifies if the published date of the RSS item is displayed.
DateFormatString- specifies the date/time format string to use to format the published date.
Using these properties we can easily gussy-up the look and feel of the RssFeed control. With Visual Studio .NET this can be accomplished by merely setting the properties in the Design tab. The advantage of this is that the changes made to the RssFeed's properties are automatically reflected in the WYSIWYG designer. The markup below shows the declarative syntax for the RssFeed control with a number of properties set to improve the look and feel:
Handling Errors and Alternate Syndication Formats
Well-designed blog engines won't emit invalid RSS syndication feeds. That is, they will check to ensure that the feed being emitted conforms to XML formatting standards and uses only those elements outlined in the RSS specification. However, I have run across some syndication feeds that do not always admit syntactically valid XML. (For shame!) If RssFeed encounters an XML feed it cannot parse it displays the message There was an error reading this XML feed... where the RSS items would normally be displayed.
Also, realize that as of the time of this writing (October 29, 2003), RssFeed only supports syndication formats
that correspond to the RSS specification. Some sites use the RDF format for syndication.
It is planned to eventually add support for RDF as well - keep your eyes posted on the
RssFeed Project Page for the latest information about
UPDATE: As of RssFeed Version 1.1, RssFeed now supports RSS 2.0 and the RDF (or RSS 0.92/1.0) specification as well!
UPDATE: As of RssFeed Version 1.2, RssFeed now supports templates, which allow for a high degree of customization. Check out this demo for an example of the user-interface customizability available with templates!
UPDATE: As of RssFeed Version 1.9, RssFeed now supports the
element in the RSS 2.0 spec and also has decoupled the RSS slurping engine from the actual ASP.NET server control.
Learn more at Displaying RSS Feeds - A Look at RssFeed Version 1.9.
|For More Examples of RssFeed in Action...|
|Interested in seeing more examples of RssFeed in action? Check out: http://scottonwriting.net/demos/RssFeedDemos.aspx and http://scottonwriting.net/demos/RssExpandableDemo.aspx.|
|Syndicating Your Site's Content with RSS|
|Interested in learning how you can syndicate your Web site's content in the RSS format? If so, check out this article: Syndicating Your Web Site's Content with ASP.NET. (There's also another article, Syndicating Your Web Site's Content with RSS, that looks at syndication using classic ASP instead of ASP.NET...|
In this article we looked at RssFeed, an open-source custom ASP.NET server control I developed for displaying content from an RSS syndication feed. Specifically, we looked at how to obtain this control and how to start using it in an ASP.NET Web application. As we saw, with just a few lines of code and with simply dragging and dropping in Visual Studio .NET, we can quickly display the contents of a syndication feed.