<?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>測試
//解析自定義節點spa
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;
}orm
}
return person;
}
}
}
//實體類xml
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; }
}
}
}
//測試配置utf-8
using System;
using System.Configuration;
using System.Windows.Forms;
using Model;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}get
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;
}
}string
}
}it