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:

<Systems>
<GUID>7f939d72-122e-4d9b-8bfb-3bfca94c813c</GUID>
<Name>LOCALHOST</Name>
<IPv4>127.0.0.1</IPv4>
<Memory_Size>0</Memory_Size>
<IsDataCollector>True</IsDataCollector>
<IsLoadBot>False</IsLoadBot>
<DataCollector_TimeOut>30</DataCollector_TimeOut>
<LoadBot_SessionStreams>0</LoadBot_SessionStreams>
<LoadBot_Slots>0</LoadBot_Slots>
<WOL>False</WOL>
</Systems>
<Systems>
<GUID>cc126325-2eb9-4547-b99b-4c73a36f357e</GUID>
<Name>LoadBot01</Name>
<IPv4>172.17.2.72</IPv4>
<Memory_Size>49152</Memory_Size>
<OS_Family>Windows 7</OS_Family>
<IsDataCollector>False</IsDataCollector>
<IsLoadBot>True</IsLoadBot>
<DataCollector_TimeOut>30</DataCollector_TimeOut>
<LoadBot_SessionStreams>5</LoadBot_SessionStreams>
<LoadBot_Slots>500</LoadBot_Slots>
<GUID_RunAsAccount>f96312d1-7973-4448-9509-34b15eb78848</GUID_RunAsAccount>
<WOL>False</WOL>
</Systems>
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)
Try
'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)
objFileStream.SetLength(strContentOfXMLFile.Length)
'Write new content to XML file
streamWriter.Write(strContentOfXMLFile)
streamWriter.Flush()
streamWriter.Close()
'Close file
objFileStream.Close()
Catch
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

*

Donate

%d bloggers like this: