|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
ContentsIntroductionMany 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 InformationFor information to be used by this configuration processor, it must be contained within the <?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 ReaderAfter 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
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
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
ConclusionThe 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
|
|||||||||||||||||||||||||||||||||||||||||||||||