Create RSS Feed from your data with .NET
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!
Archives
- May 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- January 2010
- December 2009
- October 2009
- August 2009
- July 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- June 2007
Tags
.net 2008 asp.net asp.net mvc asus eee c# corona cpa doctrine dom eee eee pc Internet Marketing ios iPad iphone iPhone development iphone sdk javascript make money netbook online marketing pagerank passion php playstation playstation 3 ps3 ranking ror ruby on rails self help Self Improvement self motivation seo sql success symfony take action the secret ultra compact laptop ultra compact notebook vb.net web development wordpress




