7

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?

Community
  • 1
  • 1
user626528
  • 13,999
  • 30
  • 78
  • 146

1 Answers1

7

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

}
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189