配置文件读取器
轻松读取和使用 XML 配置文件。
目录
引言
许多应用程序需要使用持久化数据存储的信息,例如 XML 配置文件,才能成功运行或处理。有几种方法可以获取这些信息,包括使用 .NET 提供的类和方法,或者创建自己的类。本文将演示如何使用一个配置文件读取器类,该类可以轻松处理当今普遍存在的 XML 配置文件。该读取器类可与各种类型的程序集(控制台应用程序、Web 应用程序、服务等)配合使用,并提供了一种处理配置文件的统一方法。
提供的类解决方案和控制台测试工具是使用 Visual Studio 2005 构建的。可以使用解决方案中的 .cs 文件创建 VS2003 版本。下载的存档中还包含一个帮助文件(CHM)。
建议为读取器类建立强名称,以便它可以驻留在 GAC 中。
示例配置文件信息
为了让信息被此配置处理器使用,它必须包含在文件的 <configuration>
区域的 <appSettings>
部分中。您可以在此部分中包含任意数量的键/值对。实际的配置文件独立于运行的程序集,这使得为不同场景和/或同一个应用程序拥有多个配置文件变得更加容易。
<?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>
使用读取器
在您的环境中成功构建读取器类后,您可以通过将读取器程序集的引用添加到您的项目中来开始使用它。然后,通过提供您希望处理的配置文件的文件名来实例化它。如果您有多个配置文件需要处理,请创建多个读取器实例。
ConfigurationFileReader.ParmProcessor WeeklyParms = new
ConfigurationFileReader.ParmProcessor(@"d:\appcon" +
@"fig\myapp1\processweekly.config");
ConfigurationFileReader.ParmProcessor MonthlyParms = new
ConfigurationFileReader.ParmProcessor(@"d:\appconfig" +
@"\myapp1\processmonthly.config");
实例化后,读取器类会解析配置文件,您就可以使用下面列出的各种方法之一来检索值。
以下属性和方法使您能够检索有关文件解析结果以及键/值数据的信息。
属性
IsError
- 一个布尔值,表示文件解析过程中是否发生错误。Error
- 一个字符串消息,显示发生的错误(如果有)。如果未发生错误,则为空。Count
- 一个整数,表示可供检索的总键/值对数量。Version
- 配置读取器的程序集版本。KeyList
- 一个ArrayList
,包含所有已解析的键。KeyValuePairs
- 一个NameValueCollection
,包含从文件中解析的所有键/值对。ListAllKeyValuePairs
- 一个字符串,包含所有键及其关联值。此格式适合在控制台显示。
使用上面的配置文件和此测试工具代码
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);
}
产生这些结果
检索方法
GetParameter(which_parameter_key_to_get)
- 检索指定键的字符串值。GetParameter(which_parameter_key_to_get, default_returned_value)
- 检索指定键的字符串值。如果指定的键丢失或其值为空,则返回default_returned_value
。GetParameterInt(which_parameter_key_to_get)
- 检索指定键的整数值。如果指定的键丢失、为空或不是整数,此方法将返回零。GetParameterInt(which_parameter_key_to_get, default_returned_integer)
- 检索指定键的整数值。如果指定的键丢失、为空或不是整数,此方法将返回default_returned_integer
。GetParameterBool(which_parameter_key_to_get)
- 检索指定键的布尔值。如果指定的键存在且其值为 'true'、1、'on' 或 'yes',则返回true
。如果指定的键存在且其值为 'false'、0、'off' 或 'no',则返回false
。如果指定的键丢失或为空,此方法将返回false
。GetParameterBool(which_parameter_key_to_get, default_returned_boolean)
- 检索指定键的布尔值。如果指定的键存在且其值为 'true'、1、'on' 或 'yes',则返回true
。如果指定的键存在且其值为 'false'、0、'off' 或 'no',则返回false
。如果指定的键丢失或为空,此方法将返回布尔值default_returned_boolean
。
使用此配置文件
<?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>
产生这些结果
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
结论
提供的配置读取器类解决方案和测试工具代码将使您能够在您的环境中实现此工具,以一致地处理您的配置文件。
修订历史
- 2006-04-01 - 原始提交。