web.config 或 app的config中自定义节点
博客专区 > RyanXing 的博客 > 博客详情
web.config 或 app的config中自定义节点
RyanXing 发表于2年前
web.config 或 app的config中自定义节点
  • 发表于 2年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: web.config 或 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;
           }
       }

   }
}

共有 人打赏支持
粉丝 0
博文 21
码字总数 6521
评论 (0)
×
RyanXing
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: