A more efficient RSS feed with C#

The contents of an RSS feed only change when items that appear in it are added or amended, so the feed created in the previous article (Create an RSS Feed In ASP.NET 2.0 C#) is rather inefficient, in that the output is generated everytime the feed is requested. Here's an improvement.

This instalment only runs when it is called by the page that is used to add or amend articles on the site. It can be placed in the ItemInserted or ItemUpdated event, or wrapped as a method and called after ExecuteNonQuery() in your ADO.NET code related to the adding and amending of items.

The principal change from the previous article is that now, a physical file is created on the web server, and this saves multiple calls to the database. The physical path to the root is obtained from HttpContext.Current.Request.PhysicalApplicationPath, and this is checked to ensure that the trailing slash is present. If not, it is added. This is then passed along with the file name to the XmlTextWriter object instead of Response.OutputStream as previously. Finally, Response.End is NOT called at the end of the method.

using System.Data.SqlClient;
using System.Text;
using System.Xml;
...
string path = HttpContext.Current.Request.PhysicalApplicationPath;
if (path.LastIndexOf(@"\") != path.Length)
  path += @"\";

int copyrightyear = DateTime.Now.Year;
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter(path + "rss.xml", 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();
    }
  }
}

You might also like...