假设我有一个自定义配置文件,它对应于自定义定义的 ConfigurationSection 和 Config 元素。这些配置类存储在库中。
配置文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<Schoool Name="RT">
<Student></Student>
</Schoool>
如何从代码中以编程方式加载和使用此配置文件?
我不想使用原始 XML 处理,而是利用已经定义的配置类。
您必须根据您的要求进行调整,但这是我在我的一个项目中使用的代码来做到这一点:
var fileMap = new ConfigurationFileMap("pathtoconfigfile");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs
var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyTarget.Namespace.Properties.Settings"); // This is the section name, change to your needs
var setting = section.Settings.Get("SettingName"); // This is the setting name, change to your needs
return setting.Value.ValueXml.InnerText;
请注意,我正在读取有效的 .net 配置文件。我正在使用此代码从 DLL 读取 EXE 的配置文件。我不确定这是否适用于您在问题中提供的示例配置文件,但这应该是一个好的开始。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)