ReadXML True vs true

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I've run across an issue that I'm trying to work around. I take that
back, I have a work around but I'm looking to see if anyone has a fix
for it.

The issue is that when loading a Boolean into a dataset using ReadXml,
..NET requires the "true" or "false" to be all lower case or 1 and 0. If
you use "True" or have any character capitalized, you get a
System.FormatException saying, "The string was not recognized as a
valid Boolean value."

The issue I have is that sometimes the XML is edited manually. I can't
guarantee that the person editing it won't unintentionally put "True".

I have a simple work around, I just catch the exception, find and
replace in the file and call ReadXml again but I'm looking to see if a
fix exist out there for it.

Here is some sample code.

Dataset1 ds = new Dataset1();

try
{
ds.ReadXml(@"C:\Test.xml");
}
catch (System.FormatException eFormat)
{
string temp = eFormat.Message;
}

<?xml version="1.0" standalone="yes"?>
<Dataset1 xmlns="http://tempuri.org/Dataset1.xsd">
<Table1>
<TestString>Blah</TestString>
<TestBool>True</TestBool>
</Table1>
<Table1>
<TestString>Blah2</TestString>
<TestBool>false</TestBool>
</Table1>
</Dataset1>
 
No one will ever convince me that having a programming language be case
sensitive is smart. I have been programming for years in Delphi and its not
case sensitive and I've never once had a case where it mattered. The only
argument for it would be that you force people using your functions or other
functions to always make them look the same in the source code, but other
than that it causes more problems than its worth. I know there are a few
other things that people use the case stuff for but that doesn't make things
more readable. It only makes you have to pay more attention to what your
reading to insure you are calling what you think you are calling. Sorry,
I'll get off my soap box... :-)

I'm watching your thread though because its only a matter of time before I
have this same problem.

Good luck,

glenn
 
No one will ever convince me that having a programming language
be case sensitive is smart. I have been programming for years
in Delphi and its not case sensitive and I've never once had a
case where it mattered. The only argument for it would be that
you force people using your functions or other functions to
always make them look the same in the source code, but other
than that it causes more problems than its worth. I know there
are a few other things that people use the case stuff for but
that doesn't make things more readable. It only makes you have
to pay more attention to what your reading to insure you are
calling what you think you are calling. Sorry, I'll get off my
soap box... :-)

I'm watching your thread though because its only a matter of
time before I have this same problem.

Glenn,

What does that have to do with Steve's question?

Steve is running into a certain behavior of the .Net FRAMEWORK, not
C#. He'd have the same problem if his code were written in Delphi
for .Net (or any other .Net language, for that matter).

Chris.
 
I've run across an issue that I'm trying to work around. I take
that back, I have a work around but I'm looking to see if anyone
has a fix for it.

The issue is that when loading a Boolean into a dataset using
ReadXml, .NET requires the "true" or "false" to be all lower
case or 1 and 0. If you use "True" or have any character
capitalized, you get a System.FormatException saying, "The
string was not recognized as a valid Boolean value."

The issue I have is that sometimes the XML is edited manually. I
can't guarantee that the person editing it won't unintentionally
put "True".

I have a simple work around, I just catch the exception, find
and replace in the file and call ReadXml again but I'm looking
to see if a fix exist out there for it.

Steve,

The behavior you're seeing is due to how the
System.Xml.XmlConvert.ToBoolean method works. As you observed, it
converts "true" or 1 to true, and "false" or 0 to false.
Anything else will throw a System.FormatException.

The only immediate fix I can think of is the one you are
already doing. If the XML file is small enough, and/or
the exception occurs more often then not, then you may
consider changing the algorithm. You could load the XML
file into a string, do any necessary replacements, and then
call a different version of ReadXml:

string xmlContents = string.Empty;
using (StreamReader sr = new StreamReader(@"C:\Test.xml"))
xmlContents = sr.ReadToEnd();

xmlContents = Regex.Replace(xmlContents, ">\s*?true\s*?<", ">true<", RegexOptions.IgnoreCase);
xmlContents = Regex.Replace(xmlContents, ">\s*?false\s*?<", ">false<", RegexOptions.IgnoreCase);

ds.ReadXml(new XmlTextReader(new StringReader(xmlContents));
 
Back
Top