Xml Serialization vs. “True” and “False”

前端 未结 10 987
傲寒
傲寒 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:32

    I stumbled upon the same issue, and based on the answer by jman, I solved it this way:

        [XmlIgnore]
        public bool BadBoolField { get; set; }
    
        [XmlAttribute("badBoolField")]
        public string BadBoolFieldSerializable
        {
            get
            {
                return this.BadBoolField.ToString();
            }
            set
            {
                this.BadBoolField= Convert.ToBoolean(value);
            }
        }
    

    Be aware this is not necessarily by XML / Serialization specification, but it works good and it can handle a widespread conversion values (i.e. string like "True", "true", if you would replace the constraint of being a string it could handle numbers as well).

提交回复
热议问题