In this article I will explain how to implement simple database driven RSS feeds for your website in ASP.Net.
The RSS Feed will be build using a Generic Handler and the data will be fetched from database.
RSS Feed Structure
RSS Feed is nothing but an XML file which contains information about the website and the updated data for a website within it.
RSS Feeds are read by the browsers and whenever the RSS Feed is updated user is notified.
Database
I have created two tables Channel and Feeds. As described earlier Channel will store the RSS Channel details while the Feeds will store the RSS items information.
Note: The SQL for creating the database is provided in the attached sample code.
Implementing the RSS Feed using Generic Handler
Below is the Generic Handler implementation for the RSS Feed. Inside the ProcessRequest method of the Generic Handler, I have made call to the BuildFeedXML which will build the RSS Feed XML and then write it to the Response.
Inside the BuildFeedXML method, first the Channel data is populated and after that the RSS Feed items are fetched based on Channel ID and are appended to the XML string
Finally the built XML is written to the Response.
C#
<%@ WebHandler Language="C#" Class="RSS_CS" %>
using System;
using System.Web;
using System.Xml;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public class RSS_CS : IHttpHandler
{
public void ProcessRequest (HttpContext context) {
BuildFeedXML(context, 1);
}
private void BuildFeedXML(HttpContext context, int channelId)
{
using (XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
{
DataTable dt = GetData("SELECT * FROM Channel WHERE Id = @ChannelId", channelId);
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", dt.Rows[0]["Title"].ToString());
writer.WriteElementString("link", dt.Rows[0]["Link"].ToString());
writer.WriteElementString("description", dt.Rows[0]["Description"].ToString());
writer.WriteElementString("copyright", dt.Rows[0]["Copyright"].ToString());
dt = GetData("SELECT * FROM Feeds WHERE ChannelId = @ChannelId", channelId);
foreach (DataRow dr in dt.Rows)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", dr["Title"].ToString());
writer.WriteElementString("description", dr["Description"].ToString());
writer.WriteElementString("link", dr["Link"].ToString());
writer.WriteElementString("guid", dr["Id"].ToString());
writer.WriteElementString("pubDate", Convert.ToDateTime(dr["PublishedDate"]).ToString("R"));
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
private DataTable GetData(string query, int channelId)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ChannelId", channelId);
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return dt;
}
public bool IsReusable {
get {
return false;
}
}
}
VB.Net
<%@ WebHandler Language="VB" Class="RSS_VB" %>
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class RSS_VB : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
BuildFeedXML(context, 1)
End Sub
Private Sub BuildFeedXML(context As HttpContext, channelId As Integer)
Using writer As New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)
Dim dt As DataTable = GetData("SELECT * FROM Channel WHERE Id = @ChannelId", channelId)
writer.WriteStartDocument()
writer.WriteStartElement("rss")
writer.WriteAttributeString("version", "2.0")
writer.WriteStartElement("channel")
writer.WriteElementString("title", dt.Rows(0)("Title").ToString())
writer.WriteElementString("link", dt.Rows(0)("Link").ToString())
writer.WriteElementString("description", dt.Rows(0)("Description").ToString())
writer.WriteElementString("copyright", dt.Rows(0)("Copyright").ToString())
dt = GetData("SELECT * FROM Feeds WHERE ChannelId = @ChannelId", channelId)
For Each dr As DataRow In dt.Rows
writer.WriteStartElement("item")
writer.WriteElementString("title", dr("Title").ToString())
writer.WriteElementString("description", dr("Description").ToString())
writer.WriteElementString("link", dr("Link").ToString())
writer.WriteElementString("guid", dr("Id").ToString())
writer.WriteElementString("pubDate", Convert.ToDateTime(dr("PublishedDate")).ToString("R"))
writer.WriteEndElement()
Next
writer.WriteEndElement()
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
writer.Close()
End Using
End Sub
Private Function GetData(query As String, channelId As Integer) As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@ChannelId", channelId)
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
End Using
End Using
End Using
Return dt
End Function
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Demo
Downloads