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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With