3. Custom configuration structure (using IConfigurationSectionHandler)
Assuming that you have the following configuration information that can be repeated many times in MyInfo, how do you read the configuration? You will need to use a custom configuration program.
<myConfigs>
<myinfo area= "Fuzhou" device= "Printer" customer= "Muf"/>
<myinfo area= "Shanghai" device= "Mobile" customer= "Liny"/>
</myConfig>
The access code is as follows:
Hashtable cfgtable = (Hashtable) configurationsettings.getconfig ("Myconfigs");
In order to access the configuration structure using the access code above, we need to generate a specific configuration read class (Configurationsectionhandler), which is simple enough to explain:
public class Myinfosectionhandler:iconfigurationsectionhandler
{
public Object Create (object parent, Object configcontext, System.Xml.XmlNode section)
{
Hashtable config = new Hashtable ();
foreach (XmlNode node in section. ChildNodes)
{
if (node. Name!= "MyInfo")
throw new System.Configuration.ConfigurationException ("Unrecognized configuration item", node);
Hashtable item = new Hashtable ();
foreach (XmlAttribute attr in node. Attributes)
{
Switch (attr. Name)
{
Case "Area":
Case "Device":
Case "Customer":
Item. ADD (attr. Name, attr. Value);
Break
Default
throw new System.Configuration.ConfigurationException ("Unrecognized configuration Attribute", attr);
}
}
Config. ADD (item["area"], item);
}
return config;
}
}
We then define the configuration instructions. Wherein, Mynamespace.myinfosectionhandler is the complete name of the Myinfosectionhandler class with the name space; myApp is the name of the assembly that defines the Myinfosectionhandler class without an extension (such as MyApp.dll or MyApp.exe):
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<!--The following are the declarations for custom configurations-->
<configSections>
<section name= "Myconfig" type= "Mynamespace.myinfosectionhandler, myApp"/>
</configSections>
<myConfigs>
<myinfo area= "Fuzhou" device= "Printer" customer= "Muf"/>
<myinfo area= "Shanghai" device= "Mobile" customer= "Liny"/>
</myConfig>
</configuration>
Based on the above example, we can use IConfigurationSectionHandler to implement any configuration file structure.
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.