Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialize boolean as 0 and 1

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?

like image 247
Igal Tabachnik Avatar asked Sep 17 '08 15:09

Igal Tabachnik


People also ask

What is the correct way of using XML serialization?

As with the CreatePo method, you must first construct an XmlSerializer, passing the type of class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.

What is serializing XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is XML serialization and Deserialization?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

Why do we serialize XML?

Advantages of Using XML Serialization For example, XmlSerializer enables you to: Specify whether a field or property should be encoded as an attribute or an element. Specify an XML namespace to use. Specify the name of an element or attribute if a field or property name is inappropriate.


2 Answers

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.

like image 166
Simon Steele Avatar answered Sep 27 '22 21:09

Simon Steele


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

like image 41
Wolfwyrd Avatar answered Sep 27 '22 20:09

Wolfwyrd