Skip Navigation LinksFront Page > Archive > Syndicating Neowin Headlines in ASPnet
posted on:12/12/2004 10:44:29 PM
neowinASP.net makes it very easy to syndicate an RSS feed. In this article we will review syndicating the neowin rss feed located at : http://www.neowin.net/backend.php

To take advantage of ASP.nets XML and XSL parsing you need to add the following name spaces at the top of your page.

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>

Then add the following code to the Page_Load procedure on your page (in vb)

Dim strXmlSrc As String = "http://www.neowin.net/backend.php"
Dim strXslFile As String = Server.MapPath("neowin-XSL.xsl")
Dim myXmlDoc As XmlDocument = New XmlDocument()
myXmlDoc.Load(strXmlSrc)
Dim myXslDoc As XslTransform = New XslTransform()
myXslDoc.Load(strXslFile)

Dim myStringBuilder As StringBuilder = New StringBuilder()
Dim myStringWriter As StringWriter = New StringWriter(myStringBuilder)
myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter)
neowin.Text = Replace(Replace(myStringBuilder.ToString,"<br>","<br />"),"&","&amp;")

You will notice I am using the replace function on the variable myStringBuilder.ToString, this is because the output from the neowin rss feed is not valid XHTML, it closes the line break tags and changes & to &amp. If you do not require XHTML compliancy you can remove the replace functions and the script will run a bit faster. You will also notice a path to an xsl file. The xsl file is used to control the format of the output. the contents of the xsl file is below.

neowin-XSL.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">
<span class="style1"><strong><xsl:value-of select="title" /></strong></span>
<br />
<span class="style1"><xsl:value-of disable-output-escaping="no" select="substring(description,1,150)" /></span>
<br /><br />
<a href = "{link}">READ FULL ARTICLE</a>
<br /><br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

This xsl transformation is pretty straightforward in this case. There is a great XSLT reference available at: http://www.w3schools.com/xsl/default.asp.I am using the XPATH function substring() to limit the number of character in the description to 150. To learn more about XPATH functions, refer to: http://infohost.nmt.edu/tcc/help/pubs/xslt/xpath-sect.html. Neowin preformats the description field to include icons or other links. If you wish to show the HTML output of the neowin description field change value-of disable-output-escaping="no" to value-of disable-output-escaping="yes".

Now that we have transformed out XML file all that left to do is display it. This is easy using an asp literal.<asp:Literal id="neowin" runat="server" />

On non-SGML characters.

The neowin RSS feed contains many occurrences on non-SGML character. These characters will cause your web site to fail XHTML validation. This is caused because most articles on neowin are posted by users. These users may be using strange operating system from the beyond or are in different countries using different character sets. To see a list of SGML characters please refer to: http://www.tiffanybbrown.com/htmlentities.php or http://maniacalrage.net/projects/special/. If you wish to remove these characters you can use the function below.

function StripSymbols(sString)
Dim nCharPos, sOut, nChar
nCharPos = 1
sOut = ""
For nCharPos = 1 To Len(sString)
nChar = Asc(Lcase(Mid(sString, nCharPos, 1)))
If ((nChar > 31 And nChar < 63) or (nChar > 92 And nChar < 126)) Then
sOut = sOut & Mid(sString, nCharPos, 1)
End If
Next
StripSymbols = sOut
End function

The above function will work in asp or asp.net, rather than replacing bad characters we are checking that these characters are in an acceptable range. Currently I am not using this method on my current web site, because it made page loads too slow. By limiting the number of characters to 150 in the description field, I seem to have omitted any occurrences of these characters. If you wish to display the entire article and remain XHTML compliant you will need to run the variable through this function.

On Output Caching

If you want to push you page load times into overdrive you can cache your output. Since neowin updates thier articles every few ours, you dont need to request and parse the XML every single time someone requests your page. By declaring an output cahce once someone request your page that output is cached by IIS and each subsequent user will see that output over a period of time that you control. To enable output caching add the line. <%@ OutputCache Duration="3600" VaryByParam="none" %>



Comments For This Article:

Be the first to comment!

Post A Comment:

  Your Name:


  Your Comment:

  Please Enter The Captcha Image: