无法识别的元素';添加';读取web.config时出错

本文关键字:web config 出错 读取 添加 元素 识别 | 更新日期: 2023-09-27 18:14:36

当我执行此ConfigurationManager.GetSection("SectionA/sectionD")时,我收到此错误:

无法识别的元素"add">

我想阅读本节中的所有"添加"元素来创建一个集合。

<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>

无法识别的元素';添加';读取web.config时出错

以下是配置元素集合代码delcaration的示例:

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>