Click here to Skip to main content
14,981,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Regards,

In short, i'm making a WPF application. The application has 5 objects that store data. The objects are called:

DataGeneraleAudit, DataAudit, DataTechnologie, dataacquisition, DataResultat

The object DataGeneraleAudit contains a list of DataAudit object.

The object DataAudit contains the others objects: DataTechnologie, dataacquisition and DataResultat.

I want to SERIALIZE DataGeneraleAudit object for recover data from all others objects.

Here I leave the 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"
    Friend _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

#Region "Méthodes"
       Public Sub New()
     _DataTotale = New List(Of DataAudit)
    End Sub

    Public Sub AddDataTotale(ByVal AllDonnees As String) 
        Dim DatosTotal = New DataAudit(AllDonnees)
        Me.DonneeTotale.Add(DatosTotal)

    End Sub
#End Region
End Class

Now I show you the object DataAudit:

VB
Public Class DataAudit 

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


    Private _textNomAudit As String
    Private _textDateAudit As String

    
#End Region


    Private _allDonnees As String

    Sub New(ByVal AllDonnees As String)
        'TODO: Complete member initialization 
                _allDonnees = AllDonnees
    End Sub

#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 Resultats As DataResultats
        Get
            Return _Resultats
        End Get

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

#End Region

#Region "Propiétés "
        Public Property _NomAudit() As String
        Get
            Return _textNomAudit
        End Get

        Set(ByVal value As String)
            _textNomAudit = value
        End Set
    End Property

    
    Public Property _DateAudit() As String
        Get
            Return _textDateAudit
        End Get

        Set(ByVal value As String)
            _textDateAudit = value
        End Set
    End Property

           
#End Region
#Region "Méthodes"
     'Creation de constructeur
    Public Sub New()

    End Sub




    Public Sub AllDonneesAudit(ByVal technologie As DataTechnologie, ByVal acquisition As DataAcquisition, ByVal resultats As DataResultats, _
                               ByVal TextoNomAudit As String, ByVal TextoDateAudit As String, ByVal TextoNuOccupants As String)

        Me.Technologie = technologie
        Me.Acquisition = acquisition
        Me.Resultats = resultats
        Me._NomAudit = TextoNomAudit
        Me._DateAudit = TextoDateAudit
          End Sub

#End Region

End Class


The other objects have only a few attributes.

Finally, the class where i made the serialization (MainWindow class):

VB
Friend Config As New DataGeneraleAudit()
Private Sub MainWindow_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        Try
        
            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

Now the problem arises when I try to serialize a DataGeneraleAudit I always get the following error:

"An error occurred During The reflection type"

As far as I found, this type of error comes from the serialization. But I could not fix it.

Could someone help me with this little problem?

thanks
Posted

1 solution

Why using this kind of serialization at all? I would say, it's obsolete and it wasn't very well designed. Instead, use Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the easiest to use, non-intrusive and most comprehensive. You don't have to modify your data types in any way: no need to implement any interfaces, derive from any certain classes or anything like that. You only mark some types and members (those you want to be part of contract) with appropriate attributes. You can store and later restore any object graph, even with circular references in it.

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...[^].

—SA
   
Comments
alonetmr 29-Jul-13 10:46am
   
HI Sergey,
Thanks for you answer, but i can't understand your point.
If possible to you to give an exemple with my program?
In another words, you can use the Data Contract with the program that I just put up?
thanks you
Sergey Alexandrovich Kryukov 29-Jul-13 11:17am
   
There are enough examples in the documentation. The usage is way too simple.
You simply create some data types having no serialization in mind. You don't have to make members to serialize public, or anything special. Decide what should be stored. Mark types with [DataContract] and members with [DataMember] attributes. Use DataContractStreamSerializer to store/load some graph in memory.

Will you simply try it by yourself and ask a follow-up question if you stuck? How about that?

—SA
alonetmr 29-Jul-13 11:34am
   
Thank you again for you answer.
It seems that your method is best to solve my problem. After seeing your suggestion, I found a lot of information online about it. However, when I try to apply this method, I get a problem. When i write : <datacnntract ()=""> _ with types, i get this problem:
Error 1 Type 'DataContract' is not defined.
Why i have this problem?
The same problem for <datamember()>
Thank you
Sergey Alexandrovich Kryukov 29-Jul-13 12:15pm
   
This is based on System.Runtime.Serialization.DataContractSerializer, so you need to reference the assembly System.Runtime.Serialization.dll (but I think you already did). The same goes about DataMemberAttrubute and DataContractAttribute. Check up fully-qualified names (that is, with namespaces) and make sure you use correct names.

Also, don't forget the naming convention for .NET attributes. For example, if the attribute name is "DataMemberAttrubute", it can be used in abbreviated form at the point of application:



[DataType(Namespace="http://myDomain.com/myNamespace"]
class MyClass {

[DataMember]
MyType MyMember;

//...

}


If you got it, consider accepting my answer formally (green button).
In all cases, your follow-up questions will be welcome, of course.

—SA
alonetmr 29-Jul-13 11:46am
   
Is made a mistake in the comment, is not _with types, is [DataContract]

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