Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have to add a configuration tag in app.config, how can i access the tag values in C#.I have already googled and got some solutions. I tried that and got the below error

"Only one <configsections> element allowed per config file and if present must be the first child of the root
<configuration>
element."

My App.Config is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="Category" type="Hang.MyConfigSection" />

  </configSections>

  <Category>
    <add name="Animals" FilePath="Animals.txt" />
    <add name="Cities" FilePath="Cities.txt" />
  </Category>
</configuration>



and C# Code is
C#
public class MyConfigSection : ConfigurationSection
   {
       [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
       public MyConfigInstanceCollection Instances
       {
           get { return (MyConfigInstanceCollection)this[""]; }
           set { this[""] = value; }
       }
   }

   public class MyConfigInstanceCollection : ConfigurationElementCollection
   {
       protected override ConfigurationElement CreateNewElement()
       {
           return new MyConfigInstanceElement();
       }

       protected override object GetElementKey(ConfigurationElement element)
       {
           //set to whatever Element Property you want to use for a key
           return ((MyConfigInstanceElement)element).Name;
       }
   }

   public class MyConfigInstanceElement : ConfigurationElement
   {
       //Make sure to set IsKey=true for property exposed as the GetElementKey above
       [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
       public string Name
       {
           get { return (string)base["name"]; }
           set { base["name"] = value; }
       }

       [ConfigurationProperty("FilePath", IsRequired = true)]
       public string Code
       {
           get { return (string)base["FilePath"]; }
           set { base["FilePath"] = value; }
       }
   }


and calling by
C#
var config = ConfigurationManager.GetSection("Category")
                 as MyConfigSection;




Where am i missing?...


Thanks All.
Posted

1 solution

Dear Friend,

Your error says it all. Correct your code like this:-

C#
<configuration>
  <configsections>
    <section name="Category" type="Hang.MyConfigSection" />
  </configsections>
  <startup>
    <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <category>
    <add name="Animals" filepath="Animals.txt" />
    <add name="Cities" filepath="Cities.txt" />
  </category>
</configuration>


for reference:-Visit (^)

configSections must be the first child of the root element:

This link will help you out:- Access Custom Tag in app.config


Please mark it as your answer if it helps you out.

Regards

Varun Sareen
 
Share this answer
 
Comments
Geo Jackson 24-Apr-15 8:37am    
Thanks, getting this error "An error occurred creating the configuration section handler for Category: Could not load type 'Hang.MyConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" ..
Varun Sareen 24-Apr-15 8:56am    
https://msdn.microsoft.com/en-us/en-en/library/2tw134k3.aspx

go to this link for correction
Varun Sareen 24-Apr-15 8:56am    
http://stackoverflow.com/questions/452004/c-sharp-configurationmanager-getsection-could-not-load-file-or-assembly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900