Home  >  Article  >  Backend Development  >  .Net - Use .net built-in handlers to process custom nodes Demo

.Net - Use .net built-in handlers to process custom nodes Demo

黄舟
黄舟Original
2017-02-24 10:34:221519browse

In .net, because different nodes have corresponding classes to process them. For the sake of convenience, some classes have been built-in for our use in net, so that when we read the configuration file, we do not have to define classes ourselves to process our own custom nodes.

Below we wrote such a configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!--使用IgnoreSection处理自定义节点-->
    <!--<section name="mailServeraddress" type="System.Configuration.IgnoreSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowLocation="false " restartOnExternalChanges="true"/>-->

    <section name="mailServeraddress" type="System.Configuration.SingleTagSectionHandler" />
    
    <!--注意,指定处理程序的配置文件要写在自定义配置文件的前面-->
  </configSections>
  
  
  <mailServeraddress address="mail.tracefact.net" username="lhc" password="124324"/>

 
</configuration>

The node name is: mailServeraddress, which has three attributes, in section A SingleTagSectionHandler is defined to handle this node.

namespace 自定义节点和内置处理程序
{
    class Program
    {
        static void Main(string[] args)
        {
            ExampleSingleTagSectionHandler();

        }

        private static void ExampleSingleTagSectionHandler() {
            //SingleTagSectionHandler会以hashtable的形式返回节点的所有属性
            Hashtable mailServer = (Hashtable)ConfigurationManager.GetSection("mailServeraddress");//调用GetSection会返回一个hashtable

            string address = mailServer["address"].ToString();
            string username = mailServer["username"].ToString();
            string passWord = mailServer["password"].ToString();

            Console.WriteLine(address+"----"+username+"------"+passWord);

            
        }
    }
}

After the configuration file is written, after calling GetSection to force the hashtable, you can read the attribute value of the node in the form of key-value.

In .net, in addition to the type in the above example, we can also use other built-in types to handle custom nodes.


The above is .Net - use the .net built-in handler to process the content of the custom node Demo. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!










#

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