app.config 中的自定义配置部分 -> 无法识别的元素'add'



因此,我正在尝试为app.config创建一个自定义配置部分,该部分将处理以下模式:

<domainSolutionSection>
<domainSolutionGroups>
<Group groupname="Legacy">
<add name="Old App #1"/>
<add name="Old App #2" />
</Group>
<Group groupname="Modern">
<add name="New App #1" />
<add name="New App #2" />
<add name="New App #3" />
</Group>
</domainSolutionGroups>
</domainSolutionSection>

我花了一整天的时间试图让它发挥作用
现在,我收到一个错误:

系统。配置ConfigurationErrorsException:无法识别的元素'add'

以下是我的DomainSolutionSection库中的类:

public class DomainSolutionSection : ConfigurationSection
{
[ConfigurationProperty("domainSolutionGroups")]
public DomainSolutionGroupCollection DomainSolutionGroups
{
get
{
return (DomainSolutionGroupCollection) this["domainSolutionGroups"];
}
set
{
this["domainSolutionGroups"] = value;
}
}
}
[ConfigurationCollection(typeof(GroupConfigElement))]
public class DomainSolutionGroupCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new GroupConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((GroupConfigElement)element).GroupName;
}
protected override string ElementName
{
get { return "Group"; }
}
protected override bool IsElementName(string elementName)
{
return !String.IsNullOrEmpty(elementName) && elementName == "Group";
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public GroupConfigElement this[int index]
{
get
{
return base.BaseGet(index) as GroupConfigElement;
}
}
public new GroupConfigElement this[string key]
{
get
{
return base.BaseGet(key) as GroupConfigElement;
}
}
}

public class GroupConfigElement : ConfigurationElement
{
[ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
public string GroupName
{
get { return (string)this["groupname"]; }
set { this["groupname"] = value; }
}
public PhraseCollection Phrases
{
get { return (PhraseCollection) base["Groups"]; } 
}
}
[ConfigurationCollection(typeof(PhraseConfigElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class PhraseCollection: ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new PhraseConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((PhraseConfigElement)element).Name;
}
}
public class PhraseConfigElement: ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

没有发现任何例子;添加";属性是在这个嵌套级别使用的,所以很明显,我还没有"有线连接";";组";正确地有人能提供指导吗?

谢谢!

很抱歉这不是您问题的直接答案。我没有时间分解你所做的事情,但这里有一个我过去做过的类似的例子,它很有效。也许这里有什么可以帮助的。

myconfig.config:

<?xml version="1.0"?>
<myconfig>
<moduleIPAccessRules>
<allowRules>
<add module="ModuleA"   subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleA intranet users" />
<add module="ModuleA"   subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleA local box" />
<add module="ModuleB"     subnetIP="172.16.0.0"     subnetMask="255.255.0.0"      name="ModuleB intranet users" />
<add module="ModuleB"     subnetIP="127.0.0.0"      subnetMask="255.255.0.0"      name="ModuleB local box" />
<add module="ModuleB"     subnetIP="123.45.67.89"   subnetMask="255.255.255.255"  name="some remote machine" />
</allowRules>
</moduleIPAccessRules>
</myconfig>

支持类别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
namespace Myproject.Configuration
{
public class IPAccessSection : ConfigurationSection
{
public static IPAccessSection GetConfig()
{
return ConfigurationManager.GetSection("myconfig/moduleIPAccessRules") as IPAccessSection;
}
[ConfigurationProperty("allowRules")]
public AllowRuleCollection AllowRules
{
get { return this["allowRules"] as AllowRuleCollection; }
}
}
public class AllowRuleCollection : ConfigurationElementCollection
{
public AllowRule this[int index]
{
get
{
return base.BaseGet(index) as AllowRule;
}
set
{
if (base.BaseGet(index) != null)
base.BaseRemoveAt(index);
this.BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new AllowRule();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AllowRule)element).Name;
}
}
public class AllowRule : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return this["name"] as string; }
}
[ConfigurationProperty("module", IsRequired = true)]
public string Module
{
get { return this["module"] as string; }
}
[ConfigurationProperty("subnetIP", IsRequired = true)]
public string SubnetIP
{
get { return this["subnetIP"] as string; }
}
[ConfigurationProperty("subnetMask", IsRequired = true)]
public string SubnetMask
{
get { return this["subnetMask"] as string; }
}
}
}

事实上,我大约10分钟前才知道答案。基本上,我并没有真正";钩住";将PhraseCollection正确地添加到GroupConfigElement。。。我也没有完全充实Phrase Collection

public class GroupConfigElement : ConfigurationElement
{
[ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
public string GroupName
{
get { return (string)this["groupname"]; }
set { this["groupname"] = value; }
}
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public PhraseCollection Phrases
{
get { return (PhraseCollection) base[""]; }
}
}
[ConfigurationCollection(typeof(PhraseConfigElement))]
public class PhraseCollection: ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new PhraseConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((PhraseConfigElement)element).Name;
}
public PhraseConfigElement this[int index]
{
get
{
return base.BaseGet(index) as PhraseConfigElement;
}
}
public new PhraseConfigElement this[string key]
{
get
{
return base.BaseGet(key) as PhraseConfigElement;
}
}
}
public class PhraseConfigElement: ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

}

关键的修复是";短语"释义然后我不得不将这两个公共方法添加到";短语集合"释义

谢谢!

相关内容

最新更新