views:

86

answers:

1

Hello,

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?

+3  A: 

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?

Marc Gravell
Thanks for your answer. Indeed my current approach is less work, despite it's not the best one. May be when I have some spare time I'll thy to mess with IXmlSerializable or or XmlAttributeOverrides.
Koynov