How to write code for this XML configuration section?

W

W. Jordan

Hello,

I would like to add a section in the configuration file like that:

<job name="cleanup">
<executor type="Test.MyExecutor"/>
<parameters>
<string name="outputFile" value="C:\output.log" />
<int name="maxValue" value="12" />
<timespan name="timespan" value="0:0:15"/>
</parameters>
</job>

A job can have multiple (0 to n) parameters. The parameters can be
some values, with a name, of any types.

How can I put it into the exe configuration file? And what kind of classes
should I build to support the configuration?
 
K

Kevin Spencer

What version of the .Net Framework?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
W

W. Jordan

Oops, it is .net 2.0.

I saw something like ConfigurationSection, ConfigurationElement,
ConfigurationSectionGroup, and some other classes, but I did not
see an appropriate example of how to implement the XML config
that I posted at the beginning of this thread.

--


Best Regards,
W. Jordan
 
K

Kevin Spencer

I'm not sure where you're looking, and therefore where you "saw" what you
saw. Since I don't know what resources you have available locally, I can
only point you to some references online.

First, the documentation for the .Net Framework 2.0 can be found at:
http://msdn2.microsoft.com/en-us/library/default.aspx

Notice that this is MSDN2, not MSDN.

Here are several other salient points of interest with regards to working
with Configuration in .Net 2.0:

http://msdn2.microsoft.com/en-us/library/kza1yk3a(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/sbk7ee6x(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.configuration(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/1fk1t1t0(VS.80).aspx

Now, as to the specifics, you will want to look over the
System.Configuration Namespace:

http://msdn2.microsoft.com/en-us/library/system.configuration(VS.80).aspx

There are several classes in particular that you will work with most often:

http://msdn2.microsoft.com/en-us/library/system.configuration.configuration(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationelement(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/li...on.configurationelementcollection(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsection(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/li...on.configurationsectioncollection(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsectiongroup(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/li...nfigurationsectiongroupcollection(VS.80).aspx
( Look for the sample code included)

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsettings(VS.80).aspx
( Look for the sample code included)

These are your major players. the .Net Framework 2.0 has a much more
powerful and strongly-typed Configuration model. You will need to create
some custom Configuration classes to work with your custom Configuration
Sections.

The bottom-most class is the ConfigurationElement class, which is used for
individual Configuration Elements, at the lowest node level of the XML.
Above this, you have the ConfigurationElementCollection, which manages a
collection of ConfigurationElements.

Generally, you will also need a ConfigurationSection, and possibly a
ConfigurationSectionGroup class to group your ConfigurationElements into
Sections.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
W

W. Jordan

Hello Kevin,

I am sorry that I am inexperienced. I have the .Net Framework 2.0
Documentation installed on my machine. Thanks for pointing out the
locations of the online resources. I will learn those examples and
try to find out a way to implement my XML configuration sections.


--


Best Regards,
W. Jordan
 
K

Kevin Spencer

Don't be discouraged. When I first started working with the new
configuration model, I had a hard time figuring it out myself.
Unfortunately, that was about a year ago, and I had nobody to point me to at
least the right classes and points of reference. Once I figured those out,
the rest was not so difficult.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
G

Guest

There was a really excellent article on "The Last Configuration File I'll
Ever Need" that applied to .Net v1.0.

http://www.codinghorror.com/blog/archives/000161.html

But I've never seen any update anywhere related to .Net v2.0. It basically
uses XML serialization to make generic section readers, as I understand it.
Do you have any insight on an updated version of this valuable technique for
v2.0?
 
K

Kevin Spencer

Configuration in .Net 2.0 is fundamentally different than in .Net 1.1. I
would advise you to stick with the resources I posted!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

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