Create an RSS Feed In ASP.NET 2.0 C#

4.37 (19 votes)

Using the XmlTextWrite to output an XML file that serves as an RSS feed.

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

int copyrightyear = DateTime.Now.Year;
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", "Mikesdotnetting News Feed");
writer.WriteElementString("link", 
  wrap"http://www.mikesdotnetting.com/rss.aspx");
writer.WriteElementString("description", 
  wrap"Latest additions to the rubbish that appears on Mikesdotnetting.com.");
writer.WriteElementString("copyright", "(c) " + copyrightyear.ToString() +
  wrap", Mikesdotnetting. All rights reserved.");
string connectionString = Utils.GetConnString();
using (SqlConnection conn = new SqlConnection(connectionString))
{
  using (SqlCommand objCommand = new SqlCommand("GetRss", conn))
  {
    objCommand.CommandType = CommandType.StoredProcedure;
    conn.Open();
    using (SqlDataReader objReader = objCommand.ExecuteReader())
    {
      while (objReader.Read())
      {
        writer.WriteStartElement("item");
        writer.WriteElementString("title", objReader.GetString(1));
        writer.WriteElementString("description", objReader.GetString(2));
        writer.WriteElementString("link", "http://www.mikesdotnetting.com/Article.aspx?ArticleID=
          wrap" + objReader.GetInt32(0).ToString());
        writer.WriteElementString("pubDate", 
          wrapobjReader.GetDateTime(3).ToString("R"));
        writer.WriteEndElement();
      } 
      objReader.Close();
      conn.Close();
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.WriteEndDocument();
      writer.Flush();
      writer.Close();
      Response.End();
    }
  }
}

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 15367

2 Comments

- dhruv joshi

how can i remove <html doctype from rss generated script .plz.

I dont want DocType page generated for rss xml script.

- Mike

@dhruv joshi

Put the code in the ProcessRequest event of a Generic Handler.

Recent Comments

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...

Harris Boyce 04/07/2017 04:17
In response to Razor Pages - The Elevator Pitch
As a developer of a couple "trivial" web pages applications used by non-profits that wouldn't have I...

Cyrus 28/06/2017 20:25
In response to Razor Pages - Getting Started With The Preview
.net core 2.0 preview 2: <a...

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...