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
|
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
private ArrayList arrList = new ArrayList();
//....(ArrayList füllen .....)
/*****************
* XmlSerializer *
*****************/
private void serializeXml() {
using (FileStream fs = new FileStream("Personen.xml", FileMode.Create)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
xmlSer.Serialize(fs, arrList);
}
}
private void deserializeXml() {
try {
using (FileStream fs = new FileStream("Personen.xml", FileMode.Open)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
arrList = (ArrayList)xmlSer.Deserialize(fs);
}
} catch (IOException ex) {
MessageBox.Show(ex.Message);
}
lstAusgabe.Items.Clear();
foreach (Person p in arrList)
lstAusgabe.Items.Add(p);
}
/******************
* Soap Formatter *
******************/
private void serializeSoap() {
using (FileStream fs = new FileStream(@"./soap.dat", FileMode.Create)) {
SoapFormatter soaFormatter = new SoapFormatter();
soaFormatter.Serialize(fs, arrList);
}
}
private void deserializeSoap() {
try {
using (FileStream fs = new FileStream(@"./soap.dat", FileMode.Open)) {
SoapFormatter soaFormatter = new SoapFormatter();
arrList = (ArrayList)soaFormatter.Deserialize(fs);
}
} catch (IOException ex) {
MessageBox.Show(ex.Message);
}
lstAusgabe.Items.Clear();
foreach (Person p in arrList)
lstAusgabe.Items.Add(p);
}
/********************
* Binary Formatter *
********************/
private void serializeBinary() {
using (FileStream fs = new FileStream(@"./binary.dat", FileMode.Create)) {
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(fs, arrList);
}
}
private void deserializeBinary() {
try {
using (FileStream fs = new FileStream(@"./binary.dat", FileMode.Open)) {
BinaryFormatter binFormatter = new BinaryFormatter();
arrList = (ArrayList)binFormatter.Deserialize(fs);
}
} catch (IOException ex) {
MessageBox.Show(ex.Message);
}
lstAusgabe.Items.Clear();
foreach (Person p in arrList)
lstAusgabe.Items.Add(p);
}
//Die zu serialisierende Klasse
//(Die XML-Attribute werden nur für die Xml-serialisierung gebraucht)
[Serializable()]
public class Person {
[XmlElement("Vorname",DataType="string")]
private string vorname;
[XmlElement("Nachname", DataType = "string")]
private string name;
[XmlElement("Alter", DataType = "int")]
private int alter;
[XmlAttribute("Land", DataType = "string")]
private string land;
public Person() { }
public string Vorname {
get { return vorname; }
set { vorname = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public int Alter {
get { return alter; }
set { alter = value; }
}
public string Land {
get { return land; }
set { land = value; }
}
public override String ToString() {
return vorname + "\t" + name + "\t" + alter + "\t" + land;
}
}
|