5,316,870 members and growing! (16,433 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Distribute Your Sites Content With RSS 2.0 Using ASP.NET/VB.NET/MS SQL 2005

By AdamNThompson

Distribute Your Sites Content With RSS 2.0 Using ASP.NET/VB.NET/MS SQL 2005
VB 8.0, VB, .NET, Windows, WinXP, Visual Studio, ASP.NET, Dev

Posted: 4 Sep 2007
Updated: 28 Sep 2007
Views: 9,269
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.40 Rating: 4.38 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 33.3%
4
3 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This is an easy way to distribute your sites content using RSS 2.0. The code pulls the items from a MS SQL database and dynmaicly creates the XML using the XmlTextWriter Object. You can filter the resuling items by 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 that from 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 sql As String = "SELECT * FROM tblArticles"
        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

License

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

About the Author

AdamNThompson


I am a .NET Developer for company that builds custom web applications. It's an interesting job because each site is different. We build anything that the mind can imagine, and the client can afford.

Fun stuff...

CP is my favorite site for code samples, news, and articles. I like the community here and I like the fact that it mainly caters to developers using the .NET platform.
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
Generalgo with XML auto in SQLSERVER2005.memberDileep.M20:47 28 Sep '07  
GeneralRe: go with XML auto in SQLSERVER2005.memberDileep.M21:03 28 Sep '07  
GeneralRe: go with XML auto in SQLSERVER2005. [modified]memberAdamNThompson9:03 29 Sep '07  
GeneralRe: go with XML auto in SQLSERVER2005.memberDileep.M0:18 30 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Sep 2007
Editor:
Copyright 2007 by AdamNThompson
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project