I posted some examples Friday but they had more stuff in it then they needed. Here is
a more basic example of this bug. Below is two classes, code to put on a form with a
button1 and the error message I am getting. Notice: if I replace the items added to
the arraylist (sub load) with strings instead of objects, the serialization works.
Why does serialization work with an arraylist of strings and not objects?
Error:
An unhandled exception of type 'System.InvalidOperationException' occurred in
system.xml.dll
Additional information: There was an error generating the XML document.
Public Class DataHolder
Dim mArray As New ArrayList()
<Xml.Serialization.XmlArray()> _
Public Property Array() As ArrayList
Get
Return mArray
End Get
Set(ByVal Value As ArrayList)
mArray = Value
End Set
End Property
Public Sub Load()
Dim v As New ArrayItem()
v.Data = "First Element"
Me.mArray.Add(v)
v = New ArrayItem()
v.Data = "Second Element"
Me.mArray.Add(v)
End Sub
Public Function GetXML()
Me.Array.TrimToSize()
Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(DataHolder))
Dim writer As New System.IO.StringWriter()
Dim Obj As DataHolder
Obj = Me
serializer.Serialize(writer, Obj)
Dim s As String
s = writer.ToString
Console.Write(s)
writer.Close()
Return s
End Function
End Class
Public Class ArrayItem
Dim mData As String
Public Property Data() As String
Get
Return mData
End Get
Set(ByVal Value As String)
mData = Value
End Set
End Property
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim X As New DataHolder()
X.Load()
MsgBox(X.GetXML)
End Sub
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.