Problème de sérialisation liste de classe

Résolu
Withere Messages postés 3 Date d'inscription mercredi 7 août 2013 Statut Membre Dernière intervention 8 août 2013 - 7 août 2013 à 17:14
Withere Messages postés 3 Date d'inscription mercredi 7 août 2013 Statut Membre Dernière intervention 8 août 2013 - 8 août 2013 à 13:16
Bonjour,
débutant en vb.net, après un bon passage sous VB6, me voici sur vb net pour améliorer mes compétences.
Souhaitant attaquer la sérialisation de données en XML, voici mon problème.

J'ai une classe Box :
<Serializable()>
Public Class Box
    Public _Id As Integer
    Public _Name As String
    Public _Length As Integer
    Public _Width As Integer
    Public _Height As Integer
    Public _Weight As Integer
    Sub New()
        ' TODO: Complete member initialization 
    End Sub

    <XmlElement("ID")> _
    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property
    <XmlElement("Name")> _
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    <XmlElement("Length")> _
    Public Property Length() As Integer
        Get
            Return _Length
        End Get
        Set(ByVal value As Integer)
            _Length = value
        End Set
    End Property
    <XmlElement("Width")> _
    Public Property Width() As Integer
        Get
            Return _Width
        End Get
        Set(ByVal value As Integer)
            _Width = value
        End Set
    End Property
    <XmlElement("Height")> _
    Public Property Height() As Integer
        Get
            Return _Height
        End Get
        Set(ByVal value As Integer)
            _Height = value
        End Set
    End Property
    <XmlElement("Height")> _
    Public Property Weight() As Integer
        Get
            Return _Weight
        End Get
        Set(ByVal value As Integer)
            _Weight = value
        End Set
    End Property
End Class


Ainsi qu'une classe regroupant l'ensemble de mes classes "Box"

<Serializable(), XmlRoot("PMLBoxList")> _
Public Class BoxList
    Public boxlist()
    Private _Items As New List(Of Box)

    <XmlElement("Box")> _
    Public Property Items() As List(Of Box)
        Get
            Return _Items
        End Get
        Set(ByVal value As List(Of Box))
            _Items = value
        End Set
    End Property
End Class

Puis ma fonction de sérialisation : 
<code>        Dim bnList As List(Of Box) = New List(Of Box)
        Dim boxlist As BoxList = New BoxList()
        Dim bnItem As Box = New Box()

        bnList.Add(New Box With {.Id = 1, .Name = "toto", .Length = 125, .Width = 13, .Height = 125, .Weight = 12})
        boxlist.Items = bnList

        'On crée le fichier et récupère son flux
        Dim FluxDeFichier As FileStream = File.Create("Box.xml")
        Dim Serialiseur As New XmlSerializer(GetType(BoxList)) 'Ligne de l'erreur
        'Serialisation et écriture
        Serialiseur.Serialize(FluxDeFichier, boxlist)
        'Fermeture du fichier
        FluxDeFichier.Close()


Pendant la compilation j'obtiens l'erreur suivante :
Une erreur s'est produite lors de la réflexion du type 'A.BoxList'.

J'ai bien regardé sur les forums sans réellement comprendre le problème.
En vous remerciant tous d'avance !

4 réponses

Utilisateur anonyme
8 août 2013 à 07:29
Salut,

Rajoute un constructeur par défaut (sub new) a ta classe BoxList.
1
Je m'étais plus préoccupé par l'absence de ton constructeur que par reste du code.
Il y a une erreur ici :
<XmlElement("Height")> _
 Public Property Weight() As Integer

Remplace Height par Weight sur l'attribut de la propriété.



--
1
Withere Messages postés 3 Date d'inscription mercredi 7 août 2013 Statut Membre Dernière intervention 8 août 2013
Modifié par Withere le 8/08/2013 à 09:51
Bonjour,
tout d'abord merci de la réponse.
J'ai bien rajouté le constructeur par défaut ce qui me donne la classe suivante :
<Serializable(), XmlRoot("PMLBoxList")> _
Public Class BoxList
    Public boxlist()
    Private _Items As New List(Of Box)

    <XmlElement("Box")> _
    Public Property Items() As List(Of Box)
        Get
            Return _Items
        End Get
        Set(ByVal value As List(Of Box))
            _Items = value
        End Set
    End Property

    Sub New()
        ' TODO: Complete member initialization 
    End Sub


Cependant lors de l'initialisation de l'instance "XmlSerialiseur" , toujours la même erreur en retour.
"Une erreur s'est produite lors de la réflexion du type 'A.BoxList'."

Une autre idée ? Car je sèche un peu à vrai dire !

Serait ce dû au type que je transmet lors de l'instanciation ?
Dim Serialiseur As New XmlSerializer(GetType(BoxList))
0
Withere Messages postés 3 Date d'inscription mercredi 7 août 2013 Statut Membre Dernière intervention 8 août 2013
8 août 2013 à 13:16
Resalut !

Oui je m'en suis rendu compte de l'erreur après avoir lu le "détail" de l'erreur (qui finalement aide plutôt bien, malgré son côté assez "chargé")

En tout cas merci, c'est résolu et ça fonctionne correctement :]
0
Rejoignez-nous