vbnet RSS Feed
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Partial Class rss_Default
Inherits System.Web.UI.Page
Public RssSite As String = "Example.com"
Public RssUrl As String = "http://www.example.com/"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim RssFeed As String = "" 'whole feed
Dim XmlHeader As String = "<?xml version=""1.0""?>" & vbCrLf 'header element
Dim Rss As XElement = <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"></rss> 'rss element
Dim Channel As XElement = <channel>
<title><%= RssSite %> RSS Feed</title>
<link><%= RssUrl %></link>
<description><%= RssSite %> RSS 2.0 Feed</description>
<language>en</language>
<pubDate><%= RssDate() %></pubDate>
<generator>SlickCMS</generator>
<ttl>60</ttl>
<atom></atom>
</channel>
'add the <items> to <channel>, then <channel> to the <rss>
Rss.Add(GetItems(Channel))
'build up the resulting xml, first with the <xml> header
RssFeed = XmlHeader
'then add the Atom element with the <rss>
RssFeed += AddAtom(Rss.ToString())
'render the RssFeed to User
Response.Write(RssFeed)
End Sub
Public Function GetItems(ByVal Channel As XElement) As XElement
'loops through RSS recordset, returning them as <item>s
Using connection As New SqlConnection("ConnectionString appears here")
connection.Open()
Dim dr As SqlDataReader
Dim cmd As SqlCommand = New SqlCommand
With cmd
.CommandText = "RSS"
.CommandType = CommandType.StoredProcedure
.Connection = connection
dr = .ExecuteReader
End With
While dr.Read
Dim RssItem As XElement = <item>
<title><%= dr("Title") %></title>
<link><%= dr("Link") %></link>
<guid><%= dr("GUID") %></guid>
<description><%= AddCDATA(dr("Description")) %></description>
<comments><%= dr("Link") %>#comments</comments>
<pubDate><%= dr("PublishDate") %></pubDate>
</item>
Channel.Add(RssItem)
End While
dr.Close()
connection.Close()
connection.Dispose()
End Using
'once looped, return the original <channel>, now with <item>s
Return Channel
End Function
Public Function RssDate() As String
'short syntax version
Return (System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss ") & "GMT")
End Function
Public Function RssDateLong() As String
'long syntax version
'returns today's date formatted to RSS specification
'reference: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx and http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
Dim dDate As Date = CDate(System.DateTime.Now)
Dim dDay, dDays, dMonth, dYear As String
Dim dHours, dMinutes, dSeconds As String
dDay = dDate.ToString("ddd")
dDays = Day(dDate).ToString()
dMonth = MonthName(Month(dDate), True).ToString()
dYear = Year(dDate).ToString()
dHours = ZeroPad(Hour(dDate).ToString(), 2)
dMinutes = ZeroPad(Minute(dDate).ToString(), 2)
dSeconds = ZeroPad(Second(dDate).ToString(), 2)
'"Thursday, 07 Oct 2010 21:30:01 GMT"
Return dDay & ", " & dDays & " " & dMonth & " " & dYear & " " & dHours & ":" & dMinutes & ":" & dSeconds & " GMT"
End Function
Public Function ZeroPad(ByVal m As String, ByVal t As Int32) As String
If Len(m) < t Then
For i = Len(m) To (t - 1)
m = "0" & m 'add suffix of 0
Next
End If
Return m
End Function
Public Function AddAtom(ByVal Xml As String) As String
'add the <atom> element
Return Xml.Replace("<atom></atom>", "<atom:link href=""" & RssUrl & "/rss/?c=" & CatID.ToString() & """ rel=""self"" type=""application/rss+xml""/>")
End Function
Public Function AddCDATA(ByVal RawXml As String) As String
'adds prefix/suffix CDATA for encoding
Return "<![CDATA[" & RawXml & "]]>"
End Function
End ClassCreates an RSS 2.0 Feed from a recordset.
Updated: Thursday 7th October 2010, 06:04pm
There are 0 comments
Comments are currently closed.