zoukankan      html  css  js  c++  java
  • .Net——使用.net内置处理程序处理自己定义节点Demo


             在.net中。由于对不同的节点,都相应着类去对它进行处理,。net里面为了方便。已经内置了一些类供我们使用。使我们在读取配置文件时。不必自己去定义类去处理自己定义的自己定义节点。


            以下我们写了这样一个配置文件:


              

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



                  节点名称为:mailServeraddress。有三个属性,在section里定义了SingleTagSectionHandler来处理这个节点。


                   

    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);
    
                
            }
        }
    }

             

                 配置文件写好后,调用GetSection强转hashtable后,就能够用key——value的形式读取节点的属性值了。


                 在.net中,除了上面样例中的这个type,我们也能够使用其他内置type来处理自己定义节点。













  • 相关阅读:
    ByteBuffer的mark、position、limit、flip、reset,get方法介绍ok
    java.nio.ByteBuffer的flip、rewind和compact几个方法的区分使用
    maven之一:maven安装和eclipse集成
    Java 8 函数式接口
    Lambda 表达式
    jdk8十大新的特性
    阿里巴巴73款开源产品列表,值得收藏
    【Java】java.util.Objects 工具类方法研究
    ARIMA 模型简单介绍
    python 二维数组取值
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5152825.html
Copyright © 2011-2022 走看看