XmlSerializer Keep zero string properties?

Possible duplicate:
XML serialization and null value - C #
change how XmlSerializer serializes empty elements

How to make XmlSerializer store empty tags for string properties that have null values ​​instead of skipping this property?

+7
source share
1 answer

You want you to want this:

<parent> <child1>Hello World</child1> <child2 /> </parent> 

instead

 <parent> <child1>Hello World</child1> </parent> 

your class should look like this: A serializer calls the ShouldSerializePropertyName method by definition (if one exists) to determine if a property should be serialized (for example, Windows Forms Designer).

 public class Parent { [XmlElement("Child1")] public string Child1 { get; set; } [XmlElement("Child2")] public string Child2 { get; set; } public bool ShouldSerializeChild2() { return true; } } 
+6
source

All Articles