Friday, December 11, 2009

Reading an RSS feed - two methods

RSS feeds are a great way to enhance your web page. Your web page can be dynamic with daily headlines and stories, all in an autmated fashion. Parsing RSS feeds is fairly straightforward.


Below are two methods to reading an RSS feed.


Method 1:  This method requires very little code. It basically reads the feed via an XmlTextReader into a dataset. Then this dataset can be bound to a gridview, etc.


'Set up a Try/Catch in case the file isn't read correctly
Try

     'Open an XML file/RSS feed
     Dim reader As XmlTextReader = New XmlTextReader(http://stltomorrow.tumblr.com/rss)

     'Set up the dataset for the reader
     Dim ds As DataSet = New DataSet

     'Read the feed
     ds.ReadXml(reader)

     'Create the datatable
     Dim dtSTLtomorrow As DataTable
     dtSTLtomorrow = ds.Tables(2)

     'Bind to the datagrids/gridview
     dgSTLtomorrowVer.DataSource = dtSTLtomorrow
     dgSTLtomorrowVer.DataBind()

Catch ex As Exception

     'Show that there was an error reading the RSS feed

End Try


Method 2: This method requires a little bit more code, but it basically uses an XmlTextReader to read in the RSS feed and then loops through the feed node by node.

'Set up a Try/Catch in case the file isn't read correctly
Try

'Create the dataset to hold the incoming headlines
Dim dstSTLtomorrow As DataSet

'Set up the tables and columns
Dim dtblSTLtomorrow As DataTable
Dim dcolSTLtomorrowID As DataColumn
Dim pkColumns(1) As DataColumn

'Create the dataset
dstSTLtomorrow = New DataSet("dstSTLtomorrow")

'Create the datatable
dtblSTLtomorrow = dstSTLtomorrow.Tables.Add("STLtomorrow")

'Create the columns
'Create the id for the data table
dcolSTLtomorrowID = New DataColumn("STLtomorrowID", GetType(Int32))
dcolSTLtomorrowID.AutoIncrement = True
dcolSTLtomorrowID.Unique = True

'Add the columns to the date table
dtblSTLtomorrow.Columns.Add(dcolSTLtomorrowID)
dtblSTLtomorrow.Columns.Add("description", GetType(String))

'Assign the primary key
pkColumns(0) = dcolSTLtomorrowID
dtblSTLtomorrow.PrimaryKey = pkColumns

'Read the rss
Dim xmlReader As New XmlTextReader(http://stltomorrow.tumblr.com/rss)

'set up a datarow
Dim drowSTLtomorrow As DataRow

'Create a flag to figure out which element we are on
Dim elemFlag As Integer

'Loop through the rss
While xmlReader.Read()

     'Figure out which node type we are on
     Select Case xmlReader.NodeType

          'Element node type?
          Case XmlNodeType.Element

          'If this element name is the description then set a flag
          'to indicate that this is the node we want
          If xmlReader.Name = "description" Then

               'set flag to indicate that yes we do want this node
               elemFlag = 1

          Else

               'set flag to indicate that no we do not want this node
               elemFlag = 0

          End If

          'Text node type?
          Case XmlNodeType.Text

               'Check the flag
               'If it is equal to 1 then this is the description node
               'go ahead and grab the text
               If elemFlag = 1 Then

                    'Make sure there is an href in the string so that the user
                    'has something to click on
                    If InStr(xmlReader.Value, "<a href") > 0 Then

                         'Add Rows to the datatable with the data coming from the feed
                         drowSTLtomorrow = dstSTLtomorrow.Tables("STLtomorrow").NewRow()

                         'get the node value and put it in the datatable
                         drowSTLtomorrow("description") = Replace(xmlReader.Value, Chr(34) & ">", Chr(34) & " target='_new'>")

                         'Add the row
                         dstSTLtomorrow.Tables("STLtomorrow").Rows.Add(drowSTLtomorrow)

                    End If

               End If

     End Select

End While

'Bind to the datagrid/gridview
dgSTLtomorrowVer.DataSource = dstSTLtomorrow.Tables("STLtomorrow")
dgSTLtomorrowVer.DataBind()

'close the dataset
dstSTLtomorrow = Nothing

Catch ex As Exception

     'Show that there was an error reading the RSS feed

End Try

No comments: