Either remove the
DefaultValue
attribute, or override the default value when you serialize to XML:
XmlAttributes.XmlDefaultValue Property (System.Xml.Serialization) | Microsoft Docs[
^]
public XmlSerializer CreateSerializer()
{
var attributeOverrides = new XmlAttributeOverrides();
attributeOverrides.Add(typeof(Person), nameof(Person.Length), new XmlAttributes
{
XmlDefaultValue = double.NaN,
XmlAttribute = new XmlAttributeAttribute(),
});
return new XmlSerializer(typeof(Person), attributeOverrides);
}
NB: When you override the serialization attributes for a property, you need to restate
all serialization attributes, which is why you need to provide the
XmlAttribute
property as well as the
XmlDefaultValue
property.