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

发布于 2024-12-15 12:38:26 字数 1015 浏览 6 评论 0原文

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

无法识别的元素“add”

我想读取本节中的所有“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>

When I do this ConfigurationManager.GetSection("SectionA/sectionD") I receive this error:

Unrecognized element 'add'

I'd like read all "add" element from this section to create a collection.

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长安忆 2024-12-22 12:38:26

这是配置元素集合代码声明的示例:

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

所以在 config 中它看起来如下:

<indicators>
    <add ... />
</indicators>

Here's an example of configuration element collection code 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;
    }
}

So in config it looks as following:

<indicators>
    <add ... />
</indicators>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文