5

This is copied example from:

How to read custom config section in app.config in c#

I want to read following custom section from app.config:

<StartupFolders>    
   <Folders name="a">
      <add folderType="Inst" path="c:\foo" />
      <add folderType="Prof" path="C:\foo1" />      
   </Folders>
   <Folders name="b">
      <add folderType="Inst" path="c:\foo" />
      <add folderType="Prof" path="C:\foo1" />      
   </Folders> 
</StartupFolders>

And this is my case too. However, I don't want to create custom class for handling values, defining this class in web.config, and then finally using it. It is heavy-weight for my needs.

Instead I would like to do something very simple -- retrieve a section as XML. Then I could use regular Linq.Xml to parse it. This way, I don't need to create new classes per each section, I don't need to declare them. For my purpose it is sufficient on one hand, and minimal at the other (I do it once, key-value mapper for nested sections). I.e. perfect.

The only missing piece is (my question) -- how to get a web.config section as XML? Note about the section:

  • it cannot be encoded, because it has to be edited by hand
  • it cannot be serialized for the same reason

So I am not looking for a workaround how to squeeze entire section as value in appSettings, but I am really looking for a method to get proper section as XML.

I would like to get it from ConfigManager (!), because this way I don't have to deal with resolving which web.config should I read, etc. I.e. less chance to make mistake than mimicing web.config precedence manually.


Forgive me for reminding this, but please avoid "answers", you shouldn't do this, use custom class per each section, etc. I already considered this, and opted against it.

Community
  • 1
  • 1
greenoldman
  • 16,895
  • 26
  • 119
  • 185

3 Answers3

2

I think you either have to do it manually and load the Web config into memory:

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config")); 

Or you will need to create the custom configuration sections you want to avoid.

James Osborn
  • 1,275
  • 7
  • 12
  • Yup, it seems that either you do everything manually, or you do as MS says, there is no middle ground, because ConfigurationManager is super strict when validating config, you cannot even put anything extra in the config file. Pity :-( – greenoldman Mar 16 '12 at 13:39
0

You can define a re-usable custom config section that exposes the section XML as you desire. The key is, that you don't have to define a different class for each custom config section.

For clarity, my project namespace is "ConsoleApp1" as is the assembly name (this appears in type definitions).

First, create a custom config section that exposes the XML reader:

public class XmlConfigSection : ConfigurationSection
{
    public XmlReader Xml { get; private set; }

    override protected void DeserializeSection(XmlReader reader)
    {
        Xml = reader;
    }
}

You can then define any of your custom sections to use this class in the app.config:

<configSections>
    <section name="StartupFolders" type="ConsoleApp1.XmlConfigSection, ConsoleApp1" />
    <section name="AnotherCustomSection" type="ConsoleApp1.XmlConfigSection, ConsoleApp1" />
</configSections>

Then in your code, you can access raw XmlReader of the config section like this:

var xmlReader = (ConfigurationManager.GetSection("StartupFolders") as XmlConfigSection).Xml;

If you then want an XML string instead of the reader you can do something like this (though I'd suggest sticking with the XmlReader):

StringBuilder sb = new StringBuilder();

while (xmlReader.Read())
    sb.AppendLine(xmlReader.ReadOuterXml());

var xmlStr = sb.ToString();
JohnB
  • 451
  • 5
  • 7
-1

Totally untested but could you use something like this? :

ConfigurationSection exampleSection = 
    (ConfigurationSection)ConfigurationManager
                                        .GetSection("system.web/exampleSection");

Then possibly use exampleSection.ElementInformation to get more info?

Greg
  • 31,180
  • 18
  • 65
  • 85