Gavin Stevens's Blog

the ramblings of another developer....

  Home  |   Contact  |   Syndication    |   Login
  34 Posts | 0 Stories | 57 Comments | 212 Trackbacks



Monday, August 9, 2004 #

I was trying a very simple prototype of manually serializing objects and storing them in a memory stream.  Obviously this sample is not the final implementation, it's just a test.  So tell me why I get this stupid error....  here is the code:

Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml
Imports System.Text

<Serializable()> _
Public Class TestObject
Public Firstname As String
Public lastname As String
Public DOB As Date
Public age As Integer

Dim oTO As New TestObject
oTO.Firstname = "John"
oTO.lastname = "Doe"
oTO.DOB = "02/13/1972"
oTO.age = 32

'Serialize Object to Memory Stream
Dim oSerializer As New XmlSerializer(GetType(TestObject))
Dim oMemStream As New MemoryStream
oSerializer.Serialize(oMemStream, oTO)

'Deserialize Object
oTO = CType(oSerializer.Deserialize(oMemStream), TestObject)
Console.WriteLine(oTO.Firstname & " " & oTO.lastname)
Console.WriteLine(oTO.DOB & " " & oTO.age)

Catch ex As Exception
End Try
End Sub

Thats it, pretty simple, but I get this error:

System.InvalidOperationException: There is an error in XML document (0, 0). ---> System.Xml.XmlException: The root element is missing.
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.MoveToContent()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read4_TestObject()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)