原文地址:通过ConfigurationSection来轻松地加载配置文件
最近写了一段自定义的ConfigurationSection继承类,通过该class可以轻松得定义和读取配置文件信息,
注意这里使用的是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
>
注意order和lineItem节点都是允许重复出现的
以下是继承自ConfigurationSection的自定义配置节点类:
1
public
class
OrdersSection : ConfigurationSection
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
[ConfigurationProperty("companyID", IsRequired = true)]
4
public string CompanyID
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
get
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
return (string)base["companyID"];
9
}
10
set
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
base["companyID"] = value;
13
}
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
[ConfigurationProperty("", IsDefaultCollection = true)]
17
public OrderElementCollection Orders
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
get
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
return (OrderElementCollection)base[""];
22
}
23
}
24
}
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
public
class
OrderElementCollection : ConfigurationElementCollection
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
28
protected override ConfigurationElement CreateNewElement()
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
return new OrderElement();
31
}
32
protected override object GetElementKey(ConfigurationElement element)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
return ((OrderElement)element).Number;
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
public override ConfigurationElementCollectionType CollectionType
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
get
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
return ConfigurationElementCollectionType.BasicMap;
42
}
43
}
44
protected override string ElementName
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
get
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
return "order";
49
}
50
}
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
public OrderElement this[int index]
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
get
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
return (OrderElement)BaseGet(index);
57
}
58
set
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
if (BaseGet(index) != null)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
BaseRemoveAt(index);
63
}
64
BaseAdd(index, value);
65
}
66
}
67
}
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
public
class
OrderElement : ConfigurationElement
70
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
71
[ConfigurationProperty("number", IsRequired = true)]
72
public string Number
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
get
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
return (string)base["number"];
77
}
78
set
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
base["number"] = value;
81
}
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
[ConfigurationProperty("amount", IsRequired = true)]
85
public double Amount
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
get
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
return (double)base["amount"];
90
}
91
set
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
base["amount"] = value;
94
}
95
}
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
[ConfigurationProperty("lineItems", IsDefaultCollection = true)]
98
public LineItemElementCollection LineItems
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
get
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102
return (LineItemElementCollection)base["lineItems"];
103
}
104
}
105
}
106
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
107
public
class
LineItemElementCollection : ConfigurationElementCollection
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
109
[ConfigurationProperty("warehouseNumber", IsRequired = true)]
110
public string WarehouseNumber
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
get
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
return (string)base["warehouseNumber"];
115
}
116
set
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
base["warehouseNumber"] = value;
119
}
120
}
121![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
protected override ConfigurationElement CreateNewElement()
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
return new LineItemElement();
125
}
126
protected override object GetElementKey(ConfigurationElement element)
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
return ( (LineItemElement)element ).Number;
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
public override ConfigurationElementCollectionType CollectionType
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
get
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
return ConfigurationElementCollectionType.BasicMap;
136
}
137
}
138
protected override string ElementName
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140
get
141![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142
return "lineItem";
143
}
144
}
145![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
146
public LineItemElement this[int index]
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
get
149![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
return (LineItemElement)BaseGet(index);
151
}
152
set
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
154
if (BaseGet(index) != null)
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
BaseRemoveAt(index);
157
}
158
BaseAdd(index, value);
159
}
160
}
161
}
162
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
163
public
class
LineItemElement : ConfigurationElement
164
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
165
[ConfigurationProperty("number", IsKey=true, IsRequired = true)]
166
public string Number
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
get
169![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
return (string)base["number"];
171
}
172
set
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
base["number"] = value;
175
}
176
}
177![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
178
[ConfigurationProperty("description", IsRequired = true)]
179
public string Description
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
get
182![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
return (string)base["description"];
184
}
185
set
186![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
187
base["description"] = value;
188
}
189
}
190
}
原文地址:使用 ConfigurationSection 创建自定义配置节
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来实现自定义配置节,在1.0中当然也可以通过IconfigurationSectionHandler 接口创建自定义配置节!这里我们主要学一下通过ConfigurationSection类来实现简单的配置处理程序.
先看一下在web.config文件中的配置情况,在这里有两个元素,第一个mysection,有两个属性user,password,第二个也有两个属性element1,和element2。配置比较简单。
<!--
//
-->
<
configSections
>
<
sectionGroup
name
="mygroup"
>
<
section
name
="mysection"
type
="ConfigSection"
allowDefinition
="Everywhere"
allowLocation
="true"
/>
</
sectionGroup
>
</
configSections
>
<!--
//
-->
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
mygroup
>
<
mysection
user
="用户"
password
="密码"
>
<
element
element1
="属性1"
element2
="属性2"
></
element
>
</
mysection
>
</
mygroup
>
理解配置文件结构后,我们就需要用继承自System.Configuration.ConfigurationSection的基类来实现简单的配置类ConfigSection,在2.0中,我们只需要这一个类就能实现完成配置,下面请看代码:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
///
<summary>
///
ConfigSection 的摘要说明
///
</summary>
public
class
ConfigSection:ConfigurationSection
{
public
ConfigSection()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
[ConfigurationProperty(
"
user
"
,DefaultValue
=
"
yanghong
"
,IsRequired
=
true
)]
public
string
User
{
get
{
return
(
string
)
this
[
"
user
"
]; }
set
{
this
[
"
user
"
]
=
value; }
}
[ConfigurationProperty(
"
password
"
,DefaultValue
=
"
password
"
,IsRequired
=
true
)]
public
string
PassWord
{
get
{
return
(
string
)
this
[
"
password
"
]; }
set
{
this
[
"
password
"
]
=
value; }
}
[ConfigurationProperty(
"
element
"
)]
public
elementinfo Element
{
get
{
return
(elementinfo)
this
[
"
element
"
]; }
set
{
this
[
"
element
"
]
=
value; }
}
}
public
class
elementinfo : ConfigurationElement
{
public
elementinfo() { }
[ConfigurationProperty(
"
element1
"
, DefaultValue
=
"
element1
"
, IsRequired
=
true
)]
public
string
Element1
{
get
{
return
(
string
)
this
[
"
element1
"
]; }
}
[ConfigurationProperty(
"
element2
"
,DefaultValue
=
"
element2
"
,IsRequired
=
true
)]
public
string
Element2
{
get
{
return
(
string
)
this
[
"
element2
"
]; }
}
}
通过下面的代码就可以获得在配置文件中设置的值了
ConfigSection config
=
(ConfigSection)ConfigurationManager.GetSection(
"
mygroup/mysection
"
);
Response.Write(
"
用户名:
"
+
config.User.ToString()
+
"
密码:
"
+
config.PassWord.ToString()
+
"
元素属性:
"
+
config.Element.Element1.ToString()
+
config.Element.Element2.ToString());