Create an RSS Feed for your Website

Easy way to create an RSS Feed for your Website in ASP.NET


I spent quite a bit of time looking at RSS and what it could do for my site, and while it was a bit trial and error the first time, I eventually got there.

I am guessing because you are here that you probably understand what RSS is, if not check the link out.

Below is a sample from my old site's RSS feed, which was set up to run to the RSS Atom specifications.

Sample RSS

<rss xmlns:atom="" version="2.0">
        <atom:link href="" rel="self" type="application/rss+xml"/>
        <title>gsclayton rss feed</title>
        <copyright>Copyright gsclayton 2012</copyright>
        <description>The la blogs and articles from gsclayton</description>
            <title>Disk Space and Database Size Alerts</title>
   Server 2008, Disk Space and Database Size Alerts stored procedure to check your server
            <pubDate>Wed, 20 Nov 2013 22:16:08 GMT</pubDate>

The code probably looks a lot more complex than it is, so lets have a look at what it does.

First we import the namespaces for SQL and XML.

Then the connection to the database is set, this is pulling it from the web config in this instance.


Imports System.Data.SqlClient
Imports System.Xml
Imports System.Data
Imports Claytabase.LanguageConvert

Partial Class RSS
  Inherits System.Web.UI.Page
  Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString)
  Private Sub LoadFeed() Handles Me.Load
  'Clear any previous output from the buffer
  Dim lg As String = Replace(Page.RouteData.Values("lg").ToString, "'""''")
  Dim BaseURL As String = ""
  Dim MyTitle As String = ConvertText("RSSTitle", lg)
  Dim MyDescr As String = ConvertText("RSSDesc", lg)

  Response.ContentType = "text/xml"
  Response.Charset = "Utf-8"
  Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
  'The mandatory rss tag
  'The channel tag contains RSS feed details
  xtwFeed.WriteRaw("<atom:link href=""" & BaseURL & lg & "/rss"" rel=""self"" type=""application/rss+xml"" />")
  xtwFeed.WriteElementString("title", MyTitle)
  xtwFeed.WriteElementString("link", baseURL)
  xtwFeed.WriteElementString("copyright""Copyright Claytabase 2012")
  xtwFeed.WriteElementString("language", lg)
  xtwFeed.WriteElementString("description", MyDescr)

  'Objects needed for connecting to the SQL 
  Using com As New SqlCommand("EXEC GetRSS '" + lg + "'", con)
  If con.State = ConnectionState.Closed Then
  End If
  Using dr = com.ExecuteReader()
  'Loop through the content of the database and add them to the RSS feed 
  While dr.Read()
  xtwFeed.WriteElementString("title", dr.Item(0).ToString())
  xtwFeed.WriteElementString("description", dr.Item(1).ToString())
  xtwFeed.WriteElementString("link", BaseURL + dr.Item(2).ToString())
  xtwFeed.WriteElementString("pubDate", Format(CDate(dr.Item(3).ToString()), "ddd, dd MMM yyyy hh:mm:ss") + " GMT")
  xtwFeed.WriteElementString("category", dr.Item(4).ToString())
  xtwFeed.WriteElementString("guid", BaseURL + "/" + dr.Item(5).ToString())
   End While
  End Using
  End Using
  'Close all tags 
  End Sub
End Class

Wrapping up

Onto the code from the page load, and here is where it has been a bit more creative.

The field lg a base part of this site, telling the system which language is being used for each request, and the base URL will be the same for everything, these can be re-used later on in the script.

The next two fields (MyTitle and MyDescr) are put through a built in language converter, where the database returns a string dependent on the language input. 

The next few lines of code set out the encoding and response type, open an XML writer and set out some of the required headings, as these will rarely change, I have set these manually.

We can now move onto reading the data, so first job is to create an SQL command, and in this case I simply call a stored procedure which returns the required fields from the database, dependent on language input. 

We then open the SQL connection, and declare a data reader to loop through the result set from the database. 

We already know that the XML tag is item for each document, so we can open this up straight away. 

Then populate each required item with the data, and ensure that your date is in the correct format, we will then close the tag by using the WriteEndElement. 

Once the data has all been read, the code is closing the data reader, connections and the writing the end tags for each element opened earlier.

Once you have written and published yours, be sure to check it on the W3C RSS Validator.

Ousia Logo