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>13232@qq.com</address> <username>lhc</ username> <password>2343254</password> </mailServer> <mailserver client= " http://www.cnblogs.com/liuhuichao/"> <address>132345232@qq.com</address> <username > paddy fields such as </userName> <password>2343453254</password> </mailServer> </ Mailservergroup>
Then write the corresponding class for processing:
Namespace mailhandlerclass{public class MailServer {//Storage mailserver child nodes (<address>13232@qq.com</addre The value of ss><username>lhc</username><password>2343254</password>)//And the value of the client private has Htable 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 for Mailservergroup node public object Create (object parent, Object Configcontext, System.Xml.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:
The above is the. net--implements the IConfigurationSectionHandler interface definition handler to handle the content of the custom node, and more about topic.alibabacloud.com (www.php.cn)!