Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Configuration File Reader

0.00/5 (No votes)
3 Apr 2006 1  
Easily read and use XML configuration files.

Contents

Introduction

Many applications require the use of information from a persistent data store, such as an XML configuration file, to operate or process successfully. There are several ways to obtain this information, including using the .NET provided classes and methods, or creating your own class. This article will demonstrate how to use a configuration file reader class that easily works with the XML configuration files prevalent today. The reader class works with various types of assemblies (console applications, web applications, services, �) and provides a consistent approach to processing configuration files.

The provided class solution and console test harness were constructed using Visual Studio 2005. A VS2003 version of the solution could be created using the .cs files from the provided solution. A help file (CHM) is also included in the downloadable archive.

It is recommended that the reader class be built with a strong name so it may reside in the GAC.

Sample Configuration File Information

For information to be used by this configuration processor, it must be contained within the <appSettings> section of the <configuration> area of a file. You may have as many key/value statements as needed in this section. The actual configuration file is independent of the assembly running, making it easier to have multiple configuration files for different scenarios and/or the same application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="smtphost" value="yoursmtp.yourname.com" />
    <add key="interval" value="120" />
    <add key="useproxy" value="true" />
    <add key="showdebug" value="0" />
    </appSettings>
</configuration>

Using the Reader

After successfully building the reader class in your environment, you can begin using it by adding a reference to the reader assembly in your project. You then instantiate an instance of it by providing the filename of the configuration file you wish to process. Create multiple reader instances if you have multiple configuration files to process.

ConfigurationFileReader.ParmProcessor WeeklyParms = new 
   ConfigurationFileReader.ParmProcessor(@"d:\appcon" + 
   @"fig\myapp1\processweekly.config");
ConfigurationFileReader.ParmProcessor MonthlyParms = new 
   ConfigurationFileReader.ParmProcessor(@"d:\appconfig" + 
   @"\myapp1\processmonthly.config");

Once instantiated, the reader class parses the configuration file and you're able to retrieve the values using any of the various methods as listed below.

The following properties and methods enable you to retrieve information about the results of the file parsing as well as the key/value data.

Properties

  • IsError - Boolean denoting if an error has occurred during the parsing of the file.
  • Error - A string message showing what the error is, if one has occurred. It will be empty if an error has not occurred.
  • Count - An integer denoting the total number of key/value pairs that are available for retrieval.
  • Version - The assembly version of the configuration reader.
  • KeyList - An ArrayList of all the keys that were parsed.
  • KeyValuePairs - A NameValueCollection of all the key/value pairs parsed from the file.
  • ListAllKeyValuePairs - A string that contains all the keys and their associated values that are available. This format is suitable for console display.

Using the configuration file above with this test harness code:

ConfigurationFileReader.ParmProcessor WeeklyParms  = 
  new ConfigurationFileReader.ParmProcessor("processweekly.config");
Console.WriteLine("IsError=" + WeeklyParms.IsError);
Console.WriteLine("Error=" + WeeklyParms.Error);
if (!(WeeklyParms.IsError))
{
 Console.WriteLine("Count=" + WeeklyParms.Count);
 Console.WriteLine("Version=" + WeeklyParms.Version);
 if (WeeklyParms.Count > 0)
 {
  Console.WriteLine("\nShow all the keys:");
  ArrayList al = WeeklyParms.KeyList;
  foreach (string s in al)
  Console.WriteLine("key=" + s);
 }
 Console.WriteLine("\nAll the key/value pairs:\n" + 
                   WeeklyParms.ListAllKeyValuePairs);
}

produces these results:

Retrieval Methods

  • GetParameter(which_parameter_key_to_get) - Retrieves the string value for the specified key.
  • GetParameter(which_parameter_key_to_get, default_returned_value) - Retrieves the string value for the specified key. Returns default_returned_value if the specified key is missing or the value it contains is blank.
  • GetParameterInt(which_parameter_key_to_get) - Retrieves an integer for the specified key. This method will return a zero if the specified key is missing, is blank, or is not an integer.
  • GetParameterInt(which_parameter_key_to_get, default_returned_integer) - Retrieves an integer for the specified key. This method will return default_returned_integer if the specified key is missing, is blank, or is not an integer.
  • GetParameterBool(which_parameter_key_to_get) - Retrieves a boolean value for the specified key. If the specified key is present and has a value of 'true', 1, 'on', or 'yes', it will return true. If the specified key is present and has a value of 'false', 0, 'off', or 'no', it will return false. This method will return false if the specified key is missing or is blank.
  • GetParameterBool(which_parameter_key_to_get, default_returned_boolean) - Retrieves a boolean value for the specified key. If the specified key is present and has a value of 'true', 1, 'on', or 'yes', it will return true. If the specified key is present and has a value of 'false', 0, 'off', or 'no', it will return false. This method will return the boolean default_returned_boolean if the specified key is missing or is blank.

Using this configuration file:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <appSettings>
    <!-- booleanexample1 through booleanexampleX - 
     examples of boolean type processing -->
    <add key="booleanexample1" value="1" />
    <add key="booleanexample2" value="0" />
    <add key="booleanexample3" value="yes" />
    <add key="booleanexample4" value="no" />
    <!-- notice 5 and 6 are missing -->
    <add key="booleanexample7" value="on" />
    <add key="booleanexample8" value="off" />
    <add key="booleanexample9" value="true" />
    <add key="booleanexample10" value="false" />
    <add key="booleanexample11" value="1ortheother" />
    <add key="booleanexample12" value="abc" />

    <!-- textexample1 through textexampleX - 
     examples of text type processing -->
    <add key="textexample1" value="foo" />
    <add key="textexample2" value="A,B,C,D,E,F,G,H" />
    <!-- notice 3 is missing -->
    <add key="textexample4" value="" />

    <!-- integerexample1 through integerexampleX - 
     examples of integer type processing -->
    <add key="integerexample1" value="100" />
    <add key="integerexample2" value="foo" />
    <add key="integerexample3" value="1.618" />
    <!-- notice 4 is missing -->
    <add key="integerexample5" value="100" />
    <add key="integerexample6" value="" />

  </appSettings>
</configuration>

produces these results:

Boolean examples, using default of 'true'
no default booleanexample1=True
with default booleanexample1=True
no default booleanexample2=False
with default booleanexample2=False
no default booleanexample3=True
with default booleanexample3=True
no default booleanexample4=False
with default booleanexample4=False
no default booleanexample5=False
with default booleanexample5=True
no default booleanexample6=False
with default booleanexample6=True
no default booleanexample7=True
with default booleanexample7=True
no default booleanexample8=False
with default booleanexample8=False
no default booleanexample9=True
with default booleanexample9=True
no default booleanexample10=False
with default booleanexample10=False
no default booleanexample11=False
with default booleanexample11=True
no default booleanexample12=False
with default booleanexample12=True


Text examples, using default of 'useThisIfMissing'
no default textexample1=foo
with default textexample1=foo
no default textexample2=A,B,C,D,E,F,G,H
with default textexample2=A,B,C,D,E,F,G,H
no default textexample3=
with default textexample3=useThisIfMissing
no default textexample4=
with default textexample4=useThisIfMissing


Integer examples, using default of 50
no default integerexample1=100
with default integerexample1=100
no default integerexample2=0
with default integerexample2=50
no default integerexample3=0
with default integerexample3=50
no default integerexample4=0
with default integerexample4=50
no default integerexample5=100
with default integerexample5=100
no default integerexample6=0
with default integerexample6=50

Conclusion

The configuration reader class solution and test harness code provided will enable you to implement this tool in your environment to consistently process your configuration files.

Revision History

  • 2006-04-01 - Original submission.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here