DefaultValue Attribute breaks XmlSerializer

G

Guest

I have a boolean property that I want to serialize to disk using XmlSerializer.

I noticed that if the property includes the attribute "DefaultValue(true)"
or "DefaultValue(false)" that the property will not serialilze to an XML
file. If the attribute is removed the serialization occurs just fine.

Is there a way around this bug? This behavior seems to work with any type
of property.
 
M

Marc Gravell

Do you have a code sample here? I am pretty sure that I have used
[XmlAttribute, DefaultValue(false)] (to abbreviate the xml fragment)
and AFAIK it worked just fine. The only related thing I am aware of is
a known bug in WSE3 relating to [XmlAttribute] on byte[] properties.

Also - is this using XmlSerializer directly, or is it during usage in a
web-service? If the latter, then you should also clarify the specifics:
vanilla SOAP? WSE? (and if WSE, which version)

Marc
 
M

Marc Gravell

Futher to my previous answer, what do you mean "will not serialize"?
As, no, by definition if you have DefaultValue(false), and the value
*is* false, then it *won't* go into the file. This is the expected
behaviour, and is similar to the way the forms designer doesn't write
code for properties you haven't changed.

If you don't want this behaviour, then either don't specify a
DefaultValue, else perform the serialization manually using
IXmlSerializable (IIRC; don't have VS to hand).

Marc
 
G

Guest

Thank you. I didn't realize that if the "default value assigned in the init"
and the "DefaultValue(...)" - if the same - would not write the value out. I
just verified in my sample code and your statements are correct. Thank you.
Where did you learn that the behavior (when they are the same) is NOT to
write out the value?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top