【问题标题】:ConfigurationCollection - Unrecognized element 'entry'ConfigurationCollection - 无法识别的元素“条目”
【发布时间】:2013-10-14 04:57:42
【问题描述】:

我希望有人能帮我弄清楚我在这里做错了什么。我的 web.config 中有一个自定义部分:

<invalidCharactorGroup>
 <entries>
   <entry name="entry1" oldChar="É" newChar="E"/>
   <entry name="entry2" oldChar="B" newChar="C"/>
 </entries>
</invalidCharactorGroup>

声明

<sectionGroup name="invalidCharactorGroup">
  <section name="entries" 
           type="WSTG.Config.InvalidCharactorSection, WSTGEcomLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
           allowLocation="true" 
           allowDefinition="Everywhere" />          
</sectionGroup>

它得到这个错误:

Unrecognized element 'entry'.

这是我的课程:

public class InvalidCharactorSection : ConfigurationSection
{
    [ConfigurationProperty("entries")]
    [ConfigurationCollection(typeof(InvalidEntryElementCollection), AddItemName = "entry")]
    public InvalidEntryElementCollection Entries
    {
        get { return ((InvalidEntryElementCollection)(base["entries"])); }
        set { base["entries"] = value; }
    }
}


public class InvalidEntryElementCollection : ConfigurationElementCollection
{
    internal const string PropertyName = "Entry";

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMapAlternate;
        }
    }
    protected override string ElementName
    {
        get
        {
            return PropertyName;
        }
    }

    protected override bool IsElementName(string elementName)
    {
        return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
    }


    public override bool IsReadOnly()
    {
        return false;
    }


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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((Entry)(element)).name;
    }

    public Entry this[int idx]
    {
        get
        {
            return (Entry)BaseGet(idx);
        }
    }
}


public class Entry : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
    [ConfigurationProperty("oldChar", DefaultValue = "", IsKey = false, IsRequired = true)]
    public string oldChar
    {
        get { return (string)base["oldChar"]; }
        set { base["oldChar"] = value; }
    }


    [ConfigurationProperty("newChar", DefaultValue = "", IsKey = false, IsRequired = true)]
    public string newChar
    {
        get { return (string)base["newChar"]; }
        set { base["newChar"] = value; }
    }

}

【问题讨论】:

    标签: c# configuration .net-3.5 web-config


    【解决方案1】:

    您似乎已将您的 PropertyName 声明为“Entry”,这与“entry”不同。

    更新

    我尝试了你的实际代码,我只需要在配置文件中更改你的节组定义:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <!--<sectionGroup name="invalidCharactorGroup">
          <section name="entries"
                   type="WSTG.Config.InvalidCharactorSection, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                   allowLocation="true"
                   allowDefinition="Everywhere" />
        </sectionGroup>-->
        <section name="invalidCharactorGroup" type="WSTG.Config.InvalidCharactorSection, ConsoleApplication1"/>
      </configSections>
      <invalidCharactorGroup>
        <entries>
          <entry name="entry1" oldChar="É" newChar="E"/>
          <entry name="entry2" oldChar="B" newChar="C"/>
        </entries>
      </invalidCharactorGroup>
    </configuration>
    

    将“ConsoleApplication1”更改为您的二进制文件的名称。您应该能够通过以下方式读取您的配置:

    InvalidCharactorSection section = ConfigurationManager.GetSection("invalidCharactorGroup") as InvalidCharactorSection;
    
    Entry entry = section.Entries[0];
    

    【讨论】:

    猜你喜欢
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2011-12-14
    相关资源
    最近更新 更多