tags:

views:

1612

answers:

3

Suppose I have a Custom Config File which corresponds to a Custom-defined ConfigurationSection and Config elements. These config classes are stored in a library.

Config File looks like this

<?xml version="1.0" encoding="utf-8" ?>
<Schoool Name="RT">
  <Student></Student>
</Schoool>

How can I programmatically load and use this config file from Code?

I don't want to use raw XML handling, but leverage the config classes already defined.

+4  A: 

Check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

Highly recommended, well written and extremely helpful!

You can't really load any XML fragment - what you can load is a complete, separate config file that looks and feels like app.config.

If you want to create and design your own custom configuration sections, you should definitely also check out the Configuration Section Designer on CodePlex - a Visual Studio addin that allows you to visually design the config sections and have all the necessary plumbing code generated for you.

Marc

marc_s
+1  A: 

The configSource attribute allows you to move any configuration element into a seperate file. In your main app.config you would do something like this:

<configuration>
  <configSections>
    <add name="schools" type="..." />
  </configSections>

  <schools configSource="schools.config />
</configuration>
Jørn Schou-Rode
True - but even in this case, you need to create your own custom configuration element for the <configSections> definition part.
marc_s
@marc_s: as I read the OP, the author already has a "Custom-defined ConfigurationSection", but is unsure how to make utilize this as a "Custom Config File". I might be wrong though.
Jørn Schou-Rode
You could be right :-) Guess I didn't read the question carefully enough - mea culpa.
marc_s
A: 

Use XmlElement.SelectNodes.

e.g:

var school= ConfigurationManager.GetSection("Schoool")
XmlNodeList nodeList= school. SelectNodes("Student");
string studentValue= nodeList[0].Value;
Ngu Soon Hui
The OP specifically mentioned he didnt' want to use straight XML processing....
marc_s