2

I hope someone can help me figure out what I am doing wrong here. I have a custom section in my web.config:

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

Declaration

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

It get this error:

Unrecognized element 'entry'.

Here are my classes:

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

}
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64

1 Answers1

2

Seems that you've declared your PropertyName as "Entry", which is different from "entry".

UPDATE

I tried your actual code, I just had to change your section group definition in the config file for this one:

<?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>

Change "ConsoleApplication1" to the name of your binary. You should be able to read your configuration with something like this:

InvalidCharactorSection section = ConfigurationManager.GetSection("invalidCharactorGroup") as InvalidCharactorSection;

Entry entry = section.Entries[0];
Dani Carbonell
  • 477
  • 5
  • 10
  • changing the PropertyName to "entry", did not work. Same error – PhillyNJ Oct 07 '13 at 14:55
  • You can also check out this question: http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection it may help you – Dani Carbonell Oct 07 '13 at 15:23