序列化可空&LT;&日期时间GT;到XML [英] Serializing a Nullable<DateTime> in to XML
问题描述
我试图序列化类的几个数据成员是可空的对象,下面是一个例子
I am trying to serialize a class several of the data-members are Nullable objects, here is a example
[XmlAttribute("AccountExpirationDate")]
public Nullable<DateTime> AccountExpirationDate
{
get { return userPrincipal.AccountExpirationDate; }
set { userPrincipal.AccountExpirationDate = value; }
}
但在运行时出现错误
However at runtime I get the error
无法序列类型System.Nullable`1 [System.DateTime的]的成员AccountExpirationDate。 XmlAttribute / XMLTEXT不能使用EN code复杂的类型。
Cannot serialize member 'AccountExpirationDate' of type System.Nullable`1[System.DateTime]. XmlAttribute/XmlText cannot be used to encode complex types.
不过,我检查,可为空值是 SerializableAttribute 。我究竟做错了什么?
However I checked and Nullable is a SerializableAttribute. What am I doing wrong?
推荐答案
您可以只序列它作为一个的XmlElement
,而不是作为一个 XmlAttribute
,作为再presentation是一个属性过于复杂。这就是例外是告诉你。
You can only serialize it as an XmlElement
, not as an XmlAttribute
, as the representation is too complex for an attribute. That's what the exception is telling you.
这篇关于序列化可空&LT;&日期时间GT;到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!