Rss Feed Tutorial

RSS is a standardized, XML-formatted means for syndication Web site content. The history of RSS can be traced back to the Resource Description Framework (RDF), a W3C standard first drafted in October 1997. The purpose of RDF, in a nutshell, was to provide a standard means representing information about resources on the Web, such as information like the title, author, the date published, a description, copyright notices, and other metadata for online articles, message board posts, FAQs, etc.

Understand that RSS is intended to provide information about a Web site's latest content.Notice that RSS provides a synopsis for the recent articles or news items. It does not provide the complete content for all of the news items or articles. With this in mind, let's look at the RSS format.

Next step is to create an ASP.NET Web page that will display a list of the most recent news items as a properly formatted RSS 2.0 XML file. Before examining how to accomplish this transformation in an ASP.NET Web page, let's first take a moment to examine the RSS 2.0 specification. While looking over the specification, keep in mind that RSS is designed to provide a data model to syndicate content. Not surprisingly, then, it has a series of XML elements for information about the Web site syndicating the content, as well as a series of XML elements to describe a particular news item. Finally, don't forget that RSS syndication files, like any XML-formatted file, must adhere to XML formatting:
  1. All XML elements be properly nested,
  2. All attribute values be quoted, and
  3. All instances of <, >, &, " and ' be replaced with <, >, &, " and ', respectively.
The root element of an RSS document is <rss>. It has precisely one child element, <channel>. The <channel> element contains information about the syndicated content, as well as information about each content item being syndicated. There are three required channel elements:
  • <title> - provides a title for the channel. For the RSS feed this is: "24x7aspnet.blogspot.com Title"
  • <link> - a URL to the channel http://feeds2.feedburner.com/blogspot/HoUi
  • <description> - a short description of the channel. 24x7aspnet is an online resource site for ASP.Net C# Microsoft Professional!"
There are additional optional elements, such as <language>, <copyright>, <webMaster>, and others. For a complete list refer to the RSS 2.0 specification.

After those elements that describe the channel, there are a series of <item> elements, one for each content item being syndicated. Each <item> contains elements that describe the content item. Commonly, the <item> elements will contain the following four elements:
  • <title> - provides a title for the content item (for the 24x7aspnet RSS feed, it's the article's name)
  • <link> - a URL to the content item
  • <description> - a synopsis of the content item
  • <pubDate> - the date the content item was published.
Realize that only the <title> or the <description> element is required. In addition to the <title>, <link>, and <description> elements, there are additional optional elements, such as <author>, <category>, and <comments>, among others. For more information review the RSS 2.0 specification.

To syndicate content as RSS, let's create an ASP.NET Web page, RssFeed.aspx, that simply emits XML content. That is, this page is going to do nothing but return the corresponding XML output. There are a couple of ways to generate XML output. The cleanest approach is to use the XML classes in the .NET Framework (found in the System.Xml namespace). For this example, since we just want to write out a stream of XML, we'd want to use the XmlTextWriter class.

The RSS specification
While you can use XML to pretty much present any data in any way, naturally our fancy little content aggregators and the like are going to expect that it be in a certain format. So before we look at the specification, let's first look at a spot of XML from this very site's 24x7Aspnet feed:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>ASP.Net C# Microsoft Professional</title>
<link>24x7aspnet.blogspot.com/24x7Aspnet.aspx</link>
<description>Intended to provide you with useful information about ASP.net, C#, AJAX, VB.net, .Net Framework, WCF, WPF, WWF,Programming .Net Articles, Links, FAQ, Tips, Interview Question. We hope it will be a useful resource for Software Professional and all readers. On the way of being a Microsoft Visual Studio .Net Developer.</description>
<copyright>(c) 2009, ASP.Net C# Microsoft Professional. All rights reserved.</copyright>
<ttl>5</ttl>
<item>
<title>Partial Classes in ASP.Net 2.0</title>
<description>It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.</description>
<link>http://24x7aspnet.blogspot.com/2009/06/partial-classes-in-aspnet-20.html</link>
<pubDate>Mon, 22 Jun 2009 04:48:34 GMT</pubDate>
</item>
<item>
<title>Register directive in ASP.Net</title>
<description>The @Register directive associates aliases with namespaces and class names for notation in custom server control syntax. When you drag and drop a user control onto your .aspx pages, the Visual Studio 2005 automatically creates an @Register directive at the top of the page.</description>
<link>http://24x7aspnet.blogspot.com/2009/06/register-directive-in-aspnet.html</link>
<pubDate>Mon, 22 Jun 2009 03:48:35 GMT</pubDate>
</item>
<item>
<title>Microsoft Certified Exam</title>
<description>Certifications are available for most Microsoft technologies and skill levels from business workers to IT professionals.</description>
<link>http://24x7aspnet.blogspot.com/2009/06/microsoft-certified-exam.html</link>
<pubDate>Mon, 22 Jun 2009 06:14:21 GMT</pubDate>
</item>
</channel>
</rss>

URL for our RSS feed. We're going to make the .aspx file contain only two lnes:

<%@ Page Codebehind="24x7Aspnet.aspx.cs" Inherits="24x7Aspnet.RSS.24x7Aspnet" EnableViewState="false" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>

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

namespace 24x7Aspnet.RSS
{
public class 24x7Aspnet : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version","2.0");
objX.WriteStartElement("channel");
objX.WriteElementString("title", "ASP.Net C# Microsoft Professional 24x7Aspnet");
objX.WriteElementString("link","24x7aspnet.blogspot.com/24x7Aspnet.aspx");
objX.WriteElementString("description","Intended to provide you with useful information about ASP.net, C#, AJAX, VB.net, .Net Framework, WCF, WPF, WWF,Programming .Net Articles, Links, FAQ, Tips, Interview Question. We hope it will be a useful resource for Software Professional and all readers. On the way of being a Microsoft Visual Studio .Net Developer..");
objX.WriteElementString("copyright","(c) 2004, ASP.Net C# Microsoft Professional. All rights reserved.");
objX.WriteElementString("ttl","5");
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["BlogConnectionString"]);
objConnection.Open();
string sql = "SELECT * Title, Summary, ArticleID, PostTime FROM Blogger ORDER BY PostTime DESC";
SqlCommand objCommand = new SqlCommand(sql, objConnection);
SqlDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
objX.WriteStartElement("item");
objX.WriteElementString("title",objReader.GetString(0));
objX.WriteElementString("description",objReader.GetString(1));
objX.WriteElementString("link","http://24x7aspnet.blogspot.com/2009/06/" + objReader.GetString(2).ToString());
objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"));
objX.WriteEndElement();
}
objReader.Close();
objConnection.Close();

objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
Response.End();
}
}
}
Tags: , , , ,
Hot on Web:


About author