ConfigurationSection类主要是方便我们用于扩展自定义webcongfig中的节点信息。我们可以方便的通过以下方式获取【自定义节点对象】

【你自定义的对象】 config = (【你自定义的对象】)ConfigurationManager.GetSection("【你自定义的节点名称,如果是sectiongroup的话,请使用XPATH方式】");

使用自定义节点,可能会涉及到这几个对象的使用:ConfigurationSection【配置域】、ConfigurationElement【节点】、ConfigurationElementCollection【节点列表】

用法一: 配置如下webconfig自定义信息,注意order和lineItem节点都是允许重复出现的

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>

下面我们要定义相应的实体对象,该实体对象中会有一个子对象【用来表示节点列表信息】(ConfigurationElementCollection)

public class OrdersSection : ConfigurationSection
    {
        [ConfigurationProperty("companyID", IsRequired = true)]
        public string CompanyID
        {
            get
            {
                return (string)base["companyID"];
            }
            set
            {
                base["companyID"] = value;
            }
        }

[ConfigurationProperty("", IsDefaultCollection = true)]
        public OrderElementCollection Orders
        {
            get
            {
                return (OrderElementCollection)base[""];
            }
        }
    }

接下来我们在看看节点列表对象的定义,其中会包含一个子对象【ConfigurationElementCollection】

public class OrderElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new OrderElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((OrderElement)element).Number;
        }

public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "order";
            }
        }

public OrderElement this[int index]
        {
            get
            {
                return (OrderElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    }

那么我们再看看节点对象的定义

public class OrderElement : ConfigurationElement
    {
        [ConfigurationProperty("number", IsRequired = true)]
        public string Number
        {
            get
            {
                return (string)base["number"];
            }
            set
            {
                base["number"] = value;
            }
        }

[ConfigurationProperty("amount", IsRequired = true)]
        public double Amount
        {
            get
            {
                return (double)base["amount"];
            }
            set
            {
                base["amount"] = value;
            }
        }

[ConfigurationProperty("lineItems", IsDefaultCollection = true)]
        public LineItemElementCollection LineItems
        {
            get
            {
                return (LineItemElementCollection)base["lineItems"];
            }
        }
    }

另外,还有一个子节点列表对象需要定义,【LineItemElementCollection】(ConfigurationElementCollection)

public class LineItemElementCollection : ConfigurationElementCollection
    {
        [ConfigurationProperty("warehouseNumber", IsRequired = true)]
        public string WarehouseNumber
        {
            get
            {
                return (string)base["warehouseNumber"];
            }
            set
            {
                base["warehouseNumber"] = value;
            }
        }

protected override ConfigurationElement CreateNewElement()
        {
            return new LineItemElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ( (LineItemElement)element ).Number;
        }

public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "lineItem";
            }
        }

public LineItemElement this[int index]
        {
            get
            {
                return (LineItemElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    }

当然我们还得再定义一个节点对象【LineItemElement

public class LineItemElement : ConfigurationElement
    {
        [ConfigurationProperty("number", IsKey=true, IsRequired = true)]
        public string Number
        {
            get
            {
                return (string)base["number"];
            }
            set
            {
                base["number"] = value;
            }
        }

[ConfigurationProperty("description", IsRequired = true)]
        public string Description
        {
            get
            {
                return (string)base["description"];
            }
            set
            {
                base["description"] = value;
            }
        }
    }

这样我们就完成了webconfig节点的自定义和对象的实体化, 我们在使用的使用值需要简单的代码就能获取到相应对象的实体信息;如:

OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders");

另一中用法:sectionGroup 配置 。如要配置出如下webconfig信息

<configSections>
    <sectionGroup name="mygroup">
      <section name="mysection"
                       type="ConfigSection"
                        allowDefinition="Everywhere"
                         allowLocation="true"/>
    </sectionGroup>
  </configSections>

<mygroup>
    <mysection  user="用户" password="密码">
      <element element1="属性1" element2="属性2"></element>
    </mysection>
  </mygroup>

那么我们看下实体是如何定义的 。

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());

ConfigurationSection类使用心得的更多相关文章

  1. iOS中编写单例类的心得

    单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...

  2. C++11模板类使用心得

    1.推荐使用std::shared_ptr<TaskT>代替指针TaskT*使用,shared_ptr是一种智能指针,能自主销毁释放内存,在c++11中被引入,在多线程编程中有很大的用处, ...

  3. 学习Python类的心得

      类的注意事项 1)命名规则 需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的, 是特殊变量,特殊变量是可以直接访问的,不是private变量, ...

  4. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  5. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  6. .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...

  7. c# ConfigurationSection

    怎么把自己的配置文件配置到app.config中? 方案1:在app.config中添加 <!--应用配置--> <appSettings configSource="Co ...

  8. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  9. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

随机推荐

  1. Android动画之旅-Android动画基本介绍

    在上一篇博客中.我们简单了解了在Android中的动画特效.小伙伴们是不是意犹未尽呀. 还没有看的猛戳这里:Android动画之旅一开篇动画简单介绍 本篇博客.将和大家一起来分析Android中的四大 ...

  2. Unity3D性能优化小tips——把this.transform缓存缓存起来

    Unity3D开发时中有一个小tips,这在官方的文档里其实有提及的,但不那么显眼,这里小说一下: 在MonoBehaviour进行编程时,我们经常会用this.transform, this.gam ...

  3. 嵌入web字体

    @font-face模块         可以帮助我们轻松解决Web页面中使用优雅字体的方式,而且我们可以根据需要自定义多种字体,但是每个@font-face只能定义一种,若需要多个字体就启用多个@f ...

  4. 请问大家ndk中LOCAL_SHARED_LIBRARIES LOCAL_LDLIBS什么区别

    请问大家ndk中LOCAL_SHARED_LIBRARIES LOCAL_LDLIBS什么区别啊 我先是编译了一个.so 然后在此次编译的使用调用,请问用LOCAL_SHARED_LIBRARIES和 ...

  5. 【Linux】xshell连接中断后就无法连接虚拟机中的Linux

    具体情景是这样的: 在使用Linux的时候,本来一直好好的,突然就断了,我去百度了一番,网上的说法有千万种 有的说:是由于防火墙的问题 有的说:是由于Linux与其他ip冲突造成 ... 说法千万种, ...

  6. mysql课程记录

    thread_pool可以使用Percona的版本和Mariadb的版本  都是支持的  主从切换是根据HA的方式,TDDL(Taobao Distribute Data Layer) 的方式的话,推 ...

  7. Ffmpeg 视频教程 向视频中添加文字

    Ffmpeg支持添加文字功能,具体如何将文字叠加到视频中的每一张图片,FFmpeg调用了文字库FreeSerif.ttf.当我们 用到ffmpeg 添加文字功能时 我们需要先下载改文字库,下载地址是h ...

  8. Android View Attributes

    ImageView android:adjustViewBounds  Set this to true if you want the ImageView to adjust its bounds ...

  9. python代码 构建验证码

    1.python代码编写 (随机验证码): #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ...

  10. Visual Studio/Eclipse调用 JBoss5中的WebService

    1. HelloWebService.java package com.xx.webservices; import javax.jws.WebMethod; import javax.jws.Web ...