Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Partial Page Caching - Consuming and Displaying RSS XML Feeds via XSL

3.50/5 (4 votes)
28 Jul 20064 min read 1   193  
A tip on partial page caching & consuming and diaplying RSS XML feeds via XSL

Sample Image - rssxml_image.gif

Introduction

This tip lightly covers three areas:

  1. Consuming an RSS feed (not hard)
  2. Partial page caching (not hard), and
  3. 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

User Control

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.

ASP.NET
<%@ OutputCache Duration="120" VaryByParam="none" %>

(Duration is in seconds, so the above page cache will expire every 2 minutes.)

The 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 create different instances of my default.aspx page...

http://www.celebsafari.com/default.aspx?PgID=4

....by adding a "PgID" parameter.

ASP.NET
<%@ 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, almost everyone knows how to do it, so I won't 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.

C#
WebRequest request = WebRequest.Create(MyStream);
request.Timeout=10000;
WebResponse response = request.GetResponse();

XSL

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.

XML
<xsl:if test="position() > 11">
...
</xsl:if>

Setting the XSL target attribute will make sure the hrefs open in a new browser.

XML
<xsl:attribute name="target">_new</xsl:attribute>

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.

XML
<xsl:attribute name="style">text decoration:none</xsl:attribute>

It’s considered a shortcut by some, but I needed to make sure markups like &quot; didn’t output as &quot;, which they were without using the DOE attribute. The arguments not to use DOE don’t really apply to this example/use.

XML
<xsl:value-of select="title" disable-output-escaping="yes" />

ASPX Page

This is the simplest part. Just register your user control (.ascx file).

ASP.NET
<%@ Register tagPrefix="myCtrls" tagName="rss" Src="rss.ascx" %>

Once the control is registered, you can use that anywhere in your page.

HTML
<html>
<body>
<div align="left"><myCtrls:rss runat="server" /></div>
<body>
</html>

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/.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.