[XmlSerializer] Utilisation de cette classe avec une liste de classe et sérialisation XML
Bonjours à tous,
Je voudrais m'initier un peut dans la sérialisation XML.
J'ai crée une classe clsClient:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| Public Class clsClient
Private _Id As Integer
Private _Nom As String
Private _Prenom As String
Private _Telephone As String
Private _Portable As String
Private _Adresse As String
Private _CodePostal As String
Private _Ville As String
Private _Email As String
Private _NbReserv As Integer
Private _Commentaire As String
Private _Alerte As Boolean
Public Sub New()
_Id = 0
_Nom = ""
_Prenom = ""
_Telephone = ""
_Portable = ""
_Adresse = ""
_CodePostal = ""
_Ville = ""
_Email = ""
_NbReserv = 0
_Commentaire = ""
_Alerte = False
End Sub
Public Property ID() As Integer
Get
ID = _Id
End Get
Set(ByVal value As Integer)
_Id = value
End Set
End Property
Public Property NbReserv() As Integer
Get
NbReserv = _NbReserv
End Get
Set(ByVal value As Integer)
_NbReserv = value
End Set
End Property
Public Property Nom() As String
Get
Nom = _Nom
End Get
Set(ByVal value As String)
_Nom = value
End Set
End Property
Public Property Prenom() As String
Get
Prenom = _Prenom
End Get
Set(ByVal value As String)
_Prenom = value
End Set
End Property
Public Property Telephone() As String
Get
Telephone = _Telephone
End Get
Set(ByVal value As String)
_Telephone = value
End Set
End Property
Public Property Portable() As String
Get
Portable = _Portable
End Get
Set(ByVal value As String)
_Portable = value
End Set
End Property
Public Property CodePostal() As String
Get
CodePostal = _Portable
End Get
Set(ByVal value As String)
_CodePostal = value
End Set
End Property
Public Property Adresse() As String
Get
Adresse = _Adresse
End Get
Set(ByVal value As String)
_Adresse = value
End Set
End Property
Public Property Ville() As String
Get
Ville = _Ville
End Get
Set(ByVal value As String)
_Ville = value
End Set
End Property
Public Property Email() As String
Get
Email = _Email
End Get
Set(ByVal value As String)
_Email = value
End Set
End Property
Public Property Commentaire() As String
Get
Commentaire = _Commentaire
End Get
Set(ByVal value As String)
_Commentaire = value
End Set
End Property
Public Property Alerte() As Boolean
Get
Alerte = _Alerte
End Get
Set(ByVal value As Boolean)
_Alerte = value
End Set
End Property
End Class |
Pour mes essais je crée un fichier client XML avec ceux ci:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| Private Sub btValider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btValider.Click
Dim client As clsClient
Dim xs As XmlSerializer
Dim wr As StreamWriter
Dim List As New List(Of clsClient)
Dim i As Integer
'Pour les essaies je crée 6 clients
reprise:
client = New clsClient
client.ID = CInt(txtIDClient.Text) + i
client.Nom = txtNom.Text
client.Prenom = txtPrenom.Text
client.Adresse = txtAdress.Text
client.CodePostal = txtCodePostale.Text
client.Commentaire = txtCommentaire.Text
client.Email = txtEmail.Text
client.Portable = txtPortable.Text
client.Telephone = txtTelephone.Text
client.Ville = txtVille.Text
List.Add(client)
If i <= 5 Then
i += 1
GoTo reprise
End If
wr = New StreamWriter("Client.xml")
xs = New XmlSerializer(GetType(List(Of clsClient)))
xs.Serialize(wr, List)
wr.Close()
End Sub |
Cela fonctionne super bien. Mais lorsque je souhaite faire l'inverse cela fonctionne pas
Code:
1 2 3 4 5 6 7 8 9 10 11
| Public Sub loadClients()
Dim sr As New StringReader(Application.StartupPath & "/Client.xml")
Dim Lists As New List(Of clsClient)
Dim xs As XmlSerializer
Dim client As clsClient
xs = New XmlSerializer(GetType(List(Of clsClient)))
Lists = xs.Deserialize(sr) 'cela plante ici
sr.close()
End Sub |
Il me dit :
Une erreur s'est produite lors de la création du formulaire. Pour plus d'informations, consultez Exception.InnerException. L'erreur est : Il existe une erreur dans le document XML (1, 1).
Merci de votre aide, je recherche de mon coté.