6

G'da to everyone,

For hours I've been trying to figure how to read settings from app.config file:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="Databases" type="McFix.DatabaseSection, McFix"/>
  </configSections>

  <Databases>
    <Database name="database">
      <Tables>
        <Table name="be_sessions">
          <Columns>
            <Column name="sess_id">
            </Column>
          </Columns>
        </Table>
      </Tables>
    </Database>
  </Databases>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

The the code for custom handler classes is here, also copied below:

public class DatabaseSection : ConfigurationSection
{
    [ConfigurationProperty("Databases", IsDefaultCollection = false)]
    public DatabaseInstanceCollection Databases
    {
        get { return (DatabaseInstanceCollection)this["Databases"]; }
        set { this[""] = value; }
    }
}
[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap )]
public class DatabaseInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new DatabaseElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DatabaseElement)element).Name;
    }
}
public class DatabaseElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

public class TableSection : ConfigurationSection
{
    [ConfigurationProperty("Tables", IsDefaultCollection = true)]
    public TableInstanceCollection Tables
    {
        get { return (TableInstanceCollection)this["Tables"]; }
        set { this[""] = value; }
    }
}

[ConfigurationCollection(typeof(TableElement), AddItemName = "Table", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class TableInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TableElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TableElement)element).Name;
    }
}

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

public class ColumnSection : ConfigurationSection
{
    [ConfigurationProperty("Columns", IsDefaultCollection = true)]
    public ColumnInstanceCollection Columns
    {
        get { return (ColumnInstanceCollection)this["Columns"]; }
        set { this[""] = value; }
    }
}

[ConfigurationCollection(typeof(ColumnElement), AddItemName = "Column", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class ColumnInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new ColumnElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ColumnElement)element).Name;
    }
}

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

The problem is when I attempt to get "Databases" section via GetSection method:

Configuration Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
DatabaseSection DbConfig = Config.GetSection("Databases") as DatabaseSection;

program throws an ConfigurationErrorsException, reporting "Unrecognized element 'Database'", although it does that after going through get method of DatabaseSection, even though I define AddItemName for DatabaseInstanceCollection as "Database". Am I missing something, attribute that will let underlying code read the app.config correctly?

4
  • 2
    What's wrong with SO formatting? Also, you'll need to do more work for this. Describe what didn't work for you and what your problem is. Nobody is going to read through your code to figure out what went wrong. Give examples of your failures. Apr 7, 2011 at 4:41
  • 1
    Are you asking how to read in the config settings using the classes you defined? I didn't see anything in your code where you actually read it in. Apr 7, 2011 at 4:43
  • If you want to post code, XML or data samples, highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it! SO will format and display it quite nicely if you do!
    – marc_s
    Apr 7, 2011 at 4:52
  • My apologies to everyone, having not posted questions on the forums for a while (mostly because I was answering those of others), I have lost the touch. Apr 7, 2011 at 23:17

2 Answers 2

7

Obligatory links:

After a cursory look it seems like your problem is on this line:

[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap )]

This indicates that the .config file should look like this:

<Databases>
    <add><!-- Database goes here --></add>
</Databases>

I.e. your Databases element is expecting an "add" child, to indicate that an item is to be added to the colleciton.

You should try changing the AddItemName property from "add" to "Database":

[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "Database", CollectionType = ConfigurationElementCollectionType.BasicMap )]

(I've not had a chance to test this, there may be other problems)

3
  • Thanks Kragen, I've browsed Q & A for similar Application Configuration for .NET here and visited links you mentioned before but alas. I have attempted to change AddItemName to "Database" but to no avail either. I have a suspicion that "Database" element has to be declared for section "Databases", although I would've thought it's done through "AddItemName" property of ConfigurationCollection attribute. Apr 8, 2011 at 0:24
  • 1
    @user696034 Does it make a difference to the error message? I've also spotted that the DatabaseElement configuration element has no reference to the TableSection configuration section, and so I expect that would probably cause a failure even after resolving the above.
    – Justin
    Apr 8, 2011 at 0:53
  • 1
    I've managed to get the whole thing going, but you're right Kragen, it was necessary to add TableInstanceCollection to DatabaseElement and ColumnInstanceCollection to TableElement. Also, the DatabaseSection had to be made like this to work: Apr 8, 2011 at 1:16
1

You're correct, Kragen, I had to remove Table/ColumnSection and add Table/ColumnInstanceCollection to Database/TableElement. DatabaseSection' Databases property had to be like:

[ConfigurationProperty("", IsDefaultCollection = true)]
        public DatabaseInstanceCollection Databases
        {
            get { return (DatabaseInstanceCollection)this[""]; }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.