Xml Serialization vs. “True” and “False”

前端 未结 10 978
傲寒
傲寒 2021-01-07 20:23

I\'m having an issue with deserializing an XML file with boolean values. The source XML files I\'m deserializing were created from a VB6 app, where all boolean values are c

10条回答
  •  离开以前
    2021-01-07 20:30

    Based on another stack overflow question you can do:

    public class MySerilizedObject
    {
        [XmlIgnore]
        public bool BadBoolField { get; set; }
    
        [XmlElement("BadBoolField")]
        public string BadBoolFieldSerialize
        {
            get { return this.BadBoolField ? "True" : "False"; }
            set
            {
                if(value.Equals("True"))
                    this.BadBoolField = true;
                else if(value.Equals("False"))
                    this.BadBoolField = false;
                else
                    this.BadBoolField = XmlConvert.ToBoolean(value);
            }
        }
    }
    

提交回复
热议问题