XmlIgnoreAttribute使得无法序列号的原因

在用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属性。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值