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