This tip lightly covers three areas;
- consuming an RSS feed (not hard),
- partial page caching (not hard) and
- presenting the XML data as HTML via an XSL template (not hard).
I decided to put this together because I recently wanted to add some RSS feed content to my site and couldn’t seem to find an article that offered the exact technical guidance I needed. Not to say that this tip is complete, but something like this is what I needed. If there is any code resuse, it's because there are very few ways to write this. The idea here is just to bring a few simple concepts together for practical use.
As you know, there are lots of great free RSS feeds for anyone to consume, and for a small site like mine, those feeds can provide good complimentary and authoritative content. What isn’t so great is that often consuming those feeds can slow down your site, especially if there are multiple feeds to consume. And in some cases they can cause IIS to completely lockup. My site (even the homepage) is primarily dynamic - I can’t cache the whole page(s) because much of the content, outside of the XML data, changes frequently. I found partial page caching the XML data to be a good solution.
I wanted a clean and simple way to present the RSS XML content. Using an XSL (eXtensible Stylesheet Language) template was the way to go. There are a few pretty cool (not complex) tricks I learned with XSL that I thought I should share.
Using the code
Partial page caching is super easy. You just need to make a user control (pagelet) for each area of the page you want to cache, which in this case is nothing more than a regular .aspx page, but with an extension of .ascx. It's in the header of this page that you put the cache timeout parameter, not in the hosting .aspx file.
<%@ OutputCache Duration="120" VaryByParam="none" %>
(duration is in seconds, so the above page cache will expire every 2 minutes)
VaryByParam is required, even if you don’t use it. If you do want to create different versions of cache based on URL parameters, leveraging
VaryByParam is how you would do it. For example, I could crate different instances of my default.aspx page...
....by adding a "PgID" parameter.
<%@ OutputCache Duration="120" VaryByParam="PgID" %>
You can do more with caching (controls, etc,..) but the above is more than enough for this example.
As far as consuming an XML Stream, most everyone knows how to do it, so I want go over it line by line. But I do want to point out that you should definitely use a timeout parameter before trying to get a response. Also, make sure you use some exceptions handling in the request procedure. Unlike the duration in the page caching, the length of time for the request timeout is in milliseconds.
WebRequest request = WebRequest.Create(MyStream);
WebResponse response = request.GetResponse();
Using the .NET asp:xml server control, you can apply a XSL template and transform the XML data into well formatted HTML (text too, if you like). Although my example is a simple use, there is a lot more cool stuff you can do with XSL. There are dozens of XSL samples and tools out there to help you get out of the gate, but I will point out a few things that are in my loop block.
This block will check the current position, making sure the results displayed are less than 10.
<xsl:if test="position() > 11">
Setting the XSL target attribute will make sure the href’s open in a new browser.
Unlike the links on my host page, I didn’t want my XML links underlined. I display them with a small font, and there are just too many links - having them underlined would make them difficult to read.
<xsl:attribute name="style">text decoration:none</xsl:attribute>
It’s considered a shortcut by some, but I needed to make sure markups like " didn’t output as ", which they were without using the DOE attribute. The arguments not to use DOE don’t really apply to this example/use.
<xsl:value-of select="title" disable-output-escaping="yes" />
This is the simplest part. Just register your user control (.ascx file).
<%@ Register tagPrefix="myCtrls" tagName="rss" Src="rss.ascx" %>
Once the control is registered, you can use that anywhere in your page.
<div align="left"><myCtrls:rss runat="server" /></div>
Points of Interest
You should be able to save the three attached files in your local web root and bring up the example in your browser at http://localhost/rss.aspx
You can checkout this code in action at my site, at http://www.celebsafari.com/