XML serialization and DefaultValue(“”) related problem in c#

前端 未结 4 1651
青春惊慌失措
青春惊慌失措 2021-01-02 06:37

my class property has default value which will be serialize.

public class DeclaredValue
{
    [XmlElement(ElementName = \"Amount\", DataType = \"double\", Is         


        
4条回答
  •  遥遥无期
    2021-01-02 07:14

    Per the note on MSDN:

    A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.

    Somewhat surprisingly the DefaultValue only regulates the writing of an object, members that are equal to their DefaultValue will not be written out.

    You must still initialize members before or after loading yourself, for example in the constructor.

提交回复
热议问题