ASP.NET consume a RSS (Really Simple Syndication) feed
Here is how to consume and RSS 2.0 feed and
display it in a Repeater.
If the feed is not very well formed this
would require another function to parse the invalid
charaters from the string, making it valid XML.
Here is the for the DAL (Data Access Layer)
Reading the feed into a string to help avoid errors from invalid characters.
Also allowing for the removal of in valid characters if need be.
Imports System.Net
Imports System.IO
Public Class dalFeeds
Public Function getUrlAsString(ByVal strURI As String) As String
'DECLARE THE STRING VARIBLE TO HOLD THE XML AND RETURN
Dim strXML As String = ""
Try
'DECLARE AND SET THE URI
Dim objURI As Uri = New Uri(strURI)
'DECLARE AND CREATE THE WEBREQUEST
Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
'GET THE WEBREQUEST RESPONSE
Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
'READ THE RESPONSE IN TO A STREAM
Dim objStream As Stream = objWebResponse.GetResponseStream()
'READ THE STREAM IN TO A STREAM READER
Dim objStreamReader As StreamReader = New StreamReader(objStream)
'READ TO THE END IN A STRING VARIABLE TO RETURN
strXML = objStreamReader.ReadToEnd
Catch ex As Exception
'CREATE A XML ERROR TO RETURN WHICH WILL DISPLAY ON THE PAGE
strXML = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>" & _
"<rss version=" & Chr(34) & "2.0" & Chr(34) & ">" & _
"<channel>" & _
"<item>" & _
"<title>Error occured retrieving feed.</title>" & _
"<link> </link>" & _
"<description>Sorry for any inconvenience this was not your fault. There was an error getting the feed.</description>" & _
"<pubdate>" & Now.ToString("r") & "</pubdate>" & _
"</item>" & _
"</channel>" & _
"</rss>"
End Try
'RETURN THE XML AS A STRING
Return strXML
End Function
End Class
Here is the code for the BLL (Business Logic Layer)
Creating a Datatable to return and bind on the page.
Then creating an XML Reader from the XML string
and loop through it adding the values to the Datatable being returned.
Imports System.Data
Imports System.Xml
Public Class bllFeeds
Private Function getDataTable() As DataTable
'CREATE THE NEW DATATABLE OBJECT
Dim dt As DataTable = New DataTable()
'DECLARE DATACOLUMN AND DATAROW VARIABLS
Dim row As DataRow
Dim column As DataColumn
'CREATE THE NEW DATACOLUMNS, SET THE DATATYPE, COLUMNNAME AND ADD IT TO THE DATATABLE
column = New DataColumn()
column.DataType = System.Type.GetType("System.String")
column.ColumnName = "pubDate"
dt.Columns.Add(column)
'CREATE THE LINK COLUMN
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "link"
dt.Columns.Add(column)
'CREATE THE TITLE COLUMN
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "title"
dt.Columns.Add(column)
'CREATE THE DESCRIPTION COLUMN
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "description"
dt.Columns.Add(column)
'RETURN THE DATATABLE
Return dt
End Function
Public Function DtFeed(ByVal Url As String) As DataTable
'GET REFERENCE TO THE DATA ACESS LAYER
Dim feed As dalFeeds = New dalFeeds
'GET THE XML STRING
Dim strXML As String = feed.getUrlAsString(Url)
'DECLAR THE VARIBLES TO HOLD THE VAUES FOR EACH NEW DATAROW
Dim pubDate As String = ""
Dim link As String = ""
Dim title As String = ""
Dim description As String = ""
'CREATE THE DATATABLE TO RETURN
Dim dt As DataTable = getDataTable()
'DECLARE THE DATAROW TO POPULATE AND ADD TO THE TABLE
Dim row As DataRow
'CREATE AN XML READER FROM THE XML STRING AND
'READ THE FEED XML ADDING THE VALUES TO THE DATATABLE
Using reader As XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(strXML))
'READ THROUGH ALL THE NODES
While (reader.Read())
'THE NODES I WANT ARE THE ITEM NODES
If (reader.Name = "item") Then
While (reader.Read())
'THIS NODE HAS THE TEXT FOR THE PUBDATE
If (reader.Name = "pubDate") Then pubDate = reader.ReadString()
'THIS NODE HAS THE TEXT FOR THE LINK
If (reader.Name = "link") Then link = reader.ReadString()
'THIS NODE HAS THE TEXT FOR THE TITLE
If (reader.Name = "title") Then title = reader.ReadString()
'THIS NODE HAS THE TEXT FOR THE DESCRIPTION
If (reader.Name = "description") Then description = reader.ReadString()
'CHECK IF ALL THE VALUES HAVE BEEN SET
If pubDate.Length > 0 And link.Length > 0 And title.Length > 0 And description.Length > 0 Then
'CREATE THE NEW DATAROW
row = dt.NewRow
'ADD THE VALUES TO THE DATAROW
row("pubDate") = pubDate
row("link") = link
row("title") = title
row("description") = description
'ADD THIS ROW OF VALUES TO THE DATATABLE
dt.Rows.Add(row)
'CLEAR VARIBLES FOR NEXT LOOP
pubDate = String.Empty
link = String.Empty
title = String.Empty
description = String.Empty
End If
End While
End If 'If (reader.Name = "item") Then
End While
End Using
'RETURN THE DATATABLE
Return dt
End Function
End Class
Here is the server code binding the Datatable to a Repeater
<%@ Import Namespace="System.Data"%>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'CREATE A FREFERENCE TO THE BUSINESS LOGIC LAYER
Dim bll As bllFeeds = New bllFeeds
'DECLARE AND CREATE THE DATABLE TO BIND TO THE REPEATER
Dim dt As DataTable = bll.DtFeed(THE URL TO THE FEED TO CONSUME AS A STRING)
'BIND THE DATATABLE DATASOURCE
MyRepeater.DataSource = dt
MyRepeater.DataBind()
'CLEAN UP THE DATATABLE
dt.Dispose()
End Sub
</script>
Here is the Repeater displaying the return on the page.
<asp:Repeater runat="server" ID="MyRepeater ">
<ItemTemplate>
<table>
<tr><td><b><%#DataBinder.Eval(Container.DataItem, "pubDate")%></b></td></tr>
<tr><td><a target="_blank" href="<%#DataBinder.Eval(Container.DataItem, "link")%>"><%#DataBinder.Eval(Container.DataItem, "title")%></a></td></tr>
<tr><td><%#DataBinder.Eval(Container.DataItem, "description")%></td></tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>