0

What I'm trying to achieve is reading an app.config param that looks like this:

    <SomeConfig>
      <SomeParam>SomeText</SomeParam>
    </SomeConfig>

Code property declaration is like this

    [ConfigurationProperty("SomeParam")]
    public string SomeParam
    {
        get { return (string)this["SomeParam"]; }
        set { this["SomeParam"] = value; }
    }

However, I'm getting this error message on app start: "Property 'SomeParam' is not a ConfigurationElement"
How can I declare it correctly?

user626528
  • 13,999
  • 30
  • 78
  • 146
  • [Check][1] these link might help you.!!! [1]: http://stackoverflow.com/questions/7044871/how-do-i-use-net-custom-configurationelement-properties-on-descendent-elements – H. Mahida Feb 05 '15 at 04:49
  • @H. Mahida, unfortunately it can't as I need to store the value as an element body rather than element attribute. – user626528 Feb 05 '15 at 04:55

2 Answers2

1

Solution:

Your App.config should look like:

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SomeConfig" type="ConfigReader.SomeConfigSection,ConfigReader" />
  </configSections>
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <SomeConfig>
    <SomeParam>SomeText</SomeParam>
  </SomeConfig>
</configuration>

Program.cs

using System;
using System.Configuration;
using System.Xml;

namespace ConfigReader
{
    class Program
    {
        static void Main(string[] args)
        {
            SomeConfigSection configSection = ConfigurationManager.GetSection("SomeConfig") as SomeConfigSection;

            if (configSection != null)
                Console.WriteLine("Value={0}", configSection.SomeParam.Value);    
        }
    }
    public class SomeConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("SomeParam")]
        public SomeParamElement SomeParam
        {
            get { return this["SomeParam"] as SomeParamElement; }
            set { this["SomeParam"] = value; }
        }
    }

    public class SomeParamElement:ConfigurationElement
    {
        protected override void DeserializeElement(XmlReader reader, bool s)
        {
            Value = reader.ReadElementContentAs(typeof(string), null) as string;
        }
        public string Value { get; private set; }
    }
}

EDIT: Screenshot

enter image description here

Vinkal
  • 2,964
  • 1
  • 19
  • 19
0

I think you will need to override the OnDeserializeUnrecognizedElement . Please take a look at this answer.

Using the above approach, here is how I was able to achieve the result for your requirement:-

My SomeConfigSection class looks like this:-

public class SomeConfigSection : ConfigurationSection
{
    [ConfigurationProperty("SomeConfig", IsRequired = true)]
    public string SomeConfig
    {
        get { return (string)base["SomeConfig"]; }
        set { base["SomeConfig"] = value; }
    }

    XElement _SomeParam;
    public XElement SomeParam
    {
        get { return _SomeParam; }
    }

    protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
    {
        if (elementName == "SomeParam")
        {
            _SomeParam = (XElement)XElement.ReadFrom(reader);
            return true;
        }
        else
            return base.OnDeserializeUnrecognizedElement(elementName, reader);
    }
}

My App.config looks like this:-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SomeConfig" type="ConfigTest.SomeConfigSection,ConfigTest" />
  </configSections>
  <SomeConfig>
    <SomeParam>SomeText</SomeParam>
  </SomeConfig>
</configuration>

In my form, below is how I read the value :-

    SomeConfigSection configSection = ConfigurationManager.GetSection("SomeConfig") as SomeConfigSection;

    if (configSection != null)
        label1.Text= configSection.SomeParam.Value;

Hope This Helps!

Community
  • 1
  • 1
amitthk
  • 1,115
  • 1
  • 11
  • 19
  • It doesn't seem to work in this case, as exception is thrown before this handler gets called for my property's element. – user626528 Feb 05 '15 at 07:15
  • Hello, thanks for replying. I updated the reply with code which worked for me. If the exception is coming from some other place, then we will need to analyze the stack-trace. For me the above code seems to give results correctly. – amitthk Feb 05 '15 at 07:57