Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlSerializer. Keep null string properties? [duplicate]

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?

like image 408
user626528 Avatar asked May 24 '12 08:05

user626528


1 Answers

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

}
like image 53
Jürgen Steinblock Avatar answered Oct 04 '22 15:10

Jürgen Steinblock