How to avoid serialization defaults?

How should I mark properties that they should not be serialized if they have default values? For example, if I have a boolean, then it will be serialized, if only true is set, the same with a zero value, if they are zero, I do not want to include them in my serialization file.

+8
c #
source share
2 answers

Ok, I found it myself. This is [DefaultValue(false)] . If I mark a property with this attr, then it will be serialized only if it differs from the value in ().

System.ComponentModel.DefaultValueAttribute

+22
source share

There is a property like Specified. I cannot find the msdn documentation, but this article should be helpful. Basically you need to write something like this:

 //this property would not be serialized if it contains String.Empty value public string PropertyName { get; set; } [XmlIgnore] public bool PropertyNameSpecified { get { return PropertyName != String.Empty; } } 
+4
source share

All Articles