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 }; string json = JsonSerializer.Serialize <University[]>(universities); Console.WriteLine(json); University[] deserial; deserial = JsonSerializer.Deserialize <University[]>(json); foreach (Dept d in deserial[0].Departments) { foreach (Human h in d.Staff) { if (h is Professor) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } if (h is Human) { 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); } if (h is Human) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } } } Console.ReadKey(); }
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("Dataser.xml", FileMode.Create, FileAccess.Write, FileShare.None)) { serializer.WriteObject(file, universities); } // Десериализация University[] deserial; using (Stream file = File.OpenRead("Dataser.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(); }
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 }; University[] deserial; var serializer = new DataContractSerializer(typeof(University[]), new Type[] { typeof(Dept), typeof(Professor), typeof(Human) }); using (FileStream fs = new FileStream("universities.xml", FileMode.OpenOrCreate)) { serializer.WriteObject(fs, universities); } using (FileStream fs = new FileStream("universities.xml", FileMode.OpenOrCreate)) { deserial = (University[])serializer.ReadObject(fs); } foreach (Dept d in deserial[0].Departments) { foreach (Human h in d.Staff) { if (h is Professor) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } if (h is Human) { 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); } if (h is Human) { Console.WriteLine(d.DeptName + " prof.: " + h.Name); } } } Console.ReadKey(); }
static async 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 }; // Сериализация using (Stream file = new FileStream("JsonSer.json", FileMode.Create, FileAccess.Write, FileShare.None)) { await JsonSerializer.SerializeAsync(file, universities); } // Десериализация University[] deserial; using (Stream file = File.OpenRead("JsonSer.json")) { deserial = await JsonSerializer.DeserializeAsync <University[]>(file); Array.ForEach(deserial, Console.WriteLine); } 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(); }