XML Serialize boolean as 0 and 1 XML Serialize boolean as 0 and 1 xml xml

XML Serialize boolean as 0 and 1


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

[XmlIgnore]public bool MyValue { get; set; }/// <summary>Get a value purely for serialization purposes</summary>[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.


You can implement IXmlSerializable which will allow you to alter the serialized output of your class however you want. This will entail creating the 3 methods GetSchema(), ReadXml(XmlReader r) and WriteXml(XmlWriter r). When you implement the interface, these methods are called instead of .NET trying to serialize the object itself.

Examples can be found at:

http://www.developerfusion.co.uk/show/4639/ and

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx


No, not using the default System.Xml.XmlSerializer: you'd need to change the data type to an int to achieve that, or muck around with providing your own serialization code (possible, but not much fun).

However, you can simply post-process the generated XML instead, of course, either using XSLT, or simply using string substitution. A bit of a hack, but pretty quick, both in development time and run time...