Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 342739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:49:57+00:00 2026-05-12T10:49:57+00:00

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

  • 0

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:49:57+00:00Added an answer on May 12, 2026 at 10:49 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a custom section in the App.config file and all the Configuration
I have defined two endpoints in my App.Config file as <system.serviceModel> <services> <service name=HostDirectAddress.ITestService
I have a custom configuration section registered in the app/web.config, let's call it MySection
I have the following ASP.NET Membership section defined in the Web.config file: <membership defaultProvider=AspNetActiveDirectoryMembershipProvider>
Suppose I define a configuration section in an ASP.NET web.config like: <?xml version=1.0?> <configuration>
I have this variable defined in my web.config file : <appSettings> <add key =version
I have an app.config section I want to define, as a simple IDictionary<string, IDictionary<string,
I am writing my own custom configuration section and have a ConfigurationProperty defined in
I have a class Bar that has a user-defined list of config keys and
I have a custom 404 view defined in my Pyramid app: @view_config(context=HTTPNotFound, renderer='404.pt') def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.