XML array of arrays serialization in VB

B

boris.golubov

I'm trying to write an object hierarchy that is serialized into/from
XML file of the following structure:
<?xml version='1.0'>
<Session>
<Level1 attr1="1">
<Level2 attr2="11">TEXT11</Level2>
<Level2 attr2="12">TEXT12</Level2>
...................................................
</Level1>
<Level1 attr2="2">
<Level2 attr2="21">TEXT21</Level2>
<Level2 attr2="22">TEXT22</Level2>
...................................................
</Level1>
.................................
</Session>

Basically , Session is an array of Level1-s, and and Level1 is an array
of Level2-s.

MSDN give the following example for 1-level of hierarchy after
exclusion of irrelevant to my question details):
Option Explicit
Option Strict
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
Public Class Group
<XmlArrayItem(GetType(Employee))> _
Public Employees() As Employee
End Class

Public Class Employee
Public Name As String
End Class

Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("TypeDoc.xml")
test.DeserializeObject("TypeDoc.xml")
End Sub

Public Sub SerializeObject(ByVal filename As String)
Dim s As New XmlSerializer(GetType(Group))
Dim writer As New StreamWriter(filename)
Dim group As New Group()
Dim emp1 As New Employee()
Dim emp2 As New Employee()
emp1.Name = "Seiko"
emp2.Name = "Martina"
Dim emps() As Employee = {emp1, emp2}
group.Employees = emps
s.Serialize(writer, group)
writer.Close()
End Sub

Public Sub DeserializeObject(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.Open)
Dim x As New XmlSerializer(GetType(Group))
Dim g As Group = CType(x.Deserialize(fs), Group)
Console.WriteLine("Members:")
Dim e As Employee
For Each e In g.Employees
Console.WriteLine(ControlChars.Tab & e.Name)
Next e
End Sub
End Class

However, when I include similar code inside Employee so it contains an
array of another class type XXXX, creating
XmlSerializer(GetType(Group)) throws System.InvalidOperationException
with additional information "There was an error reflecting type
'SerExample.Group'.".

Could anybody post an example of how to create a correct object
hierarchy? Thank you.
 

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