vb.net RSS Feed Reader or Reading XML into Dataset

Todays task was to build an RSS reader for a template site. This template site would allow for any of our firms to put in their company info along with their RSS Feed and the website that is created will format all the info and also show their RSS Feed in a nice format.

My Search began and I soon came across Display RSS Feeds using ASP.Net. It worked great, this code grabs the XML with the XMLTextReader puts it into a DataSet and then binds it to a repeater. Exactly what I wanted. Using my Repeater I could pull only the pubDate and Title from the feed and format it how ever was needed. This worked great for the first 3 firms, then I ran into a problem.

As per the XML standards, you are allowed to have multiple tags for each item, so if your post had 2 links you’d see

<br> 'used to center code for blog
<link>http://tek-works.com</link>
<link>http://richardcurry.com</link>
<br>

The problem with this is that when you start adding the XML to your Dataset it tries to create 2 columns with the same name and you receive an error:

A column named ‘link’ already belongs to this DataTable: cannot set a nested table name to the same name.

I also had one blog that always showed up as 403 Forbidden. (also fixed by the code below)

I tried for a couple hours and while i’m sure there’s a way around this, I couldn’t find it so it was back to the searches.

I than ran across How To Create An RSS Reader in VB.NET on forums.asp.net

This code didn’t use the repeater but actually pulled each piece of the XML that you needed and then allowed you to format it.

I had to make a few changes, I needed the date and also had to add some formatting. After finishing my changes I tested the code with MANY RSS Feeds and it worked for all of them.

a couple of the gotcha’s:

  1. In XML you have to call the tag exactly by it name (case sensitive)  i.e. pubDate
  2. Like when pulling from SQL watch for Null, except in XML the tag may not even be there

I call and post the results with (feed1 is a label):


<br>'used to center code for blog
Feed1.Text = ProcessRSS("<a href="http://feeds.feedburner.com/Tek-workscom">http://feeds.feedburner.com/Tek-workscom</a>?format=xml", "Tek-Works")

<br>

and the code with my changes


<br> 'used to center code for blog
Public Shared Function ProcessRSS(ByVal rssURL As String, ByVal feed As String) As String
 Dim request As WebRequest = WebRequest.Create(rssURL)
 Dim response As WebResponse = request.GetResponse()
 Dim sb As New StringBuilder("")
 Dim rssStream As Stream = response.GetResponseStream()
 Dim rssDoc As New XmlDocument()
 rssDoc.Load(rssStream)
 Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")

Dim title As String = ""
 Dim pubdate As String = ""
 Dim datepubdate As Date
 Dim format As String = "MMM d, yyyy"
 Dim link As String = ""
 Dim upperlimit As Integer = rssItems.Count
 If upperlimit > 4 Then
 upperlimit = 4
 End If
 If upperlimit > 0 Then
 sb.Append("<table style='padding-left: 10px; width: 290px; vertical-align: top;'><tr><td style='padding-left: 20px;' align='left'><br />")
 Dim i As Integer = 0
 While i < upperlimit
 Dim rssDetail As XmlNode
 rssDetail = rssItems.Item(i).SelectSingleNode("pubDate")
 If rssDetail IsNot Nothing Then
 datepubdate = CDate(rssDetail.InnerText)
 sb.Append("<div class='sectionTextsmallergrey'>")
 sb.Append(datepubdate.ToString(format) + "</div>")
 Else
 pubdate = ""
 sb.Append("<div class='sectionTextsmallergrey'>")
 sb.Append(pubdate + "</div>")
 End If

 rssDetail = rssItems.Item(i).SelectSingleNode("title")
 If rssDetail IsNot Nothing Then
 title = rssDetail.InnerText
 Else
 title = ""
 End If

 sb.Append("<div class='sectionText'>")
 sb.Append(title + "</div><hr id='lblhr1' runat='server' style='width: 100%;' />")
 i += 1
 End While
 sb.Append("</td></tr></table>")
 End If
 Return sb.ToString()
 End Function

<br>

Technorati Tags: , , , ,

     

Leave a Reply