方法一、


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="color" type="System.Configuration.NameValueSectionHandler" />
<section name="message" type="System.Configuration.DictionarySectionHandler"/>
<section name="name" type="System.Configuration.SingleTagSectionHandler"/>
</configSections>
<color>
<add key="red" value="#ff0000"/>
<add key="green" value="#00ff00"/>
<add key="blue" value="#0000ff"/>
</color>
<message>
<add key="welcome" value="你好,欢迎"/>
</message>
<name firstName="陈" lastName="明明"/>
</configuration>
<configuration>
<configSections>
<section name="color" type="System.Configuration.NameValueSectionHandler" />
<section name="message" type="System.Configuration.DictionarySectionHandler"/>
<section name="name" type="System.Configuration.SingleTagSectionHandler"/>
</configSections>
<color>
<add key="red" value="#ff0000"/>
<add key="green" value="#00ff00"/>
<add key="blue" value="#0000ff"/>
</color>
<message>
<add key="welcome" value="你好,欢迎"/>
</message>
<name firstName="陈" lastName="明明"/>
</configuration>
对于自定义的配置节,应该先在 <configSections>中声明要配置的节与类型


public static void Main(string[] args)
{
//get color
NameValueCollection color = (NameValueCollection)ConfigurationManager.GetSection("color");
foreach (String str in color.AllKeys) {
Console.WriteLine(str+":"+color[str]);
}
//get message
IDictionary message = (IDictionary)ConfigurationManager.GetSection("message");
foreach (String str in message.Keys) {
Console.WriteLine(str+":"+message[str]);
}
// get name
IDictionary name = (IDictionary)ConfigurationManager.GetSection("name");
foreach (String str in name.Keys)
{
Console.WriteLine(str + ":" + name[str]);
}
//Console.WriteLine(name["firstName"]);
Console.Read();
}
{
//get color
NameValueCollection color = (NameValueCollection)ConfigurationManager.GetSection("color");
foreach (String str in color.AllKeys) {
Console.WriteLine(str+":"+color[str]);
}
//get message
IDictionary message = (IDictionary)ConfigurationManager.GetSection("message");
foreach (String str in message.Keys) {
Console.WriteLine(str+":"+message[str]);
}
// get name
IDictionary name = (IDictionary)ConfigurationManager.GetSection("name");
foreach (String str in name.Keys)
{
Console.WriteLine(str + ":" + name[str]);
}
//Console.WriteLine(name["firstName"]);
Console.Read();
}
方法二、通过ConfigurationSection【配置域】、ConfigurationElement【节点】、ConfigurationElementCollection【节点列表】实现自定义节


<configuration> <configSections> <section name="orders" type="ConsoleApplication4.OrdersSection, ConsoleApplication4"/> </configSections> <orders companyID="2001"> <order number="100001" amount="222.22"> </order> <order number="300001" amount="33.33"> </order> </orders> </configuration>
下面我们要定义相应的实体对象,该实体对象中会有一个子对象【用来表示节点列表信息】(ConfigurationElementCollection)


namespace ConsoleApplication4 { public class OrdersSection : ConfigurationSection { [ConfigurationProperty("companyID", IsRequired = true)] public string CompanyID { get { return (string)base["companyID"]; } set { base["companyID"] = value; } } [ConfigurationProperty("", IsDefaultCollection = true)] public OrderElementCollection Orders { get { return (OrderElementCollection)base[""]; } } } public class OrderElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new OrderElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((OrderElement)element).Number; } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } protected override string ElementName { get { return "order"; } } public OrderElement this[int index] { get { return (OrderElement)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } } public class OrderElement : ConfigurationElement { [ConfigurationProperty("number", IsRequired = true)] public string Number { get { return (string)base["number"]; } set { base["number"] = value; } } [ConfigurationProperty("amount", IsRequired = true)] public double Amount { get { return (double)base["amount"]; } set { base["amount"] = value; } } } }


OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders"); Console.WriteLine("CompanyId={0}",config.CompanyID); for (int i = 0; i < config.Orders.Count; i++) { Console.WriteLine("Amount={0},Number={1}", config.Orders[i].Amount, config.Orders[i].Number); }