Free Information Technology Magazines and eBooks

Saturday, April 03, 2010

How To Read RSS Using VB.NET and ASP.NET

RSS (Really Simple Syndication) is one of web feed formats that is commonly used to share recently updated web contents such as blogs. With this article, you will learn how to incorporate RSS with your VB or ASP.NET application. The sample code below contains a simple function that process the RSS feed from a sample URL. It reads the title, link and description of each RSS item from the sample URL and display it on a web page. First you need to import the following namespaces.



Imports System.Net
Imports System.IO
Imports System.Xml


Then copy and paste the following code for ReadRSS function. This function performs the dirty work.


Private Sub ReadRSS(ByVal URL As String)

Dim wr As WebRequest = System.Net.WebRequest.Create(URL)
Dim resp As WebResponse = wr.GetResponse()

Dim rssStream As Stream = resp.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)

Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")

Dim title As String = ""
Dim link As String = ""
Dim description As String = ""
Dim i As Integer

For i = 0 To rssItems.Count - 1
Dim rssDetail As XmlNode
'Get the title
rssDetail = rssItems.Item(i).SelectSingleNode("title")
If rssDetail.Equals(Nothing) = False Then
title = rssDetail.InnerText
Else
title = ""
End If
'Get the link
rssDetail = rssItems.Item(i).SelectSingleNode("link")
If rssDetail.Equals(Nothing) = False Then
link = rssDetail.InnerText
Else
link = ""
End If
'Get the description
rssDetail = rssItems.Item(i).SelectSingleNode("description")
If rssDetail.Equals(Nothing) = False Then
description = rssDetail.InnerText
Else
description = ""
End If

Response.Write("" + title + "
")
Response.Write(description + "
")
Next
End Sub


Now call it from the Page_Load event as shown below.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim URL As String = "http://feeds.feedburner.com/fryan0911/postfeed"
ReadRSS(URL)
End Sub


You can also download the complete sample project here. It is written in Visual Studio 2008.

For More Coding Tips & Tricks, subscribe now.

4 comments:

Wrecks said...

http://awesvb.blogspot.com/2009/07/load-rss-feed-into-gridview.html

George said...

Good article.

Instead of response.write to output the rss list you can use a repeater in your .aspx page and an arraylist as datasource. Example:

.aspx page


<%# Container.DataItem %>



Code behind page
------------------
add this before the loop:
Dim ItemList As New ArrayList()

and replace response.write lines with the following:

ItemList.Add("" + title + "")
RSSFEED.DataSource = ItemList
RSSFEED.DataBind()

Phinuss said...

thanks for share,

before edite response, i get this error:

Error 1 'Response' is not declared. It may be inaccessible due to its protection level. c:\documents and settings\albimani\my documents\visual studio 2010\Projects\testsRSS\testsRSS\Form1.vb

can u fix this??

Phinuss said...

thanks bro..
but i have a problem:


Error: 'Response' is not declared. It may be inaccessible due to its protection level. c:\documents and settings\albimani\my documents\visual studio 2010\Projects\testsRSS\testsRSS\Form1.vb 48 13 testsRSS

can fix this??