Rastko
Rastko

Reputation: 940

Loading of the custom configuration section issue

I have an issue with loading from the custom configuration section.

What is the problematic part. I have two elements (First and Second) that I have defined under the XSD schema within the choice element. User can choose only one when he/she is configuring. Let say something like this :

<customSection>
  <First attribute ="test" />    
</customSection>

or

<customSection>
  <Second attribute ="test" attribute2 ="np" />  
</customSection>

When I load that configuration, in both cases configuration elements First and Second will be loaded, they will not be null (objects will be empty). How to accomplish, per instance in first case, that Second object is null ?

Thank you

Upvotes: 0

Views: 1222

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

First you create class of mapping

public class YourCustomConfig : ConfigurationSection 
{
 ....
}

full sample here : http://nnish.com/2009/09/17/custom-configuration-section-in-c/

Second you get your datas

YourCustomConfig section = ConfigurationManager.GetSection("customSection") as YourCustomConfig;

Upvotes: 2

Rastko
Rastko

Reputation: 940

I have found what I needed. Each configuration element has property ElementInformation (http://msdn.microsoft.com/en-us/library/system.configuration.elementinformation.aspx), where is further present property IsPresent that indicates does specific configuration element was present in configuration file. Official description :

IsPresent : Gets a value indicating whether the associated ConfigurationElement object is in the configuration file.

With this I can track which elements are loaded and which one are not. Previously I could not say is something equal or not with null. It will be never equal with null independently with that does it has values.

Upvotes: 0

Related Questions