XML Deserialzing causes exception

S

Simon Jefferies

Hello,

I'm trying to deserialize a document created with the following pieces of
code.

I get an exception "Additional information: There is an error in XML
document (2, 2).", why does this happen?

Serializing code:

Dim fs As New FileStream(Filename, FileMode.Create, FileAccess.Write)
Dim SoundTableItem As SoundTableItem = Nothing
For Each SoundTableItem In SoundItems

Dim XmlEntry As New
Xml.Serialization.XmlSerializer(GetType(SoundTableItem))
XmlEntry.Serialize(fs, SoundTableItem)

Next

fs.Close()

Deserializing code:

Dim fs As New FileStream(Filename, FileMode.Open, FileAccess.Read)
Dim tr As New Xml.XmlTextReader(fs)
Dim XmlEntry As New Xml.Serialization.XmlSerializer(GetType(SoundTableItem))
While Not tr.EOF()
Dim SoundEntry As SoundTableItem = Nothing
SoundEntry = XmlEntry.Deserialize(tr)
SoundItems.Add(SoundEntry)
End While

--
Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
www.headfirst.co.uk www.callofcthulhu.com
-
 
C

Cor Ligthert

Hi Simon.

I made an other serialize routine using the stringwriter, you can try it.
It is changed the code in this message so there can be typos.

Dim Serializer As New
Xml.Serialization.XmlSerializer(GetType(BelgenCollection))
Dim sw As New System.IO.StringWriter
Serializer.Serialize(sw, Belgen)
dim mystring = sw.ToString

Dim Deserializer As New Xml.Serialization.XmlSerializer _
(GetType(BelgenCollection))
Dim sr As New System.IO.StringReader(Charles)
Dim reader As New System.Xml.XmlTextReader(sr)
Dim Vlamingen As BelgenCollection
Vlamingen = CType(Deserializer.Deserialize(reader), _
BelgenCollection)

It has to be a proper object.

Cor
 
P

Peter Huang

Hi Simon,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to serialized an array
of objects into xml.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

In addition to to Cor's suggestion, I think you may try to take a look at
the link below.

How to xml serialize arraylist of objects?
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&selm=bC07akcA
EHA.612%40cpmsftngxa06.phx.gbl&rnum=3

If you still have any concern on this issue, please feel free to let me
know.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top