C# 如何从 app.config 读取自定义 XML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1148611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Read Custom XML from the app.config?
提问by Jimmy
I want to read the custom XML section from the app.config
of a C# windows service.
我想从app.config
C# windows 服务中读取自定义 XML 部分。
How do I go about it?
我该怎么做?
The XML is below:
XML 如下:
<Books>
<Book name="name1" title="title1"/>
<Book name="name2" title="title2"/>
</Books>
采纳答案by Colin Mackay
What you want to do is read up on Custom Configuration Sections.
您要做的是阅读自定义配置部分。
回答by Kevin Nisbet
In a project I developed I use something similar for configuration that I found. I believe the article was called the last configuration section handler I'll ever need (I can't find a working link, maybe someone can link it for me).
在我开发的一个项目中,我使用类似于我发现的配置。我相信这篇文章被称为我将需要的最后一个配置部分处理程序(我找不到工作链接,也许有人可以为我链接)。
This method takes what you want to do one step further, and actually de-serializes the object into memory. I'm just copying code from my project, but it should be fairly simple to take a step backwards if all you want is the XML.
此方法将您想要做的更进一步,实际上是将对象反序列化到内存中。我只是从我的项目中复制代码,但是如果您想要的只是 XML,那么向后退一步应该相当简单。
First, you need to define a class that handles your configuration settings.
首先,您需要定义一个处理配置设置的类。
using System;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
namespace Ariel.config
{
class XmlSerializerSectionHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, XmlNode section)
{
XPathNavigator nav = section.CreateNavigator();
string typename = (string)nav.Evaluate("string(@type)");
Type t = Type.GetType(typename);
XmlSerializer ser = new XmlSerializer(t);
return ser.Deserialize(new XmlNodeReader(section));
}
#endregion
}
}
Now, say you want to load a section of configuration... super easy, cast to the type of object you're expecting to XML Serialize to, and pass the section you're looking for (in this case SearchSettings
.
现在,假设您要加载一部分配置......超级简单,转换为您期望 XML 序列化到的对象类型,并传递您正在寻找的部分(在本例中为SearchSettings
.
try
{
config = (Eagle.Search.SearchSettings)ConfigurationSettings.GetConfig("SearchSettings");
}
catch (System.Configuration.ConfigurationException ex)
{
syslog.FatalException("Loading search configuration failed, you likely have an error", ex);
return;
}
Now, all you need is your App.config file. I chose to split mine into separate files (1 file per section) just to make managing the config a little easier. You define a section, give it a name, and define the type (whatever you called the class listed above) and the assembly's name.
现在,您所需要的只是您的 App.config 文件。我选择将我的文件拆分为单独的文件(每个部分 1 个文件),只是为了使管理配置更容易一些。您定义一个部分,给它一个名称,然后定义类型(无论您如何称呼上面列出的类)和程序集的名称。
App.config:
应用程序配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SearchSettings" type="Ariel.config.XmlSerializerSectionHandler, Ariel"/>
</configSections>
<SearchSettings configSource="Config\Search.config" />
</configuration>
Now, all that's left is the config file to be de-serialized. What's important here is that the block matches your section name, and your type is whatever object it should de-serialize to, and the Assembly name.
现在,剩下的就是要反序列化的配置文件。这里重要的是块与您的节名称匹配,您的类型是它应该反序列化的任何对象,以及程序集名称。
<?xml version="1.0" encoding="utf-8" ?>
<SearchSettings type="Eagle.Search.SearchSettings, Eagle">
<NumThreads>4</NumThreads>
</SearchSettings>
If you just want the pure raw XML, all you should need to do is modify the Object that handles the section to return the XML or do whatever you need to do.
如果您只想要纯原始 XML,您需要做的就是修改处理该部分的对象以返回 XML 或执行您需要执行的任何操作。
回答by Pieter
I use custom xml in my config.app. file and create a app.XSD from it. The XSD file includes the schema of the config.app file. Then XSD file can be translated to a vb class or a C# class file using 'xsd.exe'. Now all you have to do is deserialize the configfile to the class.
我在 config.app 中使用自定义 xml。文件并从中创建一个 app.XSD。XSD 文件包括 config.app 文件的架构。然后可以使用“xsd.exe”将 XSD 文件转换为 vb 类或 C# 类文件。现在您要做的就是将配置文件反序列化为类。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomConfig" type="Object" />
</configSections>
<CustomConfig>
<ActiveEnvironment>QAS</ActiveEnvironment>
<Environments>
<Environment name ="PRD" log="Y">
</Environment>
<Environment name ="QAS" log="N">
</Environment>
<Environment name ="DEV" log="Y">
</Environment>
</Environments>
</CustomConfig>
</configuration>
回答by Tamir Daniely
Since IConfigurationSectionHandler
is deprecated I thought it's worth mentioning that you can still implement a pure serialized section just by overriding ConfigurationSection.DeserializeSection
and not calling the base implementation.
由于IConfigurationSectionHandler
已弃用,我认为值得一提的是,您仍然可以通过覆盖ConfigurationSection.DeserializeSection
而不是调用基本实现来实现纯序列化部分。
Here is a very basic example that I reuse a lot. A simple configuration section that loads an object graph from inline XAML. (Naturally you can implement with XmlSerializer
instead)
这是我经常重复使用的一个非常基本的示例。从内联 XAML 加载对象图的简单配置部分。(当然,您可以XmlSerializer
改为实现)
using System.Configuration;
using System.Xaml;
using System.Xml;
...
public class XamlConfigurationSection<T> : ConfigurationSection
{
public static XamlConfigurationSection<T> Get(string sectionName)
{
return (XamlConfigurationSection<T>)ConfigurationManager.GetSection(sectionName);
}
public T Content { get; set; }
protected override void DeserializeSection(XmlReader xmlReader)
{
xmlReader.Read();
using (var xamlReader = new XamlXmlReader(xmlReader))
Content = (T)XamlServices.Load(xamlReader);
}
}