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")
private _rssReader = New System.Xml.RssChannel(new streamreader("path"))
Channel tag Values:
_rssReader.getChannelValue("link")
_rssReader.getChannelValue("description")
Channel tag Attributes:
_rssReader.getChannelAttribute("atom:link")("href")
item tag Value
While i < _rssReader.count - 1
string += _rssReader(i).value("title")
i += 1
End While
Item tag Attributes:
While i < _rssReader.count - 1
string += _rssReader(i).attribute("guid")("isPermaLink")
i += 1
End While
Item tag array:
While i < _rssReader.count - 1
string += _rssReader(i).value("category")(0)
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
Try
hash(node.Name).count()
hash(node.Name).add(node.InnerText)
Catch ex As Exception
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.
HistoryVersion 1: 09/04/2010