Is there a way to get a System.Configuration.Configuration instance based on arbitrary xml?

陌路散爱 提交于 2019-11-27 22:55:36

There is actually a way I've discovered....

You need to define a new class inheriting from your original configuration section as follows:

public class MyXmlCustomConfigSection : MyCustomConfigSection
{
    public MyXmlCustomConfigSection (string configXml)
    {
        XmlTextReader reader = new XmlTextReader(new StringReader(configXml));
        DeserializeSection(reader);
    }
}


You can then instantiate your ConfigurationSection object as follows:

string configXml = "<?xml version=\"1.0\"?><configuration>...</configuration>";
MyCustomConfigSection config = new MyXmlCustomConfigSection(configXml);

Hope it helps someone :-)

I think what you're looking for is ConfigurationManager.OpenMappedExeConfiguration

It allows you to open a configuration file that you specify with a file path (wrapped inside a ExeConfigurationFileMap)

If what the other poster said is true, and you don't wish to create a whole new XML file for testing, then I'd recommend you put your Configuration edits in the Test method itself, then run your tests against the freshly changed configuration data.

Looking at the members of the class, I'd say the answer is probably no*. I'm not sure why you'd want to do this anyway, rather than create your own XML configuration file.

*That's no, excluding messy reflection hacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!