Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem With XML Serialization 1

Status
Not open for further replies.

Korach

Programmer
Jun 2, 2003
303
IL
I try to serialize a simpley array:
Code:
        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:
Code:
<?xml version="1.0" ?> 
- <ArrayOfInt xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance">[/URL]
  <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?
 
this might sound like a dumb question but just to check...is that minus sign (-) there because you copied from Internet Explorer?...

or is it in your xml code?

If it's in your xml code then that's the problem.
 
Thanks. It is Internet Explorer. My code is fine.
From Notepad:
Code:
<?xml version="1.0"?>
<ArrayOfInt xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance">[/URL]
  <int>1</int>
  <int>2</int>
  <int>3</int>
  <int>4</int>
</ArrayOfInt>
 
It's not happy with the tmpstream being left open

close tmpstream and declare New after the serialize method...


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

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

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

'*****
tmpStream.Close()

tmpStream = New IO.FileStream("C:\str.xml", IO.FileMode.Open)
'*****

arr = tmpSerializer.Deserialize(tmpStream)


Microsoft's disclaimer at the top of the MSDN Serializer page is a bit of a worry...

"This member supports the .NET Framework infrastructure and is not intended to be used directly from your code."

Sometimes this kind of language translates as "There are some issues here that we're not sharing...use at own risk".

 
Thanks a lot!
It's working now.
In which page of MSDN you saw the warning: "This member supports the .NET Framework infrastructure and is not intended to be used directly from your code." ?
 
I can't use Serialize?
Do you know the right approach to serialize?
 
Sorry, can't help - I'll have a look around though.

You probably can use it to some extent but that statement sounds like you'll come across some limitations (usually right at the point where you were about to accomplish something no doubt)
 
have just had a quick look around via Google and there seem to be some good tutorials and examples of Serialize including this one...


...and there doesn't seem to be any grief so it looks OK...not really sure what Microsoft is saying with that statement but it does look a bit ominous.
 
Thanks. It seems like I'm not the only one that use the serialize method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top