Advanced operating practices of the Config configuration file ConfigurationSectionGroup

Today further ConfigurationSectionGroup class and related classes and methods in the System.Configuration were studied. Found to build a multi-level nested XML tags must be used ConfigurationSectionGroup class

We look at the following XML document:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="appGroupC" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
        </sectionGroup>
        <sectionGroup name="appGroupD" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
        </sectionGroup>
        <sectionGroup name="appGroupE" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
        </sectionGroup>
        <sectionGroup name="MyGroup" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
            <section name="Nameissection1" type="Study_System.Configuration.AppSectionA, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            <sectionGroup name="GroupAB" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
                <section name="NameissectionA" type="Study_System.Configuration.AppSectionB, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                <section name="NameissectionB" type="Study_System.Configuration.AppSectionB, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </sectionGroup>
        </sectionGroup>
    </configSections>
    <connectionStrings>
        <add name="连接的名称,就类同于Key" connectionString="具体的连接字符串" providerName="与连接字符串一起使用的提供程序的名称。这个有时可以没有" />
    </connectionStrings>
    <appSettings>
        <add key="huangbo" value="1234567890" />
    </appSettings>
    <MyGroup>
        <Nameissection1 KeyName="" KeyValue="">
            <AppElement KeyName="this is key" KeyValue="this is value" KeyValue2="this is value2" />
        </Nameissection1>
        <GroupAB>
            <NameissectionA KeyName="hahahaha" KeyValue="1234567">
                <ElementCollection KeyName="" />
            </NameissectionA>
            <NameissectionB KeyName="this is key name" KeyValue="this is key value">
                <ElementCollection KeyName="">
                    <add KeyName="e1 key" KeyValue="e1 value" KeyValue2="e1 value2" />
                    <add KeyName="e2 key" KeyValue="e2 value" KeyValue2="e2 value2" />
                    <add KeyName="e3 key" KeyValue="e3 value" KeyValue2="e3 value2" />
                </ElementCollection>
            </NameissectionB>
        </GroupAB>
    </MyGroup>
</configuration>


It can be found as long as ConfigurationSectionGroup will appear in the declaration section <configSections> area of ​​the label. Section to show all levels.

Then press on all Section below this level.


ConfigurationSectionGroup objects very easy to use.

First, create multiple levels of nested XML

1, directly ConfigurationSectionGroup or write a class that inherits ConfigurationSectionGroup. You can not add attributes like Section and elements as in this class.

2, in the Section added to ConfigurationSectionGroup. If a plurality of nested levels, may also be added to the object ConfigurationSectionGroup ConfigurationSectionGroup object. It's like there are an array as an array.

3, add the object to the topmost ConfigurationSectionGroup Configuration.


Second, if there is to read the contents of the object ConfigurationSectionGroup

1, when the reading has ConfigurationSectionGroup object we can get it, remember to Group To get converted to the corresponding inherited from that class ConfigurationSectionGroup with Configuration.GetSectionGroup ( "GroupName").

2, to give the corresponding target ConfigurationSectionGroup inherited class, the number can be obtained by ConfigurationSectionGroup.Sections.Count Section of this Group, with ConfigurationSectionGroup.Sections [0] .SectionInformation.Name obtained Section name

For example: to give an example of the XML object MyGroup AppSectionB Name Name and object code:

((AppGroup)cfg.GetSectionGroup("MyGroup")).Name;
((AppSectionB)cfg.GetSectionGroup("MyGroup").SectionGroups[0].Sections[1]).KeyName

Third, on more than one element in a Section

If there are multiple elements must be inherited ConfigurationElementCollection with a user class. Do not try to inherit from a custom class Section class with an array of AppElement way. Because ConfigurationManager can not read from XML to elements of the array are not included ConfigurationElementCollection object.

A very intuitive example is the .NET predefined two objects: Configuration.AppSettings.Settings and Configuration..ConnectionStrings.ConnectionStrings which we have seen and AppSettings objects Section ConnectionStrings inherited from class, Settings and ConnectionStrings is inherited from ConfigurationElementCollection Object. So we should set the parameters in the XML structure that it forms and for now there is no other way to read element is not included ConfigurationElementCollection.

Fourth, on Configuration.SectionGroups.Count and Configuration.Sections.Count

如果你的Section对象是直接加到Configuration中去的,那么这个时候你可能要杯具了。因为由于Configuration中存在了很多.NET预定义好的Section和Group所以你用Configuration.SectionGroups.Count将得到10。Configuration.Sections.Count将得到22。这个时候你千万不要奇怪和郁闷,可以使用Configuration.GetSectionGroup(GroupName)先得到顶层的你定义的那个Group,之后在这个Group下面的SectionGroups.Count 和 Sections.Count 将是正常的。

换句话说就是你无法直接从Configuration的GetSectionGroup() 及 GetSection() 这两个方法得到不属于顶层的对象。


转载于:https://www.cnblogs.com/kevinGao/p/4188739.html

Guess you like

Origin blog.csdn.net/weixin_34168880/article/details/93051819