I'm having an issue with deserializing an XML file with boolean values. The source XML files I'm deserializing were created from a VB6 app, where all boolean values are capitalized (True
, False
). When I try to deserialize the XML, I'm getting a
System.FormatException: The string 'False' is not a valid Boolean value.
Is there a way to say ignore case with an attribute?
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.
XML. XML is a human-readable serialization protocol. A well known XML-like format is HTML which is used to determine the structure of web pages.
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. To serialize or deserialize objects, use the XmlSerializer class.
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.
Based on another stack overflow question you can do:
public class MySerilizedObject
{
[XmlIgnore]
public bool BadBoolField { get; set; }
[XmlElement("BadBoolField")]
public string BadBoolFieldSerialize
{
get { return this.BadBoolField ? "True" : "False"; }
set
{
if(value.Equals("True"))
this.BadBoolField = true;
else if(value.Equals("False"))
this.BadBoolField = false;
else
this.BadBoolField = XmlConvert.ToBoolean(value);
}
}
}
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