How to create an RSS Feed in ASP.NET

Article translations Article translations
Close Close
Article ID: 2060083 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

This article describes an All-In-One Code Framework sample that is available for download. This code sample demonstrates how to create a RSS Feed in ASP.NET. 

Before you go through this sample, we assume that you are familiar with RSS, including its usage, format, and so on. If not, refer to the RSS Tutorial Web site in the "References" section of this article. It describes what RSS is and the basic format of a standard RSS file.

Note The code sample that is described in this article is not intended to be used in a production environment. It is provided only for illustration. This code sample is released under the terms of the Microsoft Public License (MS-PL).

Difficulty level

Collapse this imageExpand this image
2149311

Download information

To download this code sample, click the following link:

  


Technical overview

The sample provides two pages: AddArticle.aspx and Rss.aspx.

The AddArticle.aspx page supports a feature of updating the database via a simple FormView control. You can insert, edit and delete a record by using this FormView control when you are operating some articles. The Rss.aspx page can give the users who subscribe the RSS Feed the latest status of these records in the data table. 

To achieve the Rss.aspx page, you first need to import the XML namespace and change the page's ContentTpye to XML as the RSS file is an XML file other than a normal web page.

using System.Xml;

protected void Page_Load(object sender, EventArgs e)

{

    //Change the page's ContentTpye to XML.
    Response.ContentType = "application/rss+xml";
}
The RSS Feed content contains three parts: the opening, the body and the ending. You can also separate the code into three methods: WriteRssOpening(), WriteRssBody() and WriteRssEnding().


In the WriteRssOpening() method, you need to set the version, the title, the simple description and some other information of this RSS Feed according to the RSS’s definition.

private void WriteRssOpening(XmlTextWriter rsswriter)

{

    rsswriter.WriteStartElement("rss");

    rsswriter.WriteAttributeString("version", "2.0");

    rsswriter.WriteStartElement("channel");

    rsswriter.WriteElementString("title", "RSS title");

    rsswriter.WriteElementString("link", Request.Url.Host);

    rsswriter.WriteElementString("description", "RSS description");
}
When you write the body part, you can use the “For… Each…” statement to loop through the rows in the source Table and output them one by one.
foreach (DataRow rssitem in data.Rows)

{

    rsswriter.WriteStartElement("item");

    rsswriter.WriteElementString("title", rssitem["Title"].ToString());

    rsswriter.WriteElementString("author", rssitem["author"].ToString());

    rsswriter.WriteElementString("link", rssitem["Link"].ToString());

    rsswriter.WriteElementString("description", rssitem["Description"].ToString());

    rsswriter.WriteElementString("pubDate", rssitem["PubDate"].ToString());

    rsswriter.WriteEndElement();
}
After all the records have been written to the XML, you need to close the opened element in the WriteRssEnding() method.
private void WriteRssEnding(XmlTextWriter rsswriter)

{

    rsswriter.WriteEndElement();

    rsswriter.WriteEndElement();
}
Also, you need to end the Response of the current request from the client. Otherwise, an error will occur and all the operations you performed will be discarded. When this problem occurs, you will recieve an error message "Internet Explorer cannot display this feed" on the page.


NoteFor more information about how to create the sample application and how to deploy the sample application, see the Readme.txt file that is included in the download package.

Technology category

  • ASP.NET

Languages

This code sample contains the following programming languages:

Collapse this tableExpand this table
LanguageProject Name
Visual C#CSASPNETRssFeeds
Visual Basic.NETVBASPNETRssFeeds

Prerequisites

  • This sample application was created by using Visual Studio 2008 with Service Pack 1 installed.


MORE INFORMATION

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site:
http://1code.codeplex.com

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, you can simply click the following link:
http://support.microsoft.com/gp/gp_kbcodefx

REFERENCES

For more information, visit the following Web sites:

XmlTextWriter Class

RSS Tutorial
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Properties

Article ID: 2060083 - Last Review: September 28, 2010 - Revision: 2.0
APPLIES TO
  • Microsoft Visual Studio 2008 Service Pack 1
Keywords: 
kbcodefx kbrapidpub kbnomt atdownload kbsurveynew kbinfo KB2060083

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com