How to create RSS feeds for your site in .Net?

Really Simple Syndication (RSS) is a simple way of sharing information on the web. RSS has become popular to share information from any website. There are tools available to consume the RSS feeds from the websites. Anyhow to utilize this method of sharing knowledge over the web you need to know how to create RSS feeds for your site so that other can use that to get information from your site.

Basically RSS is an XML document with special tags meant for giving links to the contents that are fed from a website. A simple RSS document would look like:

<rss version="2.0">
<channel>
<title>Yoursite.com – To Share information</title>
<link>www.yoursite.com</link>
<description>New Articles</description>
<copyright>Copyright (C) YourSite.com. </copyright>
<generator>www.YourSite.com RSS Generator</generator>
<item>
<title>How to track emails?</title>
<link>http://www.yoursite.com/newarticle.aspx?id=075</link>
<description>Learn how to track emails...</description>
<pubDate>Thu, 18 Aug 2005 12:00:00 AM GMT</pubDate>
</item>
</channel>
</rss>

The root tag of the XML document is the <rss> tag and the root node is the <channel> tag and there can be other ‘channel’ tags in the document. The elements within the ‘channel’ node are title, link, description, copyright, generator, and item. The item tag contains title, link, description and pubdate. In the above document the tags above the item tag pertains to that of the site that gives the rss feed and the tags within the item tag are that of the item that is shared with the users. You can write a method to fill the item tag with information from a database. If you can achieve this you can create the RSS feed from your website. You can use the XMLWriter class and generate an OutputStream which gives information for the tags within the item element. The following code snippets gives you an idea on how to write a class to generate RSS feeds.

Public Class cRss
Public OutputStream As Stream
Public Title As String
Public Url As String
...
‘Create variables for other rss tags
Public ItmSource As DataSet
Public ItmTitle As String
Public ItmUrl As String
Public ItmDescription As String
Public ItmPubDate As String
Public Shared Function PubRss(ByVal oRss As cRss)
Dim writer As New XmlTextWriter(oRss.OutputStream,
System.Text.Encoding.ASCII)
writer.WriteStartElement("rss")
writer.WriteAttributeString("version", "2.0")
writer.WriteStartElement("channel")
writer.WriteElementString("title", oRss.Title)
writer.WriteElementString("link", oRss.Url)
writer.WriteElementString("description", oRss.Desc)
...
‘Write other elements also.
For Each row As DataRow In r.ItemSource.Tables(0).Rows
writer.WriteStartElement("item")
writer.WriteElementString("title", row(oRss.ItmTitle))
writer.WriteElementString("link", row(oRss.ItmUrl))
...
‘Write other elements of the item tag here
writer.WriteEndElement()
Next
writer.WriteEndElement()
writer.WriteEndElement()
writer.Flush()
End Function
End Class

The method PubRss does the job of publishing the RSS feed. You can write a function that returns a DataSet that contains information for the tags in the item element of the RSS document. Once you write a function for returning a dataset, you can write the following code to finish the job of publishing the RSS feed in the Page Load event of the aspx page.

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim r As New cRss
Dim dst As DataSet = ‘write the func that returns dataset here.
r.OutputStream = Response.OutputStream
...
r.Copyright = "Copyright (C) Yoursite.com."
r.Generator = "YourSite.com RSS Generator"
r.ItmSource = dst
r.ItmTitle = "title"
r.ItmDescription = "desc"
r.ItmPubDate = "pubdate"
r.ItmUrl = "url"
‘The above values like title, desc, pubdate, and url are
‘database fields.
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.ContentType = "text/xml"
cRss.PubRss(r)
Response.End()
End Sub

Thus you can try creating an RSS feed for your site so that other can consume that RSS feed.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.