* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / standalone_tests / icollection.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using System.Xml.Serialization;
5
6 public class Test
7 {
8         public static void Main()
9         {
10                 Test t=new Test();
11                 t.Create("icollection.xml");
12                 t.Read("icollection.xml");
13         }
14
15         private void Create(string filename)
16         {
17                 Employees emps=new Employees();
18
19                 /* Note that only the collection is serialized, not
20                  * the CollectionName or any other public property of
21                  * the class.
22                  */
23                 emps.CollectionName="Employees";
24                 Employee john100=new Employee("John", "100xxx");
25                 emps.Add(john100);
26
27                 XmlSerializer ser=new XmlSerializer(typeof(Employees));
28                 TextWriter writer=new StreamWriter(filename);
29                 ser.Serialize(writer, emps);
30                 writer.Close();
31         }
32
33         private void Read(string filename)
34         {
35                 XmlSerializer ser=new XmlSerializer(typeof(Employees));
36                 FileStream fs=new FileStream(filename, FileMode.Open);
37                 Employees emps;
38
39                 emps=(Employees)ser.Deserialize(fs);
40                 fs.Close();
41
42                 /* Not serialized! */
43                 Console.WriteLine("Collection name: "+emps.CollectionName);
44                 foreach(Employee emp in emps) 
45                 {
46                         Console.WriteLine("Employee name: "+emp.EmpName);
47                         Console.WriteLine("Employee ID: "+emp.EmpID);
48                 }
49         }
50 }
51
52 public class Employees:ICollection
53 {
54         public string CollectionName;
55         private ArrayList empArray=new ArrayList();
56
57         public Employee this[int index]
58         {
59                 get {
60                         return((Employee)empArray[index]);
61                 }
62         }
63
64         public void CopyTo(Array a, int index)
65         {
66                 empArray.CopyTo(a, index);
67         }
68
69         public int Count
70         {
71                 get {
72                         return(empArray.Count);
73                 }
74         }
75
76         public object SyncRoot
77         {
78                 get {
79                         return(this);
80                 }
81         }
82
83         public bool IsSynchronized
84         {
85                 get {
86                         return(false);
87                 }
88         }
89
90         public IEnumerator GetEnumerator()
91         {
92                 return(empArray.GetEnumerator());
93         }
94
95         public void Add(Employee newEmployee) 
96         {
97                 empArray.Add(newEmployee);
98         }
99 }
100
101 public class Employee
102 {
103         public string EmpName;
104         public string EmpID;
105
106         public Employee()
107         {}
108
109         public Employee(string empName, string empID)
110         {
111                 EmpName=empName;
112                 EmpID=empID;
113         }
114 }