Question on ConfigurationElementCollection design

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 2 questions on using element collections when building a custom
section handler. I'm using the "declarative" model and I can post the code
if necessary. Basically I'm following the example described here:

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

My code works and the XML in app.config for my custom section looks like this:

<MySection name="x"
<Stores>
<add name="store1" addr="x" />
<add name="store2" addr="y" />
</Stores>
</MySection>

But I have 2 sort of "cosmetic" questions about changing the XML:

1. If I change "<add..." to "<Store..." it gives a runtime error that it
doesn't recognize the Store element. But I don't see anywhere in my code
where I tell the system to look for the "add" element, so why doesn't it
complain about that? I'd prefer the "<Store..." syntax but can't see how to
override or redefine this behavior.

2. In some cases it might also be nicer to skip the outermost "<Stores>"
element and just have this:

<MySection name="x"
<Store name="store1" addr="x" />
<Store name="store2" addr="y" />
</MySection>

But I don't see how to arrange for this in the declarations of a
ConfigurationElementCollection. It seems to require the outermost element to
key on its name. What I want is sort of an "unnamed" collection of Stores.
 
Hi Tim,
2. In some cases it might also be nicer to skip the outermost "<Stores>"
element and just have this:

<MySection name="x"
<Store name="store1" addr="x" />
<Store name="store2" addr="y" />
</MySection>

But I don't see how to arrange for this in the declarations of a
ConfigurationElementCollection.

This one I *am* sure of. A ConfigurationSection can contain
ConfigurationElementCollections, or individual ConfigurationElements. Each
individual ConfigurationElement can (and should) have its own name. Each
ConfigurationElement in a ConfigurationSection is a property of that
ConfigurationSection. That is, the number is fixed by the class definition.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
So I think what you're saying is that if you want XML that's useable by a
ConfigurationElementCollection, you *have* to have a "<Stores>" parent
element while its child element's names can all be the same, ie "<add ...>"
or preferably "<Store ...>" in my example. And that even though XML with
same-named elements might be well-formed, it's not useable in the Collection
context. Is that about right?

Tim
 
"Stores" is a reference to the Collection itself. Each "add" member is a
Collection element.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
I understand, but where is it determined that the name of each member element
must be "add"? Why can't I make it anything I want, like "Store"? Even in
the configSections collection itself, its members are not called "add" but
"section". I'd just like to follow that model to make my member elements
more meaningful.

Tim
 
That's something I have not done before. I have never been able to find any
reference that tells how this is done. It may have something to do with the
AddElementName property of the ConfigurationElementCollection, but again, I
just don't know.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Ok, I found out how to do this based on your last message. In the
declarative approach I'm using I just add the AddItemName "decoration" to the
attribute like this:

[ConfigurationProperty("DataStores", IsDefaultCollection=false, IsRequired =
true)]
[ConfigurationCollection(typeof(DataStoresCollection),
AddItemName="Store")]
public DataStoresCollection DataStores
{
get
{
return (DataStoresCollection)this["DataStores"];
}
}

There are also decorations to override ClearItemsName and RemoveItemName
(from the normal "clear" and "remove" labels), but I don't currently need
them since I'm not support the set operation, only the get.

So now my xml can look like this, which I like better:

<MySection name="x"
<Stores>
<Store name="store1" addr="x" />
<Store name="store2" addr="y" />
</Stores>
</MySection>

Thanks for helping out on this one.

Tim
 
Hey, thanks for sharing your solution!

--
:-D,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Back
Top