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

Question :

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

Answer :

I’m trying to unit test a custom ConfigurationSection I’ve written, and I’d like to load some arbitrary configuration XML into afor 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:

Configuration testConfig = new Configuration("...");  MyCustomConfigSection section = testConfig.GetSection("mycustomconfigsection");  Assert.That(section != null);  

However, it looks like  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?

,

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 = "...";  MyCustomConfigSection config = new MyXmlCustomConfigSection(configXml);  

Hope it helps someone 🙂

That’s the answer Is there a way to get a System.Configuration.Configuration instance based on arbitrary xml?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Best implementation for Key Value Pair Data Structure?