Create RSS Feed Using ASP.NET

An 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:

  1. <HeaderTemplate>   
  2.            <rss version="2.0">   
  3.                 <channel>   
  4.                     <title>RSS Example</title>   
  5.                     <link>http://www.test.com/RSS.aspx</link>   
  6.                     <description>   
  7.                     RSS For Article  
  8.                     </description>   
  9. </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:
  1. <%@ 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:
  1. <%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="RSS.aspx.cs" Inherits="AndroitWebApp.RSS.RSS" %>  
  2. <asp:repeater ID="RSS" runat="server">  
  3. </asp:repeater>
STEP 3

Pick query string values for the variables:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     int SVID = Convert.ToInt32(Request["SVID"].ToString());  
  4.     int RVID = Convert.ToInt32(Request["RVID"].ToString());  
  5.     GetChatmsg(SVID,RVID);  
  6. }
Now go to the RSS.aspx.cs and create a function to bind data from the database:

  1. protected void GetChatmsg(int SVID, int RVID)  
  2. {  
  3.     try  
  4.     {  
  5.         using (SqlCommand cmd = new SqlCommand())  
  6.         {  
  7.             cmd.CommandText = "spr_Visitor_View_UserChatMsgs";  
  8.             cmd.Parameters.AddWithValue("@SVID", SVID.ToString());  
  9.             cmd.Parameters.AddWithValue("@RVID", RVID.ToString());  
  10.             //System.Data.DataTable dt = DBHelper.SqlExecuteReader(cmd);  
  11.             RSS.DataSource = DBHelper.SqlExecuteReader(cmd);  
  12.             RSS.DataBind();  
  13.         }  
  14.     }  
  15.     catch (Exception ex)  
  16.     {  
  17.         throw ex;  
  18.   
  19.     }  
  20. } 

Here we used the DBHelper class for the SQL Connection.

DBHelper.cs
 

 

  1. public class DBHelper  
  2. {  
  3.     private DBHelper()  
  4.     {  
  5.         //  
  6.         // TODO: Add constructor logic here  
  7.         //  
  8.     }  
  9.     #region Connection String  
  10.     private static string conn = "Data Source=SQL6; uid=sa; pwd=SQL@345; Initial catalog=NewApp";  
  11.     #endregion  
  12.     public static DataTable SqlExecuteReader(SqlCommand cmd)  
  13.     {  
  14.         DataTable dt = new DataTable();  
  15.         using (SqlConnection con = new SqlConnection(conn))  
  16.         {  
  17.             cmd.CommandType = CommandType.StoredProcedure;  
  18.             cmd.Connection = con;  
  19.             con.Open();  
  20.             SqlDataReader dr = cmd.ExecuteReader();  
  21.             dt.Load(dr);  
  22.             con.Close();  
  23.         }   
  24.         return dt;  
  25.     }   
  26. }  

Procedure To Bind Data

  1. CREATE PROC spr_Visitor_View_UserChatMsgs   
  2. @SVID INT,   
  3. @RVID INT   
  4. AS   
  5. BEGIN   
  6. SET NOCOUNT ON   
  7. IF EXISTS( SELECT Visitorid from tblVisitorMaster where VisitorID=@RVID and IsActive=1)   
  8. BEGIN   
  9. SELECT VM.VisitorID ReceiverVID ,VM.VisitorName ReceiverName,VMM.VisitorID SenderVID,VMM.VisitorName SenderName,CM.ChatMsg,Convert(datetime,CM.EnteredOn,103) EnteredOn   
  10.     from tblChatMessages CM INNER JOIN tblVisitorMaster VM ON CM.RVID=VM.VisitorID   
  11.  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     
  12. END   
  13. END

STEP 4

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

  1. protected string RemoveIllegalChar (object val)  
  2. {  
  3.     // cast the input to a string   
  4.     string data = val.ToString();  
  5.     // replace illegal characters in XML documents with their entity references   
  6.     data = data.Replace("&""&");  
  7.     data = data.Replace("\""""");  
  8.     data = data.Replace("'""'");  
  9.     data = data.Replace("<""<");  
  10.     data = data.Replace(">"">");  
  11.     return data;  
  12. } 

STEP 5

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

  1. <%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="RSS.aspx.cs" Inherits="AndroitWebApp.RSS.RSS" %>  
  2. <asp:repeater ID=" RSS " runat="server">  
  3.       <HeaderTemplate>   
  4.            <rss version="2.0">   
  5.                 <channel>   
  6.                     <title>RSS Example</title>   
  7.                     <link>http://www.test.com/RSS.aspx</link>   
  8.                     <description>   
  9.                     RSS For Article  
  10.                     </description>   
  11.         </HeaderTemplate>  
  12.         <ItemTemplate>   
  13.             <item>    
  14.                 <RVID><%# DataBinder.Eval(Container.DataItem, "ReceiverVID")%></RVID>   
  15.                 <RName><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "ReceiverName"))%></RName>   
  16.                 <SVID><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "SenderVID"))%></SVID>   
  17.                 <SName><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "SenderName"))%></SName>  
  18.                 <Message><%# RemoveIllegalChar (DataBinder.Eval(Container.DataItem, "ChatMsg"))%></Message>   
  19.                 <ChatDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem, "EnteredOn"))%></ChatDate>   
  20.         </item>   
  21.         </ItemTemplate>   
  22.         <FooterTemplate>   
  23.                 </channel>   
  24.             </rss>  
  25.         </FooterTemplate>  
  26. </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


Similar Articles