<p>I'm trying to unit test a custom ConfigurationSection I've written, and I'd like to load some arbitrary configuration XML into a <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx" rel="noreferrer">System.Configuration.Configuration</a> for each test (rather than put the test configuration xml in the Tests.dll.config file.  That is, I'd like to do something like this:</p>

<pre><code>Configuration testConfig = new Configuration("&lt;?xml version=\"1.0\"?&gt;&lt;configuration&gt;...&lt;/configuration&gt;");
MyCustomConfigSection section = testConfig.GetSection("mycustomconfigsection");
Assert.That(section != null);
</code></pre>

<p>However, it looks like <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx" rel="noreferrer">ConfigurationManager</a> will only give you Configuration instances that are associated with an EXE file or a machine config.  Is there a way to load arbitrary XML into a Configuration instance?</p>

[{'answer_id': 21005, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>*That's no, excluding messy reflection hacks</p>\n"}, {'answer_id': 24938, 'author': 'Nicholas', 'author_id': 2658, 'author_profile': 'https://Stackoverflow.com/users/2658', 'pm_score': 1, 'selected': False, 'text': '<p>I think what you\'re looking for is ConfigurationManager.<a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx" rel="nofollow noreferrer">OpenMappedExeConfiguration</a></p>\n\n<p>It allows you to open a configuration file that you specify with a file path (wrapped inside a <a href="http://msdn.microsoft.com/en-us/library/system.configuration.execonfigurationfilemap.aspx" rel="nofollow noreferrer">ExeConfigurationFileMap</a>)</p>\n\n<p>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.</p>\n'}, {'answer_id': 704817, 'author': 'Oliver Pearmain', 'author_id': 334395, 'author_profile': 'https://Stackoverflow.com/users/334395', 'pm_score': 5, 'selected': True, 'text': '<p>There is actually a way I\'ve discovered....</p>\n\n<p>You need to define a new class inheriting from your original configuration section as follows:</p>\n\n<pre><code>public class MyXmlCustomConfigSection : MyCustomConfigSection\n{\n    public MyXmlCustomConfigSection (string configXml)\n    {\n        XmlTextReader reader = new XmlTextReader(new StringReader(configXml));\n        DeserializeSection(reader);\n    }\n}\n</code></pre>\n\n<p><br/>\nYou can then instantiate your ConfigurationSection object as follows:</p>\n\n<pre><code>string configXml = "&lt;?xml version=\\"1.0\\"?&gt;&lt;configuration&gt;...&lt;/configuration&gt;";\nMyCustomConfigSection config = new MyXmlCustomConfigSection(configXml);\n</code></pre>\n\n<p>Hope it helps someone :-)</p>\n'}]
['https://Stackoverflow.com/questions/20952', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2338/']