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

前端 未结 3 1930
清酒与你
清酒与你 2020-12-06 05:42

I\'m trying to unit test a custom ConfigurationSection I\'ve written, and I\'d like to load some arbitrary configuration XML into a System.Configuration.Configuration for ea

3条回答
  •  不知归路
    2020-12-06 05:59

    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 :-)

提交回复
热议问题