简体   繁体   English

字符串“ false”不是有效的布尔值

[英]the string 'false' is not a valid boolean value

I am reading a value that is being written as a string using XmlWriter. 我正在读取使用XmlWriter以字符串形式写入的值。 Then it is being read by XmlReader and converted to a boolean. 然后,它由XmlReader读取并转换为布尔值。 Finally, the same XmlWriter method is called to write out to a separate Xml file. 最后,调用相同的XmlWriter方法以写出到单独的Xml文件。 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? 所以我的问题是..与我目前在这里做的事情相比,我是否需要做其他什么工作才能将“ false”转换为XmlSerialization中的有效布尔值? And if so, what might that be? 如果是这样,那可能是什么?

XmlConvert.ToBoolean only accept false in lower case string. XmlConvert.ToBoolean仅在小写字符串中接受false You are probably confusing it with Convert.ToBoolean 您可能将其与Convert.ToBoolean混淆了

XmlConvert.ToBoolean Method (String) XmlConvert.ToBoolean方法(字符串)

Valid strings are "1" or "true" for true and "0" or "false" for false. 有效字符串为“ 1”或“ true”表示“ true”,为“ 0”或“ false”表示“ false”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串未被识别为有效的布尔值 - String was not recognized as a valid boolean 如果布尔值为false,则在字符串中添加逗号 - Add comma in string if boolean is false 字符串在排序时未被识别为有效布尔值 - String was not recognized as a valid boolean on sorting SqlConnectionStringBuilder-无法将字符串识别为有效的布尔值 - SqlConnectionStringBuilder - String was not recognized as a valid Boolean 字符串“Sunday”未被识别为有效 Boolean - String "Sunday" was not recognized as a valid Boolean SSIS C# SQLBulkCopy .csv 文件错误:无法将参数值从字符串转换为布尔值。 字符串未被识别为有效的布尔值 - SSIS C# SQLBulkCopy .csv file Error: Failed to convert parameter value from a String to a Boolean. String not recognized as a valid Boolean 流畅的NHibernate OptimisticLock.None()导致“字符串'none'不是有效的布尔值。” - Fluent NHibernate OptimisticLock.None() causes “The string 'none' is not a valid Boolean value.” 如果布尔值为false,则在字符串中添加逗号和其他单词 - Add comma and other words in string if boolean is false System.FormatException:字符串未被识别为有效的布尔值 - System.FormatException: String was not recognized as a valid Boolean 附加信息:字符串未被识别为有效的布尔值 - Additional information: String was not recognized as a valid Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM