你好
任何人都可以向我解释如何从 .config 文件中获取配置元素。 我知道如何处理属性而不是元素。例如,我想解析以下内容:
<MySection enabled="true">
<header><![CDATA[ <div> .... </div> ]]></header>
<title> .... </title>
</MySection>
到目前为止,我的 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
{
???
}
}
它适用于属性,我该如何处理元素(上面代码中的 header 属性)?
最佳答案
还有另一种方法可以做同样的事情。
我们可以通过覆盖 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;
}
}
关于c# - 如何获取配置元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/887437/