An ASP.NET RSS Feed Reader
by John Peterson
Introduction
A few months ago, I published an article (A MegaTokyo RSS Feed Reader)
that showed how you could use classic ASP and Microsoft's XML parser to convert an
RSS feed into HTML for display on a web page. While the code in that article worked
fine, there were two main complaints that I seemed to keep hearing from readers:
- It was written in classic ASP and not ASP.NET.
- The format of the resulting HTML was a pain to change because I had hard-coded it into the script.
This time around I set out to address both of those issues.
If you're not familiar with RSS, you might find
XML Files's RSS section helpful.
An ASP.NET RSS Feed Reader
If I was going to go through the trouble of doing this in ASP.NET, I was going to do
it right and, while it is possible, I didn't want to call the COM objects that we used
the first time around. This is .NET after all so naturally I wanted to use the .NET Framework's
XmlDocument object. Aside from using the .NET object, this part of the script is basically the
same. We create an XML object and load an XML file into it either from the remote server or from
the file system.
Now to the formatting. Since I was basically starting from scratch anyway, I once again figured that
I should take the time and do it right. So instead of all the string parsing and editing I did via VBScript
the first time around, this time I set out to create an XSL stylesheet to handle the conversion from the
RSS feed's XML format to the desired HTML format. The bad news is that my XSL skills are very weak and,
while I knew how to use a stylesheet, actually writing one took me a lot longer than it should have.
The good news is that after several tries I finally did get a couple different ones hammered out so you
can see how easy it is to change the format of the resulting HTML. All you do is load a different
stylesheet... it really couldn't be any simpler.
The last issue that I feel I should mention is that this time around I didn't have to write any caching
code. I simply set the page to output cache and presto... instant caching... isn't ASP.NET great!
Why MegaTokyo?
Aside from the fact that they keep changing their format (I miss the thumbnails) and they keep forgetting to
update their feed... why not? It's something I read and I want to know when it get's updated. Enough said.
For those who haven't yet been there and have no idea what I'm talking about, you might want to visit
MegaTokyo.
Despite the fact that I'm using MegaTokyo's feed, there's nothing in the code that should stop you from using
it with most any RSS feed. You'll probably need to tweak the XSL files to get your feed to look how you
want, but aside from that the script should work with most RSS feeds right "out of the box".
The Code
megatokyo_rss.aspx
<%@ Page Language="VB" Debug="False" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<script language="VB" runat=server>
Sub Page_Load(sender As Object, e As EventArgs)
' Using a live RSS feed... could also use a cached XML file.
Dim strXmlSrc As String = "http://www.megatokyo.com/rss/megatokyo.xml"
'Dim strXmlSrc As String = Server.MapPath("megatokyo.xml")
' Path to our XSL file. Changing the XSL file changes the
' look of the HTML output. Try toggling the commenting on the
' following two lines to give it a try.
Dim strXslFile As String = Server.MapPath("megatokyo.xsl")
'Dim strXslFile As String = Server.MapPath("megatokyo2.xsl")
' Load our XML file into the XmlDocument object.
Dim myXmlDoc As XmlDocument = New XmlDocument()
myXmlDoc.Load(strXmlSrc)
' Load our XSL file into the XslTransform object.
Dim myXslDoc As XslTransform = New XslTransform()
myXslDoc.Load(strXslFile)
' Create a StringBuilder and then point a StringWriter at it.
' We'll use this to hold the HTML output by the Transform method.
Dim myStringBuilder As StringBuilder = New StringBuilder()
Dim myStringWriter As StringWriter = New StringWriter(myStringBuilder)
' Call the Transform method of the XslTransform object passing it
' our input via the XmlDocument and getting output via the StringWriter.
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter)
' Since I've got the page set to cache, I tag on a little
' footer indicating when the page was actually built.
myStringBuilder.Append(vbCrLf & "<p><em>Cached at: " _
& Now() & "</em></p>" & vbCrLf)
' Take our resulting HTML and display it via an ASP.NET
' literal control.
litMegaTokyoRssHtml.Text = myStringBuilder.ToString
End Sub
</script>
<html>
<head>
<title>ASP 101's ASP.NET MegaTokyo RSS Feed Reader</title>
</head>
<body>
<asp:Literal id="litMegaTokyoRssHtml" runat="server" />
</body>
</html>
|
Here are the listings for the two included XSL files as well:
megatokyo.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<p>
<a href="{link}"><strong><xsl:value-of select="title" /></strong></a>
<xsl:value-of disable-output-escaping="yes" select="description" />
</p>
<hr />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
megatokyo2.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:for-each select="rss/channel">
<h2><a href="{link}"><xsl:value-of select="title" /></a></h2>
</xsl:for-each>
<ul>
<xsl:for-each select="rss/channel/item">
<li><a href="{link}"><strong><xsl:value-of select="title" /></strong></a></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
|
The Output
Just so you can see what you'll be getting, here are examples of what the output from each stylesheet looks like:
Sample megatokyo.xsl Output
|
Comic [614] - "I wish i could start over"
chapter 5, episode 80 [click to read]
[piro] posted: 09.27.2004 [3:00 Pm EST]
Comic [613] - "but some girls like pathetic dorks"
chapter 5, episode 79 [click to read]
[piro] posted: 09.24.2004 [11:00 am EST]
Comic [612] - "playing poorly"
chapter 5, episode 78 [click to read]
[piro] posted: 09.22.2004 [12:34 am EST]
Cached at: 9/28/2004 1:01:00 PM
|
Sample megatokyo2.xsl Output
Megatokyo News
-
Comic [614] - "I wish i could start over"
-
Comic [613] - "but some girls like pathetic dorks"
-
Comic [612] - "playing poorly"
Cached at: 9/28/2004 1:01:00 PM
|
Download
For those who don't like cutting and pasting, you can download a zip file containing the ASP.NET code and the two
XSL files from here: megatokyo_rss_aspx.zip (3.8 KB).
Related Information
- A MegaTokyo RSS Feed Reader - The classic ASP version of the above code, but without the XSL-based formatting.