. net--implement IConfigurationSectionHandler interface definition handlers handle custom nodes

Source: Internet
Author: User
Tags configuration settings


In addition to using the built-in handlers provided inside. NET to handle our custom nodes, we can also define processing classes to handle our custom nodes through a variety of methods, this article mainly introduces the way to implement the custom handlers by implementing the IConfigurationSectionHandler interface.



First, let's write the following custom node in the config file:


<!--using custom nodes and the above custom handlers--  <mailservergroup provider= "www.baidu.com" >    <mailserver client= " http://blog.csdn.net/lhc1105 ">      <address>[email protected]</address>      <USERNAME>LHC </userName>      <password>2343254</password>    </mailServer>        <mailserver Client= "http://www.cnblogs.com/liuhuichao/" >      <address>[email protected]</address>      < username> Paddy </userName>      <password>2343453254</password>    </mailServer>      </mailServerGroup>



then write the corresponding class for processing:



      

Namespace mailhandlerclass{public class MailServer {//Storage mailserver child nodes (<address>[email protected] </address><userName>lhc</userName><password>2343254</password>) value//And the value of client p       Rivate Hashtable Servernode;       Constructor 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;}} }//corresponds to Mailservergroup public class Mailserverconfig:list<mailserver> {//Map provider value public Strin      G Provider {get; set;} }//Custom configuration node Mailservergroup handler public class Mailserverconfigurationhandler:iconfigurationsectionhandler{//section is Mailservergroup node public object Create (object parent, Object Configcontext, System.X ml.           XmlNode section) {//Set method returns the Configuration object, which can be any type mailserverconfig config = new Mailserverconfig (); Gets the property information for the node config. Provider = section. attributes["Provider"] = = null? ": section. attributes["Provider"].           Value; Gets the MailServer node foreach (System.Xml.XmlNode.                ChildNodes) {mailserver Server = new MailServer (); Add the Client property if (child. attributes["Client"]!=null) {server. Servernode.add ("Client", child. attributes["Client"].                Value); }//Get mailserver under Name,username,password node foreach (System.Xml.XmlNode Grand Child in a child. ChildNodes) {//Add text server. Servernode.add (Grandchild.name, Grandchild.innErtext); }//Add server to Mailserverconfig config.            ADD (server);        } return config; }}}



In fact, from the code can be seen, the implementation of the custom processing class is still using Hashtable to achieve key-value reading and storage + XML processing;


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



<configuration>  <!--define classes for processing Mailservergroup configuration Sections---  <configSections>    <section Name = "Mailservergroup" type= "Mailhandlerclass.mailserverconfigurationhandler, implements IConfigurationSectionHandler interface"/>        <!--Note type: Specifies the name of the configuration section handler class that is used to handle the configuration settings in the section or element specified in the Name property. Use the following format: type= "Fully qualified class name, assembly file name, version, culture, public key token" The definition must match the assembly reference. -          </configSections>


Be sure to note the position of the parameter when the type specifies the value!!!!! Otherwise, there will be an error loading the handler.


Write segment code under test:

namespace mailhandlerclass{    ///You can also complete class program    {        static void Main by inheriting the ConfigurationSection class ( String[] args)        {            Mailserverconfig mailserverconfig = (mailserverconfig) configurationmanager.getsection (" Mailservergroup ");            Read node value            Mailserverconfig.foreach (m + = {                Console.WriteLine (m.client+ ";") +m.address+ ";" +m.username+ ";" +m.password);}             );}}    


well, in fact, it is recommended to change the form of as what.



in fact, the configuration file initially looks like this:





But when you're done, you look at the config file, and it becomes this:





Do you feel so easy~~~~ ~~~









. net--implement IConfigurationSectionHandler interface definition handlers to handle custom nodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.