Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help

ADO.Net

A RSS Reader using ASP.NET 2.0 and VB.NET
RSS Feed is very popular in Internet. This tutorial will show you how to create a RSS Reader using ASP.NET 2.0 and VB.NET.

At first, import the namespace of System.Net, System.IO, and System.Xml

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

In this sample, we created a simple function to process the RSS feed from a sample URL. This function define a string of rssURL as its parameter. This string contains the RSS's URL. It will use the value of rssURL to create a WebRequest.

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The response to this request will be put into WebResponse object. The WebResponse class is the abstract base class from which protocol-specific response classes are derived. Applications can participate in request and response transactions in a protocol-agnostic manner using instances of the WebResponse class while protocol-specific classes derived from WebResponse carry out the details of the request. Client applications do not create WebResponse objects directly; they are created by calling the GetResponse method on a WebRequest instance.

After then, the WebResponse object will be used to create a stream to get the XML data. Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. The Stream class and its derived classes provide a generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices. The we used a XmlDocument to store the stream data. XmlDocument manipulating the data of XML, finally read the RSS contents from Feed.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rssURL As String = "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml"
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />")
ProcessRSSItem(rssURL)
Response.Write("<hr />")

rssURL = "http://www.developer.com/icom_includes/feeds/special/dev-5.xml"
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />")
Call ProcessRSSItem(rssURL)
End Sub
Private Sub ProcessRSSItem(ByVal rssURL As String)

Dim myRequest As WebRequest = System.Net.WebRequest.Create(rssURL)
Dim myResponse As WebResponse = myRequest.GetResponse()

Dim rssStream As Stream = myResponse.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

rssDetail = rssItems.Item(i).SelectSingleNode("title")
If rssDetail.Equals(Nothing) = False Then

title = rssDetail.InnerText

Else

title = ""
End If

rssDetail = rssItems.Item(i).SelectSingleNode("link")
If rssDetail.Equals(Nothing) = False Then

link = rssDetail.InnerText

Else

link = ""
End If

rssDetail = rssItems.Item(i).SelectSingleNode("description")
If rssDetail.Equals(Nothing) = False Then

description = rssDetail.InnerText

Else

description = ""
End If

Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>")
Response.Write(description + "</p>")
Next
End Sub
End Class

The front end Default.aspx page looks something like this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>RSS</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style='height: 383px'>
<legend><strong>RSS</strong></legend><br />
</fieldset>
</div>
</form>
</body>
</html>

The flow for the code behind page is as follows.

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

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rssURL As String = "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml"
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />")
ProcessRSSItem(rssURL)
Response.Write("<hr />")

rssURL = "http://www.developer.com/icom_includes/feeds/special/dev-5.xml"
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />")
Call ProcessRSSItem(rssURL)
End Sub
Private Sub ProcessRSSItem(ByVal rssURL As String)

Dim myRequest As WebRequest = System.Net.WebRequest.Create(rssURL)
Dim myResponse As WebResponse = myRequest.GetResponse()

Dim rssStream As Stream = myResponse.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

rssDetail = rssItems.Item(i).SelectSingleNode("title")
If rssDetail.Equals(Nothing) = False Then

title = rssDetail.InnerText

Else

title = ""
End If

rssDetail = rssItems.Item(i).SelectSingleNode("link")
If rssDetail.Equals(Nothing) = False Then

link = rssDetail.InnerText

Else

link = ""
End If

rssDetail = rssItems.Item(i).SelectSingleNode("description")
If rssDetail.Equals(Nothing) = False Then

description = rssDetail.InnerText

Else

description = ""
End If

Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>")
Response.Write(description + "</p>")
Next
End Sub
End Class
 


Published Saturday, February 02, 2008 4:59 PM by admin

Filed under: , , , ,

Comments

No Comments
Anonymous comments are disabled
 
Powered by Community Server, by Telligent Systems