Consider the following configuration:
[HTML]<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<section name="DummySect ion" type="ConsoleAp plication1.Dumm ySection, ConsoleApplicat ion1" />
</configSections>
<DummySection >
<DummyElement name="1" description="fi rst element">
<ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
<ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
<ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
<ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
</DummyElement>
<DummyElement name="2" description="se cond element">
<ElementData name="2.1" attr1="2.1.1" attr2="2.1.2" />
<ElementData name="2.2" attr1="2.2.1" attr2="2.2.2" />
<ElementData name="2.3" attr1="2.3.1" attr2="2.3.2" />
<ElementData name="2.4" attr1="2.4.1" attr2="2.4.2" />
</DummyElement>
<DummyElement name="3" description="th ird element">
<ElementData name="3.1" attr1="3.1.1" attr2="3.1.2" />
<ElementData name="3.2" attr1="3.2.1" attr2="3.2.2" />
<ElementData name="3.3" attr1="3.3.1" attr2="3.3.2" />
<ElementData name="3.4" attr1="3.4.1" attr2="3.4.2" />
</DummyElement>
</DummySection>
</configuration>[/HTML]
Perfect, I have a sample configuration file that can be used to configure my application, so now I've gotta create the classes that read it...
The code is kind of long winded...
I've got (for brevity I've cut the code down to just the bare bones):
Expand|Select|Wrap|Line Numbers
- Public Class ConfigClass : ConfigurationSection{
- [ConfigurationCollection(GetType(DummyElementCollection), AddItemName:="DummyElement"]
- DummyElement Elements{
- get { return (DummyElementCollection)mybase("DummyElement"); }
- }
- //Collections
- Public Class DummyCollection : ConfigurationElementCollection{
- //Cut out code
- }
- Public Class ElementDataCollection : ConfigurationElementCollection{
- }
- //Configuration elements
- Public Class DummyElement : ConfigurationElement{
- }
- Public Class ElementData : ConfigurationElement{
- }
[HTML]<MySection>
<MyCollection A>
<add key="A1" value="blahA1" />
<add key="A2" value="blahA2" />
</MyCollectionA>
<MyCollection B>
<add key="B1" value="blahB1" />
<add key="B2" value="blahB2" />
</MyCollectionB>
</MySection>[/HTML]
I can make all these work just fine... I know about the
[ConfigurationEl ementCollection (GetType(DummyE lementCollectio n)AddItemName:= "DummyEleme nt"]
and
[ConfigurationEl ementCollection (GetType(Elemen tDataCollection ), AddItemName:="E lementData"]
but where do I put these? Ahead of my class declarations for my collections or on the property where the collection is referenced? In both spots I get errors that one parameter or another is missing.
What happens when you have elements that have attributes and are also collections? The major difference I see between the config file I want to use and the standard one is that I'd like to have attributes in elements that are also headers for collections.
If I have attributes in the collection header, my code bombs telling me that there are unrecognized attributes.
Can anyone point me in the direction of decent documentation for this type of thing?