Create Rss Feeds In ASP.NET Consume With Custom Feed Reader

In this example i'm going to describe how to Create Rss Feeds In ASP.NET 2.0,3.5,4.0 And Consume With Custom Feed Reader for your web application using C# VB.Net, First of all we need to create a SQL server database to store and fetch data for feeds.

Create a database and name it RSS and create a table according to image below

And add some data in this table.


Now create a new website in Visual studio and name it RssFeed
Add a new web form and name it Employees.

Go to html source of the page
And add this page directive below the first line on the page

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


Now go to code behind of Employee page and write this code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
using System.Data.SqlClient;
public partial class Employees : System.Web.UI.Page
{
string strConnection =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Clear the response buffer contents
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter rssFeed = new XmlTextWriter
(Response.OutputStream, Encoding.UTF8);

//writing RSS tags
rssFeed.WriteStartDocument();
rssFeed.WriteStartElement("rss");
rssFeed.WriteAttributeString("version", "2.0");
rssFeed.WriteStartElement("channel");
rssFeed.WriteElementString("title", "Employee Details");
rssFeed.WriteElementString("link", "http://localhost:2923/RssFeed");
rssFeed.WriteElementString("description", "Details of Employees");

// create sql connection and connect to database
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Employee";
cmd.Connection = con;
con.Open();
SqlDataReader dReader ;
dReader = cmd.ExecuteReader();
while (dReader.Read())
{
rssFeed.WriteStartElement("item");
rssFeed.WriteElementString("title", dReader["FirstName"].ToString()
+ " " +dReader["LastName"].ToString());
rssFeed.WriteElementString("description", dReader["Location"].ToString());
rssFeed.WriteElementString("link",
"http://localhost:2923/RssFeed/Employees.aspx?EmpID=" +
dReader["ID"]);
rssFeed.WriteElementString("pubDate", DateTime.Now.ToString());
rssFeed.WriteEndElement();
}
dReader.Close();
con.Close();
rssFeed.WriteEndElement();
rssFeed.WriteEndElement();
rssFeed.WriteEndDocument();
rssFeed.Flush();
rssFeed.Close();
Response.End();

}

}
}

Save, build and run the project

Creating custom RSS FEED reader

Create a new project and add new web form to it , name it FeedReader.
Add a new web form and name it anything you want, go to html source of the page and add this
inside <form> tag of the form
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<table class="NormalText" runat="server"
id="tblNews" cellpadding="0" cellspacing="0">
</table>
</td>
</tr>
</table>


Now go to code behind of the page and write this code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string rss =
"http://localhost:2923/RssFeed/Employees.aspx".ToString();
try
{
FetchRssFeeds(rss);
}
catch (Exception ex)
{
}
}
public void FetchRssFeeds(string rss)
{
// Read the RSS feed
WebRequest rssRequest = WebRequest.Create(rss);
WebResponse rssResponse = rssRequest.GetResponse();

Stream rssStream = rssResponse.GetResponseStream();

// Load XML Document
XmlDocument rssDocument = new XmlDocument();
rssDocument.Load(rssStream);

XmlNodeList rssList = rssDocument.SelectNodes("rss/channel/item");

string title = "";
string link = "";
string description = "";

// Loop through RSS Feed items
for (int i = 0; i < rssList.Count; i++)
{
XmlNode rssDetail;

rssDetail = rssList.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}

rssDetail = rssList.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}

rssDetail = rssList.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}

// Populate the HTML table rows and cells
HtmlTableCell cell = new HtmlTableCell();
cell.InnerHtml = "<b><a href='" + link + "' target='new'>"
+ title + "</a></b>";
HtmlTableRow trow = new HtmlTableRow();
trow.Cells.Add(cell);
tblNews.Rows.Add(trow);
HtmlTableCell cell2 = new HtmlTableCell();
cell2.InnerHtml = "<p align='justify'>" + description + "</p>";
HtmlTableRow trow2 = new HtmlTableRow();
trow2.Cells.Add(cell2);
tblNews.Rows.Add(trow2);
}
}
}


Save, build and run the project
Download the sample code




Other Posts:
C#.NET Articles -Cascading DropDownList Populate dropdown based on selection of other dropdown in ASP.NET

Install configure and troubleshooting sql server reporting services 2005

Highlight gridview row on mouse over using javascript in asp.net and C# c-sharp
If you like this post than join us or share

14 comments:

Avinash said...

code is not available from that link.. .pls chk


amiT jaiN said...

@Avinash:

Download link is fixed :)


K-Bone said...

This is a very cool example and I love the feel of C#


Anonymous said...

this is very long-winded. serialization is a much better option.


Anonymous said...

Pretty sweet example. Thank you very much for helping save me a couple of hours of my time. :) I appreciate it very much.


idrops45 said...

I get a parser error when I try to build the reader. Is something missing ( a directive, assembly, etc..?)


amiT jaiN said...

@idrops45:

Have you add these namespaces in code behind ?

using System.Net;
using System.Xml;
using System.IO;


idrops45 said...

Figured it out. I renamed _default to FeedReader so it was inheriting from the wrong page, also the path to the RSS feed is different on my machine. It works very nicely now. Great example, will be valuable towards my project. Thanks!


Tim Acheson said...

The quickest, easiest way to add an RSS feed to an ASP.NET web application is to write a custom ActionResult. About 5 lines of code does everything for you:

http://www.timacheson.com/Posts/2009/jun/adding_rss_feed_to_asp_net_mvc_web_site


Edwin Garcia said...

I know you made this code available two years ago, but do you still have it by any chance?


Anonymous said...

This is a very interesting site. The content is very informative and I am so glad that I dropped by. Thanks!


Anonymous said...

Hi I am doing the same thing ... but when i do the second part of the project in which u r getting the rss data into the html table and dispalying it on the web page that doesnot work for me.. though the xml data is available in the page source ... how r we accessing it in the html table .. sorry I am a newbie so need some help


amiT jaiN said...

@Above : Check this liner of code

string rss =
"http://localhost:2923/RssFeed/Employees.aspx".ToString();


Your path might be different from this one depending on your configuration and the nsame of aspx page you created in 1st part of code


Deon said...

Is this available in VB?


Find More Articles