Create RSS Feed Using ASP.NET

A RSS Feed is generally used to return the output in plain text or XML format.

To parse the results from our application we use the RSS feed result that can be easily parsed.

We have a tag format for the RSS.

It's main tag is the channel tag:

<channel> </channel>

The other tags inside the channel tag are title, link and description.

Title tag

The name of the channel is similar to the title of a website or web page.

Example: Make RSS Feeds

Tag: <title></title>

Link tag

The URL to the HTML website.

Example: http://www.make-rss-feed.com

Tag: <link></link>

Description Tag

The phrase(s) or sentence(s) describing the content of the entire feed.

Example: Details RSS feed creation in a step-by-step fashion

Tag: <description></description>

All together they look like:

<HeaderTemplate> 
           <rss version="2.0"> 
                <channel> 
                    <title>RSS Example</title> 
                    <link>http://www.test.com/RSS.aspx</link> 
                    <description> 
                    RSS For Article
                    </description> 
</HeaderTemplate> 


Now to create a RSS in ASP.Net.

STEP 1

Create a new RSS.aspx page and remove all the HTML markup from RSS.aspx.

The code will look like:

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="RSS.aspx.cs" Inherits="AndroitWebApp.RSS.RSS" %>

STEP 2

Now add a Repeater Control on the page:

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="RSS.aspx.cs" Inherits="AndroitWebApp.RSS.RSS" %>

<asp:repeater ID="RSS" runat="server">
</asp:repeater>

STEP 3

Pick query string values for the variables:

protected void Page_Load(object sender, EventArgs e)
{
    int SVID = Convert.ToInt32(Request["SVID"].ToString());
    int RVID = Convert.ToInt32(Request["RVID"].ToString());
    GetChatmsg(SVID,RVID);
}


Now go to the RSS.aspx.cs and create a function to bind data from the database:

protected void GetChatmsg(int SVID, int RVID)

{
    try

    {
        using (SqlCommand cmd = new SqlCommand())

        {

            cmd.CommandText = "spr_Visitor_View_UserChatMsgs";

            cmd.Parameters.AddWithValue("@SVID", SVID.ToString());

            cmd.Parameters.AddWithValue("@RVID", RVID.ToString());

            //System.Data.DataTable dt = DBHelper.SqlExecuteReader(cmd);

 

            RSS.DataSource = DBHelper.SqlExecuteReader(cmd);

            RSS.DataBind();
        }
    }
    catch (Exception ex)

    {
        throw ex;

    }
}


Here we used the DBHelper class for the SQL Connection.

DBHelper.cs
 

public class DBHelper

{

    private DBHelper()

    {

        //

        // TODO: Add constructor logic here

        //

    }

    #region Connection String

    private static string conn = "Data Source=SQL6; uid=sa; pwd=SQL@345; Initial catalog=NewApp";

    #endregion

 

    public static DataTable SqlExecuteReader(SqlCommand cmd)

    {

        DataTable dt = new DataTable();

 

        using (SqlConnection con = new SqlConnection(conn))

        {

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Connection = con;

            con.Open();

 

            SqlDataReader dr = cmd.ExecuteReader();

            dt.Load(dr);

 

            con.Close();
        } 

        return dt;

    } 

}

/*==========================================

Procedure To Bind Data

CREATE PROC spr_Visitor_View_UserChatMsgs 

@SVID INT, 

@RVID INT 

AS 

BEGIN 

SET NOCOUNT ON 


IF EXISTS( SELECT Visitorid from tblVisitorMaster where VisitorID=@RVID and IsActive=1) 

BEGIN 

 

SELECT VM.VisitorID ReceiverVID ,VM.VisitorName ReceiverName,VMM.VisitorID SenderVID,VMM.VisitorName SenderName,CM.ChatMsg,Convert(datetime,CM.EnteredOn,103) EnteredOn 

    from tblChatMessages CM INNER JOIN tblVisitorMaster VM ON CM.RVID=VM.VisitorID 

 INNER JOIN tblVisitorMaster VMM ON CM.SVID=VMM.VisitorID WHERE CM.RVID=@RVID AND CM.SVID=@SVID AND CM.Delivered=0 AND CM.IsActive=1   

     

END 

 

END 

/*=========================================

STEP 4

Now create a function to remove all illegal characters from the values.

protected string RemoveIllegalChar (object val)

{

    // cast the input to a string 

    string data = val.ToString();

 

    // replace illegal characters in XML documents with their entity references 

    data = data.Replace("&", "&amp;");

    data = data.Replace("\"", "&quot;");

    data = data.Replace("'", "&apos;");

    data = data.Replace("<", "&lt;");

    data = data.Replace(">", "&gt;");

 

    return data;
}

 

STEP 5

Now create RSS tags in the RSS.aspx page and bind the Repeater.

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="RSS.aspx.cs" Inherits="AndroitWebApp.RSS.RSS" %>

 

<asp:repeater ID=" RSS " runat="server">

 

      <HeaderTemplate> 

           <rss version="2.0"> 

                <channel> 

                    <title>RSS Example</title> 

                    <link>http://www.test.com/RSS.aspx</link> 

                    <description> 

                    RSS For Article

                    </description> 

        </HeaderTemplate> 

 

        <ItemTemplate> 

            <item>  

                <RVID><%# DataBinder.Eval(Container.DataItem, "ReceiverVID")%></RVID> 

                <RName><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "ReceiverName"))%></RName> 

                <SVID><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "SenderVID"))%></SVID> 

                <SName><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "SenderName"))%></SName> 

                <Message><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "ChatMsg"))%></Message> 

                <ChatDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem, "EnteredOn"))%></ChatDate> 

        </item> 

        </ItemTemplate> 

        <FooterTemplate> 

                </channel> 

            </rss>   

        </FooterTemplate>

</asp:repeater>

Sample To run this RSS:

http://localhost:52075/RSS/RSS.aspx?SVID=3&RVID=4

The output will look like this:

RSSFeed.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all