Question Cannot fix deserialization error

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I have a class instance that I want to serialize, upload to SkyDrive, download from SkyDrive, and then deserialize. I have the serialization, uploading, and downloading working fine, but the deserialization gives me the following error:

There is an error in XML document (2, 2).

The generated XML looks like the following:

<?xml version="1.0"?>
<SyncData>
<Recipients>
<Recipient>
<ID>46</ID>
<Name>Mom</Name>
</Recipient>
<Recipient>
<ID>0</ID>
<Name>Nate</Name>
</Recipient>
</Recipients>
<Gifts>
<Gift>
<Description>Vacation</Description>
<Price>0</Price>
<ID>0</ID>
<Recipient>46</Recipient>
<AvailableAt />
<Notes />
<Buyer />
</Gift>
<Gift>
<Description>WP8</Description>
<Price>0</Price>
<ID>735256239</ID>
<Recipient>0</Recipient>
<AvailableAt />
<Notes />
<Buyer />
</Gift>
</Gifts>
</SyncData>

The code I use to deserialize the XML is as follows:

If New XmlSerializer(GetType(SyncData)).CanDeserialize(XmlReader.Create(e.Result)) Then
e.Result.Position = 0
Dim tempdata As SyncData = CType(New XmlSerializer(GetType(SyncData)).Deserialize(e.Result), SyncData) '<-This line give error "There is an error in XML document (2, 2)."
CType(Application.Current, App).Recipients = tempdata.Recipients
CType(Application.Current, App).Gifts = tempdata.Gifts
End If

I have also tried the following code for testing purposes:

Dim temp As Integer = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))
temp = e.Result.ReadByte()
System.Diagnostics.Debug.WriteLine("{0} {1}", temp, Text.Encoding.UTF8.GetChars({CByte(temp)})(0))

Which gave the following output:

60 <
63 ?
120 x
109 m
108 l
32
118 v
101 e
114 r
115 s

This obviously confirms that e.Result does contain the data I expect (the first characters are just the normal characters, not some special character that would cause a problem). However, it also shows that there is no Byte Order Mark. Is it possible that this could be the problem? If I need to add a Byte Order Mark, how would I do it? Is there something else that I am doing wrong? Any help would be appreciated. Thanks.
 
The thing that had changed since my previous thread was that I modified the serialization code to not include any namespaces, so it now looks like the following:

Dim stream As New MemoryStream()
Dim ns As New XmlSerializerNamespaces() : ns.Add("", "")
Dim xml As New XmlSerializer(GetType(SyncData))
xml.Serialize(stream, New SyncData(CType(Application.Current, App).Recipients, CType(Application.Current, App).Gifts), ns)
stream.Position = 0
Me.client.UploadAsync("me/skydrive", datafilename, stream, OverwriteOption.Overwrite)

But I think you have solved my problem! When I looked at the InnerException, it said SyncData could not be serialized because it did not have a parameterless constructor. I guess I need to look at the details of the exceptions more closely, I think I didn't notice the keywords here ("parameterless constructor") because they were not immediately visible in the window, but once I added a parameterless constructor to the class, it worked! Thank you so much (and go ahead, call me lazy and stupid for not looking at the exception closely enough, I deserve it)!
 
That is weird, XmlSerializer throws InvalidOperationException from its constructor when type to serialize doesn't have a parameterless constructor.
 
Back
Top