Serializing complex object

C

Chris Dunaway

Consider the following simple class:

<Serializable()> _
Public Class SimpleClass
Private m_AString As String
Private m_AByteArray As Byte()

Public Property AString() As String
. . .
End Property

Public Property AByteArray() As Byte()
. . .
End Property
End Class

I can use xml serialization to serialize an instance of this class to a
file. Works fine. Now I want to make it a little more complex like the
following two classes:

<Serializable()> _
Public Class DataFile
Private m_FileName As String
Private m_FileData As Byte()

Public Property FileName() As String
. . .
End Property

Public Property FileData() As Byte()
. . .
End Property
End Class

<Serializable()> _
Public Class ComplexClass
Private m_AString As String
'An array of DataFile objects
Private m_aDataFiles As DataFile()

Public Property AString() As String
. . .
End Property

Public Property DataFiles() As DataFile()
. . .
End Property
End Class

When I try to serialize an instance of the ComplexClass, I get an
exception:

"There was an exception reflecting type 'Complex Class'

Here is the code I use to serialize the object. The exception occurs on
the line indicated:

Private Sub Button1_Click(...) Handles Button1.Click
Dim oTest As New ComplexClass

oTest.AString = "One"
ReDim oTest.DataFiles(1)

oTest.DataFiles(0) = New DataFile("c:\test\testme.en.wav")
oTest.DataFiles(1) = New DataFile("c:\test\my recording.en.wav")

Dim fs As New FileStream("c:\Complex.xml", FileMode.Create)


'**** ERROR OCCURS ON FOLLOWING LINE ****
Dim xs As New XmlSerializer(GetType(ComplexClass))

xs.Serialize(fs, oTest)
End Sub


Is it possible to do what I want?

Thanks,

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

Chris Dunaway

Is it possible to do what I want?

Wouldn't you know it? Shortly after I submitted this question, I found the
answer. By digging into the stack trace, it turns out that the classes
involved must have a default parameterless constructor, which I did not
have in my test. Once I added that, it worked like a charm.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

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