C# 如何获取配置元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/887437/
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 get configuration element
提问by majkinetor
Helo
直升机
Can anybody explain me how to get configuration element from .config file. I know how to handle attributes but not elements. As example, I want to parse following:
任何人都可以解释我如何从 .config 文件中获取配置元素。我知道如何处理属性而不是元素。例如,我想解析以下内容:
<MySection enabled="true">
<header><![CDATA[ <div> .... </div> ]]></header>
<title> .... </title>
</MySection>
My c# code looks like this so far:
到目前为止,我的 C# 代码如下所示:
public class MyConfiguration : ConfigurationSection
{
[ConfigurationProperty("enabled", DefaultValue = "true")]
public bool Enabled
{
get { return this["enabled"].ToString().ToLower() == "true" ? true : false; }
}
[ConfigurationProperty("header")]
public string header
{
???
}
}
It works with attributes, how do I do with elements (header property in above code) ?
它适用于属性,我如何处理元素(上面代码中的标题属性)?
采纳答案by majkinetor
I finally found one way to do it.
我终于找到了一种方法来做到这一点。
There is IConfigurationSectionHandler interface that allows for things I want. It requires the one to write the method
有 IConfigurationSectionHandler 接口允许我想要的东西。它需要编写方法
public object Create(object parent, object configContext, XmlNode section)
After it, u parse sectionon your own so I was able to fetch XmlElement's without a problem:
之后,你自己解析部分,所以我能够毫无问题地获取 XmlElement:
header = s["header"] != null ? s["header"].InnerText : String.Empty;
title = s["title"] != null ? s["title"].InnerText : String.Empty;
The down side of this is that interface is outdated but MSDN states that it will not be removed from future versions of the frameworks as it is used internally.
不利的一面是接口已经过时,但 MSDN 声明它不会从框架的未来版本中删除,因为它是在内部使用的。
回答by Kirtan
You can use the ConfigurationManager.GetSection("SectionName")method for getting the configuration section in the config files.
您可以使用ConfigurationManager.GetSection("SectionName")方法来获取配置文件中的配置节。
回答by Vizu
Here's a pretty good custom config section designer tool you can use (and it's free):
这是一个非常好的自定义配置部分设计器工具,您可以使用(而且是免费的):
Configuration Section Designer
EDIT:
编辑:
I was looking into MSDN and it seems that custom config sections can't do what you want, ie. getting the config value from an element. Custom config elements can contain other config elements, but the config values always come from attributes.
我正在查看 MSDN,似乎自定义配置部分无法执行您想要的操作,即。从元素获取配置值。自定义配置元素可以包含其他配置元素,但配置值始终来自属性。
Maybe you can put your html snippets into other files and refer to them from the config, like this.
也许您可以将 html 片段放入其他文件并从配置中引用它们,就像这样。
<MySection enabled="true">
<header filename="myheader.txt" />
<title filename="mytitle.txt" />
</MySection>
回答by Joe
You can create a class that inherits from System.Configuration.ConfigurationElement that represents an element in your configuration section.
您可以创建一个继承自 System.Configuration.ConfigurationElement 的类,该类代表您的配置部分中的一个元素。
There's a simple example in the MSDN documentation for ConfigurationElement.
在ConfigurationElement 的 MSDN 文档中有一个简单的例子。
回答by ToddS
Inherit the ConfigurationElement class and override its deserialize method. Use the new class to represent elements with text content.
继承 ConfigurationElement 类并覆盖其反序列化方法。使用新类来表示具有文本内容的元素。
http://www.codeproject.com/KB/XML/ConfigurationTextElement.aspx
http://www.codeproject.com/KB/XML/ConfigurationTextElement.aspx
回答by g t
According to MSDN, in .NET 4there's a new CurrentConfiguration
property which gives you a reference to the top-level Configuration
instance that represents the configuration hierarchy that the current ConfigurationElement
instance belongs to.
根据MSDN,在.NET 4 中有一个新CurrentConfiguration
属性,它为您提供对顶级Configuration
实例的引用,该实例表示当前ConfigurationElement
实例所属的配置层次结构。
回答by Vikash Kumar
There is another approach for doing the same thing.
还有另一种方法可以做同样的事情。
We could create an element by overriding DeserializeElement
method to get string value:
我们可以通过重写DeserializeElement
方法来创建一个元素来获取字符串值:
public class EmailTextElement : ConfigurationElement {
public string Value { get; private set; }
protected override void DeserializeElement(XmlReader reader, bool s) {
Value = reader.ReadElementContentAs(typeof(string), null) as string;
}
}
回答by Juls
Working with your example, you are going to override the Deserialization of "header" in the ConfigurationElement to get the CDATA value.
使用您的示例,您将覆盖 ConfigurationElement 中“header”的反序列化以获取 CDATA 值。
<MySection enabled="true">
<header name="foo"><![CDATA[ <div> .... </div> ]]></header>
<title> .... </title>
</MySection>
public sealed class HeaderSection: ConfigurationElement {
private string __Name, __CDATA;
[ConfigurationProperty("name", IsRequired = true)]
public string Name {
get {
return this.__Name;
}
set {
this.__Name = value;
}
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value {
get {
return this.__CDATA;
}
set {
this.__CDATA = value;
}
}
protected override void DeserializeElement(System.Xml.XmlReader reader, bool s) {
this.Name = reader.GetAttribute("name").Trim();
string cdata = reader.ReadElementContentAs(typeof(string), null) as string;
this.Value = cdata.Trim();
}
}