Creating An ASP.NET RSS Feed, Using Data From SQL Server And HTTP Handler

View the RSSFeed.ashx HTTP handler in action

This is a demo of using a Web Handler (.ashx) file to create an RSS feed from SQL Server database records.

Page Code

            <%@ WebHandler Language="VB" Class="RSSFeed" %>

            Imports System
            Imports System.Web
            Imports System.Xml
            Imports System.Text
            Imports System.Data
            Imports System.Data.SqlClient

            Public Class RSSFeed : Implements IHttpHandler
                
                Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
                    'send document as response
                    context.Response.ContentType = "text/xml"
                    context.Response.ContentEncoding = System.Text.Encoding.UTF8
                    context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600))
                    context.Response.Cache.SetCacheability(HttpCacheability.Public)
                    
                    'create RSS xml document
                    Dim xml As New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)
                    xml.Formatting = Formatting.Indented
                    
                    xml.WriteStartDocument()
                    xml.WriteStartElement("rss")
                    xml.WriteAttributeString("version", "2.0")
                    
                    'create feed header section
                    xml.WriteStartElement("channel")
                    xml.WriteElementString("title", "ASP.NET RSS Feed - Web Handler Demo")
                    xml.WriteElementString("link", "http://www.dougv.net")
                    xml.WriteElementString("description", "A demonstration of using an ASP.NET Web Handler to create an RSS feed of SQL Server data. Hat tip to Gawker.com for the data.")
                    xml.WriteElementString("language", "en-us")
                    xml.WriteElementString("pubDate", DateTime.UtcNow.ToString("r"))
                    xml.WriteElementString("lastBuildDate", DateTime.UtcNow.ToString("r"))
                    xml.WriteElementString("managingEditor", "dougvanderweide@gmail.com")
                    xml.WriteElementString("webMaster", "dougvanderweide@gmail.com")
                    xml.WriteElementString("ttl", "60")
                    
                    'get top stories
                    Dim ds As DataSet = GetArticles()
                    
                    'add stories to feed
                    Dim row As DataRow
                    For Each row In ds.Tables(0).Rows
                        xml.WriteStartElement("item")
                        xml.WriteElementString("title", row("article_title"))
                        xml.WriteElementString("link", row("article_link"))
                        
                        'output story as cdata
                        xml.WriteStartElement("description")
                        xml.WriteCData(row("article_text"))
                        xml.WriteEndElement()
                        
                        xml.WriteElementString("pubDate", CType(row("article_date"), DateTime).ToString("r"))
                        xml.WriteEndElement()
                    Next
                    
                    'close channel
                    xml.WriteEndElement()
                    'close rss
                    xml.WriteEndElement()
                    'close document
                    xml.WriteEndDocument()
                    xml.Flush()
                    xml.Close()
                       
                End Sub
                
                Private Function GetArticles() As DataSet
                    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("my_connection_string").ConnectionString)
                    Dim cmd As New SqlCommand("my_stored_procedure", conn)
                    cmd.CommandType = CommandType.StoredProcedure
                    
                    Dim ds As New DataSet
                    
                    conn.Open()
                    Dim da As New SqlDataAdapter(cmd)
                    da.Fill(ds)
                    conn.Close()
                    cmd.Dispose()
                    conn.Dispose()

                    Return ds
                End Function
             
                Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                    Get
                        Return False
                    End Get
                End Property

            End Class