我可以在 .NET 配置部分添加文本节点而不是属性吗?
我目前有一个 .NET 自定义配置部分,如下所示:
<customSection name="My section" />
文本节点(我不确定这是否是正确的术语?),如下所示:
<customSection>
<name>My Section</name>
</customSection>
我当前的 customSection 类如下所示:
public class CustomSection: ConfigurationSection {
[ConfigurationProperty("name")]
public String Name {
get {
return (String)this["name"];
}
}
}
我想要的是将其编写为 我要把它变成一个文本节点吗?
I currently have a .NET custom configurationsection that looks like this:
<customSection name="My section" />
What I want is to write it as a textnode (I'm not sure if this is the correct term?) like this:
<customSection>
<name>My Section</name>
</customSection>
My current customSection class looks like this:
public class CustomSection: ConfigurationSection {
[ConfigurationProperty("name")]
public String Name {
get {
return (String)this["name"];
}
}
}
What should I do to make it a textnode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些研究表明,现有的配置类不支持该类型的元素,除非创建自定义类来处理它。 这篇 CodeProject 文章介绍了如何创建一个新的
ConfigurationTextElement
类是通用的,可以将序列化的字符串解析为对象(包括字符串,这就是文章显示的内容)。类代码很简短:
A bit of research suggests that the existing configuration classes do not support that type of element without creating a custom class to handle it. This CodeProject article covers creating a new
ConfigurationTextElement
class that is generic and can parse a serialized string into an object (including a string, which is what the article shows).The class code is brief:
如果您希望能够同时拥有属性和文本内容,例如
,那么您可以像这样覆盖
DeserializeElement
:希望这会有所帮助。
If you want to be able to have both attributes as well as text content e.g.
Then you can override
DeserializeElement
like so:Hope this helps.