13

I use IConfigurationSectionHandler interface to get information about my custom config section. But it's deprecated and I want to use ConfigurationSection instead.

How to create custom ConfigurationSection with this view and using ConfigurationSection instead IConfigurationSectionHandler:

<CustomSectionBlaBla>
   <Parent name="DB">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
    ...
   <Parent name="UI">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
<CustomSectionBlaBla/>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109
  • You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject. - [Unraveling the mysteries of .NET 2.0 configuration](http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx) - [Decoding the mysteries of .NET 2.0 configuration](http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration2.aspx) - [Cracking the mysteries of .NET 2.0 configuration](http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration3.aspx) Highly recommended, well written and extremely helpful! It should show you how to get to your desired result - step by step. The other it – marc_s Nov 02 '11 at 15:43

1 Answers1

22

Here's an example of a configuration section that I created. Should point you in the right direction.

public class ImportConfiguration : ConfigurationSection
{
    [ConfigurationProperty("importMap")]
    public ImportMapElementCollection ImportMap
    {
        get
        {
            return this["importMap"] as ImportMapElementCollection;
        }
    }
}

public class ImportColumnMapElement : ConfigurationElement
{
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
    public string LocalName
    {
        get
        {
            return this["localName"] as string;
        }
        set
        {
            this["localName"] = value;
        }
    }

    [ConfigurationProperty("sourceName", IsRequired = true)]
    public string SourceName
    {
        get
        {
            return this["sourceName"] as string;
        }
        set
        {
            this["sourceName"] = value;
        }
    }
}

public class ImportMapElementCollection : ConfigurationElementCollection
{
    public ImportColumnMapElement this[object key]
    {
        get
        {
            return base.BaseGet(key) as ImportColumnMapElement;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "columnMap";
        }
    }

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if (!String.IsNullOrEmpty(elementName))
            isName = elementName.Equals("columnMap");
        return isName;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImportColumnMapElement)element).LocalName;
    }
}

And here it is being used in the web.config:

<importConfiguration>
    <importMap>
        <columnMap localName="PropertyID" sourceName="Detail Number"/>
        <columnMap localName="DateOfOpen" sourceName="Open Date &amp; Time"/>
        <columnMap localName="StartTime" sourceName="Open Date &amp; Time"/>
        <columnMap localName="ClosingTime" sourceName="Close Date &amp; Time"/>
        <columnMap localName="StreetAddress" sourceName="Street Address"/>
    </importMap>
</importConfiguration>
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • 3
    For completeness, implement with : `ImportConfiguration columns = (ImportConfiguration)ConfigurationManager.GetSection("importConfiguration"); foreach (ImportColumnMapElement col in columns.ImportMap) { Debug.Write(col.localName + col.sourceName); }` – amackay11 Sep 22 '16 at 12:59