XmlSerializer. Keep null string properties? [duplicate]

喜你入骨 提交于 2019-12-23 08:52:22

问题


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?


回答1:


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

}


来源:https://stackoverflow.com/questions/10733731/xmlserializer-keep-null-string-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!