假设我有一个自定义配置文件,它对应于自定义的 ConfigurationSection 和 Config 元素。这些配置类存储在库中。
配置文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<Schoool Name="RT">
<Student></Student>
</Schoool>
如何以编程方式从 Code 加载和使用此配置文件?
我不想使用原始 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 配置的三部分系列。
强烈推荐,写得很好,非常有帮助!
您无法真正加载任何 XML 片段 - 您可以加载的是一个完整的、单独的配置文件,看起来和感觉都像 app.config。
如果您想创建和设计自己的自定义配置部分,您绝对应该查看 CodePlex 上的配置部分设计器- 一个 Visual Studio 插件,可让您直观地设计配置部分并为您生成所有必要的管道代码。
该configSource
属性允许您将任何配置元素移动到单独的文件中。在您的主 app.config 中,您将执行以下操作:
<configuration>
<configSections>
<add name="schools" type="..." />
</configSections>
<schools configSource="schools.config />
</configuration>
以下代码将允许您将 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<Student> Students { get; set; }
}
[XmlRoot("Student")]
public class Student
{
[XmlAttribute("Index")]
public int Index { get; set; }
}
'app.config' 的内容
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="School" type="SectionSchool, ConsoleApplication1"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<School Name="RT">
<Student Index="1"></Student>
<Student />
<Student />
<Student />
<Student Index="2"/>
<Student />
<Student />
<Student Index="3"/>
<Student Index="4"/>
</School>
</configuration>
XML文件的内容:
<?xml version="1.0" encoding="utf-8" ?>
<School Name="RT">
<Student Index="1"></Student>
<Student />
<Student />
<Student />
<Student Index="2"/>
<Student />
<Student />
<Student Index="3"/>
<Student Index="4"/>
</School>
已在 Visual Studio 2010 (.Net 4.0) 中检查发布的代码。如果您更改串行器的构造,它将在 .Net 4.5.1 中工作
new XmlSerializer(typeof(School))
至
new XmlSerializer(typeof(School), null, null, null, null);
如果提供的示例是在调试器之外启动的,那么它将使用最简单的构造函数,但是如果从带有调试的 VS2013 IDE 启动,则需要更改构造函数,否则会发生 FileNotFoundException(至少在我的情况下是这样)。