Exemplo n.º 1
0
        public bool DeSerialize(List <Staff> StaffList)
        {
            bool flag = false;

            Type[] staffTypes = { typeof(TeachingStaff), typeof(AdministrativeStaff), typeof(SupportStaff) };

            XmlSerializer deserializer = new XmlSerializer(typeof(List <Staff>), staffTypes);

            try
            {
                using (StreamReader reader = new StreamReader(@"C:\Users\Win8.1 Pro 64bit\source\repos\Staff\Staff\XMLFile1.xml"))
                {
                    object outObject = deserializer.Deserialize(reader);

                    foreach (var staff in (dynamic)outObject)
                    {
                        string staffType = staff.StaffType.ToString();
                        switch (staffType)
                        {
                        case "Teaching":
                            TeachingStaff teachingStaff = new TeachingStaff(staff.Name, staff.EmpCode, staff.StaffType, staff.Subject, staff.ContactNumber, staff.DateOfJoin);
                            StaffList.Add(teachingStaff);
                            break;

                        case "Administrative":
                            AdministrativeStaff administrativeStaff = new AdministrativeStaff(staff.Name, staff.EmpCode, staff.StaffType, staff.Role, staff.ContactNumber, staff.DateOfJoin);
                            StaffList.Add(administrativeStaff);
                            break;

                        case "Support":
                            SupportStaff supportStaff = new SupportStaff(staff.Name, staff.EmpCode, staff.StaffType, staff.Department, staff.ContactNumber, staff.DateOfJoin);
                            StaffList.Add(supportStaff);
                            break;
                        }
                    }
                    return(flag);
                }
            }
            catch (Exception e)
            {
                return(flag);
            }
        }
Exemplo n.º 2
0
        public bool DeSerialize(List <Staff> StaffList)
        {
            bool flag = false;

            try
            {
                string jsonString = File.ReadAllText(@"C:\Users\Win8.1 Pro 64bit\source\repos\Staff\Staff\Staff.json");
                object obj        = JsonConvert.DeserializeObject(jsonString);
                foreach (var staff in (dynamic)obj)
                {
                    string staffType = staff.StaffType.ToString();
                    switch (staffType)
                    {
                    case "Teaching":
                        TeachingStaff ts            = (TeachingStaff)staff.ToObject(typeof(TeachingStaff));
                        TeachingStaff teachingStaff = new TeachingStaff(ts.Name, ts.EmpCode, ts.StaffType, ts.Subject, ts.ContactNumber, ts.DateOfJoin);
                        StaffList.Add(teachingStaff);
                        break;

                    case "Administrative":
                        AdministrativeStaff adminStaff          = (AdministrativeStaff)staff.ToObject(typeof(AdministrativeStaff));
                        AdministrativeStaff administrativeStaff = new AdministrativeStaff(adminStaff.Name, adminStaff.EmpCode, adminStaff.StaffType, adminStaff.Role, adminStaff.ContactNumber, adminStaff.DateOfJoin);
                        StaffList.Add(administrativeStaff);
                        break;

                    case "Support":
                        SupportStaff ss           = (SupportStaff)staff.ToObject(typeof(SupportStaff));
                        SupportStaff supportStaff = new SupportStaff(ss.Name, ss.EmpCode, ss.StaffType, ss.Department, ss.ContactNumber, ss.DateOfJoin);
                        StaffList.Add(supportStaff);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                return(flag);
            }


            return(flag);
        }