1

I have a small problem with a custom configuration section in App.config. The problem is: I have a CustomElement (let's call it 'a') whose properties CustomElement another (let's call it 'b').

I would like to have b as a value not null only if specified as a secondary element.

Eg

In this case, I have a b as not null, and that's ok.

<a prop_a1="" prop_a2="">
    <B prop_b1 = "" />
</ A>

But, in the following case, b should be null, but it is not.

<a prop_a1="" prop_a2="" />

Is it possible to have a null value in case 2?

Thanks to all those who want to help me.

public class Cfg : ConfigurationElement
{
        [ConfigurationProperty("prop", IsRequired = true)]
        public ErrorCfg Prop
        {
            get { return (string)this["prop"]; }
            set { this["prop"] = value; }
        }
}

public class ErrorCfg : ConfigurationElement
{
    [ConfigurationProperty("prop1", IsRequired = true)]
    public string Prop1
    {
        get { return (string)this["prop1"]; }
        set { this["prop1"] = value; }
    }

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

if (_cfgItem.ErrorCfg != null)
{....}

In this case ErrorCfg is not null, but Prop1 and Pro2 are empty string

Luigi Russo
  • 105
  • 6

1 Answers1

1

I think this is not possible. But you can find out if the ConfigurationElement is present in the configuration file:

if (_cfgItem.ErrorCfg.ElementInformation.IsPresent)
{....}
Stefan Bormann
  • 643
  • 7
  • 25