How to ignore a nullable property from serialization if it is null or empty?

后端 未结 5 1075
星月不相逢
星月不相逢 2021-01-14 15:15

I have a class which is used for Xml Serialization.

Inside which I have a nullable property which is decorated with XmlAttribute:

 [XmlAttribute(\"la         


        
5条回答
  •  孤城傲影
    2021-01-14 16:02

    This is working for me.

        [XmlIgnore]
        public float? Speed { get; set; }
    
        [XmlAttribute("Speed")]
        public float SpeedSerializable
        {
            get
            {
                return this.Speed.Value;
            }
            set { this.Speed = value; }
        }
    
        public bool ShouldSerializeSpeedSerializable()
        {
              return Speed.HasValue;
        }
    

提交回复
热议问题