xsd.exe generated classes don't serialize default value attributes

倖福魔咒の 提交于 2019-12-23 10:28:12

问题


I have few .cs files generated by xsd.exe by a XSD schema.

My problem is that when i try to serialize those classes to xml, the attributes whose values match the default values defined in the xsd schema are not being serialized.

I found out that my problem is solved when i remove [System.ComponentModel.DefaultValueAttribute(typeof(<someType>), "<SomeValue>")] attribute for the member representing a default field.

So my question isn't there more elegant way to do this? For example is there any way to specify that I want the default values to be serialized anyway?


回答1:


The short versions is: yes. it looks like that is the simplest approach.

I've tried the "obvious" things (in particular the *Specified and ShouldSerialize* patterns that XmlSerializer supports, but also IsNullable) and it just keeps on not including the value.

The next options would be either IXmlSerializable (which is plain hard), or XmlAttributeOverrides (a duplication of effort) - neither of which is particularly enticing here. I wonder if your current approach is less work? You could of course simply export the WSDL and edit that to remove the default (and generate from the edited version).

As an aside - if this was WCF, there is the [DataMember(EmitDefaultValue = ...)] that does control this; maybe they added this as a feature request?



来源:https://stackoverflow.com/questions/2385634/xsd-exe-generated-classes-dont-serialize-default-value-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!