XML Encoding

May 11, 2011 at 10:30 AM

I save a table to XML file:

Dim fs As New IsolatedStorageFileStream("data.xml", FileMode.Create, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication)
ViewModel.db.Table(Of Person).WriteDTableToStream(fs)

But when I try to deserialize this XML file:
Dim oFile As IsolatedStorage.IsolatedStorageFile = IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication
Dim oStream As New IsolatedStorage.IsolatedStorageFileStream("data.xml", FileMode.Open, oFile)
Dim xs As New XmlSerializer(GetType(ObservableCollection(Of Person)))
Dim sXML As ObservableCollection(Of Person) = TryCast(xs.Deserialize(oStream), ObservableCollection(Of Person))
I get this error: "There is an error in XML document (0, 0)."
After analysing the XML file I found out that Encoding is set to "UTF-16". When looking into the WriteDTableToStream method I realised that XML file is
created by StreamWriter(stream). MSDN tells me that default Encoding should be UTF-8. Why is it set to UTF-16?
May 11, 2011 at 10:44 AM

You probably need to use the same code to read as you do when you write.  You can explicitly set encoding on the writer to pass to serializer.    Check the read code in the project you downloaded and make sure you match it exactly.  If that does not work, email me a quick project that reproduces the error, and I will take a look