RSS definition From Wikipedia
"RSS (formally "RDF Site Summary", known colloquially as "Really Simple Syndication") is a family of Web feed formats used to publish frequently updated content such as blog entries, news headlines or podcasts. An RSS document, which is called a "feed", "web feed", or "channel", contains either a summary of content from an associated web site or the full text. RSS makes it possible for people to keep up with their favorite web sites in an automated manner that's easier than checking them manually.
RSS content can be read using software called an "RSS reader", "feed reader" or an "aggregator". The user subscribes to a feed by entering the feed's link into the reader or by clicking an RSS icon in a browser that initiates the subscription process. The reader checks the user's subscribed feeds regularly for new content, downloading any updates that it finds."
What I call it is a great way to get my news. I've got 4 feeds that I read in my Outlook daily. On my blog I have 4 other feeds including one of my own. You see them on almost every news site, blog, forum, website you visit. So why a blog entry on RSS Readers? Well the problem is that there aren't many options to add an aggregator so that you can display your favorite feeds on your site for your visitors. So today we are going to take a little bit of .NET and add some XSLT and finally throw in a few features to display the feed on your community website.
So lets get started. First add a user control to your project. Here in our control you can see that we are simply using an XSLCompiledtransform object to transform the feed. You will also notice that we have added a property to our user control so that we could set the path to the feed at runtime.
Imports System.Xml Imports System.Xml.Xsl Imports System.IO Partial Public Class ucBlogReader Inherits System.Web.UI.UserControl Private m_xmlURL As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load LoadBlog(m_xmlURL) End Sub Protected Sub LoadBlog(ByVal path As String) If path = "" Then XMLBlogReader.Text = "Please select a blog from the left" Else Dim myXSL As New XslCompiledTransform Dim tempStringWriter As New StringWriter Dim tempString As XmlWriter = New XmlTextWriter(tempStringWriter) myXSL.Load(Server.MapPath("~/XSL/rss-reader.xslt")) myXSL.Transform(path, tempString) Me.XMLBlogReader.Text = tempStringWriter.ToString() End If End Sub Public Property XMLUrl() As String Get Return m_xmlURL End Get Set(ByVal value As String) m_xmlURL = value End Set End Property End Class
Now comes the fun part. Once we have our RSS feed we have various options to transform it. We could use XMLDocument, XMLReader, or XMLDatasourse and many other options to traverse the feed. For this example we are going to build the XSLT we use in the code above to transform our RSS Feed.
There are two key parts that I want to point out in our XSLT document.
The first being our loop. Here we are testing the position of each "item" node with in the XPATH statement. When that position is greater than 5 we exit the loop.
The second area I wanted to look at is where we display the "description" node. What we are doing here is taking the first 500 characters of the description using the substring function with in XPATH.
<xsl:value-of select="substring(description, 1, 500)" disable-output-escaping="yes"/>
There are many different ways of building an XSLT document, such as building a template for the title, description, links etc... In our example we are just creating a single template and keeping our document as simple as possible.
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/"> <xsl:output method="xml"/> <xsl:template match="/"> <Div id="wrap"> <Div style="text-align: left;"> <xsl:attribute name="id">title</xsl:attribute> <H3> <xsl:value-of select="//dc:creator" /> </H3> </Div> <Div> <xsl:attribute name="id"> content:<xsl:value-of select="rss/channel/title" /> </xsl:attribute> <xsl:for-each select="rss/channel/item[position()<=5]"> <P style="text-align: left;"> <A> <xsl:attribute name="href"> <xsl:value-of select="link"/> </xsl:attribute> <xsl:attribute name="style"> color: black; font-weight: bold; </xsl:attribute> <xsl:attribute name="target">_new</xsl:attribute> <xsl:value-of select="title"/> </A> <br/> <xsl:value-of select="pubDate"/> <br/> </P> <Div style="text-align: left; padding-top: 10px; padding-bottom: 5px; padding-left: 15px;"> <xsl:value-of select="substring(description, 1, 500)" disable-output-escaping="yes"/> <xsl:text>...</xsl:text> </Div> </xsl:for-each> </Div> </Div> </xsl:template> </xsl:stylesheet>
The final step is to add your new control to your site and set the value of the XMLUrl property to the URL of your desired feed. I'm going to leave that with you to accomplish, however if you would like to see our new control working, you can do so at http://www.proessent.com/blog.aspx
That's it for now. Check back next week when we will look at building another RSS Reader using LINQ in Visual Studio 2008.
See you on the Flip Side