1

I am trying to use the configuration Section and configuration Element. Not getting the value only getting the key. I have a app.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>   
    <sectionGroup name="ProductSettings">
      <section name="DellSettings" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>
  <ProductSettings>
    <DellSettings>
      <add key = "ProductNumber" value="20001" ></add>
      <add key =" ProductName" value="Dell Inspiron"></add>
      <add key ="Color" value="Black"></add>
      <add key =" Warranty" value ="2 Years" ></add>
    </DellSettings>
  </ProductSettings>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

Product Setting class

like this :

 public class ProductSettings : ConfigurationSection
    {
        [ConfigurationProperty("DellSettings", IsRequired = true)]
        public DellFeatures DellFeatures
        {
            get
            {
                return (DellFeatures)this["DellSettings"];
            }
        }
    }

DellFeaturesClass

like this which parses every element in the config

 public class DellFeatures : ConfigurationElement
    {
        [ConfigurationProperty("ProductNumber", IsRequired = true)]
        public int ProductNumber
        {
            get
            {
                return (int)this["ProductNumber"];
            }
        }

        [ConfigurationProperty("ProductName", IsRequired = true)]
        public string ProductName
        {
            get { return (string)this[nameof(ProductName)]; }
            set { this[nameof(ProductName)] = value; }

        }

        [ConfigurationProperty("Color", IsRequired = false)]
        public string Color
        {
            get
            {
                return (string)this["Color"];
            }
        }
        [ConfigurationProperty("Warranty", IsRequired = false)]
        public string Warranty
        {
            get
            {
                return (string)this["Warranty"];
            }
        }
    }

Main class code is like this:

    static void Main(string[] args)
    {
        var productSettings =
            ConfigurationManager.GetSection("ProductSettings/DellSettings") as NameValueCollection;
        if (productSettings == null)
        {
            Console.WriteLine("Product Settings are not defined");
        }
    }

I only get to see the keys not the value. Where am i doing wrong.

Brad Reiter
  • 31
  • 1
  • 8
  • Does this answer your question? [How to get the values of a ConfigurationSection of type NameValueSectionHandler](https://stackoverflow.com/questions/3461418/how-to-get-the-values-of-a-configurationsection-of-type-namevaluesectionhandler) – Erik T. Apr 07 '20 at 12:42

1 Answers1

0

The change has to happen in the following

  1. When you are setting the assembly and type (FQN), you have to register like below one

    <section name="BlogSettings" type="Fully.Qualified.TypeName.ProductSettings,   
     AssemblyName" />
    
  2. When you are trying to add the configuration, you have to be using like below

    <DellSettings
         ProductNumber="20001"  .../>
    

This should be done in the configuration side, when you are going to access the config values in the code, you can try the following

int productNo = DellSettings.Settings.ProductNumber;
Console.WriteLine(productNo);

In case of any working example, please refer the below link https://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx/

Saravanan
  • 7,637
  • 5
  • 41
  • 72