简体   繁体   English

删除元素的定制配置无法识别的属性

[英]Custom configuration unrecognized attribute for remove element

I have a custom configuration section that is throwing the following error: 我有一个自定义配置部分,它将引发以下错误:

Unrecognized attribute 'path'. Note that attribute names are case-sensitive.

The rest of the section does not have any problems loading. 本节的其余部分在加载时没有任何问题。 But once I add a <remove> element to the collection in question, the configuration fails to load. 但是,一旦我向相关集合中添加了<remove>元素,配置就无法加载。

Here's the code for the problematic section: 这是有问题的部分的代码:

[ConfigurationCollection(typeof(PathElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public class PathElementCollection : ConfigurationElementCollection
{
    public PathElementCollection()
    {
        // Load the default values...
        BaseAdd(new PathElement() { Path = "/content/" });
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new PathElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PathElement)element).Path;
    }
}

public class PathElement : ConfigurationElement
{
    public const string PathPropertyName = "path";

    [ConfigurationProperty(PathPropertyName, IsRequired = true)]
    public string Path
    {
        get { return (string)this[PathPropertyName]; }
        set { this[PathPropertyName] = value; }
    }
}

How I'm loading the section in the module that uses it: 我如何在使用该模块的模块中加载该部分:

CustomSection configSection = (CustomSection)ConfigurationManager.GetSection(CustomSection.SectionName);

Example configuration: 配置示例:

<ignoredPaths>
    <remove path="/content/" />
    <add path="/test/" />
</ignoredPaths>

Does anyone have any ideas as to what I'm doing wrong? 有人对我在做什么错有任何想法吗?

Your classes should look like this 您的课程应如下所示

    [XmlRoot("ignoredPaths")]
    public class IgnoredPaths
    {
        PathElementCollection paths {get;set;}
    }
    [XmlInclude(typeof(Remove))]
    [XmlInclude(typeof(Add))]
    public class PathElementCollection
    {
        [XmlAttribute("path")]
        public string path {get;set;}
    }
    [XmlRoot("remove")]
    public class Remove : PathElementCollection
    {
    }
    [XmlRoot("add")]
    public class Add : PathElementCollection
    {
    }​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法识别的属性自定义配置(再次) - Unrecognized attribute custom configuration (again) 自定义配置集合-无法识别的元素“ addService” - Custom Configuration Collection - Unrecognized element 'addService' 配置错误:无法识别的属性&#39;maxBufferSize&#39; - Configuration Error : Unrecognized attribute 'maxBufferSize' 元素中的“包含”属性<packagereference>无法识别</packagereference> - The attribute "Include" in element <PackageReference> is unrecognized 元素中的属性“版本”<PackageReference> 不被承认 - The attribute "Version" in element <PackageReference> is unrecognized 自定义ConfigurationSection中无法识别的元素异常 - Unrecognized element exception in custom ConfigurationSection System.Configuration.ConfigurationErrorsException-无法识别的元素“设置” - System.Configuration.ConfigurationErrorsException - Unrecognized element 'setting' 加载csproj元素中的属性“ExcludeAssets”<packagereference> 无法识别</packagereference> - Load csproj The attribute “ExcludeAssets” in element <PackageReference> is unrecognized 自定义.config文件中无法识别的属性“xmlns” - Unrecognized attribute 'xmlns' in custom .config file 无法识别的元素“删除”web.config - Unrecognized element 'remove' web.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM