How to use. NET configuration file (ii)

Source: Internet
Author: User
Tags foreach assert config

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

Debug.Assert (Cfgtable.count = 2);
Hashtable Cfgfuzhou = (Hashtable) cfgtable["Fuzhou"];
Hashtable Cfgshanghai = (Hashtable) cfgtable["Shanghai"];
Debug.Assert (cfgfuzhou["Device"] = = "Printer");
Debug.Assert (cfgshanghai["Device"] = = "Mobile");
Debug.Assert (cfgfuzhou["Customer"] = = "Muf");
Debug.Assert (cfgshanghai["Customer"] = = "Liny");

foreach (Hashtable cfg in cfgtable.values)
{
Console.WriteLine ("Area={0} Device={1} customer={2}", cfg["area"], cfg["Device"], cfg["Customer"]);
}

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.

adjourned


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.