True is not true (?)

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

Was once an enthusiastic PepperByte employee but is now working at Citrix. His blogs are still valuable to us and we hope to you too.