CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 11, 2006

Creating RSS Feed Reader in ASP.NET

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:

  1. It was written in classic ASP and not ASP.NET.
  2. 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!

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

Continue Creating RSS Feed Reader in ASP.NET

Post a Comment