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 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:
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.
Update: How to Show a Limited Number of Items from an RSS Feed
One of the most common questions I receive about the code on this page is how to
limit the number of items displayed. For example, let's assume the RSS feed you are
pulling in lists the last 20 items published, but you only want to display the 5 most
recent items. Luckily, it's relatively easy to do. What's even better is that
you can easily do it by creating a modified .XSL file. This means that you can easily
display the abbreviated list in one place and the full list in another using the
same script just by specifying a different style sheet file.
The change to the XSL files is quite simple. Here are the same two files
presented above with the new restriction that limits the number of items
displayed to the first 5 highlighted in red.
The limiting command just says that the position of the element needs to be less then or equal to 5:
[position()<=5]. The problem is that since this command is inside an XML-formatted
file, we need to encode the < character as <
to prevent it from causing problems.
That's all there is to it. I chose to display the first five items since I figured that would
probably be similar to what most people are trying to do, but you can obviously change the
criteria to whatever happens to fit your situation.