HomeC# Creating an RSS Feed with ASP.Net Written ...
Creating an RSS Feed with ASP.Net Written in C#
RSS feeds are massively popular online. It's no wonder; they're a great way for users to receive news, blog entries, and other content in which they're interested. If you want to set one up for your web site, keep reading.
Contributed by Luke Niland Rating: / 45 July 02, 2007
RSS. Your probably already know what this is, but put simply RSS (Really Simple Syndication) is just an XML file served up by a web server, normally containing news items, stock prices and so forth.
The file contains certain tags that define it as an RSS feed (such as the opening <rss version="2.0"> tag).
RSS feed readers such SharpReader can use these XML files and display their content, headlines and more. These readers mean that, from one place, you can easily see all the news and information to which you have subscribed.
In this article, we will go though creating an RSS feed, using an ASP.Net web page, written in C#. We will also need to have a back end SQL server database that contains the news stories we are going to put into the RSS feed.
Layout of Database table
First of all, we need to have the database with some stories in it. For this article we will be using a table as described in the table below.
Field name
Field Type
Notes
id
Unique integer id of the field
ID of the story
headline
VarChar field, 75 long
Headline of the story
story_text
Text field
Full text of the story
If you are creating the database from scratch, make sure there is some test data in there to use. The headline field will be what most people first see in the RSS reader application.
Once you have the above table (call it something like tbl_stories), you can start to create the ASP.Net page that will serve the feed.
There will be two files in this project. One will contain an ASP.Net repeater control that will put the data into the XML file. The other will be a code behind page that contains all the logic for getting the data. You can create these pages in an IDE like Visual Studio or Visual Web Developer, or just use a plain text editor. If you are using a text editor, you will have to copy the pages onto your IIS server in order to test them.
First of all, create a page called rss.aspx. This is the page that contains the data repeater control, at which your users will actually point their RSS reader applications.
At the very top of the page, you need to have the line shown below. It is the page declaration statement, and tells the page how to behave:
The first declaration is the language; it tells you what code to expect on this page. It is set to C#.
The next part of this is the CodeFile section. It is telling us the code for the page is located within another file, in this case the rss.aspx.cs file.
The next section is important; it is setting the ContentType of the page. If this were a normal web page you wouldn't have to change it (it defaults to HTML), but we want to send XML to the browser (or RSS reader), so we make it text/xml.
The Inherits section tells the page what class to inherit from the code behind page. In the case it's the SyndicationDemo class, which we will be writing next.
We will now create the code behind page. It makes more sense to do this before we add the repeater to this page, so you can see what the repeater is actually outputting.
Create a new file called rss.aspx.cs in the same place you have created the rss.aspx page.
At the very top of the page, we will tell the pages about all the dot.net classes we will be using, as follows:
using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Xml;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
After that we create the namespace, and set it to be a web page
namespace SyndicationDemo
{
//Class for returning RSS feed Data
public partial class rss : System.Web.UI.Page
{
Now we will write the page load event. This will run every time someone requests the page. It basically queries our database for the information, and then binds this to the repeater control.
The first thing we do is create a SQL server connection to our database. In a real world production application you would want to add some exception handling to the code; it is left out here to made the code more readable.
Once we have connected, we run a SQL query on the database to return our stories. The SQL here is using the TOP keyword so we only return a limited number of stories to show. In the real world you might limit this to stories within a certain date, of a certain status, and so on.
The TOP keyword might not work if you are using a database that is hosted on a non-Microsoft SQL server.
We then open the connection to the database, execute the SQL against this connection, bind the SQL queries results to the repeater and close the connections.
The last part of the code behind page is a simple function that will be called from the main page. This function replaces any chars in your stories that are XML reserved characters
protected string FormatForXML(object input)
{
string data = input.ToString(); //cast input to string
// replace those characters disallowed in XML documents
data = data.Replace("&", "&");
data = data.Replace(""", """);
data = data.Replace("'", "'");
data = data.Replace("<", "<");
data = data.Replace(">", ">");
return data;
}
The function just accepts one argument, casts it to a text type and does a find and replace on the reserved characters to make them valid.
Now we have all the logic in place. We can finish off the rss.aspx page we started earlier. The Repeater control is already bound to our results from the query, so we use it now to output the content to the XML page. First we create the control on the page.
<asp:Repeaterid="rptRSS" runat="server">
Then we use the HeaderTemplate tags to define the start of the XML file.
<HeaderTemplate>
<rss version="2.0">
<channel>
<title>NW PA Feed (TEST)</title>
<link>http://nw-editiis1/nwpafeed/</link>
<description>This is a RSS Feed</description>
</HeaderTemplate>
This is the start of the XML file, and is giving it basic information about this feed. This is very simplistic, but it is telling the RSS reader application the version of RSS to which we are writing and the title of the feed, as well as giving it a link to the feed and the feed's description.
This is a very simple example of a feed. More options are available such as links to a header image and so on. See http://www.rss-specifications.com/ for details.
Now we will drop the individual items into the page.
For each of the items for which we have queried the database, we call the FormatForXML function we wrote and pass it the current record and the file in which we are interested.
To get the actual data out of the repeater, we call the dataBinder.Eval method, and pass it the data item. This item can be the name of the field or its integer value from 0 depending on the order they are returned from the query.
Remember, we need to run the data through the format function to remove some of the XML reserved characters.
When writing my version of this application, I had to make a few tweaks to the format function due to my data containing some other characters that XML didn't seem to like. These included some non-Windows line endings and some extended ASCII codes. I also made sure the headlines and main story text were only up to a certain length to improve readability and encourage the user to click on the link to the main story.
We also put the current date and time in the PubDate field, but you might already have this information in your database for each story.
Now that we've gotten the main data into the repeater, we need to write the footer tags in, and close the ASP repeater control, like this:
<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>
You now should be able to test your RSS feed. Fire up a modern browser (IE7 or Firefox; IE 6 will just display the XML) and navigate to the web URL on your web server where you dropped the code. You should be able to subscribe to the page and see all the news items.
If you click on one of your items it will take you off to the URL you specified in the link section of your XML repeater code.