Reading a collection from the App.config file

I'm trying to get a collection of Items from the configuration file of an application. Everything looks ok, but I always fetch 0 elements (regardless that I put on the configuration file...)

My code is:

using System.Configuration;

namespace CustomSettingConfiguration
{
    public class TestConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string) this["name"]; }
        }
    }

[ConfigurationCollection(typeof (TestConfigurationElement), AddItemName = "test")]
public class TestConfigurationElementCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TestConfigurationElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TestConfigurationElement) element).Name;
    }
}

public class TestConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("Tests", IsDefaultCollection = true)]
    public TestConfigurationElementCollection Tests
    {
        get { return (TestConfigurationElementCollection)this["Tests"]; }
    }
}
}

And the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
    <section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection" />
  </configSections>

  <TestConfigurationSection>
    <Tests>
      <test name="One" />
      <test name="Two" />
    </Tests>
  </TestConfigurationSection>

</configuration>

To use It:

  TestConfigurationSection a = new TestConfigurationSection();
  var tests = a.Tests;

Any idea??

Thanks in advance

Upvotes: 4

Views: 4159

Answers (2)

Jacob Proffitt
Jacob Proffitt

Reputation: 12768

Does it really need it's own configuration section? Personally, I seldom find it necessary to go beyond the simple settings in the project properties. Here's how I did it in a project where I wanted to use a list of sources that were allowed and disallowed. The object I wanted to save in configuration (in my case, user.config, but the principle is the same for app.config) is a plain c# object (it implements an interface that isn't germane to this discussion is all).

So to make it easy, I created a collection class for my object. This simplifies the setting-up part. Here's the class, in its entirety:

// This is mainly declared to ease use as a User Setting
public class SpellSourceCollection : List<SpellSource>
{
    public SpellSourceCollection() : base() { }
    public SpellSourceCollection(IEnumerable<SpellSource> ListToCopy)
        : this()
    {
        this.AddRange(ListToCopy);
    }
}

Remember that "SpellSource" has nothing special about it. Now, in the settings for the project, I can assign the Type as my collection object.

setting the property to SpellSourceCollection

You may have to "Browse" to the correct custom object. Once it's done, however, reading from app.config (or user.config) is a breeze. Here's what the config file looks like (slightly abbreviated).

<setting name="Sources" serializeAs="Xml">
    <value>
        <ArrayOfSpellSource xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <SpellSource>
                <Source>PFRPG Advanced Player's Guide</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>PFRPG Core</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Rival Guide</Source>
                <Allowed>false</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Combat</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Magic</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>Cyan</BackgroundColor>
            </SpellSource>
        </ArrayOfSpellSource>
    </value>
</setting>

Getting at the property is simply a matter of

SpellSourceCollection settingsSources = Properties.Settings.Default.Sources;
// do stuff or even later in your project, you can save this user setting
Properties.Settings.Default.Sources = settingsSources;
Properties.Settings.Default.Save();

You can apply that to your own project in similar fashion. The only mildly tricky bits are declaring the collection object and creating the setting in the project properties.

Upvotes: 1

the_joric
the_joric

Reputation: 12226

You should another code to load configuration settings:

TestConfigurationSection a = (TestConfigurationSection) System.Configuration.ConfigurationManager.GetSection("TestConfigurationSection");

also make sure that assemply is specified in your configuration file:

<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection, ConsoleApplication1" />

Upvotes: 3

Related Questions