Click here to Skip to main content
13,189,506 members (62,568 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 28 Jul 2006

Partial Page Caching - consuming and displaying RSS XML feeds via XSL

, 28 Jul 2006
Rate this:
Please Sign up or sign in to vote.
A tip on partial page caching & consuming and diaplying RSS XML feeds via XSL

Sample Image - rssxml_image.gif


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.

<%@ 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 crate different instances of my default.aspx page... 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.

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

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

<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


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


About the Author

Ken Kilar
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralXML error - < 11 Pin
doug767620-Jan-07 5:50
memberdoug767620-Jan-07 5:50 
GeneralRe: XML error - < 11 Pin
CelebSafari20-Jan-07 6:58
memberCelebSafari20-Jan-07 6:58 
Hi Doug. Thanks for the feedback. I just downloaded the source and I didn't get an error. Make sure you are using the HTML encoded character, and not the "<" in the source.

That block is nice to keep, because it checks to make sure that only the most recent 10 items of the feed are displayed. I have been running the same source at celebsafari for a long time now and have never experienced an error.

Oh, and make sure you post your celeb encounters at! Cool | :cool:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171016.2 | Last Updated 28 Jul 2006
Article Copyright 2006 by Ken Kilar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid