Easily complete custom node configuration and read Operations (common components)

Source: Internet
Author: User

In app. config and web. config, we usually use custom nodes to complete the configuration content to be extended. In the past, we had to inherit the configurationsection, configurationelementcollection, and configurationelement classes to read configuration information.CodeComplex. Later, we can easily query the configuration information through the LINQ to XML file, but this reduces the definition of the class. We can only read or fill in the class we define through xmlnode.

I. Writing of the original custom configuration file:

 
1. The definition type is cumbersome. Internal class aopconfigurationsection: configurationsection {[configurationproperty ("", isdefaultcollection = true)] public aopelementcollection AOPs // you need to define here a little {get {return (aopelementcollection) base [""] ;}}
 
2. query xelement = xelement using LINQ to XML. parse (XML); var name = from E in xelement. elements ("B") Let S = E. element ("e") Select S. attribute ("name "). value;

II. Introduction to general configuration Components

Reference: definitionconfig. dll

Object:

Displayconfigname feature (corresponding node name)

Readsection class (initialize node)

Configsectionshelper class (configuration resolution class)

Sections class (configure collection class)

Feature: Read configuration files based on custom types

Note: Define the subnode property type as sections generic collection class.

Method: getconfigsection <connconfig> (); // read the unique node type.

Getconfigsectionchild <connconfig> (); // read data that contains the subnode type

1. custom type public class connconfig {[displayconfigname ("name")] // name in the configuration file is called name public string name {Get; set;} Public String STR {Get; set ;} // if no attribute is declared, the configuration file name is called the attribute name STR}
 
2. single Node (excluding subnodes) // The type attribute in section is definitionconfig. configsectionshelper, definitionconfig <configsections> <section name = "connstr" type = "definitionconfig. configsectionshelper, definitionconfig "/> </configsections> <connstr name =" DB "str =" connstring "> </connstr> or <connstr STR =" connstring "> <Name> dB </ name> <STR> connstring </STR> </connstr> WE configured only one connstr node. There are no subnodes. Note: This node can only have one, so it cannot have multiple connstr. We read this configuration as the connconfig type. Readsection rs = new readsection ("connstr"); // instantiate readsection. The parameter is the node name connconfig conn = Rs. getconfigsection <connconfig> (); // read the configuration console through getconfigsection. writeline (Conn. name); // verify whether the data is read
 3. multiple nodes (including subnodes) 
   
    
   
   
    
    
    
    
      connstring 
     
    
    
    
      SQLite 
     
    
      connstring 
     
    
   readsection rs = new readsection ("connstrs "); // read the connstrs node var con = Rs. getconfigsectionchild 
  
    (); // getconfigsectionchild reads the configuration of a subnode. Note: You must use this command to read foreach (VAR item in Con) {console. writeline (item. name); console. writeline (item. str) ;}
  
 4. attribute: custom type (including multiple subnodes) public class connconfig {[displayconfigname ("name")] public string name {Get; set;} public connstring STR {Get; set;} // defined as the type} public class connstring {public string name {Get; set ;} public string type {Get; Set ;}} 
   
    
    
      Oracle 
     
     
     
       oracle10 
      
     
    
    readsection rs = new readsection ("connstrs "); vaR con = Rs. getconfigsectionchild 
   
     (); console. writeline (CON [0]. str. name); // oracledb 
   
  
 5. attribute: Custom set type (subnode set) public class connconfig {[displayconfigname ("name")] public string name {Get; set;} public connstring STR {Get; set;} public sections 
  
    AA {Get; set ;} // define set} public class connstring {public string name {Get; set;} public string type {Get; Set ;}} public Class AA {public string name {Get; set ;}} 
    
     
     
       Oracle 
      
      
      
        oracle10 
       
      
      
      
     
       2 
      
    
   
     readsection rs = new readsection (" connstrs "); vaR con = Rs. getconfigsectionchild 
  
    (); foreach (VAR item in CON [0]. aa) {console. writeline (item. name) ;}
  
6. the attribute is to customize multiple set types (multiple child node sets) public class connconfig {[displayconfigname ("name")] public string name {Get; set ;} public connstring STR {Get; set;} public sections <AA> AA {Get; Set ;}} public class connstring {public string name {Get; set ;} public string type {Get; Set ;}} public Class AA {public string name {Get; Set ;}public sections <BB> BB {Get; Set ;}} public Class BB {public string type {Get; Set ;}} <connstrs> <con> <Name> Oracle </Name> <STR name = "oracledb"> <type> oracle10 </type> </STR> <AA name = "1 "> <Bb TYPE =" type1 "> </BB> </AA> <Name> 2 </Name> <Bb TYPE =" type2 "> </BB> </AA> </con> </connstrs> readsection rs = new readsection ("connstrs "); vaR con = Rs. getconfigsectionchild <connconfig> (); foreach (VAR item in CON [0]. aa) {console. writeline (item. name );}
7. configure external config <section name = "mysection" type = "definitionconfig. configsectionshelper, definitionconfig "requirepermission =" false "restartonexternalchanges =" false "/> <mysection configsource =" mysection. config "/> readsection rs = new readsection (" mysection "); // read the node var list = Rs. getconfigsectionchild <configitem> ();

Download: definitionconfig

Component Demo: readconfigdemo

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.