Click here to Skip to main content
15,743,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have problem with my serialization. I have an object that contains a list of other objects. I need to serialize this object but always get an error. The principal Object is called "DataGeneraleAudit". The error is:
"An error occurred during the reflection type"

Here is my object DataGeneraleAudit:

VB
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Xml.Serialization



<XmlRoot("DonneesLogiciel")> Public Class DataGeneraleAudit
    Public Sub New()

    End Sub
#Region "Déclaration"
    Public _DataTotale As List(Of DataAudit)
#End Region

#Region "Propiétés"
     <XmlAttribute("Total")> Public Property DonneeTotale() As List(Of DataAudit)
        Get
            Return _DataTotale
        End Get

        Set(ByVal value As List(Of DataAudit))
            _DataTotale = value
        End Set
    End Property

#End Region


End Class


As you can see in the code, I create an object from a list of DataAudit. The Object DataAudit is compose for differents methodes and property and objects too.

Here is the Object "DataAudit":
VB
Public Class DataAudit 


#Region "Declarations"
     Private _Technologie As New DataTechnologie
    Private _Acquisition As New DataAcquisition
    Private _Resultats As New DataResultats


#End Region

#Region "Propiétés"
     Public Property Technologie() As DataTechnologie
        Get
            Return _Technologie
        End Get

        Set(ByVal value As DataTechnologie)
            _Technologie = value
        End Set
    End Property


    Public Property Acquisition() As DataAcquisition
        Get
            Return _Acquisition
        End Get

        Set(ByVal value As DataAcquisition)
            _Acquisition = value
        End Set
    End Property

    Public Property ResultaAs As DataResultats
        Get
            Return _Resultats
        End Get

        Set(ByVal value As DataResultats)
            _Resultats = value
        End Set
    End Property

#End Region

#Region "Méthodes"
 
    Public Sub New()
   
    End Sub

#End Region

   
End Class


and finally, here is the serialization:

VB
Public Class MainWindow
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
 Friend nomFichierConfigXml As String
    Friend Config As New DataGeneraleAudit

 Private Sub MainWindow_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        Try

            ' Sérialization XML...
            Dim xSerializer As XmlSerializer = New XmlSerializer(GetType(DataGeneraleAudit))
            Dim writer As TextWriter = New StreamWriter("nomFichierConfigXml")
            xSerializer.Serialize(writer, Config)
            writer.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


Where is the problem please? Somebody can help me?
Thank you
Posted

1 solution

Why would you use such a "manual" kind of serialization. Do yourself a great favor, switch to Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is probably the very best. First of all, it is non-intrusive: you don't modify your data classes to become persistent, you don't have to implement any interfaces or derive from any required base classes. You simply add some attributes to the types and members you need to participate in the contract, which cannot modify the behavior of your types in any other aspect. You can simply create any object graph, even having circular references (non-tree graph) and store it an any stream, and restore it in full any time later. So, this approach is the most robust yes the easiest to use.

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900