-------------------------
1. 实现目的
我希望在web.config中,配置网站信息,管理员信息,和用户信息(当然这个配置有点不切实际了,这里只是为了演示),所以,我希望按下面的格式做配置。
<siteSetting siteName="遇见未来" siteVersion="1.0" closed="false">
<siteAdmin adminId="1" adminName="guozhijian"/>
<siteUsers>
<siteUser userId="1" userName="zhanglanzhen"/>
<siteUser userId="2" userName="wdy"/>
</siteUsers>
</siteSetting>
这个siteSetting配置节点是一个稍微复杂一点的配置,自己包含有Attributes,同时包含子节点siteAdmin, siteUsers, 而siteUsers又包含多个siteUser子节点。<siteAdmin adminId="1" adminName="guozhijian"/>
<siteUsers>
<siteUser userId="1" userName="zhanglanzhen"/>
<siteUser userId="2" userName="wdy"/>
</siteUsers>
</siteSetting>
接下来我要定义几个类,分别代表各个不同的节点。有一定的规则:
代表根节点,也就是siteSetting节点的类,继承自ConfigurationSection类
代表单一子节点的siteAdmin, siteUser类,继承自ConfigurationElement类
包含多个同名子节点的siteUsers类,继承自ConfigurationElementCollection类
而siteSetting这个配置点需要注册,代码示例如下:
<configSections>
<section name="siteSetting" type="处理程序" />
</configSections>
2、实现过程
第一步:实现单一节点的类,也就是代表siteAdmin和siteUser的类:
1public class SiteAdmin : ConfigurationElement
2{
3 [ConfigurationProperty("adminId", DefaultValue = "1", IsRequired = true)]
4 public int AdminId
5 {
6 get { return Int32.Parse(this["adminId"].ToString()); }
7 set { this["adminId"] = value; }
8 }
9 [ConfigurationProperty("adminName", DefaultValue = "McJeremy", IsRequired = true)]
10 public string AdminName
11 {
12 get { return this["adminName"].ToString(); }
13 set { this["adminName"] = value; }
14 }
15}
16public class SiteUser : ConfigurationElement
17{
18 [ConfigurationProperty("userId", DefaultValue = "1", IsRequired = true)]
19 public int UserId
20 {
21 get { return Int32.Parse(this["userId"].ToString()); }
22 set { this["userId"] = value; }
23 }
24 [ConfigurationProperty("userName", DefaultValue = "McJeremy", IsRequired = true)]
25 public string UserName
26 {
27 get { return this["userName"].ToString(); }
28 set { this["userName"] = value; }
29 }
30}
第二步:实现包含多个同名节点(siteUser)的siteUsers类2{
3 [ConfigurationProperty("adminId", DefaultValue = "1", IsRequired = true)]
4 public int AdminId
5 {
6 get { return Int32.Parse(this["adminId"].ToString()); }
7 set { this["adminId"] = value; }
8 }
9 [ConfigurationProperty("adminName", DefaultValue = "McJeremy", IsRequired = true)]
10 public string AdminName
11 {
12 get { return this["adminName"].ToString(); }
13 set { this["adminName"] = value; }
14 }
15}
16public class SiteUser : ConfigurationElement
17{
18 [ConfigurationProperty("userId", DefaultValue = "1", IsRequired = true)]
19 public int UserId
20 {
21 get { return Int32.Parse(this["userId"].ToString()); }
22 set { this["userId"] = value; }
23 }
24 [ConfigurationProperty("userName", DefaultValue = "McJeremy", IsRequired = true)]
25 public string UserName
26 {
27 get { return this["userName"].ToString(); }
28 set { this["userName"] = value; }
29 }
30}
1public class SiteUsers : ConfigurationElementCollection
2{
3 override protected ConfigurationElement CreateNewElement()
4 {
5 return new SiteUser();
6 }
7 /// <summary>
8 /// 重写该方法以确定SiteUser的键值
9 /// </summary>
10 /// <param name="element"></param>
11 /// <returns>返回UserId,因此,SiteUser的键为UserId</returns>
12 override protected object GetElementKey(ConfigurationElement element)
13 {
14 SiteUser siteUser = element as SiteUser;
15 return siteUser.UserId;
16 }
17 public override ConfigurationElementCollectionType CollectionType
18 {
19 get
20 {
21 return ConfigurationElementCollectionType.BasicMap;
22 }
23 }
24 public void Add(ConfigurationElement element)
25 {
26 BaseAdd(element);
27 }
28 public void Remove(int key) //因为SiteUser的键是int型的,所以参数也要是int型的。
29 {
30 BaseRemove(key);
31 }
32 protected override string ElementName //集合下节点的名称
33 {
34 get
35 {
36 return "siteUser";
37 }
38 }
39}
40
第三步:实现根节点类2{
3 override protected ConfigurationElement CreateNewElement()
4 {
5 return new SiteUser();
6 }
7 /// <summary>
8 /// 重写该方法以确定SiteUser的键值
9 /// </summary>
10 /// <param name="element"></param>
11 /// <returns>返回UserId,因此,SiteUser的键为UserId</returns>
12 override protected object GetElementKey(ConfigurationElement element)
13 {
14 SiteUser siteUser = element as SiteUser;
15 return siteUser.UserId;
16 }
17 public override ConfigurationElementCollectionType CollectionType
18 {
19 get
20 {
21 return ConfigurationElementCollectionType.BasicMap;
22 }
23 }
24 public void Add(ConfigurationElement element)
25 {
26 BaseAdd(element);
27 }
28 public void Remove(int key) //因为SiteUser的键是int型的,所以参数也要是int型的。
29 {
30 BaseRemove(key);
31 }
32 protected override string ElementName //集合下节点的名称
33 {
34 get
35 {
36 return "siteUser";
37 }
38 }
39}
40
1public class SiteSetting : ConfigurationSection
2{
3 [ConfigurationProperty("siteName", DefaultValue = "McJeremy的测试",IsRequired=true)]
4 public string SiteName
5 {
6 get { return this["siteName"].ToString(); }
7 set { this["siteName"] = value; }
8 }
9 [ConfigurationProperty("siteVersion", DefaultValue = "1.0")]
10 public string SiteVersion
11 {
12 get { return this["siteVersion"].ToString(); }
13 set { this["siteVersion"] = value; }
14 }
15 [ConfigurationProperty("closed", DefaultValue = false, IsRequired = true)]
16 public bool Closed
17 {
18 get { return (bool)this["close"]; }
19 set { this["closed"] = value; }
20 }
21 [ConfigurationProperty("siteAdmin")]
22 public SiteAdmin SiteAdmin
23 {
24 get { return this["siteAdmin"] as SiteAdmin; }
25 set { this["siteAdmin"] = value; }
26 }
27 [ConfigurationProperty("siteUsers")]
28 public SiteUsers SiteUsers
29 {
30 get { return this["siteUsers"] as SiteUsers; }
31 }
32}
完成上面的步骤后,就可以在web.config里注册相应的节了。如2{
3 [ConfigurationProperty("siteName", DefaultValue = "McJeremy的测试",IsRequired=true)]
4 public string SiteName
5 {
6 get { return this["siteName"].ToString(); }
7 set { this["siteName"] = value; }
8 }
9 [ConfigurationProperty("siteVersion", DefaultValue = "1.0")]
10 public string SiteVersion
11 {
12 get { return this["siteVersion"].ToString(); }
13 set { this["siteVersion"] = value; }
14 }
15 [ConfigurationProperty("closed", DefaultValue = false, IsRequired = true)]
16 public bool Closed
17 {
18 get { return (bool)this["close"]; }
19 set { this["closed"] = value; }
20 }
21 [ConfigurationProperty("siteAdmin")]
22 public SiteAdmin SiteAdmin
23 {
24 get { return this["siteAdmin"] as SiteAdmin; }
25 set { this["siteAdmin"] = value; }
26 }
27 [ConfigurationProperty("siteUsers")]
28 public SiteUsers SiteUsers
29 {
30 get { return this["siteUsers"] as SiteUsers; }
31 }
32}
1<configSections>
2 <section name="siteSetting" type="SiteSetting"/>
3 </configSections>
4 <siteSetting siteName="McJeremy的测试" siteVersion="1.0" closed="false">
5 <siteAdmin adminId="11" adminName="McJeremy" />
6 <siteUsers>
7 <siteUser userId="1" userName="McJeremy" />
8 <siteUser userId="2" userName="McJeremy3" />
9 <siteUser userId="5" userName="McJeremy4" />
10 </siteUsers>
11 </siteSetting>
为siteUsers节点添加一个子节点:2 <section name="siteSetting" type="SiteSetting"/>
3 </configSections>
4 <siteSetting siteName="McJeremy的测试" siteVersion="1.0" closed="false">
5 <siteAdmin adminId="11" adminName="McJeremy" />
6 <siteUsers>
7 <siteUser userId="1" userName="McJeremy" />
8 <siteUser userId="2" userName="McJeremy3" />
9 <siteUser userId="5" userName="McJeremy4" />
10 </siteUsers>
11 </siteSetting>
1 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
2 SiteSetting myset = config.GetSection("siteSetting") as SiteSetting;
3 SiteUser user = new SiteUser();
4 user.UserName = "McJeremy4";
5 user.UserId = 5;
6 myset.SiteUsers.Add(user);
7 // myset.SiteUsers.Remove(6); 删除子节点
8 config.Save();
9 addMySettingsResult.Text = "添加成功";
读取siteSettings下的设置:2 SiteSetting myset = config.GetSection("siteSetting") as SiteSetting;
3 SiteUser user = new SiteUser();
4 user.UserName = "McJeremy4";
5 user.UserId = 5;
6 myset.SiteUsers.Add(user);
7 // myset.SiteUsers.Remove(6); 删除子节点
8 config.Save();
9 addMySettingsResult.Text = "添加成功";
1 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
2 SiteSetting myset = config.GetSection("siteSetting") as SiteSetting;
3 string msgs = "";
4 msgs += "<br />站点信息是:" + myset.SiteName + ",站点版本:" + myset.SiteVersion ;
5 SiteAdmin admin = myset.SiteAdmin;
6 msgs += "<br />管理员名:" + admin.AdminName;
7 msgs += "<br />用户信息:";
8 SiteUsers users = myset.SiteUsers;
9 foreach(SiteUser user in users)
10 {
11 msgs+=" <br />用户名:"+user.UserName+",用户ID:"+user.UserId.ToString();
12 }
13 readMySettingResult.Text = msgs;
2 SiteSetting myset = config.GetSection("siteSetting") as SiteSetting;
3 string msgs = "";
4 msgs += "<br />站点信息是:" + myset.SiteName + ",站点版本:" + myset.SiteVersion ;
5 SiteAdmin admin = myset.SiteAdmin;
6 msgs += "<br />管理员名:" + admin.AdminName;
7 msgs += "<br />用户信息:";
8 SiteUsers users = myset.SiteUsers;
9 foreach(SiteUser user in users)
10 {
11 msgs+=" <br />用户名:"+user.UserName+",用户ID:"+user.UserId.ToString();
12 }
13 readMySettingResult.Text = msgs;