Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

RSS Reader

, 1 Jul 2010
Rate this:
Please Sign up or sign in to vote.
RSS Reader
Introduction
 

I am trying to use aspx on my site because it's easy to program and is very powerful. RSS is personally the best thing to get a site's search engine listings up. I wanted to be able to optimize the number of ways people can view my feed, so I created this class.

 

 
Using the code

Declaring:

private _rssReader = New System.Xml.RssChannel("feedURL") 'for a web feed or...
 
private _rssReader = New System.Xml.RssChannel(new streamreader("path")) 'for a local feed file   
Channel tag Values:
 _rssReader.getChannelValue("link") 'to get the "<link>value</link>" value
_rssReader.getChannelValue("description") 'to get the "<description>value</description>" value etc.
Channel tag Attributes:
_rssReader.getChannelAttribute("atom:link")("href") 'href Attribute in the atom:link tag 
item tag Value 
While i < _rssReader.count - 1
        string += _rssReader(i).value("title") 'Loops Through all the Inner Values for th <title> tag of the feeds items
        i += 1
End While   
Item tag Attributes:
While i < _rssReader.count - 1
        string += _rssReader(i).attribute("guid")("isPermaLink") 'Loop Through all the items of the feed and get the guid isPermaLink Attribute
        i += 1
End While   
Item tag array:
While i < _rssReader.count - 1
        string += _rssReader(i).value("category")(0) 'Loop Through all the items of the feed and get the first category
        i += 1
End While
RssReader Source:
Imports System.Xml
Imports System.Net
Imports System.IO
Imports System.Collections
Public Class RssChannel
    Inherits CollectionBase
    Private _xmltxt As String
    Private _xml As XmlDocument
    Private _itemArray As ArrayList = New ArrayList
    Private _channelValues As New Hashtable
    Private _channelAtrValues As New Hashtable
    Public Sub New(ByVal file As IO.StreamReader)
        _xml = New XmlDocument
        _xml.Load(file)
        read()
    End Sub
    Public Sub New(ByVal url As String)
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Try
            Dim address As New Uri(url)
            request = DirectCast(WebRequest.Create(address), HttpWebRequest)
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())
            _xml = New XmlDocument
            _xml.Load(reader)
            read()
        Finally
            If Not response Is Nothing Then response.Close()
        End Try
    End Sub
    Public Function getChannelValue(ByVal name As String) As String
        Select Case _channelValues.ContainsKey(name)
            Case True
                Return _channelValues(name)
            Case Else
                Throw New Exception("ChannelVar: " + name + " Doesnt Exist")
        End Select
    End Function
    Public Function getChannelAttribute(ByVal name As String) As Hashtable
        Select Case _channelAtrValues.ContainsKey(name)
            Case True
                Return _channelAtrValues(name)
            Case Else
                Throw New Exception("ChannelAtrVar: " + name + " Doesnt Exist")
        End Select
    End Function
    Default Public Overridable ReadOnly Property Item(ByVal index As Integer) As RssItem
        Get
            If Not ((index < 0) Or (index > _itemArray.Count - 1)) Then
                Return _itemArray(index)
            Else
                Throw New Exception("Index is out of range.")
            End If
        End Get
    End Property
    Public Shadows Function count() As Integer
        Return _itemArray.Count
    End Function
#Region "Frame"
    Private Sub read()
        Try
            loadChannelVars()
            loadItemArray()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
    Private Sub loadChannelVars()
        For Each node As XmlNode In _xml.SelectNodes("/rss/channel")
            For Each node2 As XmlNode In node.ChildNodes()
                Dim AtrHashInner As New Hashtable
                If Not (node2.Name = "item") Then
                    _channelValues.Add(node2.Name, node2.InnerText())
                    Dim i = 0
                    While i < node2.Attributes.Count
                        AtrHashInner.Add(node2.Attributes(i).Name, node2.Attributes(i).InnerText)
                        i += 1
                    End While
                    _channelAtrValues.Add(node2.Name, AtrHashInner)
                End If
            Next
        Next
    End Sub
    Private Sub loadItemArray()
        For Each item As XmlNode In _xml.SelectNodes("/rss/channel/item")
            Dim hash As New Hashtable
            Dim AtrHash As New Hashtable
            Dim categorys As ArrayList = New ArrayList()
            For Each node As XmlNode In item.ChildNodes
                Dim AtrHashInner As New Hashtable
                If Not (hash.ContainsKey(node.Name)) Then
                    hash.Add(node.Name, node.InnerText)
                    Dim i = 0
                    While i < node.Attributes.Count
                        AtrHashInner.Add(node.Attributes(i).Name, node.Attributes(i).InnerText)
                        i += 1
                    End While
                    AtrHash.Add(node.Name, AtrHashInner)
                Else
                    'more then one
                    Try
                        hash(node.Name).count()
                        'Arraylist
                        hash(node.Name).add(node.InnerText)
                    Catch ex As Exception
                        'String
                        Dim Arr As New ArrayList
                        Arr.Add(hash(node.Name))
                        Arr.Add(node.InnerText)
                        hash(node.Name) = Arr
                    End Try
                End If
            Next
            _itemArray.Add(New RssItem(hash, AtrHash))
        Next
    End Sub
    Public Structure RssItem
        Public value As Hashtable
        Public attribute As Hashtable
        Public Sub New(ByVal valueHash As Hashtable, ByVal attributeHash As Hashtable)
            value = valueHash
            attribute = attributeHash
        End Sub
    End Structure
#End Region
End Class 
 
Points of Interest

I created this class because firstly I wanted to test reading an XML file and secondly as a webmaster I am well aware of the power of a feed and so I want to be able to use it.

When creating this class, I found that there are multiple ways of reading an XML file.

I settled with XMLDocument as it was like handling an array instead of a text file like some of the others.

History

Version 1: 09/04/2010 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

William Dyson

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 1 Jul 2010
Article Copyright 2010 by William Dyson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid