DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Creating an RSS Feed in ASP.NET with C#

This tutorial will show you how to create your own RSS feed using ASP.NET and C#

In this tutorial, you will learn how to create an RSS feed for your content to be subscribed to.
RSS stands for Really Simple Syndication. It allows you to easily notify people (and machines) of updates to your content. It does this by generating an XML file of your latest posts, blogs, entries, etc. For this XML file, we need to follow a certain structure to create somewhat of a standard for machines to decode our Syndication. This structure looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>title</title>
<link>http://www.link.com/</link>
<description>description</description>
<language>en-us</language>
<ttl>5</ttl>
<item>
<title>title of entry</title>
<description>description of entry</description>
<link>http://www.link.com/</link>
<pubDate>publish date</pubDate>
</item>
<item>
<title>title of entry</title>
<description>description of entry</description>
<link>http://www.link.com/</link>
<pubDate>publish date</pubDate>
</item>
</channel>
</rss>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

In order for us to generate this XML file, we need something to generate from - content. For this example, I have created a sample SQL database with one table - Articles. Within this table, we have the following columns: ID, Title, Category, DateTimeAdded, and Article. These columns aim to replicate a usual blog entry.
RSS feeds are usually found under the feed sub-directory of a site, so go ahead and add a folder named feed to your project, and then create a Default.aspx page in that folder. Once done, we will modify the ASPX code. We will not be generating any HTML, so we need to strip out the majority of the default code that is in there. All we are going to use is the following:

<%@ Page ResponseEncoding="UTF-8" Language="C#" AutoEventWireup="true" debug="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>

This OutputCache line will only allow the code-behind to run 300 seconds after it has been run. The application will store a version in cache for 300 seconds.

Now we can move to the code-behind. The first thing we want to do here is to add the following assembly references we will be using:

using System.Data.SqlClient;
using System.Xml;
using System.Text;
using System.Configuration;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The following code we will put in the Page_Load event, as we want it to run when the page loads:

Response.Clear();
Response.ContentType = "application/rss+xml";
XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version","2.0");
objX.WriteStartElement("channel");

SqlCommand cmd = new SqlCommand("Select TOP 10 * From Articles ORDER BY ID DESC", new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString));
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();

objX.WriteElementString("title","Sample Article RSS Feed");
objX.WriteElementString("link","http://www.vbasic.net/");
objX.WriteElementString("description","VBasic.NET offers simple downloadable, easy to understand tutorials and code examples using Visual Basic.NET, ASP.NET and Visual Studio. Visit often as new Tutorials and VB.NET Examples are added every day!");
objX.WriteElementString("language","en-us");
objX.WriteElementString("ttl","60");
objX.WriteElementString("image","http://vbasic.net/media/logo.gif");
objX.WriteElementString("lastBuildDate", String.Format("{0:R}",DateTime.Now));

while (dr.Read())
{
objX.WriteStartElement("item");
objX.WriteElementString("title", dr["Title"].ToString());
objX.WriteElementString("author", "VBasic.net");
objX.WriteElementString("link", "http://www.vbasic.net/");
objX.WriteStartElement("guid");
objX.WriteAttributeString("isPermaLink","true");
objX.WriteString("http://www.vbasic.net/");
objX.WriteEndElement();
objX.WriteElementString("pubDate", String.Format("{0:R}",dr["DateTimeAdded"]));
objX.WriteStartElement("category");
objX.WriteString(dr["Category"].ToString());
objX.WriteEndElement();
objX.WriteElementString("description", dr["Article"].ToString().Substring(0,100) + "..");
objX.WriteEndElement();
}

objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
Response.End();

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Here, we are simply creating a new instance of the XmlTextWriter class and using it to generate our XML document. We loop through our database to get the latest articles, and write them into the XML file.

The entire code-behind will look something like this:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Xml;
using System.Text;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/rss+xml";
XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version","2.0");
objX.WriteStartElement("channel");

SqlCommand cmd = new SqlCommand("Select TOP 10 * From Articles ORDER BY ID DESC", new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString));
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();

objX.WriteElementString("title","Sample Article RSS Feed");
objX.WriteElementString("link","http://www.vbasic.net/");
objX.WriteElementString("description","VBasic.NET offers simple downloadable, easy to understand tutorials and code examples using Visual Basic.NET, ASP.NET and Visual Studio. Visit often as new Tutorials and VB.NET Examples are added every day!");
objX.WriteElementString("language","en-us");
objX.WriteElementString("ttl","60");
objX.WriteElementString("image","http://vbasic.net/media/logo.gif");
objX.WriteElementString("lastBuildDate", String.Format("{0:R}",DateTime.Now));

while (dr.Read())
{
objX.WriteStartElement("item");
objX.WriteElementString("title", dr["Title"].ToString());
objX.WriteElementString("author", "VBasic.net");
objX.WriteElementString("link", "http://www.vbasic.net/");
objX.WriteStartElement("guid");
objX.WriteAttributeString("isPermaLink","true");
objX.WriteString("http://www.vbasic.net/");
objX.WriteEndElement();
objX.WriteElementString("pubDate", String.Format("{0:R}",dr["DateTimeAdded"]));
objX.WriteStartElement("category");
objX.WriteString(dr["Category"].ToString());
objX.WriteEndElement();
objX.WriteElementString("description", dr["Article"].ToString().Substring(0,100) + "..");
objX.WriteEndElement();
}

objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
Response.End();
}
}




Looking for more .NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!