Serialization

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I serialize an object of type Classroom that contains another object of type Students that contains an ArrayList or Student, I get xml resembling

<Classroom><Students><Student/
..
</Students></Classroom

However, when I serialize an object of type Students, I get xml resembling

<ArrayOfStudent><Student/
..
</ArrayOfStudent

When I really want is Students (like when I serialize the Classroom object) instead of ArrayOfStudents. I've tried using various serialization directives such as <System.Xml.Serialization.XmlArray("Students")> (currently commented out) but I must be missing something

Any help would be greatly appreciated

Some code follows

Option Strict O

<Serializable()>
Public Class Studen

Private mFirstName As Strin
Private mLastName As Strin
Public Property FirstName() As Strin
Ge
Return mFirstNam
End Ge
Set(ByVal Value As String
mFirstName = Valu
End Se
End Propert
Public Property LastName() As Strin
Ge
Return mLastNam
End Ge
Set(ByVal Value As String
mLastName = Valu
End Se
End Propert
Public Sub Reset(
mFirstName = String.Empt
mLastName = String.Empt
End Su
Public Sub New(
Reset(
End Su
Public Sub New(ByVal FirstName As String, ByVal LastName As String
mFirstName = FirstNam
mLastName = LastNam
End Su

End Clas

Option Strict O

<Serializable()>
Public Class Student
Implements ICollectio

'<System.Xml.Serialization.XmlArray("Students")>
'<System.Xml.Serialization.XmlElement("Students")>
Private mStudents As ArrayList = New ArrayLis
Public ReadOnly Property Count() As Integer Implements ICollection.Coun
Ge
Count = mStudents.Coun
End Ge
End Propert
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerato
Return mStudents.GetEnumerator(
End Functio
Public ReadOnly Property IsSynchronized() As Boolean Implements ICollection.IsSynchronize
Ge
Return Fals
End Ge
End Propert
Default Public Overloads ReadOnly Property Item(ByVal Index As Integer) As Studen
Ge
Return CType(mStudents(Index), Student
End Ge
End Propert
Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoo
Ge
Return M
End Ge
End Propert
Public Function Add(ByVal NewStudent As Student) As Intege
mStudents.Add(NewStudent
Return mStudents.Coun
End Functio
Public Sub CopyTo(ByVal Students As Array, ByVal Index As Integer) Implements ICollection.CopyT
mStudents.CopyTo(Students, Index
End Su
Public Sub Clear(
mStudents.Clear(
End Su

End Clas

Option Strict O

<Serializable()>
Public Class Classroo

Private mStudents As New Student
' <System.Xml.Serialization.XmlArray("Students")>
Public Property Students() As Student
Ge
Return mStudent
End Ge
Set(ByVal Value As Students
mStudents = Valu
End Se
End Propert
'Public Sub Add(ByVal NewStudent As Student
' mStudents.Add(NewStudent
'End Su
Public Sub New(
End Su

End Clas

'Utility Functio
Public Function Serialize(ByVal [Object] As Object) As Strin

Dim Serializer As New XmlSerializer([Object].GetType()
Dim Writer As New StringWrite

Serializer.Serialize(Writer, [Object]
Writer.Close(

Return Writer.ToStrin

End Functio

'Driver Cod
Dim myClassroom As New Classroo

myClassroom.Students.Add(New Student("Jon", "Doe")
myClassroom.Students.Add(New Student("Jane", "Brown")
myClassroom.Students.Add(New Student("Tom", "Jones")
myClassroom.Students.Add(New Student("Sue", "Smith")

MessageBox.Show(Serialize(myClassroom)
MessageBox.Show(Serialize(myClassroom.Students)
 
The XML serializer is performing a shallow serialization on the Students class. As this class implements the ICollection interface, the XML serializer uses the ArrayOf... notation

Do you need to implement the ICollection interface? If so, have you tried using the SOAP formatter or is it not appropriate for your task? If not, it should produce your expectations if the ICollection implementation is removed

Landle

----- TPP wrote: ----

When I serialize an object of type Classroom that contains another object of type Students that contains an ArrayList or Student, I get xml resembling

<Classroom><Students><Student/
..
</Students></Classroom

However, when I serialize an object of type Students, I get xml resembling

<ArrayOfStudent><Student/
..
</ArrayOfStudent

When I really want is Students (like when I serialize the Classroom object) instead of ArrayOfStudents. I've tried using various serialization directives such as <System.Xml.Serialization.XmlArray("Students")> (currently commented out) but I must be missing something

Any help would be greatly appreciated

Some code follows

Option Strict O

<Serializable()>
Public Class Studen

Private mFirstName As Strin
Private mLastName As Strin
Public Property FirstName() As Strin
Ge
Return mFirstNam
End Ge
Set(ByVal Value As String
mFirstName = Valu
End Se
End Propert
Public Property LastName() As Strin
Ge
Return mLastNam
End Ge
Set(ByVal Value As String
mLastName = Valu
End Se
End Propert
Public Sub Reset(
mFirstName = String.Empt
mLastName = String.Empt
End Su
Public Sub New(
Reset(
End Su
Public Sub New(ByVal FirstName As String, ByVal LastName As String
mFirstName = FirstNam
mLastName = LastNam
End Su

End Clas

Option Strict O

<Serializable()>
Public Class Student
Implements ICollectio

'<System.Xml.Serialization.XmlArray("Students")>
'<System.Xml.Serialization.XmlElement("Students")>
Private mStudents As ArrayList = New ArrayLis
Public ReadOnly Property Count() As Integer Implements ICollection.Coun
Ge
Count = mStudents.Coun
End Ge
End Propert
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerato
Return mStudents.GetEnumerator(
End Functio
Public ReadOnly Property IsSynchronized() As Boolean Implements ICollection.IsSynchronize
Ge
Return Fals
End Ge
End Propert
Default Public Overloads ReadOnly Property Item(ByVal Index As Integer) As Studen
Ge
Return CType(mStudents(Index), Student
End Ge
End Propert
Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoo
Ge
Return M
End Ge
End Propert
Public Function Add(ByVal NewStudent As Student) As Intege
mStudents.Add(NewStudent
Return mStudents.Coun
End Functio
Public Sub CopyTo(ByVal Students As Array, ByVal Index As Integer) Implements ICollection.CopyT
mStudents.CopyTo(Students, Index
End Su
Public Sub Clear(
mStudents.Clear(
End Su

End Clas

Option Strict O

<Serializable()>
Public Class Classroo

Private mStudents As New Student
' <System.Xml.Serialization.XmlArray("Students")>
Public Property Students() As Student
Get
Return mStudents
End Get
Set(ByVal Value As Students)
mStudents = Value
End Set
End Property
'Public Sub Add(ByVal NewStudent As Student)
' mStudents.Add(NewStudent)
'End Sub
Public Sub New()
End Sub

End Class

'Utility Function
Public Function Serialize(ByVal [Object] As Object) As String

Dim Serializer As New XmlSerializer([Object].GetType())
Dim Writer As New StringWriter

Serializer.Serialize(Writer, [Object])
Writer.Close()

Return Writer.ToString

End Function

'Driver Code
Dim myClassroom As New Classroom

myClassroom.Students.Add(New Student("Jon", "Doe"))
myClassroom.Students.Add(New Student("Jane", "Brown"))
myClassroom.Students.Add(New Student("Tom", "Jones"))
myClassroom.Students.Add(New Student("Sue", "Smith"))

MessageBox.Show(Serialize(myClassroom))
MessageBox.Show(Serialize(myClassroom.Students))
 
Back
Top