Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Building a web based RSS Feed Viewer using System.XML

0.00/5 (No votes)
20 Apr 2004 1  
This article illustrates building a class to parse RSS Feeds and return the details in a form that they can be eaisly manipulated and displayed.

Sample Image - screenshot.jpg

Introduction

After seeing numerous applications available on the Internet to download and view RSS Feeds off the Internet, I wondered what would be needed to develop a .NET component to Read, Parse and Display RSS Feeds in aspx pages.

The Component development is beyond the scope of this article, which will cover the base class that is used by the component to render the detail you see in the screenshot above.

RSS feeds are designed to be parsed simply using a standard XML Parser. To this end, I made extensive use of the System.XML libraries which I encapsulated in a separate class to handle loading of XML documents via a HTTP Stream.

On to the code!!

Deriving the RSS Version

It is always prudent to derive the version of the document being parsed. All RSS Feeds contain a version number. The class currently supports RSS feeds from v09.1 to v2.00. Since the XML Parser offers a simple way of selecting a specific node in the document, it was subsequently simple to return the RSS Version using the function below:

Public Enum RSSVersion
    version_Unknown = 0
    version_0_91 = 1
    version_0_92 = 2
    version_2_00 = 3
End Enum

Private Function getRSSVersion() As RSSVersion
    Dim myrootNode As XmlNode
    Dim versionAttribute As XmlAttribute
    Dim myRSSVersion As RSSVersion
    myRSSVersion = RSSVersion.version_Unknown
    myrootNode = fetchNode(Document.ChildNodes, "rss")
    versionAttribute = fetchAttribute(myrootNode, "version")
    Select Case versionAttribute.Value
        Case "0.91"
            myRSSVersion = RSSVersion.version_0_91
        Case "0.92"
            myRSSVersion = RSSVersion.version_0_92
        Case "2.00"
            myRSSVersion = RSSVersion.version_2_00
    End Select
    Return myRSSVersion
End Function

RSS documents follow a standard structure with only a few differences between versions. This class only concentrates on the standard items which are the same across all versions.

Loading the RSS Header

The RSS Header contains important information such as Author, Title, Source Link and Description. This is all loaded into the structure below and is accessible from the class.

Public Structure RSSHeader
    Dim Title As String
    Dim Link As Uri
    Dim Description As String
    Dim Language As String
    Dim Copyright As String
    Dim managingEditor As String
    Dim webMaster As String
    Dim lastBuildDate As DateTime
End Structure

This structure is populated in much the same way using the parsing functionality in the csxmlparser class.

Me.myRSSHeader.Copyright = ""
Me.myRSSHeader.Description = ""
Me.myRSSHeader.Language = ""
Me.myRSSHeader.Title = ""
Me.myRSSHeader.webMaster = ""
Me.myRSSHeader.managingEditor = ""
Me.myRSSHeader.lastBuildDate = Nothing
Mybase.DocumentRoot = Me.fetchNode(Document.ChildNodes, "rss")
Mybase.DocumentRoot = Me.fetchNode(Mybase.DocumentRoot.ChildNodes, "channel")
Me.myRSSVersion = Me.getRSSVersion
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "title")
If Not IsNothing(myNode) Then Me.myRSSHeader.Title = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "link")
If Not IsNothing(myNode) Then Me.myRSSHeader.Link = New Uri(myNode.InnerText)
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "description")
If Not IsNothing(myNode) Then Me.myRSSHeader.Description = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "language")
If Not IsNothing(myNode) Then Me.myRSSHeader.Language = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "copyright")
If Not IsNothing(myNode) Then Me.myRSSHeader.Copyright = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "managingEditor")
If Not IsNothing(myNode) Then Me.myRSSHeader.managingEditor = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "webMaster")
If Not IsNothing(myNode) Then Me.myRSSHeader.webMaster = myNode.InnerText
myNode = fetchNode(Mybase.DocumentRoot.ChildNodes, "lastBuildDate")
If Not IsNothing(myNode) Then
    Me.myRSSHeader.lastBuildDate = DateTime.Parse(myNode.InnerText)
End If

Again, it's a simple case of finding the correct nodes within the RSS Feed document.

Loading the RSS Items

I choose to store the RSS news items stored in the feed in a custom collection class inherited from CollectionBase. This collection stores the base information for a RSS Item in a structure shown below:

Public Structure RSSItem
    Dim Title As String
    Dim Description As String
    Dim pubDate As DateTime
    Dim Link As Uri
End Structure

This is enough information to render the chosen items in our control. And since the class has already derived the root node of the RSS Document (The Channel tag), then it is simply a case of stepping through all Item nodes within the Document and populating the collection as follows:

Me.myRSSItems.Clear()

For i = 0 To Mybase.DocumentRoot.ChildNodes.Count - 1
    If Mybase.DocumentRoot.ChildNodes(i).Name = "item" Then
        myItemNode = Mybase.DocumentRoot.ChildNodes(i)
        Dim myItem As RSSItem
        myItem.pubDate = Nothing
        myNode = fetchNode(myItemNode.ChildNodes, "description")
        If Not IsNothing(myNode) Then myItem.Description = myNode.InnerText
        myNode = fetchNode(myItemNode.ChildNodes, "title")
        If Not IsNothing(myNode) Then myItem.Title = myNode.InnerText
        myNode = fetchNode(myItemNode.ChildNodes, "link")
        If Not IsNothing(myNode) Then myItem.Link = New Uri(myNode.InnerText)

        myNode = fetchNode(myItemNode.ChildNodes, "pubDate")
        If Not IsNothing(myNode) Then 
            myItem.pubDate = DateTime.Parse(myNode.InnerText)

        Select Case Me.myRSSVersion
            Case RSSVersion.version_0_91
            Case RSSVersion.version_0_92
            Case RSSVersion.version_2_00
        End Select
        Me.myRSSItems.Add(myItem)
    End If
Next

Using the class

Two functions are exposed to allow loading a RSS Feed from a file or from a HTTP Stream. These can be used as follows:

Rem Load my RSS Feed from a File
Dim myRSSFeed as New csRSSFeed()
myRSSFeed.Load("C:\feeds\MSDN.rss")

Rem Load my RSS Feed from a HTTP Server
Dim myRSSFeed as New csRSSFeed()
myRSSFeed.LoadFromHTTP("http://www.theregister.co.uk/headlines.rss")

Once you have executed the relevant load function, information about the document can be found in the Header, Version and items properties, which return the RSS Header, Document Version and RSS Items respectively.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here