Click here to Skip to main content
13,198,850 members (43,928 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 4 Sep 2007

Distribute your site's contents with RSS 2.0

, 28 Sep 2007
Rate this:
Please Sign up or sign in to vote.
Distribute your site's contents with RSS 2.0 using ASP.NET/VB.NET/MS SQL Server 2005.


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:

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.ContentType = "text/xml"
        Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

        objX.WriteAttributeString("version", "2.0")
        objX.WriteElementString("title", "RedChip News")
        objX.WriteElementString("link", "")
        objX.WriteElementString("description", _
             "The latest headlines and articles from RedChip.")
        objX.WriteElementString("ttl", "5")

        Dim objConnection As New SqlConnection(_
        Dim sql As String = "SELECT Title, Summary, ArticleID, " & _ 
                "PostTime FROM tblArticles ORDER BY PostTime DESC"
        'Dim sql As String = "SELECT * FROM tblArticles"
        Dim objCommand As New SqlCommand(sql, objConnection)
        Dim objReader As SqlDataReader = objCommand.ExecuteReader()

        While objReader.Read()
            objX.WriteElementString("title", objReader.GetString(0))
            objX.WriteElementString("description", objReader.GetString(1))
            objX.WriteElementString("link", _
                 "" & _ 
                 "?ArticleID=" & CStr(objReader.GetInt16(2)))
            objX.WriteElementString("pubDate", _
        End While



    End Sub
End Class


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalgo with XML auto in SQLSERVER2005. Pin
Dileep.M28-Sep-07 19:47
memberDileep.M28-Sep-07 19:47 
GeneralRe: go with XML auto in SQLSERVER2005. Pin
Dileep.M28-Sep-07 20:03
memberDileep.M28-Sep-07 20:03 
GeneralRe: go with XML auto in SQLSERVER2005. [modified] Pin
AdamNThompson29-Sep-07 8:03
memberAdamNThompson29-Sep-07 8:03 
your technique looks solid to me. Though this article is on VB, I like that you have posted a solution in C# that achieves the the same result. If you don't mind I will put a note at the top of the article for the C# devs to check out your post.

-- modified at 14:10 Saturday 29th September, 2007

-Adam N. Thompson

GeneralRe: go with XML auto in SQLSERVER2005. Pin
Dileep.M29-Sep-07 23:18
memberDileep.M29-Sep-07 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 28 Sep 2007
Article Copyright 2007 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid