/// <summary>
        /// Get university.
        /// </summary>
        /// <returns> University </returns>
        private static University GetUniversity()
        {
            var hse = new University
            {
                UniversityName = "NRU HSE"
            };

            var deptStaff = new List <Human>
            {
                new Professor("Ivanov"),
                new Professor("Petrov")
            };

            var se = new Dept("SE", deptStaff);

            hse.Departments = new List <Dept>
            {
                se
            };

            return(hse);
        }
 /// <summary>
 /// Json serialization.
 /// </summary>
 /// <param name="path"> Path to file </param>
 /// <param name="university"> University </param>
 private static void JsonSerialization(string path, University university)
 {
     try
     {
         SerializeUniversity(path, university);
     }
     catch (SerializationException ex)
     {
         PrintMessage($"Serialization error: {ex.Message}", ConsoleColor.Red);
     }
     catch (IOException ex)
     {
         PrintMessage($"Some problem with file: {ex.Message}", ConsoleColor.Red);
     }
     catch (SecurityException ex)
     {
         PrintMessage($"Access error: {ex.Message}", ConsoleColor.Red);
     }
     catch (Exception ex)
     {
         PrintMessage($"Unexpected error: {ex.Message}", ConsoleColor.Red);
     }
 }
Beispiel #3
0
        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 };



            //XmlSerializer binformatter = new XmlSerializer(typeof(University[]), new Type[] { typeof(Dept),
            //    typeof(Professor), typeof(Human) });



            // Сериализация
            using (StreamWriter file = new StreamWriter(File.Open("BinSer.json", FileMode.Create, FileAccess.Write, FileShare.None)))
            {
                file.Write(JsonSerializer.Serialize(universities));
            }



            // Десериализация
            University[] deserial;
            using (StreamReader file = new StreamReader(File.OpenRead("BinSer.json")))
            {
                deserial = JsonSerializer.Deserialize <University[]>(file.ReadToEnd());
                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();
        }