Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lesson08/JsonFiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"DepartmentName":"Receptions","Employees":[{"EmployeeName":"Zikirillo"},{"EmployeeName":"Ronaldo"},{"EmployeeName":"Messi"},{"EmployeeName":"Anvar"}]}
112 changes: 110 additions & 2 deletions Lesson08/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Employee>
{
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<Department>(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<Employee> Employees { get; set; }
}
}
18 changes: 18 additions & 0 deletions Lesson08/XMLFiles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Department xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DepartmentName>Receptions</DepartmentName>
<Employees>
<Employee>
<EmployeeName>Zikirillo</EmployeeName>
</Employee>
<Employee>
<EmployeeName>Ronaldo</EmployeeName>
</Employee>
<Employee>
<EmployeeName>Messi</EmployeeName>
</Employee>
<Employee>
<EmployeeName>Anvar</EmployeeName>
</Employee>
</Employees>
</Department>