首先在配置文件web.config或者app.config中定义配置段:
<configSections> <section name="Country" type="Demo.CountrySection, Demo"/> </configSections>
在configuration配置节中配置Country:
<configuration> <Country CountryID="0001"> <Province ProID="100001"> <City Cid="01"> <Contry Id="001" ShortName="BJ"/> </City> </Province> <Province ProID="100002"> <City Cid="02"> <Contry Id="02" ShortName="SH"/> <Contry Id="03" ShortName="GD"/> </City> </Province> </Country> </configuration>
定义CountrySection类:

1 public class CountrySection : ConfigurationSection 2 { 3 [ConfigurationProperty("CountryID", IsRequired = true)] 4 public string CompanyID 5 { 6 get 7 { 8 return (string)base["CountryID"]; 9 } 10 set 11 { 12 base["CountryID"] = value; 13 } 14 } 15 16 [ConfigurationProperty("", IsDefaultCollection = true)] 17 public ProvinceElementCollection Province 18 { 19 get 20 { 21 return (ProvinceElementCollection)base[""]; 22 } 23 } 24 } 25 public class ProvinceElementCollection : ConfigurationElementCollection 26 { 27 protected override ConfigurationElement CreateNewElement() 28 { 29 return new ProvinceElement(); 30 } 31 32 protected override object GetElementKey(ConfigurationElement element) 33 { 34 return ((ProvinceElement)element).ProID; 35 } 36 37 public override ConfigurationElementCollectionType CollectionType 38 { 39 get 40 { 41 return ConfigurationElementCollectionType.BasicMap; 42 } 43 } 44 45 protected override string ElementName 46 { 47 get 48 { 49 return "Province"; 50 } 51 } 52 53 public ProvinceElement this[int index] 54 { 55 get 56 { 57 return (ProvinceElement)BaseGet(index); 58 } 59 set 60 { 61 if (BaseGet(index) != null) 62 { 63 BaseRemoveAt(index); 64 } 65 BaseAdd(index, value); 66 } 67 } 68 } 69 public class ProvinceElement : ConfigurationElement 70 { 71 [ConfigurationProperty("ProID", IsRequired = true)] 72 public string ProID 73 { 74 get 75 { 76 return (string)base["ProID"]; 77 } 78 set 79 { 80 base["ProID"] = value; 81 } 82 } 83 84 [ConfigurationProperty("City", IsDefaultCollection = true)] 85 public CityElementCollection City 86 { 87 get 88 { 89 return (CityElementCollection)base["City"]; 90 } 91 } 92 } 93 public class CityElementCollection : ConfigurationElementCollection 94 { 95 [ConfigurationProperty("Cid", IsRequired = true)] 96 public string CID 97 { 98 get 99 { 100 return (string)base["Cid"]; 101 } 102 set 103 { 104 base["Cid"] = value; 105 } 106 } 107 protected override ConfigurationElement CreateNewElement() 108 { 109 return new ContryElement(); 110 } 111 112 protected override object GetElementKey(ConfigurationElement element) 113 { 114 return ((ContryElement)element).Id; 115 } 116 117 public override ConfigurationElementCollectionType CollectionType 118 { 119 get 120 { 121 return ConfigurationElementCollectionType.BasicMap; 122 } 123 } 124 125 protected override string ElementName 126 { 127 get 128 { 129 return "Contry"; 130 } 131 } 132 133 public ContryElement this[int index] 134 { 135 get 136 { 137 return (ContryElement)BaseGet(index); 138 } 139 set 140 { 141 if (BaseGet(index) != null) 142 { 143 BaseRemoveAt(index); 144 } 145 BaseAdd(index, value); 146 } 147 } 148 } 149 public class ContryElement : ConfigurationElement 150 { 151 [ConfigurationProperty("Id", IsRequired = true)] 152 public string Id 153 { 154 get 155 { 156 return (string)base["Id"]; 157 } 158 set 159 { 160 base["Id"] = value; 161 } 162 } 163 164 [ConfigurationProperty("ShortName", IsRequired = true)] 165 public string ShortName 166 { 167 get 168 { 169 return (string)base["ShortName"]; 170 } 171 set 172 { 173 base["ShortName"] = value; 174 } 175 } 176 }
如果要配置组,需要定义如下:
<configSections> <sectionGroup name="Countries"> <section name="China" type="Demo.ChinaSection, Demo" allowDefinition="Everywhere" allowLocation="true"/> </sectionGroup> </configSections>
在configuration配置节中配置China:
<configuration> <Countries> <China Number="1"> <Province ShortName="JS"></Province> </China> </Countries> </configuration>
定义类如下:

1 public class ChinaSection : ConfigurationSection 2 { 3 [ConfigurationProperty("Number", DefaultValue = "", IsRequired = true)] 4 public string Number 5 { 6 get { return (string)this["Number"]; } 7 set { this["Number"] = value; } 8 } 9 10 [ConfigurationProperty("Province")] 11 public Province Province 12 { 13 get { return (Province)this["Province"]; } 14 set { this["Province"] = value; } 15 } 16 } 17 public class Province : ConfigurationElement 18 { 19 [ConfigurationProperty("ShortName", DefaultValue = "", IsRequired = true)] 20 public string ShortName 21 { 22 get { return (string)this["ShortName"]; } 23 } 24 }
调用:
CountrySection section1 = ConfigurationManager.GetSection("Country") as CountrySection; ChinaSection sec = (ChinaSection)ConfigurationManager.GetSection("Countries/China");
本文只是简要的介绍了如何去做相应的映射,更具体的内容需要依据项目实际需要而定。