Introduction
This is an easy way to distribute your site's content using RSS 2.0. The code pulls the items from a MS SQL Server database, and dynamically creates the XML using the XmlTextWriter
object. You can filter the resulting items by using the create date with your T-SQL, or use the time to live.
If you do not understand the XML that is being created, please check out the following link for detailed information on the RSS 2.0 Standards: http://cyber.law.harvard.edu/rss/rss.html
If you prefer to code in C#, there in an example posted below by a CP member that will create the items. You would still need to create the parent XML tags by using the RSS 2.0 Standard, though.
Using the code
The .aspx page only requires these two lines of code.
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="rssFeed.aspx.vb" Inherits="rssFeed" %>
<%@ OutputCache Duration="1" VaryByParam="none" %>
Here is the code-behind:
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web
Imports System.Xml
Partial Class rssFeed
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Response.Clear()
Response.ContentType = "text/xml"
Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
objX.WriteStartDocument()
objX.WriteStartElement("rss")
objX.WriteAttributeString("version", "2.0")
objX.WriteStartElement("channel")
objX.WriteElementString("title", "RedChip News")
objX.WriteElementString("link", "http://www.redchip.com")
objX.WriteElementString("description", _
"The latest headlines and articles from RedChip.")
objX.WriteElementString("ttl", "5")
Dim objConnection As New SqlConnection(_
ConfigurationManager.ConnectionStrings(_
"DatabaseConnectionString1").ToString)
objConnection.Open()
Dim sql As String = "SELECT Title, Summary, ArticleID, " & _
"PostTime FROM tblArticles ORDER BY PostTime DESC"
Dim objCommand As New SqlCommand(sql, objConnection)
Dim objReader As SqlDataReader = objCommand.ExecuteReader()
While objReader.Read()
objX.WriteStartElement("item")
objX.WriteElementString("title", objReader.GetString(0))
objX.WriteElementString("description", objReader.GetString(1))
objX.WriteElementString("link", _
"http://www.redchip.com/Articles/research.aspx" & _
"?ArticleID=" & CStr(objReader.GetInt16(2)))
objX.WriteElementString("pubDate", _
objReader.GetDateTime(3).ToString("R"))
objX.WriteEndElement()
End While
objReader.Close()
objConnection.Close()
objX.WriteEndElement()
objX.WriteEndElement()
objX.WriteEndDocument()
objX.Flush()
objX.Close()
Response.End()
End Sub
End Class