Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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.

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

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

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

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

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

ASPX Page

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.

<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

Share

About the Author

Ken Kilar

United States United States
No Biography provided

Comments and Discussions

 
GeneralXML error - < 11 Pinmemberdoug767620-Jan-07 5:50 
GeneralRe: XML error - < 11 PinmemberCelebSafari20-Jan-07 6:58 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Jul 2006
Article Copyright 2006 by Ken Kilar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid