Home  >  Article  >  Backend Development  >  .Net - implement the IConfigurationSectionHandler interface to define handlers to process custom nodes

.Net - implement the IConfigurationSectionHandler interface to define handlers to process custom nodes

黄舟
黄舟Original
2017-02-24 10:32:351314browse

In addition to using the built-in handler provided in .net to process our custom nodes, we can also use a variety of methods to define our own processing classes to process our custom nodes. This article mainly introduces the implementation IConfigurationSectionHandler interface to implement custom handlers.

First, we first write the following custom node in the configuration file:

 
  

    
      
13232@qq.com
lhc 2343254
132345232@qq.com
水田如雅 2343453254

Then write the corresponding class for processing:

namespace MailHandlerClass
{
   public  class MailServer
    {
       //存储mailServer的子节点(
13232@qq.com
lhc2343254)的值 //以及client的值 private Hashtable serverNode; //构造函数 public MailServer() { serverNode = new Hashtable(); } public Hashtable ServerNode { get { return serverNode; } } public string client { get { return serverNode["client"] as string; } } public string Address { get { return serverNode["address"] as string; } } public string UserName { get { return serverNode["userName"] as string; } } public string PassWord { get { return serverNode["password"] as string; } } } //对应mailServerGroup public class MailServerConfig : List { //映射provider值 public string Provider { get; set; } } //自定义配置节点mailServerGroup的处理程序 public class MailServerConfigurationHandler : IConfigurationSectionHandler { //section为MailServerGroup节点 public object Create(object parent, object configContext, System.Xml.XmlNode section) { //设置方法返回配置对象,可以是任何类型 MailServerConfig config = new MailServerConfig(); //获取节点的属性信息 config.Provider = section.Attributes["provider"] == null ? "" : section.Attributes["provider"].Value; //获取MailServer节点 foreach (System.Xml.XmlNode child in section.ChildNodes) { MailServer server = new MailServer(); //添加Client属性 if (child.Attributes["client"]!=null) { server.ServerNode.Add("client", child.Attributes["client"].Value); } //获取MailServer下的Name,username,password节点 foreach (System.Xml.XmlNode grandChild in child.ChildNodes) { //添加文本 server.ServerNode.Add(grandChild.Name, grandChild.InnerText); } //将server加入MailServerConfig config.Add(server); } return config; } } }

In fact, it can be seen from the code that the implementation idea of ​​the custom processing class is still to use hashtable to realize key-value reading and storage + XML processing;

After that, we associate the above classes and nodes in the configuration file:



  
  
    

Be sure to pay attention to the type parameter when specifying the value s position! ! ! ! ! Otherwise, there will be an error that the handler cannot be loaded.

Write a piece of code to test:

namespace MailHandlerClass
{
    //也可以通过继承ConfigurationSection类来完成
    class Program
    {
        static void Main(string[] args)
        {
            MailServerConfig mailServerConfig = (MailServerConfig)ConfigurationManager.GetSection("mailServerGroup");

            //读取节点值
            mailServerConfig.ForEach(m => {
                Console.WriteLine(m.client+";"+m.Address+";"+m.UserName+";"+m.PassWord); 
            });

        }
    }
}

Well, in fact, it is recommended to force the conversion to as or something.

In fact, the configuration file looks like this at first:

                                    After that, when you look at the configuration file, it will look like this: # The above is .Net - implement the IConfigurationSectionHandler interface to define the handler to process the content of the custom node. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn