3 using System.Collections;
4 using System.Xml.Serialization;
8 public static void Main()
11 t.Create("icollection.xml");
12 t.Read("icollection.xml");
15 private void Create(string filename)
17 Employees emps=new Employees();
19 /* Note that only the collection is serialized, not
20 * the CollectionName or any other public property of
23 emps.CollectionName="Employees";
24 Employee john100=new Employee("John", "100xxx");
27 XmlSerializer ser=new XmlSerializer(typeof(Employees));
28 TextWriter writer=new StreamWriter(filename);
29 ser.Serialize(writer, emps);
33 private void Read(string filename)
35 XmlSerializer ser=new XmlSerializer(typeof(Employees));
36 FileStream fs=new FileStream(filename, FileMode.Open);
39 emps=(Employees)ser.Deserialize(fs);
43 Console.WriteLine("Collection name: "+emps.CollectionName);
44 foreach(Employee emp in emps)
46 Console.WriteLine("Employee name: "+emp.EmpName);
47 Console.WriteLine("Employee ID: "+emp.EmpID);
52 public class Employees:ICollection
54 public string CollectionName;
55 private ArrayList empArray=new ArrayList();
57 public Employee this[int index]
60 return((Employee)empArray[index]);
64 public void CopyTo(Array a, int index)
66 empArray.CopyTo(a, index);
72 return(empArray.Count);
76 public object SyncRoot
83 public bool IsSynchronized
90 public IEnumerator GetEnumerator()
92 return(empArray.GetEnumerator());
95 public void Add(Employee newEmployee)
97 empArray.Add(newEmployee);
101 public class Employee
103 public string EmpName;
109 public Employee(string empName, string empID)