473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Custom Configuration Sections (app.config/web.config)

balabaster
797 Expert 512MB
Okay, I decided that I needed to understand the whole custom configuration file bits and so I've spent some time playing around with it. It seems (in the most) relatively straight forward. However, I've come unstuck with one scenario that I'm trying to figure out the code for:

Consider the following configuration:

[HTML]<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DummySection" type="ConsoleApplication1.DummySection, ConsoleApplication1" />
</configSections>
<DummySection>
<DummyElement name="1" description="first 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="second 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="third 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
  1. Public Class ConfigClass : ConfigurationSection{
  2.   [ConfigurationCollection(GetType(DummyElementCollection), AddItemName:="DummyElement"]
  3.   DummyElement Elements{
  4.     get { return (DummyElementCollection)mybase("DummyElement"); }
  5. }
  6.  
  7. //Collections
  8. Public Class DummyCollection : ConfigurationElementCollection{
  9.   //Cut out code
  10. }
  11.  
  12. Public Class ElementDataCollection : ConfigurationElementCollection{
  13. }
  14.  
  15. //Configuration elements
  16. Public Class DummyElement : ConfigurationElement{
  17. }
  18.  
  19. Public Class ElementData : ConfigurationElement{
  20. }
Now, given that I have elements that are also collections, how do I account for this in my code? I've read many examples of custom sections using the standard syntax, i.e.:

[HTML]<MySection>
<MyCollectionA>
<add key="A1" value="blahA1" />
<add key="A2" value="blahA2" />
</MyCollectionA>
<MyCollectionB>
<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

[ConfigurationElementCollection(GetType(DummyElemen tCollection)AddItemName:="DummyElement"]

and
[ConfigurationElementCollection(GetType(ElementData Collection), AddItemName:="ElementData"]

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?
Jun 20 '08 #1
4 2669
int08h
28
You are very close to the solution, however it is not a very good practice in XML structure, because XML is a string semantic language, collection element should be presented in a collection wrap, my suggestion is:
Expand|Select|Wrap|Line Numbers
  1. <DummyElement name="1" description="first element">
  2.   <ElementData>
  3.     <ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
  4.     <ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
  5.     <ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
  6.     <ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
  7.   </ElementData>
  8. </DummyElement>
For your ElementData, you must implement a collection which inherits from ConfigurationElementCollection, let's call it ElementDataCollection

Then you should make a property of ElementDataCollection in your DummyElement class and apply below attribute to it
Expand|Select|Wrap|Line Numbers
  1. [ConfigurationProperty("ElementData")]
  2. [ConfigurationCollection(typeof(ElementDataCollection), AddItemName = "ElementData")]
Jun 22 '08 #2
balabaster
797 Expert 512MB
You are very close to the solution, however it is not a very good practice in XML structure, because XML is a string semantic language, collection element should be presented in a collection wrap, my suggestion is:
Expand|Select|Wrap|Line Numbers
  1. <DummyElement name="1" description="first element">
  2. <ElementData>
  3. <ElementData name="1.1" attr1="1.1.1" attr2="1.1.2" />
  4. <ElementData name="1.2" attr1="1.2.1" attr2="1.2.2" />
  5. <ElementData name="1.3" attr1="1.3.1" attr2="1.3.2" />
  6. <ElementData name="1.4" attr1="1.4.1" attr2="1.4.2" />
  7. </ElementData>
  8. </DummyElement>
For your ElementData, you must implement a collection which inherits from ConfigurationElementCollection, let's call it ElementDataCollection

Then you should make a property of ElementDataCollection in your DummyElement class and apply below attribute to it
Expand|Select|Wrap|Line Numbers
  1. [ConfigurationProperty("ElementData")]
  2. [ConfigurationCollection(typeof(ElementDataCollection), AddItemName = "ElementData")]
Yeah, I got an example working like that on Friday...and while I agree, syntactically it may be more um..."correct", it requires two extra classes that I personally think are unnecessary. I am currently using the "correct" method and was wondering how exactly I'd go about condensing my code.
Jun 23 '08 #3
balabaster
797 Expert 512MB
After having spent quite a few hours trawling for information regarding this subject and quite a few more deciphering it to figure out how to apply more complex configuration scenarios, I decided that the information available is poor at best. So I put together a demonstration solution for custom configuration and how to work with it.

I hope this is of help to people using custom configuration. If anyone can point out errors, omissions and areas for improvement, please yell.
Attached Files
File Type: zip ConfigurationExample.zip (68.5 KB, 171 views)
Jun 23 '08 #4
balabaster
797 Expert 512MB
I've also written a document to go with the demonstration code, to assist with interpretation...
Attached Files
File Type: zip CustomConfigurationSections.zip (20.3 KB, 156 views)
Jun 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: kathy | last post by:
What Application Configuration File is? And when/where/how to use it? Any documents on that? MSDN has somrthings scatter everywhere - hard to follow. thanks,
5
by: rdcpro | last post by:
In reading MSDN docs on creating custom Configuration sections, I found this page: ...
3
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on...
1
by: Paloma GarcĂ­a | last post by:
Dear all, I have created personalized configuration sections in my web project following the instructions described in this page...
6
by: Tabi | last post by:
Hi, I want to create a custom section in my web.config that can hold my custom values. I created a section in web.config as written below. <configSections> <section name="myCustomSection"...
3
by: Jason Richmeier | last post by:
Is it possible to create a custom configuration file (other than app.config) that can be processed by classes in the System.Configuration namespace? I have seen a ton of articles that talk about...
0
by: Alex Sadomov | last post by:
Hi all I have a custom configuration section in the App.config file of the console application: --------------------------------------------------------------------------- <configuration>...
6
by: Jeff Hegedus | last post by:
I have a dll that requires some configuration data. I put the configuration data in a custom configuration section in a config file that is loaded in the installation folder of the dll. If I...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.