2

I have defined a config section in my app.config in the following way:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="RegisterCompanies"
             type="ConfigTest.RegisterCompaniesConfig, ConfigTest" 
             allowLocation="true" 
             allowDefinition="Everywhere"/>
          </configSections>      
  <RegisterCompanies>
    <Companies>
      <Company name="Tata Motors" code="Tata"/>
      <Company name="Honda Motors" code="Honda"/>
    </Companies>
  </RegisterCompanies>      
      </configuration>

To read this information i have created three classes in such way :RegisterCompaniesConfig class

public class RegisterCompaniesConfig : ConfigurationSection
    {
        public static RegisterCompaniesConfig GetConfig() 
        {
            string path = Path.Combine(Application.StartupPath, "ConfigTest.exe.config");
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
            RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;
            return serviceSection;
            //return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies");
        }

        [System.Configuration.ConfigurationProperty("Companies")]
        public Companies Companies 
        {
            get
            {
                object o = this["Companies"]; return o as Companies;
            }
        }
    }

then Companies class:

public class Companies : ConfigurationElementCollection
    {
       [System.Configuration.ConfigurationProperty("Company")]
        public Company this[int index] 
        {
            get
            {
                return base.BaseGet(index) as Company;
            } 
            set 
            { 
                if (base.BaseGet(index) != null)
                { base.BaseRemoveAt(index); } this.BaseAdd(index, value);
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new Company();
        }

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

and the last one is Company class:

public class Company : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name 
        {
            get 
            {
                return this["name"] as string; 
            } 
        }
        [ConfigurationProperty("code", IsRequired = true)]
        public string Code
        {
            get
            {
                return this["code"] as string; 
            } 
        }
    }

after that when i want to acess the section by calling following method

var config = RegisterCompaniesConfig.GetConfig();

i get the error :Configuration system failed to initialize Please anyone have a look on this above code , where is the problem , it's looking everything is fine for me....

Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

1 Answers1

0

Having just run your code, the error I received was "The element <Company> may only appear once in this section" on the line:

RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;

That seems to indicate that you can only, with the code you've currently got, have one company element in there.

In the past I've used the following without any problems:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Libraries">
      <section name="MyLibrary" type="System.Configuration.NameValueSectionHandler,system, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
    </sectionGroup>
  </configSections>

  <Libraries>
    <MyLibrary>
      <add key="Test" value="Test1"/>
    </MyLibrary>
  </Libraries>
</configuration>

Which I've then accessed with code like:

public string GetValue(string configurationKey, string defaultValue)
{
  NameValueCollection _config = (NameValueCollection)ConfigurationManager.GetSection("Libraries/MyLibrary");
  string result = (_config == null) ? null : _config[configurationKey];
  return (result == null ? defaultValue : result);
}

If you don't have to have attributes called "name" and "code" then you could just use the code above, otherwise you could use Reflector to get an idea of what the NameValueCollection does and work from there!

Rob
  • 45,296
  • 24
  • 122
  • 150
  • 1
    i found the bug , actuallt when i replaced the company element with add it's working fine.. anyway thanx for guidining me – Embedd_0913 Aug 25 '09 at 11:16