all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

Force serialization of elements


Force serialization of elements Bradley Plett
3/26/2004 12:06:52 PM
dotnet xml:
I have a very simple XSD which I turn into a class using the xsd.exe
tool. The XSD contains some binary and some string elements that have
default values. However, when I serialize an object based on the
resulting class, unless these values have been explicitly assigned,
they do not get serialized. Is there some way of forcing fields to
get serialized? If so, is that something I can specify in my XSD, or
is it something I'd have to do somehow during the serialization? I've
tried setting the "nillable" flag for the elements in question, but
that makes no difference.

Any help would be appreciated!

Thanks!
Re: Force serialization of elements Christoph Schittko [MVP]
3/28/2004 9:08:57 PM

From the .NET SDK documentation:

If the value assigned to a field or property is equal to the default value
for that field or property, the XmlSerializer will not serialize the value
to the XML-instance. This is because the assigned value can be recovered
from the XML schema. In other words, setting a field or property to its own
default value is equivalent of not setting it at all. Likewise, if no value
is set for the field or property, the XmlSerializer will use the default
value found in the schema. In both cases, (setting the property to its
default, or not setting it at all), the XML-document instance will not
contain any value for the property.


There is also an article in the Microsoft Knowledge Base [0] that explains
how to work around the problem.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0] http://support.microsoft.com/default.aspx?scid=kb;en-us;325691
[quoted text, click to view]

AddThis Social Bookmark Button