I have a class that is serialized to a XML file. There are several properties that are rarely used but always created. If I delete them within the XML the deserialization still works, because they have the default value.
These unnecessary (bool) attributes make the XML harder to read.
Can I somehow tell C# to omit elements or attributes that still have default value?
Specify the DefaultValueAttribute, and if the value matches, it won't be output.
Rowland has the answer for simple values. For more complex scenarios, you can add a method called public bool ShouldSerializeFoo()
(for property Foo
) - it it returns false
, it won't get serialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With