Using the following set of classes:

public class MyConfigSection : ConfigurationSection { [ConfigurationProperty("Items")] public ItemCollection Items { get { return ((ItemCollection)(base["Items"])); } } } [ConfigurationCollection(typeof(ItemElement), AddItemName = "Item")] public class ItemCollection : ConfigurationElementCollection { ... } public class ItemElement : ConfigurationElement { ... } 

I organize the reading of the configuration section of the following form:

 <MySection> <Items> <Item ... /> <Item ... /> <Item ... /> </Items> </MySection> 

Is it possible to somehow customize the reading, if I want the section to look like

 <MySection> <Item ... /> <Item ... /> <Item ... /> </MySection> 

those. so that the elements of the collection are not enclosed in <Items></Items> , but read directly from the section.

    1 answer 1

    Try using the IsDefaultCollection parameter and the empty string as a key:

     public class MyConfigSection : ConfigurationSection { [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)] public ItemCollection Items { get { return ((ItemCollection)(base[""])); } } } 
    • one
      It worked. With null instead of the empty string, too. (Wow! For the first time I see a place where string.Empty cannot be used instead of "" ). - i-one
    • @ i-one: yep, we need a string literal there. - VladD