开发者

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

开发者 https://www.devze.com 2022-12-21 20:40 出处:网络
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 define

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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消