I’d like the XML created by XmlSerializer to exclude properties if they have a default value. Is this possible with XmlSerializer or am I going to have to look into IXmlSerializable?
For example, I may have the following class:
public class PositionedObject
{
public float X
{ get; set; }
public float Y
{ get; set;}
}
I’d like to tell the XmlSerializer that when it serializes an instance of PositionedObject, to not include X if the value is 0 (and same with Y if it is 0).
Just declare a method named
ShouldSerializeXthat returns true when the value is not 0:The serializer will call this method to decide whether the property should be serialized or not.