自定义类型为 XMLAttribute
我有一个自定义的 struct 类型,支持与 string
之间的隐式转换。我可以将此结构序列化为元素,而在公共属性上使用 XmlText
属性不会出现任何问题。
struct Test
{
[XmlText]
public string Value {get(...);set(...);}
}
class Other
{
[XmlElement] // this renders as <TestElement>value</TestElement>
public Test TestElement {get; set;}
[XmlAttribute] // this fails at runtime
public Test TestElement {get; set;}
}
但是,我无法将其序列化为属性。有办法让它发挥作用吗?
I have a custom struct
type that supports implicit conversion to and from string
. I can serialize this struct as an element without any problems with the using the XmlText
attribute on a public property.
struct Test
{
[XmlText]
public string Value {get(...);set(...);}
}
class Other
{
[XmlElement] // this renders as <TestElement>value</TestElement>
public Test TestElement {get; set;}
[XmlAttribute] // this fails at runtime
public Test TestElement {get; set;}
}
However, I cannot serialize it as an attribute. Is there anyway to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[XmlText]
表示将数据序列化为元素的文本值。它不能用于属性。[XmlText]
means to serialize the data as the text value of an element. It cannot be used for an attribute.