PRB: XML Serialization: System.Xml.XmlSerializer Does Not Serialize Default Values (325691)
The information in this article applies to:
- Microsoft XML Classes (included with the .NET Framework 1.0)
This article was previously published under Q325691 SYMPTOMSSystem.Xml.XmlSerializer does not serialize default values that are present in the XML schemas (XSD).
For example, if the schema contains the following line, the default value of False for the <boolf> element is not serialized:
<xsd:element name="boolF" type="xsd:boolean" minOccurs="1" maxOccurs="1" default="false" />
CAUSE
The following is an excerpt from the Microsoft .NET Framework 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.
For more information about this behavior, see the following Microsoft Developer Network (MSDN) Web site:
RESOLUTION
One of following two workarounds can be used to resolve this problem:
- After the Xsd.exe file generates the classes from the schemas, manually comment out or remove all of the [System.ComponentModel.DefaultValueAttribute("myFieldName")] attributes from the class files.
- Use the class constructors instead of the schema to assign the default values.
STATUSThis behavior is by design.
Microsoft plans to change this behavior in the next major version release of the .NET Framework.
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
314150 INFO: Roadmap for XML Serialization in the .NET Framework
For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Modification Type: | Major | Last Reviewed: | 9/24/2003 |
---|
Keywords: | kbprb KB325691 kbAudDeveloper |
---|
|