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.
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.