//App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <!--添加自定义配置节点,type:解析类名,程序集名-->
  <section name="PersonSetion" type="CommonConfig.PersonSectionHandler,CommonConfig"/>
 </configSections>
        <!--自定义节点内容-->
 <PersonSetion>
  <PersonInfo name="Name"  Value="Mr Lin" ReadOnly="true"></PersonInfo>
  <PersonInfo name="Department"  Value="Development" ReadOnly="true"></PersonInfo>
  <PersonInfo name="Position"  Value="Software Engineer" ReadOnly="true"></PersonInfo>
 </PersonSetion>
</configuration>

//解析自定义节点

using System;
using System.Configuration;
using System.Xml;
using Model;
namespace CommonConfig
{
    /// <summary>
    /// 实现接口:IConfigurationSectionHandler,解析自定义配置节点,
    /// </summary>
    public class PersonSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            //解析配置文件信息,返回对象
            Person person = new Person();
            if (section != null)
                foreach (XmlNode item in section.SelectNodes("PersonInfo"))
                {
                    switch (item.Attributes["name"].InnerText)
                    {
                        case "Name":
                            person.Name = item.Attributes["Value"].InnerText;
                            person.IsNameReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
                            break;
                        case "Department":
                            person.Department = item.Attributes["Value"].InnerText;
                            person.IsDepartmentReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
                            break;
                        case "Position":
                            person.Position = item.Attributes["Value"].InnerText;
                            person.IsPositionReadOnly = Convert.ToBoolean(item.Attributes["ReadOnly"].InnerText);
                            break;
                        default:
                            break;
                    }

                }
            return person;
        }
      
    }
}
//实体类

namespace Model
{
    public class Person
    {
        private string name;
        private string department;
        private string position;
        private bool isNameReadOnly;
        private bool isDepartmentReadOnly;
        private bool isPositionReadOnly;
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 部门
        /// </summary>
        public string Department
        {
            get { return department; }
            set { department = value; }
        }

        /// <summary>
        /// 职位
        /// </summary>
        public string Position
        {
            get { return position; }
            set { position = value; }
        }
        /// <summary>
        /// 名称是否只读
        /// </summary>
        public bool IsNameReadOnly
        {
            get { return isNameReadOnly; }
            set { isNameReadOnly = value; }
        }
        /// <summary>
        /// 部门信息是否只读
        /// </summary>
        public bool IsDepartmentReadOnly
        {
            get { return isDepartmentReadOnly; }
            set { isDepartmentReadOnly = value; }
        }
        /// <summary>
        /// 职位信息是否只读
        /// </summary>
        public bool IsPositionReadOnly
        {
            get { return isPositionReadOnly; }
            set { isPositionReadOnly = value; }
        }
    }
}
//测试配置

using System;
using System.Configuration;
using System.Windows.Forms;
using Model;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetText();   
        }
        private void SetText()
        {
            //会调用object Create(object parent, object configContext, XmlNode section)
           Person person= (Person)ConfigurationSettings.GetConfig("PersonSetion");
            if (person != null)
            {
                txtDepartment.Text = person.Department;
                txtDepartment.ReadOnly = person.IsDepartmentReadOnly;
                txtName.Text = person.Name;
                txtName.ReadOnly = person.IsNameReadOnly;
                txtPosition.Text = person.Position;
                txtPosition.ReadOnly = person.IsPositionReadOnly;
            }
        }

    }
}

转转:http://blog.sina.com.cn/s/blog_5b9b514b0100p5gq.html

自定义配置节点configSections的使用的更多相关文章

  1. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  2. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  3. ASP.NET系列:自定义配置节点的复用

    appSettings太简单,为每个程序自定义配置节点太复杂,因此要解决app.config&web.config自定义配置的复用问题. 1.读取不依赖SectionName,根节点可以定义为 ...

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

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

  5. C#创建自定义配置节点

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  6. App.Config自定义配置节点

    配置文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <con ...

  7. C# 快捷使用自定义配置节点

    C#除了appSettings和connectionStrings默认配置外还允许用户自定义使用配置.C# 提供3中简单的自定义配置,配置文件如下 <?xml version="1.0 ...

  8. C# 创建自定义配置节点1

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

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

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

随机推荐

  1. Solaris下truss的使用

    Solaris下truss的使用 原文转载:http://blog.csdn.net/sunlin5000/article/details/6560736 在Solaris下面,如果需要跟踪系统的调用 ...

  2. Apache ab测试工具使用方法(无参、get传参、post传参)(转)

    转自Apache ab测试工具使用方法(无参.get传参.post传参) Ab测试工具是apache自带的测试工具,具有简单易上手的特性,下面我总结一下我的使用方法,首先去官方下载apache程序包, ...

  3. Linux之RedHat7如何更换yum源

    目前,我们常见的系统大概就是Windows.Linux和Mac OS了.Windows系统应该是大部分人最早开始接触的系统,毕竟Windows系统使用起来相当方便,只需要点点鼠标,外加会简单的打字,一 ...

  4. composer 被墙后镜像设置

    这一步主要更改镜像,不从外网直接取,现在改成了中国的一家镜像站.就是下面这个地址. https://packagist.phpcomposer.com#阿里云的composer镜像源composer ...

  5. Java并发编程实战 第16章 Java内存模型

    什么是内存模型 JMM(Java内存模型)规定了JVM必须遵循一组最小保证,这组保证规定了对变量的写入操作在何时将对其他线程可见. JMM为程序中所有的操作定义了一个偏序关系,称为Happens-Be ...

  6. 4.华为路由交换技术_IP路由选择原理(上)

    初始化时,路由表上只有直连路由 查看路由表 严格来讲任何一个具有三层路由功能的设备都有路由表 数据通信往往是双向的的,来回都要考虑. 路由是逐跳的,每一跳都要设置. 实际路由是根据已经分配的网络段进行 ...

  7. 【优化】SPA项目的基础优化

    开启gzip压缩功能 引入CDN 路由懒加载 某些第三方组件按需加载而不是全部加载 较小的图片资源用base64嵌入src中,减少http请求

  8. 【UOJ#77】A+B Problem

    传送门 题目描述 略 Sol 看到选择黑白收益不同,然后还可能有代价. 我们想到用网络流解决,并且这应该是用总可能收益-最小割得到答案. 考虑初步建图,发现那个限制可以直接 \(n^2\) 解决. 我 ...

  9. day_05 if条件判断和while循环作业题

    1. 输入姑娘的年龄后,进行以下判断: 1. 如果姑娘小于18岁,打印“不接受未成年” 2. 如果姑娘大于18岁小于25岁,打印“心动表白” 3. 如果姑娘大于25岁小于45岁,打印“阿姨好” 4. ...

  10. CMDB表结构设计

    服务器 内存.cpu.disk.nic.raid.sn.model.os.status. disk_info = { } SERVER001 storage .... NET001 网络设备 eth ...