static void Main(string[] args) { University HeSeEe = new University(); HeSeEe.UniversityName = "ХСЕ"; Human[] professors = { new Professor("Best"), new Professor("BestBest") }; Dept dept = new Dept("ПИ", professors); HeSeEe.Departments = new List <Dept>(); HeSeEe.Departments.Add(dept); string hse = JsonSerializer.Serialize <University>(HeSeEe); University Hse; Hse = JsonSerializer.Deserialize <University>(hse); Console.WriteLine(Hse.UniversityName); foreach (Dept d in Hse.Departments) { Console.WriteLine(d.DeptName); } }
static void Main(string[] args) { University HSE = new University(); HSE.UniversityName = "NRU HSE"; Human[] deptStaff = { new Professor("Ivanov"), new Professor("Petrov") }; Dept SE = new Dept("SE", deptStaff); HSE.Departments = new List <Dept>(); HSE.Departments.Add(SE); University MSU = new University(); MSU.UniversityName = "MSU"; Human[] deptStaffM = { new Professor("Ivanov"), new Professor("Chizov"), new Professor("Petrov") }; Dept SEM = new Dept("SEM", deptStaffM); MSU.Departments = new List <Dept>(); MSU.Departments.Add(SEM); University[] universities = new University[] { HSE, MSU }; DataContractSerializer serializer = new DataContractSerializer(typeof(University[]), new Type[] { typeof(Human), typeof(Professor), typeof(Dept) }); using (Stream file = new FileStream("ser.xml", FileMode.Create, FileAccess.Write, FileShare.None)) { serializer.WriteObject(file, universities); } // Десериализация University[] deserial; using (Stream file = File.OpenRead("ser.xml")) { deserial = (University[])serializer.ReadObject(file); Console.WriteLine("XML - " + deserial[0].UniversityName); Console.WriteLine("XML - " + deserial[1].UniversityName); } foreach (Dept d in deserial[0].Departments) { foreach (Human h in d.Staff) { if (h is Professor) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } } } foreach (Dept d in deserial[1].Departments) { foreach (Human h in d.Staff) { if (h is Professor) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } } } Console.ReadKey(); }