Problem With XML Serialization

G

Guest

I try to serialize a simpley array:

Dim arr() As Integer = {1, 2, 3, 4}

Dim tmpStream As New IO.FileStream("C:\str.xml", IO.FileMode.Create)

tmpSerializer = New XmlSerializer(GetType(Integer()))
tmpSerializer.Serialize(tmpStream, arr)

arr = tmpSerializer.Deserialize(tmpStream)

XML file looks like:

<?xml version="1.0"?>
<ArrayOfInt xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<int>1</int>
<int>2</int>
<int>3</int>
<int>4</int>
</ArrayOfInt>

I'm getting an error when I'm trying to Deserialize:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll
Additional information: There is an error in XML document (0, 0).

What's the problem of my code?
 
C

Chris Dunaway

Two suggestions:

1. Turn Option Strict On. The line in which you deserialize should
not compile because you need to cast the result back to an integer
array:

arr = DirectCast(tmpSerializer.Deserialize(tmpStream),Integer())

2. After you have serialized to the stream, the stream is still
positioned at the end of the stream. You need to seek back to the
beginning:

tmpStream.Seek(0, SeekOrigin.Begin)

Chris
 

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

Top