PC Review


Reply
Thread Tools Rate Thread

DefaultValue Attribute breaks XmlSerializer

 
 
=?Utf-8?B?U3RldmVU?=
Guest
Posts: n/a
 
      6th Oct 2006
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.

--
-----------
Thanks,
Steve
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      6th Oct 2006
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

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      6th Oct 2006
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

 
Reply With Quote
 
=?Utf-8?B?U3RldmVU?=
Guest
Posts: n/a
 
      6th Oct 2006
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?
--
-----------
Thanks,
Steve


"Marc Gravell" wrote:

> 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
>
>

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      6th Oct 2006
It is hinted at in the following two articles, but I can't (without a
search) find fuller mention:

http://msdn.microsoft.com/library/de...ialization.asp
http://msdn.microsoft.com/library/de...classtopic.asp

Marc

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When is the System.ComponentModel.DefaultValue attribute applied? Nathan Sokalski Microsoft ASP .NET 2 27th Jan 2009 02:46 PM
DefaultValue attribute tinyabs Microsoft C# .NET 2 8th Feb 2006 05:02 PM
Set DefaultValue property attribute to non constant value. Steve Microsoft C# .NET 2 4th Feb 2006 12:39 PM
DefaultValue Attribute usage with Color in C# Stephen M Microsoft Dot NET Framework Forms 0 28th Mar 2004 09:43 AM
PropertyGrid using DefaultValue attribute - MUST IT ?? JezB Microsoft Dot NET Framework Forms 1 9th Jan 2004 10:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:51 AM.