注意这里使用的是c# 2.0来实现的,相比1.1必须通过实现IConfigurationSectionHandler接口来自定义配置节点类方便多了
不论是web.config还是app.config,都可以使用ConfigurationManager类加载配置文件中自定义的节点内容。
以下是配置文件的层次结构:
1
<?xml version="1.0" encoding="utf-8" ?>
2
<configuration>
3
<configSections>
4
<section name="orders" type="ConsoleTest.OrdersSection, ConsoleTest"/>
5
</configSections>
6
<orders companyID="2001">
7
<order number="100001" amount="222.22">
8
<lineItems warehouseNumber="02">
9
<lineItem number="00-000-001" description="wii"/>
10
</lineItems>
11
</order>
12
<order number="300001" amount="33.33">
13
<lineItems warehouseNumber="99">
14
<lineItem number="00-000-001" description="xbox 360"/>
15
<lineItem number="00-000-003" description="playstation 3"/>
16
</lineItems>
17
</order>
18
</orders>
19
</configuration>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
注意order和lineItem节点都是允许重复出现的
以下是继承自ConfigurationSection的自定义配置节点类:
1
public class OrdersSection : ConfigurationSection
2
}
2