Serialize bools to XML differently

GateCrusher420

New Member
I'm using the \[code\]XmlSerializer\[/code\] to serialize a class to XML. The class contains a few boolean properties, that are serialized like so:\[code\]public bool Boolean1;public bool Boolean2;<Boolean1>true</Boolean1><Boolean2>false</Boolean2>\[/code\]I want the generated XML to look like this:\[code\]<Boolean1/>\[/code\]That is, true booleans will appear as an empty tag, and false booleans will not appear at all. I could add a \[code\]ShouldSerializeX\[/code\] method to both booleans to make the element disappear in case the boolean is false, but how do I just leave the tag name with no content if the element is true?Also, isn't there some sort of \[code\][XmlIgnoreIfNull]\[/code\] or \[code\][XmlIgnoreIfDefault]\[/code\] attribute that would render most \[code\]ShouldSerialize\[/code\] methods redundant?
 
Back
Top