Bonjour,
J'essaye de me battre avec la serialisation en XML mais sans succès, après plein d'erreurs, j'arrive à obtenir un fichier XML mais ou il n'y a pas les infos!
Je veux enregistrer des contact j'ai donc une classe Contact et une classe ContactCollection qui contient une liste de tous les contacts. Ensuite je sérialize la classe ContactCollection.
Je vous laisse regarder le code, ce sera plus explicite! Quelqu'un pourrait-il m'aider??
Merci beaucoup !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System; using System.Collections; using System.IO; using System.Xml; using System.Xml.Serialization; namespace WindowsFormsApplication1 { public partial class main : Form { public contactCollection liste = new contactCollection(); public add AddWindows; public main() { } private void buttonExport_Click(object sender, EventArgs e) { // on ouvre le fichier DialogResult resultDial = saveXMLFileDialog.ShowDialog(); if (resultDial == DialogResult.OK) { // creation du flux TextWriter flux = new StreamWriter(saveXMLFileDialog.FileName); // on ecrit la liste dans un fichier XML XmlSerializer s = new XmlSerializer( typeof( contactCollection ) ); TextWriter w = new StreamWriter( @"z:\list.xml" ); s.Serialize( w, liste ); w.Close(); } } private void main_Load(object sender, EventArgs e) { } private void buttonAddContact_Click(object sender, EventArgs e) { AddWindows.ShowDialog(); } } [XmlRoot("contactCollection")] public class contactCollection { private ArrayList AllContacts; //contacts est une liste de Contact public contactCollection() { AllContacts = new System.Collections.ArrayList(); } [XmlElement("contact")] public contact[] contacts { get { contact[] contacts = new contact[AllContacts.Count]; AllContacts.CopyTo(contacts); return contacts; } } public void addContact(contact c) { AllContacts.Add(c); // Ajoute un contact à la liste } } [Serializable] public class contact { [XmlAttribute("nom")] public string nom; [XmlAttribute("prenom")] public string prenom; [XmlAttribute("email")] public string email; public contact() { } public contact(string nom, string prenom, string email) { // init des parametres this.nom = nom; this.prenom = prenom; this.email = email; } } }
Partager