Bonjour !
Alors euh, je me suis lancée dans la désérialisation du fichier XML suivant:
Donc pour commencer, j'ai fait simple, j'ai modifié mon fichier XML en un truc super simple :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12<?xml version="1.0"?> <Lapins> <Lapin> <Nom>LAP1</Nom> <Prenom>lapinou</Prenom> </Lapin> <Lapin> <Nom>LAP2</Nom> <Prenom>lapinou2</Prenom> </Lapin> </Lapins>
et j'ai utilisé le code suivant qui fonctionne parfaitement:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5<?xml version="1.0"?> <Lapin> <Nom>LAP1</Nom> <Lapin>
Seulement voilà, le-dit code ne fonctionne pas quand je change le fichier... (ce qui est logique, je le concoit tout a fait !!)
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 using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; using System.Configuration; using System.IO; using System.Xml; namespace Lapin.Service { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text = DeserializeObject(@"C:\Lapin\fichier.xml"); } public string DeserializeObject(string filename) { XmlSerializer xs = new XmlSerializer(typeof(Lapin)); using (StreamReader rd = new StreamReader(filename)) { Lapin L = new Lapin(); L = xs.Deserialize(rd) as Lapin; return L.Nom; } } } public partial class Lapin { public string Nom; } }
Donc j'ai tenté d'utiliser une List<Lapin> mais euh, sans grand succès...
Voilà ce que ça a donné :
Donc si qqn pouvait m'éclairer, ça ne serait pas de refus =)
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 using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; using System.Configuration; using System.IO; using System.Xml; namespace Lapin.Service { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Text = DeserializeObject(@"C:\Lapin\fichier.xml"); } public string DeserializeObject(string filename) { XmlSerializer xs = new XmlSerializer(typeof(List<Lapin>)); using (StreamReader rd = new StreamReader(filename)) { List<Lapin> L ; L = xs.Deserialize(rd) as List<Lapin>; return ??????;// aussi une question, comment accéder aux valeurs de la liste du coup...? } } } public partial class Lapin { public string Nom; } }
(désolée pour la longueur de ce post...)
Par avance, je vous remercie !
Partager