如何以编程方式加载配置文件

假设我有一个自定义配置文件,它对应于自定义的ConfigurationSection和Config元素。 这些配置类存储在库中。

Config File看起来像这样

    

如何以编程方式从代码中加载和使用此配置文件?

我不想使用原始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的配置文件。 我不确定这是否适用于您在问题中提供的示例配置文件,但它应该是一个好的开始。

在CodeProject上查看Jon Rista关于.NET 2.0配置的三部分系列。

  • 揭开.NET 2.0配置的神秘面纱
  • 解码.NET 2.0配置的奥秘
  • 破解.NET 2.0配置的奥秘

强烈推荐,写得很好,非常有帮助!

您无法真正加载任何XML片段 – 您可以加载的是一个完整的,独立的配置文件,其外观和感觉就像是app.config。

如果您想创建和设计自己的自定义配置部分,您还应该查看CodePlex上的Configuration Section Designer – 一个Visual Studio插件,它允许您直观地设计配置部分并为您生成所有必需的管道代码。

configSource属性允许您将任何配置元素移动到单独的文件中。 在你的主app.config中,你会做这样的事情:

     
		      	

以下代码将允许您将XML中的内容加载到对象中。 根据源,app.config或其他文件,使用适当的加载程序。

 using System.Collections.Generic; using System.Xml.Serialization; using System.Configuration; using System.IO; using System.Xml; class Program { static void Main(string[] args) { var section = SectionSchool.Load(); var file = FileSchool.Load("School.xml"); } } 

文件加载器:

 public class FileSchool { public static School Load(string path) { var encoding = System.Text.Encoding.UTF8; var serializer = new XmlSerializer(typeof(School)); using (var stream = new StreamReader(path, encoding, false)) { using (var reader = new XmlTextReader(stream)) { return serializer.Deserialize(reader) as School; } } } } 

节装载机:

 public class SectionSchool : ConfigurationSection { public School Content { get; set; } protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) { var serializer = new XmlSerializer(typeof(School)); // works in 4.0 // var serializer = new XmlSerializer(type, null, null, null, null); // works in 4.5.1 Content = (Schoool)serializer.Deserialize(reader); } public static School Load() { // refresh section to make sure that it will load ConfigurationManager.RefreshSection("School"); // will work only first time if not refreshed var section = ConfigurationManager.GetSection("School") as SectionSchool; if (section == null) return null; return section.Content; } } 

数据定义:

 [XmlRoot("School")] public class School { [XmlAttribute("Name")] public string Name { get; set; } [XmlElement("Student")] public List Students { get; set; } } [XmlRoot("Student")] public class Student { [XmlAttribute("Index")] public int Index { get; set; } } 

‘app.config’的内容

    

XML文件的内容:

             

已在Visual Studio 2010(.Net 4.0)中检查已发布的代码。 如果你改变seriliazer的构造,它将在.Net 4.5.1中工作

 new XmlSerializer(typeof(School)) 

 new XmlSerializer(typeof(School), null, null, null, null); 

如果提供的示例在调试器外部启动,那么它将使用最简单的构造函数,但是如果从带有调试的VS2013 IDE启动,则需要更改构造函数,否则将发生FileNotFoundException(至少在我的情况下)。