How to create RSS Feed for a site Using Asp.Net C# VB.Net

RSS means Really Simple Syndication which is a Web content syndication format. Now let’s try to find out what RSS means in detail. To keep readers update basically the website owner provide a RSS feed – which is simply an XML document following some certain standards, describing new or latest articles about their site. So that their readers can easily go through all of their recent articles by using a RSS reader. For robust dynamic website in most cases developers need to create RSS feed from database. That’s why I am going to show you the way how we can achieve it. Since you are making or creating RSS feed from database so that you can Dynamically or Runtime create RSS feed from your aspx page easily.

First create the below table in Sql Server Database:

CREATE TABLE [dbo].[tblRSS](
	[ID] [bigint] NOT NULL,
	[Title] [varchar](max) NULL,
	[Description] [varchar](max) NULL,
	[URL] [varchar](max) NULL,
 CONSTRAINT [PK_tblRSS] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Now put some data into the Table:

INSERT INTO [dbo].[tblRSS] VALUES(1, 'Creating XML File Programmatically using Asp.Net C# VB.Net','Description 1...','http://codedisplay.com/creating-xml-file-programmatically-using-asp-net-c-vb-net/')
INSERT INTO [dbo].[tblRSS] VALUES(2, 'Runtime load XML File Data into Gridview Asp.Net C# VB.Net','Description 2...','http://codedisplay.com/runtime-load-xml-file-data-into-gridview-asp-net-c-vb-net/')
INSERT INTO [dbo].[tblRSS] VALUES(3, 'Load XML file data in Gridview without code Asp.net C# VB.Net','Description 3...','http://codedisplay.com/load-xml-file-data-in-gridview-without-code-asp-net-c-vb-net/')
INSERT INTO [dbo].[tblRSS] VALUES(4, 'Saving changes is not permitted Error SQL Server 2008 2012','Description 4...','http://codedisplay.com/saving-changes-is-not-permitted-error-sql-server-2008-2012/')
INSERT INTO [dbo].[tblRSS] VALUES(5, 'Set database to read only mode in SQL Server 2005 2008 2012','Description 5...','http://codedisplay.com/set-database-to-read-only-mode-in-sql-server-2005-2008-2012/')

Now create a new web site. Rename the default.aspx page to CreateRSS.aspx. Now open the CreateRSS.aspx page and add the below line just after the page directive:

<%@ OutputCache Duration="120" VaryByParam="*" %>

Now go to the Web.Config File & Write The Below Code:

<configuration>
  <connectionStrings>
    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TEST;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  ....................
  ....................
</configuration>

Note: Please modify your database connection string as per your Database settings.

Now go to code behind & write the code under page load event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(connectionString);
        using (conn)
        {
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * from tblRSS", conn);
            ad.Fill(dt);
        }

        Response.Clear();
        Response.ContentType = "text/xml";
        XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        TextWriter.WriteStartDocument();

        //Below tags are mandatory rss tag
        TextWriter.WriteStartElement("rss");
        TextWriter.WriteAttributeString("version", "2.0");

        // Channel tag will contain RSS feed details
        TextWriter.WriteStartElement("channel");
        TextWriter.WriteElementString("title", "Code to Help");
        TextWriter.WriteElementString("link", "http://codedisplay.com");
        TextWriter.WriteElementString("description", "Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML,GridView Articles and code examples -- by Shawpnendu Bikash");
        TextWriter.WriteElementString("copyright", "Copyright 2013 - 2014 codedisplay.com. All rights reserved.");

        foreach (DataRow oFeedItem in dt.Rows)
        {
            TextWriter.WriteStartElement("item");
            TextWriter.WriteElementString("title", oFeedItem["Title"].ToString());
            TextWriter.WriteElementString("description", oFeedItem["Description"].ToString());
            TextWriter.WriteElementString("link", oFeedItem["URL"].ToString());
            TextWriter.WriteEndElement();
        }
        TextWriter.WriteEndElement();
        TextWriter.WriteEndElement();
        TextWriter.WriteEndDocument();
        TextWriter.Flush();
        TextWriter.Close();
        Response.End();
    }

Note: Don’t forget to add “using System.Data;”,”using System.Xml;”,”using System.Data.SqlClient;”,”using System.Configuration;”, “using System.Text;” namespaces.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
        Dim dt As DataTable = New DataTable()
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Using (conn)
            Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT * from tblRSS", conn)
            ad.Fill(dt)
        End Using

        Response.Clear()
        Response.ContentType = "text/xml"
        Dim TextWriter As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        TextWriter.WriteStartDocument()

        'Below tags are mandatory rss tag
        TextWriter.WriteStartElement("rss")
        TextWriter.WriteAttributeString("version", "2.0")

        'Channel tag will contain RSS feed details
        TextWriter.WriteStartElement("channel")
        TextWriter.WriteElementString("title", "Code to Help")
        TextWriter.WriteElementString("link", "http://codedisplay.com")
        TextWriter.WriteElementString("description", "Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML,GridView Articles and code examples -- by Shawpnendu Bikash")
        TextWriter.WriteElementString("copyright", "Copyright 2013 - 2014 codedisplay.com. All rights reserved.")

        For Each oFeedItem As DataRow In dt.Rows
            TextWriter.WriteStartElement("item")
            TextWriter.WriteElementString("title", oFeedItem("Title").ToString())
            TextWriter.WriteElementString("description", oFeedItem("Description").ToString())
            TextWriter.WriteElementString("link", oFeedItem("URL").ToString())
            TextWriter.WriteEndElement()
        Next

        TextWriter.WriteEndElement()
        TextWriter.WriteEndElement()
        TextWriter.WriteEndDocument()
        TextWriter.Flush()
        TextWriter.Close()
        Response.End()
    End Sub

Note: Don’t forget to add “Imports System.Data”,”Imports System.Xml”,”Imports System.Data.SqlClient”,”Imports System.Configuration”, “Imports System.Text” namespaces.

Now build the project & run the project hope you will get an output like below:
Creating_RSS_FEED

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code