[英]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.