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.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter(path + "rss.xml", Encoding.UTF8);
writer.WriteAttributeString("version", "2.0");
writer.WriteElementString("title", "Mikesdotnetting News Feed");
  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;
    using (SqlDataReader objReader = objCommand.ExecuteReader())
      while (objReader.Read())
        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());

You might also like...

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


Recent Comments

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

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