Create an RSS Feed for your Website

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

About

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="https://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <atom:link href="https://www.gsclayton.net/rss" rel="self" type="application/rss+xml"/>
        <title>gsclayton rss feed</title>
        <link>https://www.gsclayton.net</link>
        <copyright>Copyright gsclayton 2012</copyright>
        <description>The la blogs and articles from gsclayton</description>
        <item>
            <title>Disk Space and Database Size Alerts</title>
            <description>
            gsclayton.net-SQL Server 2008, Disk Space and Database Size Alerts stored procedure to check your server
            </description>
            <link>
            https://www.gsclayton.net/Blog/SQL/1/SQL%20Server%202008,%20Disk%20Space%20and%20Database%20Size%20Alerts
            </link>
            <pubDate>Wed, 20 Nov 2013 22:16:08 GMT</pubDate>
            <category>SQL</category>
            <guid>
            https://www.gsclayton.net/8e53acf0-74d8-4e32-a627-f5e71f0fb29f
            </guid>
        </item>
    </channel>
</rss>

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.

VB

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 = "https://www.claytabase.co.uk/"
  Dim MyTitle As String = ConvertText("RSSTitle", lg)
  Dim MyDescr As String = ConvertText("RSSDesc", lg)

  Response.Clear()
  Response.ContentType = "text/xml"
  Response.Charset = "Utf-8"
  Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
  xtwFeed.WriartDocument()
  'The mandatory rss tag
  xtwFeed.WriartElement("rss")
  xtwFeed.WriteAttriburing("version""2.0")
  xtwFeed.WriteAttriburing("xmlns:atom""https://www.w3.org/2005/Atom")
  'The channel tag contains RSS feed details
  xtwFeed.WriartElement("channel")
  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
  con.Open()
  Else
  End If
  Using dr = com.ExecuteReader()
  'Loop through the content of the database and add them to the RSS feed 
  While dr.Read()
  xtwFeed.WriartElement("item")
  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())
  xtwFeed.WriteEndElement()
   End While
  End Using
  End Using
  'Close all tags 
  xtwFeed.WriteEndElement()
  xtwFeed.WriteEndElement()
  xtwFeed.WriteEndDocument()
  xtwFeed.Flush()
  xtwFeed.Close()
  Response.End()
  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