Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the string 'false' is not a valid boolean value

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?

like image 835
Tim Avatar asked Jul 26 '16 19:07

Tim


1 Answers

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.

like image 161
Habib Avatar answered Nov 09 '22 12:11

Habib