VB.net de-serialize XML and specify UTF-16 file encoding



I am doing the following to read an XML file and deserialize it:



Dim serializer As XmlSerializer = New XmlSerializer(GetType(AppStatus))
Dim appStatus As AppStatus = CType(serializer.Deserialize(File.OpenRead("c:\testing\statusData.xml")), AppStatus)


This is the XML file I am reading:



<?xml version="1.0" encoding="utf-16"?>
<AppStatus xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<CurrentFileProgressBarPercent>0</CurrentFileProgressBarPercent>
<CurrentFileKBUploaded>0</CurrentFileKBUploaded>
<CurrentFileKBTotal>0</CurrentFileKBTotal>
<OverallProgressBarPercent>0</OverallProgressBarPercent>
<OverallFileNumber>0</OverallFileNumber>
<OverallTotalFiles>0</OverallTotalFiles>
<statusMessage />
<logMessages />
</AppStatus>


This is the exception:



"There is no Unicode byte order mark. Cannot switch to Unicode


If I change the XML file to not have the encoding specified, it works fine. Is there an easy way to just specify that the file I'm reading will be UTF-16 so this won't be a problem?


I'm not very familiar with VB.net, just in case that wasn't obvious already :)


Thanks for any ideas!


No comments:

Post a Comment