This is a first part of two series article aimed at explaining; how to generate RSS Feeds and creating a RSS Feed Listener/Reader.
I have experienced that often clients will raise business requirement for enabling feeds on their news or events pages not just to popularise their site reach but also so that the target audiences who frequently visit the site can subscribe to and stay updated without visiting the site itself.
RSS (Really Simple Syndication) is a commonly adapted format for delivering frequently published changing any type of web content. In a nutshell, by subscribing to a feed, you save time to not visit the site and get the updates from your favourite site straight to you. For more information visit the following or Google as necessary.
My absolute favourite thing about ASP.Net is that there are so many great improvisations you can do with it for constructing solutions for requirements you want. So here goes how you can generate a RSS Feed for any content on your website.
Using Microsoft’s PowerHouse Visual Studio create a new file from Generic Handler Template. Name it what you want and use the code below which I think is self explanatory.
<%@ WebHandler Language="VB" Class="Rss" %> Imports System Imports System.Web Imports System.Xml Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class Rss : Implements IHttpHandler Dim writer As XmlTextWriter Dim rsslinks As DataTable = Nothing Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest context.Response.ContentType = "text/xml" writer = New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8) writer.Formatting = Formatting.Indented writer.WriteStartDocument() ' RSS 2.0 Specification writer.WriteStartElement("rss") writer.WriteAttributeString("version", "2.0") ' Channel tag will contain Rss feed writer.WriteStartElement("channel") writer.WriteElementString("title", "Title of your Rss Feed") writer.WriteElementString("link", "URL.Of.Your.Website") writer.WriteElementString("description", "some text that you want to publish, " & _ "on your rss feeds signature.") writer.WriteElementString("copyright", "blah blah. All rights reserved.") Dim links As DataTable = LinksTable() For Each row As DataRow In links.Rows If Not IsDBNull(row("Title").ToString) Then writer.WriteStartElement("item") ' I am adding only title and description columns, you can use the ones you require writer.WriteElementString("title", row("Title").ToString) writer.WriteElementString("description", row("Description").ToString) ' Create or Get the links that you want rss feeds should be linking users to writer.WriteElementString("link", "http://sitename/testurl") writer.WriteEndElement() End If Next writer.WriteEndElement() writer.WriteEndElement() writer.WriteEndDocument() writer.Flush() writer.Close() End Sub Public Function LinksTable() As DataTable If IsNothing(rsslinks) Then Dim connStr As String = ConfigurationManager.ConnectionStrings("connection.string").ConnectionString Dim conn As SqlConnection = New SqlConnection(connStr) Dim cmd As New SqlCommand("stored.procedure.that.will.pull.back.rss.entries", conn) cmd.CommandType = CommandType.StoredProcedure Dim adapter As New SqlDataAdapter(cmd) rsslinks = New DataTable adapter.Fill(rsslinks) End If Return rsslinks End Function Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property End Class
I have created a RSS Feed accessible from url/nameofyourrssfile which users can view on either mobile or website. Should you require to create a viewer of your RSS feed as a page then please follow this trail link.