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.