现在的位置: 首页 > 综合 > 正文

XmlIgnoreAttribute使得无法序列号的原因

2013年04月30日 ⁄ 综合 ⁄ 共 1098字 ⁄ 字号 评论关闭

在用XDS导出的类构造时,发现有个属性怎么赋值都无法成功序列化成XML,在阅读了msdn中的解释后彻底释然了。。。。

The [XML Schema Definition Tool (Xsd.exe) occasionally generates the
XmlIgnoreAttribute when creating classes from a schema file (.xsd).

This behavior occurs because value types cannot be set to
nullptr, but all XML data types can be.

Therefore, the tool creates two fields when it encounters an XML type that maps to a value type: one to hold the value and another special field that takes the form of
fieldnameSpecified, where the
fieldname
is replaced by the name of the field or property.

Notice, however, that this special field is generated only when the schema specifies that the element has no minimum occurrence (minOccurs = "0") and that the element has no default value.

The XmlSerializer sets and checks this special field to determine whether a value has been set for the field or
property.
Because the special field must not be serialized, the tool applies the
XmlIgnoreAttribute to it.

这个来自于中文解释。

XmlIgnoreAttribute属于一个特性系列,该系列控制
XmlSerializer 如何序列化或反序列化对象。

如果将 XmlIgnoreAttribute 应用于类的任何成员,则在序列化或反序列化该类的实例时
XmlSerializer 将忽略此成员。

因此不难理解,这个ValueSpecified是不能序列化的,只是用于限制Value的存在。在ValueSpecified属性上加上[System.Xml.Serialization.XmlIgnoreAttribute()]后,必须把ValueSpecified这个属性赋值后,才会序列化Value属性。

抱歉!评论已关闭.