Hello,
Today I’ll give you a quick guide on how to create RSS Feed channel from your existing data. Before we go to the implementation, let’s see a brief overview of What is RSS, Feed or Atom?
RSS, Feed or Atom is a format name of the method for latest generation webmaster to feed their own updated contents into external web sites or any external applications. I focus to the word updated contents because this is a point of the method. (Now I’ll call it RSS and no more Feed or Atom) The content that provide RSS Feed channel almost often updated.
RSS content can be read by any RSS reader software such as Internet Explorer 7.0, Mozilla FireFox, Microsoft Outlook or any third party. Sometime it called “Feed reader” or “Aggregator” instead of “RSS reader”. Most of RSS reader can monitoring the RSS Feed channel and seeing if there are any changes occurred in the content when compared to previous time. This is very useful when some user don’t want to spend too much time to load any UI as it takes so much time than actual contents. If you still don’t get an idea and you have some RSS reader installed, you can try it online here. (Sorry, but the content of the feed is in Thai language. Just prove to get you idea)
For webmaster or developer like us, If you want to build your web site to stay tuned with Web 2.0 trend. You should build RSS Feed channel to your web application to let external applications consume your data and get more traffics.
In technically terms. RSS, Feed or Atom are all just an XML file that had their own format which RSS reader can be read. You can see the sample of XML structure here. (Just RSS 2.0)
So, how can we coding to build up RSS Feed channel from the existing data? It’s very easy, easier than you thinks!
Let’s examine the RSS 2.0 specification first.
<?xml version=”1.0″?>
<rss version=”2.0″>
<channel>
<title>Latest Know-How</title>
<link>http://www.knowhowdd.com/</link>
<description>Latest knowhow from www.knowhowdd.com</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<managingEditor>rss@knowhowdd.com</managingEditor>
<webMaster>webmaster@knowhowdd.com</webMaster>
<item>
<title>Custom ASP.NET Membership Provider</title> <link>http://www.knowhowdd.com/ShowKnowhow.aspx?khId=11</link>
<description>How to create your own custom ASP.NET authentication membership provider</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
</item><item>
<title>Custom ASP.NET Role Provider</title>
<link>http://www.knowhowdd.com/ShowKnowhow.aspx?khId=25</link>
<description>How to create your own custom ASP.NET authentication role provider</description>
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
</item>
</channel>
</rss>
What I’ve hi-lighted in blue are header and footer sections which describe itself meaning. Next, what I’ve hi-lighted as red is body which contains your content items.
So, our development task is just to generate this XML stream from the existing data. It’s really easy to complete this task as .NET already provide a great XML class library. Let’s begin coding with VB.NET.
First of all, import some necessary namespaces.
Imports System.Text
Imports System.Xml
Then go to the code block where you want to generate the RSS Feed and coding to build header section just like the following…
‘Clear response
Response.Clear()‘Write the beginning of RSS content
Response.ContentType = “text/xml”
Dim xtwFeed As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
xtwFeed.WriteStartDocument()
xtwFeed.WriteStartElement(”rss”)
xtwFeed.WriteAttributeString(”version”, “2.0″)
xtwFeed.WriteStartElement(”channel”)
xtwFeed.WriteElementString(”title”, “KnowHowDD.com - Latest Know-How on group : ” & g.Title)
xtwFeed.WriteElementString(”link”, “http://www.knowhowdd.com”)
xtwFeed.WriteElementString(”description”, “The latest know-how from KnowHowDD.com”)
xtwFeed.WriteElementString(”copyright”, “Copyright 2006 - 2007 KnowHowDD.com. All rights reserved.”)
The header section was completed now, then we’ll begin to build the body section of the RSS feed. Normally I’ll loop through the collection of data which I want to provide as RSS Feed channel.
‘Loop through all records to generate RSS body
For Each kh As KnowHowEntity In knowHows
‘Write body (Extract from database)
xtwFeed.WriteStartElement(”item”)
xtwFeed.WriteElementString(”title”, kh.Title)
xtwFeed.WriteElementString(”description”, kh.Description)
xtwFeed.WriteElementString(”link”, “http://www.knowhowdd.com/ShowKnowHow.aspx?knowHowId=” & kh.KnowHowId)
xtwFeed.WriteElementString(”pubDate”, kh.PubDate.ToString(”dd/MM/yyyy hh:mm:ss”))
xtwFeed.WriteEndElement()
Next
Now, the body section was completely built. Let’s complete the footer section.
‘Write the ending of RSS content
xtwFeed.WriteEndElement()
xtwFeed.WriteEndElement()
xtwFeed.WriteEndDocument()
xtwFeed.Flush()
xtwFeed.Close()
Response.End()
Easy or not, now it’s completed and we all got RSS Feed channel in our Web 2.0 style application!