DotNet Tutorials

Server Intellect

 Creating an RSS Feed in ASP.NET with VB.NET

This tutorial will show you how to create your own RSS feed using ASP.NET and VB

In this tutorial, you will learn how to create an RSS feed for your content to be subscribed to.
RSS stands for Really Simple Syndication. It allows you to easily notify people (and machines) of updates to your content. It does this by generating an XML file of your latest posts, blogs, entries, etc. For this XML file, we need to follow a certain structure to create somewhat of a standard for machines to decode our Syndication. This structure looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>title</title>
<link>http://www.link.com/</link>
<description>description</description>
<language>en-us</language>
<ttl>5</ttl>
<item>
<title>title of entry</title>
<description>description of entry</description>
<link>http://www.link.com/</link>
<pubDate>publish date</pubDate>
</item>
<item>
<title>title of entry</title>
<description>description of entry</description>
<link>http://www.link.com/</link>
<pubDate>publish date</pubDate>
</item>
</channel>
</rss>

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

In order for us to generate this XML file, we need something to generate from - content. For this example, I have created a sample SQL database with one table - Articles. Within this table, we have the following columns: ID, Title, Category, DateTimeAdded, and Article. These columns aim to replicate a usual blog entry.
RSS feeds are usually found under the feed sub-directory of a site, so go ahead and add a folder named feed to your project, and then create a Default.aspx page in that folder. Once done, we will modify the ASPX code. We will not be generating any HTML, so we need to strip out the majority of the default code that is in there. All we are going to use is the following:

<%@ Page ResponseEncoding="UTF-8" Language="VB" AutoEventWireup="true" debug="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>

This OutputCache line will only allow the code-behind to run 300 seconds after it has been run. The application will store a version in cache for 300 seconds.

Now we can move to the code-behind. The first thing we want to do here is to add the following assembly references we will be using:

Imports System.Data.SqlClient
Imports System.Xml

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The following code we will put in the Page_Load event, as we want it to run when the page loads:

Response.Clear()
Response.ContentType = "application/rss+xml"
Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
objX.WriteStartDocument()
objX.WriteStartElement("rss")
objX.WriteAttributeString("version", "2.0")
objX.WriteStartElement("channel")

Dim cmd As New SqlCommand("Select TOP 10 * From Articles ORDER BY ID DESC", New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString))
cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()

objX.WriteElementString("title", "Sample Article RSS Feed")
objX.WriteElementString("link", "http://www.vbasic.net/")
objX.WriteElementString("description", "VBasic.NET offers simple downloadable, easy to understand tutorials and code examples using Visual Basic.NET, ASP.NET and Visual Studio. Visit often as new Tutorials and VB.NET Examples are added every day!")
objX.WriteElementString("language", "en-us")
objX.WriteElementString("ttl", "60")
objX.WriteElementString("image", "http://vbasic.net/media/logo.gif")
objX.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now))

Do While dr.Read()
objX.WriteStartElement("item")
objX.WriteElementString("title", dr("Title").ToString())
objX.WriteElementString("author", "VBasic.net")
objX.WriteElementString("link", "http://www.vbasic.net/")
objX.WriteStartElement("guid")
objX.WriteAttributeString("isPermaLink", "true")
objX.WriteString("http://www.vbasic.net/")
objX.WriteEndElement()
objX.WriteElementString("pubDate", String.Format("{0:R}", dr("DateTimeAdded")))
objX.WriteStartElement("category")
objX.WriteString(dr("Category").ToString())
objX.WriteEndElement()
objX.WriteElementString("description", dr("Article").ToString().Substring(0, 100) & "..")
objX.WriteEndElement()
Loop

objX.WriteEndElement()
objX.WriteEndElement()
objX.WriteEndDocument()
objX.Flush()
objX.Close()
Response.End()

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Here, we are simply creating a new instance of the XmlTextWriter class and using it to generate our XML document. We loop through our database to get the latest articles, and write them into the XML file.

The entire code-behind will look something like this:

Imports System.Data.SqlClient
Imports System.Xml

Partial Class _Default
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 = "application/rss+xml"
Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
objX.WriteStartDocument()
objX.WriteStartElement("rss")
objX.WriteAttributeString("version", "2.0")
objX.WriteStartElement("channel")

Dim cmd As New SqlCommand("Select TOP 10 * From Articles ORDER BY ID DESC", New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString))
cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()

objX.WriteElementString("title", "Sample Article RSS Feed")
objX.WriteElementString("link", "http://www.vbasic.net/")
objX.WriteElementString("description", "VBasic.NET offers simple downloadable, easy to understand tutorials and code examples using Visual Basic.NET, ASP.NET and Visual Studio. Visit often as new Tutorials and VB.NET Examples are added every day!")
objX.WriteElementString("language", "en-us")
objX.WriteElementString("ttl", "60")
objX.WriteElementString("image", "http://vbasic.net/media/logo.gif")
objX.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now))

Do While dr.Read()
objX.WriteStartElement("item")
objX.WriteElementString("title", dr("Title").ToString())
objX.WriteElementString("author", "VBasic.net")
objX.WriteElementString("link", "http://www.vbasic.net/")
objX.WriteStartElement("guid")
objX.WriteAttributeString("isPermaLink", "true")
objX.WriteString("http://www.vbasic.net/")
objX.WriteEndElement()
objX.WriteElementString("pubDate", String.Format("{0:R}", dr("DateTimeAdded")))
objX.WriteStartElement("category")
objX.WriteString(dr("Category").ToString())
objX.WriteEndElement()
objX.WriteElementString("description", dr("Article").ToString().Substring(0, 100) & "..")
objX.WriteEndElement()
Loop

objX.WriteEndElement()
objX.WriteEndElement()
objX.WriteEndDocument()
objX.Flush()
objX.Close()
Response.End()
End Sub
End Class

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect