I am reading a value that is being written as a string using XmlWriter. Then it is being read by XmlReader and converted to a boolean. Finally, the same XmlWriter method is called to write out to a separate Xml file. But when I try to write the element the second time I am getting a failed unit test that says:
Result Message:
Test method threw exception:
System.FormatException: The string 'False' is not a valid Boolean value.
I am writing the element with
writer.WriteStartElement("variableNameIsRelative");
writer.WriteCData(p.VariableNameIsRelative.ToString());
writer.WriteEndElement();
then reading with
param.VariableNameIsRelative = XmlConvert.ToBoolean(reader.ReadElementContentAsString());
and again using the same writer as before just outputting to a different file (this is where it breaks.)
writer.WriteStartElement("variableNameIsRelative");
writer.WriteCData(p.VariableNameIsRelative.ToString());
writer.WriteEndElement();
So my question is..Is there anything different I have to do to convert "false" to a valid boolean value in XmlSerialization vs what I am currently doing here? And if so, what might that be?
XmlConvert.ToBoolean
only accept false
in lower case string. You are probably confusing it with Convert.ToBoolean
XmlConvert.ToBoolean Method (String)
Valid strings are "1" or "true" for true and "0" or "false" for false.
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