//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