XML Serialize boolean as 0 and 1

前端 未结 3 1559
我在风中等你
我在风中等你 2020-12-20 11:11

The XML Schema Part 2 specifies that an instance of a datatype that is defined as boolean can have the following legal literals {true, false, 1, 0}. The following XML, for e

3条回答
  •  萌比男神i
    2020-12-20 11:36

    You can also do this by using some XmlSerializer attribute black magic:

    [XmlIgnore]
    public bool MyValue { get; set; }
    
    /// Get a value purely for serialization purposes
    [XmlElement("MyValue")]
    public string MyValueSerialize
    {
        get { return this.MyValue ? "1" : "0"; }
        set { this.MyValue = XmlConvert.ToBoolean(value); }
    }
    

    You can also use other attributes to hide this member from intellisense if you're offended by it! It's not a perfect solution, but it can be quicker than implementing IXmlSerializable.

提交回复
热议问题