How to read this custom configuration from App.config?
Asked Answered
F

4

10

How to read this custom configuration from App.config?

<root name="myRoot" type="rootType">
    <element name="myName" type="myType" />
    <element name="hisName" type="hisType" />
    <element name="yourName" type="yourType" />
  </root>

Rather than this:

<root name="myRoot" type="rootType">
  <elements>
    <element name="myName" type="myType" />
    <element name="hisName" type="hisType" />
    <element name="yourName" type="yourType" />
  </elements>
  </root>
Flavoring answered 2/6, 2011 at 18:20 Comment(1)
If these answers don't fully help you, please provide additional information so we can further assist.Unexceptionable
C
31

To enable your collection elements to sit directly within the parent element (and not a child collection element), you need to redefine your ConfigurationProperty. E.g., let's say I have a collection element such as:

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

And a collection such as:

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

I need to define the parent section/element as:

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

Notice the [ConfigurationProperty("", IsDefaultCollection = true)] attribute. Giving it an empty name, and the setting it as the default collection allows me to define my config like:

<testConfig>
  <test name="One" />
  <test name="Two" />
</testConfig>

Instead of:

<testConfig>
  <tests>
    <test name="One" />
    <test name="Two" />
  </tests>
</testConfig>
Caliban answered 11/6, 2011 at 8:32 Comment(0)
V
7

You can use System.Configuration.GetSection() method for reading custom configuration sections.

Refer to http://msdn.microsoft.com/en-us/library/system.configuration.configuration.getsection.aspx for knowing more about GetSection()

Vivienne answered 2/6, 2011 at 18:24 Comment(0)
U
4

Since this is not the standard config file format you'll have to open the config file as a XML document and then pull out the sections (using XPath for example). Open the document with this:

// Load the app.config file
XmlDocument xml = new XmlDocument();
xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Unexceptionable answered 11/6, 2011 at 7:35 Comment(0)
A
0

I think you can use

            XmlDocument appSettingsDoc = new XmlDocument();
            appSettingsDoc.Load(Assembly.GetExecutingAssembly().Location + ".config");
            XmlNode node = appSettingsDoc.SelectSingleNode("//appSettings");

            XmlElement element= (XmlElement)node.SelectSingleNode(string.Format("//add[@name='{0}']", "myname"));
            string typeValue = element.GetAttribute("type");

Hope this solves your problem. Happy Coding. :)

Animator answered 15/6, 2011 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.