none
Serialization of ObservableCollection

    Question

  • Hello,

     

    I am trying to serialize a complex recursive class. Here you have a conceptual version:

     

    <CollectionDataContract()> _

    Class RecursiveTest

    Inherits ObservableCollection(Of RecursiveTest)

     

    <DataMember()> _

    Public Name As String

    <DataMember()> _

    Public Value As Integer

     

    End Class

     

     

    The problem is that the serialization throw a StackOverflowException. I tried bot using the standard .NET 2.0 attribute <Serialize> and also the WCF attribute <CollectionDataContract>.

     

    Do you have any idea how can I solve this problem?

     

    Thanks in advance,

    Marco Ragogna

    Thursday, January 24, 2008 12:58 PM

All replies

  • Hi Marco,

     

    Based on your post, you define a class like the above format.

     

    I can serialize that class by using the binaryformatter class like the following code snippet, but I recommend that you don't define the class in the above format. You can define a new class as ObservableCollection's parameter. Then serialize the collection class. If you have any further questions, please tell me.

    Code Snippet

    Imports System.Collections.ObjectModel

    Imports System.Runtime.Serialization

    Imports System.Runtime.Serialization.Formatters.Binary

    Imports System.IO

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim testobj As New RecursiveTest()

    testobj.Name = "test"

    testobj.Add(New RecursiveTest("test two", "2222"))

    testobj.Value = 1111

    Dim formatter As IFormatter = New BinaryFormatter

    Dim streams As Stream = New FileStream("c:\obj.bin", FileMode.Create, FileAccess.Write)

    formatter.Serialize(streams, testobj)

    streams.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim formatter As IFormatter = New BinaryFormatter

    Dim streams As Stream = New FileStream("c:\obj.bin", FileMode.Open, FileAccess.Read)

    Dim testobj As RecursiveTest = formatter.Deserialize(streams)

    streams.Close()

    MessageBox.Show(testobj.Name & " " & testobj.Value.ToString)

    MessageBox.Show(testobj.Item(0).Value & " " & testobj.Item(0).Name)

    End Sub

    End Class

    <Serializable()> _

    Class RecursiveTest

    Inherits ObservableCollection(Of RecursiveTest)

    Public Sub New()

    End Sub

    Public Sub New(ByVal nstr As String, ByVal val As Integer)

    Name = nstr

    Value = val

    End Sub

    <DataMember()> _

    Public Name As String

    <DataMember()> _

    Public Value As Integer

    End Class

     

     

    Best regards,

    Riquel

    Tuesday, January 29, 2008 11:46 AM
  • thank you, it works very well

     

    do you have any idea why it fails using the Xml serializer?

     

    I also did not fully understand your sentence:

     

     You can define a new class as ObservableCollection's parameter. Then serialize the collection class.

    Tuesday, January 29, 2008 1:51 PM