1

これを行うConfigurationManager.GetSection("SectionA/sectionD")と、次のエラーが表示されます。

要素「追加」を認識できません

コレクションを作成するには、このセクションのすべての「追加」要素を読みたいと思います。

<configSections>
    <sectionGroup name="SectionA">
        <sectionGroup name="SectionB">
            <section name="sectionC" type="MyProject.SectionC,MyProject" />
            <section name="sectionD" type="MyProject.SectionD,MyProject" />
        </sectionGroup>
        <section name="sectionE" type="MyProject.SectionE,MyProject" />
    </sectionGroup>
</configSections>

<SectionA>
    <SectionB>
        <sectionD>
            <add key="PerPage10" value="10" />
            <add key="PerPage30" value="30" />
            <add key="PerPage60" value="60" />
        </sectionD>
        <sectionC attribute3="10" />
    </SectionB>
    <sectionE attribute1="3" attribute2="5" />
</SectionA>
4

1 に答える 1

1

構成要素コレクションのコード宣言の例を次に示します。

private static ConfigurationProperty propIndicators = new ConfigurationProperty("indicators", typeof(ConfigurationElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);

[ConfigurationProperty("indicators", IsRequired = true, IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ReferencedConfigurationElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public ConfigurationElementCollection Indicators
{
    get
    {
        return (ConfigurationElementCollection)this[propIndicators];
    }
    set
    {
        this[propIndicators] = value;
    }
}

したがって、構成では次のようになります。

<indicators>
    <add ... />
</indicators>
于 2011-11-14T11:11:10.233 に答える