Loading a custom section from XML

I want to add the feature of the application that I am developing to access the configuration. The application will search by default in the app.config file for the section that I give it. If it was not found in app.config, it will look for it in the database, in a specific table that has the following columns:

SectionType, SectionName, SectionData

The column SectionDatais a text column containing the section data in XML format (just like in the app.config file) I can take the SectionData content, but I can not load it into the custom ConfigurationSection, as if it were in the app file. config:

var mySectionObj = ConfigurationManager.GetSection("myCustomSection");

To simplify, my question is, how can I get my own ConfigurationSection object from an XML string instead of a configuration file?

+3
source share
2 answers

You can load the string into an XDocument object and read it from there.

0
source

I don’t think this is possible at all - with the .NET ConfigurationManager class, as far as I know, it’s not even possible to open any file you want — you are limited to the app.config file. Reading configuration data from a source other than a file? No.

You can either parse the XML-String yourself (using "XmlDocument.LoadXml (string)"), or modify the app.config file and read it again.

: CustomSection? (, , ). , CustomSection?

XML , :

XmlDocument appconfig = new XmlDocument();

appconfig.Load("[config_filename]");
XmlNode root = appconfig.DocumentElement;

XmlDocument mysection = new XmlDocument();
mysection.LoadXml([SectionData]);
XmlNode customSection = mysection.DocumentElement;

XmlNode tempNode = appconfig.ImportNode(customSection, true);
root.AppendChild(tempNode);

appconfig.Save("[config_filename]");

...

var mySectionObj = ConfigurationManager.GetSection("myCustomSection");

, : : , : .config, . ( , , , , ). , - , , : .

-: XML- XmlDocument: XmlDocument.LoadXml(xmlstring) xmldocument "doc.ChildNodes" "doc.SelectNodes(xpath)" "doc.SelectSingleNode(xpath)". , , , , . .

-1

Source: https://habr.com/ru/post/1751161/


All Articles