True is not true (?)

Written by Ingmar Verheij on October 21st, 2010. Posted in VB.Net

The string 'True' is not a valid Boolean value.
This error is thrown when an XML file is read into a DataSet using “System.Boolean” as DataType.

After a dataset is created, filled and written to an XML file using .WriteXML the content might look like this:

<OS_Family>Windows 7</OS_Family>
As you can see, both “True” and “False” starts with a capitol letter. This is what causes the problem, the .ReadXML method expects “true” or “false” instead of “True” or “False”.
Remember, the content of the file is the result of the .WriteXML method, so is it a bug or a feature?
I don’t know, but the “solution” to the problem is quite simple. After saving an XML file using the .WriteXML method replace “>Truetrue<” and do the same for False.
I usually use this code to write an XML file:

Friend Sub WriteXML(ByVal Source As DataSet, ByVal Filename As String)
'Define variables

'Save dataset to XML
Source.WriteXml(Filename, XmlWriteMode.IgnoreSchema)
'(re)open XML file
Dim objFileStream As FileStream = New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
'Create streams
Dim streamReader As New StreamReader(objFileStream)
Dim streamWriter As New StreamWriter(objFileStream)
'Read content of file
Dim strContentOfXMLFile As String = streamReader.ReadToEnd
'Replace boolean values
strContentOfXMLFile = strContentOfXMLFile.Replace(">TruetrueFalsefalse<")
'Reset position in filestream
objFileStream.Position = 0
'Reset lenght (in case content has changed)
'Write new content to XML file
'Close file
End Try
End Sub

Ingmar Verheij

Ingmar Verheij

At the time Ingmar wrote this article he worked for PepperByte as a Senior Consultant (up to May 2014). His work consisted of designing, migrating and troubleshooting Microsoft and Citrix infrastructures. He was working with technologies like Microsoft RDS, user environment management and (performance) monitoring. Ingmar is User Group leader of the Dutch Citrix User Group (DuCUG). RES Software named Ingmar RES Software Valued Professional in 2014.

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Tags: , , , , ,

Leave a comment



%d bloggers like this: