How to make a value type nullable with .NET XmlSerializer?

前端 未结 7 510
陌清茗
陌清茗 2020-12-02 11:47

Let\'s suppose I have this object:

[Serializable]
public class MyClass
{
    public int Age { get; set; }
    public int MyClassB { get; set; }
}
[Serializab         


        
7条回答
  •  失恋的感觉
    2020-12-02 12:13

    You can use XmlElementAttribute.IsNullable:

    [Serializable]
    public class MyClass
    {
        [XmlElement(IsNullable = true)]
        public int? Age { get; set; }
    
        public int MyClassB { get; set; }
    }
    

提交回复
热议问题