views:

245

answers:

2

Unless I am doing something wrong, the way I am supposed to use ConfigurationSection, ConfigurationElement and ConfigurationElementCollection, would require me to format my configuration section like so:

<serviceAuthorization>
    <credentials>
        <login username="system" password="password" mode="include">
            <services>
                <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
                <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
            </services>
        </login>
        <login username="test" password="pass" mode="exclude" />
    </credentials>
</serviceAuthorization>

I would much prefer if I had a bit more say in the format. I would like to format my section like this:

<serviceAuthorization>
    <login username="system" password="password" mode="include">
        <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
        <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
    </login>
    <login username="test" password="pass" mode="exclude" />
</serviceAuthorization>

Is there a way I can get the XML of the configuration section and just read it myself?

A: 

Well, you could do, for example:

string docName=System.Web.HttpContext.Current.Server.MapPath("Web.config");
XmlDocument configDoc = new XmlDocument();
configDoc.Load(docName);

and then work from configDoc.

Vinay Sajip
I am aware of that route; I was just hoping for something a little less crude.
Nathan Ridley
+1  A: 

You can implement System.Configuration.IConfigurationSectionHandler and configure it:

<section name="serviceAuthorization" type="[your-type]"/>

Then you get your entire section as XmlNode and can parse your custom schema.

edit: this is deprecated. here is one new way to do it.

Josef
As I understand it, IConfigurationSectionHandler has been deprecated
Nathan Ridley
You're absolutely right. I added it to my answer.
Josef