App. Config configures multiple configuration sets custom configuration (3)

Source: Internet
Author: User

Again, use app. Config to configure multiple custom methods.
Let's look at this example: American Family Simpson has a father and a mother and three children, while the Chinese old king has only one child. The structure is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <sectiongroup type= " Family.sectiongroups,family "name=" Family "> <section name=" Simpson "type=" Family.Simpson.FamilySection, Family "allowdefinition=" Everywhere "/> <section name=" Wang "type=" family.wang.wangsection,family " allowdefinition= "Everywhere"/> </sectionGroup> </configSections> <family> <simpson surname= "Simpson" > <father firstname= "James" lastname= "Simpson"/> <mother firstname= "Kate" lastname= "Simpson"/&    Gt <children > <add firstname= "Jim" lastname= "Simpson"/> <add firstname= "Aaron" lastname= "Simpson"/&      Gt <add firstname= "Lukas" lastname= "Simpson"/> </children> </simpson> <wang surname= "King" > <father firstname= "Wang" lastname= "two small"/> <mother firstname= "li" lastname= "Feifei"/> <child firstName = "Wang" lastname= "bo"/> &LT;/WANG&GT </family> </configuration>

  

The implementation of each section and ConfigurationElementCollection is described in the previous article. Not posted here.
Attention Lao Wang American Family difference is: Lao Wang home because only child, so there is no children, only a child property.

Add a Sectiongroups class that inherits from System.Configuration.ConfigurationSectionGroup.

In this sectiongroups class and configuration file, the following sentence

<sectiongroup type= "family.sectiongroups,family" name= "Family" >
This sentence is the corresponding,
"Family.sectiongroups" namespace + class name, Family for assembly; Name= "Fanmily" is the name of the configuration node.

Class SectionGroups:System.Configuration.ConfigurationSectionGroup    {public        Family.Wang.WangSection Wang        {            get {return (Family.Wang.WangSection) base. sections["Wang"]; }        } public        Family.Simpson.FamilySection Simpson        {            get {return (Family.Simpson.FamilySection) base. sections["Simpson"]; }        }    }

Test Code 1

Sectiongroups sample = (sectiongroups) System.Configuration.ConfigurationManager.OpenExeConfiguration ( System.Configuration.ConfigurationUserLevel.None). sectiongroups["Family"];            Family.Wang.WangSection w = Sample. Wang;            Father f= W.father;            Mother m = w.mother;            Child C = w.child;            Family.Simpson.FamilySection s = Sample. Simpson;            Do-to-  s.father; s.mother;s.children

Test code 2, or you can use this:

Family.Wang.WangSection w = configurationmanager.openexeconfiguration (Configurationuserlevel.none). GetSection ("Family/wang") as Family.Wang.WangSection;

Test code 3, that's fine.

 System.Configuration.Configuration config = configurationmanager.openexeconfiguration (configurationuserlevel.none            ); Configurationsectiongroupcollection sectiongroups = config.            sectiongroups; foreach (ConfigurationSectionGroup sectiongroup in sectiongroups) {foreach (configurationsectio N sections in sectiongroup.sections)//There are many other default nodes {if (section. Sectioninformation.name = = "Wang") {Family.Wang.WangSection Wang = section as F Amily.                        Wang.wangsection; Console.WriteLine ("Father:" + Wang.) Father.firstname + "" + Wang.                        Father.lastname); Console.WriteLine ("Mother:" + Wang.) Mother.firstname + "" + Wang.                    Mother.lastname); } if (section. Sectioninformation.name = = "Simpson") {Family.Simpson.FamilySection Simpson = s Ection as Family.Simpson.FamilySectiOn Console.WriteLine ("Father:" + Simpson. Father.firstname + "" + Simpson.                        Father.lastname); Console.WriteLine ("Mother:" + Simpson. Mother.firstname + "" + Simpson.                        Mother.lastname); foreach (Family.simpson. Child child in Simpson. Children) {Console.WriteLine ("Child:" + child.) FirstName + "" + Child.                        LastName); }                    }                }            }

  

Note: In the top app. config file, I began to forget the <family></family> tags, resulting in the read out of the data is always empty, wasted my 2 hours.

App. Config configures multiple configuration sets custom configuration (3)

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.