zoukankan      html  css  js  c++  java
  • 自定义配置节点(web.config)

    1.web.config配置如下

    <configuration>
      <configSections>
        <sectionGroup name="testGroup">
          <section name="root" type="CustomSectionInConfigDemo.RootConfiguration, CustomSectionInConfigDemo"/>
        </sectionGroup>
      </configSections>
      <testGroup>
        <root rootName="Test" closed="false">
          <siteAdmin adminId="1" adminName="admin A" />
          <siteUsers>
            <siteUser userId="1" userName="user A" />
            <siteUser userId="2" userName="user B" />
          </siteUsers>
        </root>
      </testGroup>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
    </configuration>

    2.与上面web.config中自定义结点所对应的类定义如下:

    /// <summary>
        /// 规则1:根结点(siteSetting结点)继承ConfigurationSection
        /// 规则2:代表单一子节点的siteAdmin类, siteUser类,继承自ConfigurationElement类 
        /// 规则3:包含多个同名子节点的siteUsers类,继承自ConfigurationElementCollection类 
        /// </summary>
        //对应root结点
    public class RootConfiguration : ConfigurationSection { //根结点本身的属性1 [ConfigurationProperty("rootName")] public String RootName { get { return this["rootName"] as String; } set { this["rootName"] = value; } } //根结点本身的属性2 [ConfigurationProperty("closed", IsRequired = true)] public Boolean Closed { get { return (Boolean)this["closed"]; } set { this["closed"] = value; } } //根结点中的子结点1 [ConfigurationProperty("siteAdmin")] public SiteAdmin SiteAdmin { get { return this["siteAdmin"] as SiteAdmin; } set { this["siteAdmin"] = value; } } //根结点中的子结点2[该结点是一个容器,包含多个结点] [ConfigurationProperty("siteUsers")] public SiteUsers SiteUsers { get { return this["siteUsers"] as SiteUsers; } set { this["siteUsers"] = value; } } } //对应siteAdmin结点
    public class SiteAdmin : ConfigurationElement { [ConfigurationProperty("adminId", IsRequired = true)] public Int32 AdminId { get { return (Int32)this["adminId"]; } set { this["adminId"] = value; } } [ConfigurationProperty("adminName")] public String AdminName { get { return this["adminName"] as String; } set { this["adminName"] = value; } } } //对应siteUser同名结点
    public class SiteUser : ConfigurationElement { [ConfigurationProperty("userId", IsRequired = true)] public Int32 UserId { get { return (Int32)this["userId"]; } set { this["userId"] = value; } } [ConfigurationProperty("userName")] public String UserName { get { return this["userName"] as String; } set { this["userName"] = value; } } } //子结点2[容器结点] public class SiteUsers : ConfigurationElementCollection { //需重写以下四个方法 protected override ConfigurationElement CreateNewElement() { //容器结点下的同名结点,所对应的元素名为"siteUser" return new SiteUser(); } protected override String ElementName { get { return "siteUser"; } } protected override object GetElementKey(ConfigurationElement element) { SiteUser siteUser = element as SiteUser; return siteUser.UserId; } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } }

    3.读取web.config中自定义结点代码如下:

                RootConfiguration siteSetting = (RootConfiguration)ConfigurationManager.GetSection("testGroup/root");
    
                Response.Write(siteSetting.RootName + "," + siteSetting.Closed.ToString());
                Response.Write("<br/>");
    
                Response.Write(siteSetting.SiteAdmin.AdminId.ToString() + "," + siteSetting.SiteAdmin.AdminName);
                Response.Write("<br/>");
    
                foreach (SiteUser u in siteSetting.SiteUsers)
                {
                    Response.Write(u.UserId.ToString() + "," + u.UserName);
                    Response.Write("<br/>");
                }
  • 相关阅读:
    开发者看过来,哪个移动平台好赚钱?
    EGit下配置Github项目
    用户接口(UI)设计的 20 条原则
    要想工作效率高,我们到底需要多少睡眠?
    Android 读取<metadata>元素的数据
    Android实现推送方式解决方案
    余晟:做个懂产品的程序员
    Gson简要使用笔记
    编程从业五年的十四条经验,句句朴实
    程序员不是包身工
  • 原文地址:https://www.cnblogs.com/notebook2011/p/3112606.html
Copyright © 2011-2022 走看看