ASP.Net Cached XML File Settings

Apr 15, 2008
by:   Tim Stanley

How to use ASP.Net Cache settings to automatically read and update values from an XML file when the file is updated, and how to lookup a value in the XML file.

.Net surprises me every day.  I think about how I want to do things and then I dig a little (some time a lot) into .Net components and viola, I find it provides some new interesting functionality that I didn't know about before. 

I recently wanted to implement some features for using configuration values for an ASP.NET application.  My requirements were as follows.

  1. Implement an Admin page to allow writing / updating the configuration file.
  2. Use an XML file format for the configuration data.
  3. Cache the data in the IIS cache once read
  4. Update the cache if the XML file was updated
  5. I did not want to utilize a database
  6. Since these would be updated frequently, I did not want to utilize the web.config

I found the ASP.NET Cache.Insert method to be the key for my needs.  By creating a Cache Dependency tied to the configuration file, when the file is updated, it will automatically update the cache.  This is the same behavior as the web.config file, but without some of the same permission access issues when trying to write to the file.

I could have used a NameValueCollection to do the same thing, but I wanted to use the DataSet in this instance.  I added the PrimaryKey value to the DataSet and .Net took care of the lookup of the data with Rows.Find(). 

Note: In order to write to an XML file via ASP.NET code, the directory must have write permissions enabled on the IIS user account (ASPNET, or Network Service, or the ID used in the application pool, or virtual directory).

The code for this particular logic was placed in the App_Code directory, so it could be accessed from code as well as ASP.NET pages via something like the following.

public static String CustomSettings(String keyValue)
    DataSet oDS;
    String szXMLFileName;
    String foundValue = "";
    oDS = (DataSet)System.Web.HttpContext.Current.Cache["Settings"];
    if (oDS == null)
        szXMLFileName = SettingFileName();
        oDS = new DataSet();
        CacheDependency oCacheDependency;
        oCacheDependency = new CacheDependency(szXMLFileName);
        System.Web.HttpContext.Current.Cache.Insert("Settings", oDS, oCacheDependency);
    DataColumn[] oKeyCols = new DataColumn[1];
    DataTable oTable;
    DataRow oRow;
    oTable = oDS.Tables["Attribute"];
    oKeyCols[0] = oTable.Columns["Key"];
    oTable.PrimaryKey = oKeyCols;   
    oRow = oTable.Rows.Find((object)keyValue);
    if (oRow != null)
        foundValue = (string)oRow["Value"];
    return foundValue;

Settings.xml located in the App_Data directory

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
            <Text>Page Title Prefix</Text>
            <Text>Page Title Suffix</Text>
            <Value> -</Value>
            <Text>Site Name</Text>
            <Text>Copyright Name</Text>
            <Value>TSI Systems LLC</Value>

Related Items