diff --git a/Lesson08/JsonFiles.json b/Lesson08/JsonFiles.json new file mode 100644 index 0000000..2c0d9e5 --- /dev/null +++ b/Lesson08/JsonFiles.json @@ -0,0 +1 @@ +{"DepartmentName":"Receptions","Employees":[{"EmployeeName":"Zikirillo"},{"EmployeeName":"Ronaldo"},{"EmployeeName":"Messi"},{"EmployeeName":"Anvar"}]} diff --git a/Lesson08/Program.cs b/Lesson08/Program.cs index dce4fc9..b74321e 100644 --- a/Lesson08/Program.cs +++ b/Lesson08/Program.cs @@ -1,10 +1,118 @@ -namespace Lesson08 +using System.Text.Json; +using System.Xml.Serialization; + +namespace Lesson08 { internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + var department = new Department() + { + DepartmentName = "Receptions", + Employees = new List + { + new Employee + { + EmployeeName = "Zikirillo" + }, + new Employee + { + EmployeeName = "Ronaldo" + }, + new Employee + { + EmployeeName = "Messi" + }, + + new Employee + { + EmployeeName = "Anvar" + } + } + }; + + WriteToTextJson(department); + + XML_DE_Serializations(department); } + + static void WriteToTextJson(Department department) + { + string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\JsonFiles.json"; + + if (!File.Exists(path)) + { + File.Create(path).Close(); + + using (StreamWriter sw = new StreamWriter(path)) + { + string json = JsonSerializer.Serialize(department); + + sw.WriteLine(json); + + Console.WriteLine("Informations added successfully!"); + } + } + + else + { + ReadToTextJson(path, department); + } + } + + static void ReadToTextJson(string path, Department department) + { + + using (StreamReader sr = new StreamReader(path)) + { + string alltext = sr.ReadToEnd(); + + var json = JsonSerializer.Deserialize(alltext); + + department.DepartmentName = json.DepartmentName; + department.Employees = json.Employees; + + Console.WriteLine($"The informations in ({path}) is reloaded into the department object."); + } + } + + static void XML_DE_Serializations(Department department) + { + XmlSerializer xmlserializer = new XmlSerializer(typeof(Department)); + + string pathxml = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\XMLFiles.xml"; + + + using (FileStream fs = new(pathxml, FileMode.OpenOrCreate)) + { + xmlserializer.Serialize(fs, department); + + Console.WriteLine("Object has been serialized"); + } + + using (FileStream fs = new(pathxml, FileMode.OpenOrCreate)) + { + + var xml = xmlserializer.Deserialize(fs) as Department; + + department.DepartmentName = xml.DepartmentName; + department.Employees = xml.Employees; + + Console.WriteLine("succes xml"); + } + } + } + + public class Employee + { + public string EmployeeName { get; set; } + + } + + public class Department + { + public string DepartmentName { get; set; } + public List Employees { get; set; } } } \ No newline at end of file diff --git a/Lesson08/XMLFiles.xml b/Lesson08/XMLFiles.xml new file mode 100644 index 0000000..b1d2a16 --- /dev/null +++ b/Lesson08/XMLFiles.xml @@ -0,0 +1,18 @@ + + + Receptions + + + Zikirillo + + + Ronaldo + + + Messi + + + Anvar + + + \ No newline at end of file