Nested lists in custom ConfigSection

F

Frank Munsberg

So far I've found some examples for custom ConfigSection implementations that
did work but fall a bit short of what I'd need to store in my app.config file.

The XML that could store what I want looks sort of like this:

<Customers>
<Customer name="Customer1">
<SomeOtherData someValue="X" />
<Channels>
<add folderType="A" path="c:\foo" />
<add folderType="B" path="C:\foo1" />
</Channels>
</Customer>
<Customer name="Customer2">
<SomeOtherData someValue="X" />
<Channels>
<add folderType="X" path="C:\bar" />
</Channels>
</Customer>
</Customers>

The basic idea is to have a list of customers and each customer can have a
data element for something and a list of channels that contain some other
settings.

Can anyone tell me if it is even possible to load this kind of XML construct
with the default .Net 3.5 configuration API ?

Greets
Frank
 
P

Pavel Minaev

So far I've found some examples for custom ConfigSection implementations that
did work but fall a bit short of what I'd need to store in my app.config file.

The XML that could store what I want looks sort of like this:

<Customers>
  <Customer name="Customer1">
    <SomeOtherData someValue="X" />
    <Channels>
      <add folderType="A" path="c:\foo" />
      <add folderType="B" path="C:\foo1" />
    </Channels>
  </Customer>
  <Customer name="Customer2">
    <SomeOtherData someValue="X" />
    <Channels>
      <add folderType="X" path="C:\bar" />
    </Channels>
  </Customer>
</Customers>

The basic idea is to have a list of customers and each customer can have a
data element for something and a list of channels that contain some other
settings.

Can anyone tell me if it is even possible to load this kind of XML construct
with the default .Net 3.5 configuration API ?

With custom section groups, yes. Since
IConfigurationSectionHandler.Create() takes an XmlNode, then obviously
you can store arbitrary kind of XML there. For example, you could use
XmlSerializer.
 
F

Frank Munsberg

With custom section groups, yes. Since
IConfigurationSectionHandler.Create() takes an XmlNode, then obviously
you can store arbitrary kind of XML there. For example, you could use
XmlSerializer.

Actually I somehow figured it out using ConfigurationElement and
ConfigurationElementCollections.
The XML that's being used right now looks like this:

<CustomerSectionGroup>
<CustomerSection>
<CustomerCollection>
<Customer name="Customer1" subdirectory="bla">
<Authentication name="4711" keyfilename="private.key"/>
<ChannelCollection>
<Channel name="channel1" folderType="A"></Channel>
<Channel name="channel2" folderType="B"></Channel>
<Channel name="channel3" folderType="C"></Channel>
</ChannelCollection>
</Customer>
<Customer name="Customer2" subdirectory="bla">
<Authentication name="4712" keyfilename="private.key"/>
<ChannelCollection>
<Channel name="channel1" folderType="X"></Channel>
</ChannelCollection>
</Customer>
</CustomerCollection>
</CustomerSection>
</CustomerSectionGroup>

now what I did was to make five classes to navigate the Elements and
Collections named

CustomerSection, CustomerCollection, CustomerElement, ChannelCollection and
ChannelElement. the ...Collection classed derive from
ConfigurationElementCollection, the Element classed from Configurationelement.

The CustomerElement implementation looks like this, the other elements are
rather similar.

public class CustomerElement
{
[ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired
= true)]
public string Name
{
get
{
return ((string)(base["name"]));
}
set
{
base["name"] = value;
}
}
[ConfigurationProperty("subdirectory", DefaultValue = "", IsKey = false,
IsRequired = true)]
public string SubDirectory
{
get
{
return ((string)(base["subdirectory"]));
}
set
{
base["subdirectory"] = value;
}
}

[ConfigurationProperty("Authentication")]
public AuthenticationElement Authentication
{
get
{
return ((AuthenticationElement)(base["Authentication"]));
}
set
{
base["Authentication"] = value;
}
}


[ConfigurationProperty("ChannelCollection", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ChannelElementCollection), AddItemName =
"Channel")]
public ChannelElementCollection Channels
{
get { return ((ChannelElementCollection)(base["ChannelCollection"])); }
}
}

The implementation of the collection classes was mostly taken from the MSDN
documentation of ConfigurationElementCollection class.

The only thing I don't quite understand is, if I add a customer to the
collection I have to save the configuration before adding channels to that
customer. If I add a Customer and its channels in one go and then save
afterwards, the channels don't get saved to the app.config.

Does anyone know if I'm missing something here? should I post the rest of
the code for clarification ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top