60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using System.Xml.Serialization;
|
|
using Aufgabe7;
|
|
|
|
Console.WriteLine("Willkommen zu Aufgabe7");
|
|
|
|
Employee Mitarbeiter1 = new Employee("Petra", "Maier", Gender.Female);
|
|
Employee Mitarbeiter2 = new Employee("Herbert", "Müller", Gender.Male);
|
|
|
|
// Ich hab die Aufgabe zuerst wie beschrieben bearbeitet,
|
|
// mich dann gefragt was passiert wenn man mehre Personen anlegen will.
|
|
// Nachdem dies mit der gewollten Lösung nicht ging (zumindest meiner),
|
|
// hab etwas nach eigenem Geschmack experimentiert.
|
|
|
|
List<Employee> employees = new List<Employee>()
|
|
{
|
|
Mitarbeiter1, Mitarbeiter2
|
|
};
|
|
|
|
Employee.SerializeAll(employees);
|
|
foreach (Employee employee in Employee.DeserializeAll())
|
|
{
|
|
Console.WriteLine($"Vorname: {employee.FirstName}, " +
|
|
$"Nachname: {employee.LastName}, " +
|
|
$"Gender: {employee.Gender}");
|
|
}
|
|
|
|
[Serializable]
|
|
public class Employee
|
|
{
|
|
public string FirstName { get; set; }
|
|
public string LastName { get; set; }
|
|
public Gender Gender { get; set; }
|
|
|
|
public Employee(){}
|
|
|
|
public Employee(string firstName, string lastName, Gender gender)
|
|
{
|
|
this.FirstName = firstName;
|
|
this.LastName = lastName;
|
|
this.Gender = gender;
|
|
}
|
|
|
|
public static void SerializeAll(List<Employee> employees)
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(List<Employee>));
|
|
using (var stream = new FileStream(@"..\Personen.xml", FileMode.Create))
|
|
{
|
|
serializer.Serialize(stream, employees);
|
|
}
|
|
}
|
|
|
|
public static List<Employee> DeserializeAll()
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(List<Employee>));
|
|
using (var stream = new FileStream(@"..\Personen.xml", FileMode.Open))
|
|
{
|
|
return (List<Employee>)serializer.Deserialize(stream);
|
|
}
|
|
}
|
|
} |