A more efficient RSS feed with C#

3.5 (4 votes)

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...

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

0 Comments

Recent Comments

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....

Satyabrata Mohapatra 13/10/2017 10:17
In response to Customising Identity in Razor Pages
Thanks for sharing. Learned a lot!!...

Cyrus 23/09/2017 11:44
In response to Routing in Razor Pages
I want to have dynamic sitemap in my razor page app so I've created a page named "SiteMap" and route...