Possible Duplicate:
XML Serialization and null value - C#
change how XmlSerializer serializes empty elements
How to make XmlSerializer store empty tags for string properties having null values, instead of skipping this property?
You mean you want this:
<parent>
<child1>Hello World</child1>
<child2 />
</parent>
instead of
<parent>
<child1>Hello World</child1>
</parent>
your class should look like this:
The serializer calls a ShouldSerializePropertyName
method by definition (if exists) to determine if a property should be serialized (like Windows Forms Designer, too).
public class Parent
{
[XmlElement("Child1")]
public string Child1 { get; set; }
[XmlElement("Child2")]
public string Child2 { get; set; }
public bool ShouldSerializeChild2() { return true; }
}
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