XmlSerializer. Keep null string properties?

.net, c#, serialization, xml

Solution

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; }

}

Problem

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?

Original source

Related problems