通过ConfigurationSection自定义配置文件中的节
自定义配置文件中的节点,如下代码,在<configuration>中自定义一个节点"mySection"。
1
<?xml version="1.0"?>2
<configuration>3
<configSections>4
<section name="mySection" type="MyTest.PersonInfo"/>5
<sectionGroup>6
内容省略。。。7
</sectionGroup>8
</configSections>9

10
<mySection name="龙" sex="男" age="23">11
<contact email="longe_mail@163.com" address="厦门思明" mobile="15959595959"></contact>12
<education school="高中" fromTime="2001" endTime="2004" number="1"></education>13
<education school="大学" fromTime="2004" endTime="2009" number="2"></education>14
</mySection>15
<appSettings/>16
<connectionStrings>17
内容省略。。。18
</connectionStrings>19
<system.web>20
内容省略。。。21
</system.web>22
</configuration>23

创建类库项目(添加引用System.Configuration),也可以在App_Code中创建。
1
using System;2
using System.Configuration;3

4
namespace MyTest5


{6
// 继承ConfigurationSection7
public class PersonInfo:ConfigurationSection8

{9
// 通过特性配置元素属性10
// "name":属性名。11
// IsRequired = false:属性不是必须的。12
// 还可以配置其他的特性。13
[ConfigurationProperty("name", IsRequired = false)]14
public string Name15

{16

get
{ return (string)base["name"]; }17

set
{ base["name"] = value; }18
}19

20
[ConfigurationProperty("sex", IsRequired = false)]21
public string Sex22

{23

get
{ return (string)base["sex"]; }24

set
{ base["sex"] = value; }25
}26

27
[ConfigurationProperty("age",IsRequired = false)]28
public int Age29

{30

get
{ return (int)base["age"]; }31

set
{ base["age"] = value; }32
}33

34
[ConfigurationProperty("contact", IsRequired = false)]35
public Contact ContactInfo36

{37

get
{ return (Contact)base["contact"]; }38

set
{ base["contact"] = value; }39
}40

41
// MSDN对IsDefaultCollection的解释:获取或设置一个值,指示此属性集合是否为经过修饰的配置属性的默认属性集合。42
// "name"不指定,在EducationCollection中通过重写ElementName属性来指定。43
[ConfigurationProperty("",IsDefaultCollection=true)]44
public EducationCollection Educations // 类型是EducationCollection(继承自ConfigurationElementCollection的一个类)45

{46

get
{ return (EducationCollection)base[""]; }47
}48
}49

50
// 继承ConfigurationElement,创建子元素,只能是单个节点,多个要用集合。51
public class Contact : ConfigurationElement52

{53
[ConfigurationProperty("address", IsRequired = false)]54
public string Address55

{56

get
{ return (string)base["address"]; }57

set
{ base["address"] = value; }58
}59

60
[ConfigurationProperty("mobile", IsRequired = false)]61
[StringValidator(MaxLength = 11)]62
public string Mobile63

{64

get
{ return (string)base["mobile"]; }65

set
{ base["mobile"] = value; }66
}67

68
[ConfigurationProperty("email", IsRequired = false)]69
public string Email70

{71

get
{ return (string)base["email"]; }72

set
{ base["email"] = value; }73
}74

75
}76

77
// 继承ConfigurationElementCollection,该节点可以是多个。78
public class EducationCollection:ConfigurationElementCollection79

{80
protected override ConfigurationElement CreateNewElement()81

{82
return new EducationElement();83
}84

85
protected override object GetElementKey(ConfigurationElement element)86

{87
return ((EducationElement)element).Number;88
}89

90
public override ConfigurationElementCollectionType CollectionType91

{92
get93

{94
return ConfigurationElementCollectionType.BasicMap;95
}96
}97

98
protected override string ElementName // 多节点的节点名99

{100
get101

{102
return "education";103
}104
}105

106
public EducationElement this[int index] // 索引获取节点信息107

{108

get
{ return (EducationElement)BaseGet(index); }109

set
{110
if (BaseGet(index) != null)111

{ 112
BaseRemoveAt(index);113
}114
else115

{116
BaseAdd(index, value);117
}118
}119
}120
}121

122

123
public class EducationElement : ConfigurationElement124

{125
[ConfigurationProperty("number",IsKey=true, IsRequired = false)]126
public string Number127

{128

get
{ return (string)base["number"]; }129

set
{ base["number"] = value; }130
}131

132
[ConfigurationProperty("school", IsRequired = false)]133
public string School134

{135

get
{ return (string)base["school"]; }136

set
{ base["school"] = value; }137
}138

139
[ConfigurationProperty("fromTime", IsRequired = false)]140
public string FromTime141

{142

get
{ return (string)base["fromTime"]; }143

set
{ base["fromTime"] = value; }144
}145

146
[ConfigurationProperty("endTime", IsRequired = false)]147
public string EndTime148

{149

get
{ return (string)base["endTime"]; }150

set
{ base["endTime"] = value; }151
}152
}153
}154

测试自定义的节点是否有效
1
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");2

3
PersonInfo personInfo = (PersonInfo)config.GetSection("mySection");4

5
StringBuilder infoStr = new StringBuilder();6

7
if (personInfo != null)8

{9
infoStr.Append("父节点属性name的值为:");10
infoStr.Append(personInfo.Name);11
infoStr.Append("<br/>子节点contact的email属性值为:");12
infoStr.Append(personInfo.ContactInfo.Email);13
infoStr.Append("<br/>子节点第一个education的school属性值为:");14
infoStr.Append(personInfo.Educations[0].School); // 多节点通过索引获取15

16
Response.Write(infoStr.ToString());17
}18
else19

{20
Response.Write("Undefined!");21
}
测试结果

该注意的:
貌似一个节点的子节点只能有一种类型的集合,例如这个web.config的自定义节点中,education被配置成了可以设置多个节点(ConfigurationElementCollection),在该节点的同级节点中,其他节点就不能配置为多个节点了。
继承ConfigurationElementCollection的类要重写2个方法和2个属性,分别是
ConfigurationElement CreateNewElement()、
object GetElementKey(ConfigurationElement element)
ConfigurationElementCollectionType CollectionType、
string ElementName

浙公网安备 33010602011771号