How to tell C # to omit the creation of attributes that are used by default during serialization?

I have a class that serializes to an XML file. There are several properties that are rarely used, but are always created. If I delete them in XML, deserialization still works because they have a default value.

These unnecessary attributes (bool) make it easy to read XML.

Can I somehow tell C # to omit elements or attributes that still have a default value?

+4
source share
3 answers

Specify DefaultValueAttribute , and if the value matches, it will not be displayed.

+12
source

Rowland has an answer for simple meanings. For more complex scenarios, you can add the public bool ShouldSerializeFoo() method (for the Foo property) - it returns false , it will not be serialized.

+6
source

Use the XMLIgnore () attribute to mark the property that should be included in Serialization / Deserialization.

0
source

All Articles