Serialization fichier XML
Bonjour à tous,
je débute en VB. Je teste la serialization. Je souhaite sauvegarder les données de mon programme(mode console) dans un fichier XML.
Après quelques recherches sur le net je suis arrivé a créer un fihcier XML avec mes données sauf que il me remet "l'en-tête XML" à chaque fois comme ci dessous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Nom>Dupond</Nom>
<Prenom>Alain</Prenom>
</Personne><?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Nom>Durand</Nom>
<Prenom>Maurice</Prenom>
</Personne><?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Nom>Leblanc</Nom>
<Prenom>Sylvie</Prenom>
</Personne> |
Je ne trouve pas comment arriver à ce que la ligne <Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ne soit pas enregistrée à chaque enregistrement.
Voici mon code :
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
|
Imports System.IO
Imports System.IO.File
Imports System.Xml.XPath
Imports System.Xml
Imports System.String
Imports System.Xml.Serialization
Module Main
Dim choix As String
Dim Nom As String
Dim Prenom As String
Dim FileName As String = "MyXmlFile.xml"
Dim ListePersonne As ArrayList = New ArrayList()
Dim UnePersonne As New Personne()
Sub Main()
Do
Console.WriteLine("1 - Créer une nouvelle personne ")
Console.WriteLine("2 - Afficher les personnes")
Console.WriteLine("0 - Quitter ")
choix = Console.ReadLine()
If (choix = "exit") Then
Exit Do
End If
Select Case choix
Case 0
Exit Do
Case 1
Console.Write("Nom : > ")
Nom = Console.ReadLine()
Console.Write("Prénom : > ")
Prenom = Console.ReadLine()
UnePersonne = New Personne(Nom, Prenom)
ListePersonne.Add(UnePersonne)
Case 2
For Each Objet In ListePersonne
Console.WriteLine(Objet.ToString())
Next
Case Else
Exit Select
End Select
Loop
Dim ObjStreamWritter As New StreamWriter(FileName)
Dim x As XmlSerializer
Dim e As XmlElement
For Each UnePersonne In ListePersonne
x = New XmlSerializer(UnePersonne.GetType)
x.Serialize(ObjStreamWritter, UnePersonne)
Next
ObjStreamWritter.Close()
MsgBox("REUSSI")
End Sub
End Module |
Ma classe est Personne avec 2 attributs : Nom et Prenom.
Quelqu'un aurait-il une idée?
Merci