27

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 example, when deserialized, sets the boolean property "Emulate" to true.

<root>
    <emulate>1</emulate>
</root>

However, when I serialize the object back to the XML, I get true instead of the numerical value. My question is, is there a way that I can control the boolean representation in the XML?

e-sushi
  • 13,786
  • 10
  • 38
  • 57
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
  • You might want to mention what you are using to do the XML serialization. – Darrel Miller Sep 17 '08 at 15:30
  • 1
    A complementary [question](http://codereview.stackexchange.com/questions/4829/presenting-a-boolean-as-an-int-for-xmlserialization-in-c) on the same topic over at CoreReview.SE – MPelletier May 08 '14 at 14:33

3 Answers3

56

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.

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
Simon Steele
  • 11,558
  • 4
  • 45
  • 67
  • 4
    This is awful, but precisely solves the problem! Upvote just for threatening the short-term retention of my breakfast. – Tom W Feb 21 '12 at 08:27
  • This is exactly what I was looking for! Thank you very much, I thougt I would never be able to store boolean values in XML as "true" and "false". – Alex Feb 28 '13 at 10:37
  • 1
    Not the best answer: now you have two properties instead of one. I found a much cleaner solution here: https://stackoverflow.com/questions/5963423/xmlserializer-property-converter#answer-7775097 In a nutshell, create a new class/struct which implements IXmlSerializable. – Fl4v Jan 25 '18 at 14:23
3

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

Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
1

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...

mdb
  • 52,000
  • 11
  • 64
  • 62