-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (60 loc) · 2.63 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (60 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace PracticalTest
{
class Program
{
private static Organization DeserializeXmlToOrganization(string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Organization));
FileStream fs = new FileStream(filename, FileMode.Open);
using (var reader = XmlReader.Create(fs))
{
var organization = (Organization)serializer.Deserialize(reader);
return organization;
}
}
private static void SerializeOrganizationToJson(Organization o, string dest)
{
OrganizationWrapper wrapper = new OrganizationWrapper(o);
var json = JsonConvert.SerializeObject(wrapper, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(dest + "/organization.json", json);
}
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Please pass the location of organization.xml and the JSON output path.");
return;
}
var sourcePath = args[0]; //"../../../resources/organization.xml"
var destPath = args[1];
// Deserialize the organization file into an object
Organization o = DeserializeXmlToOrganization(sourcePath);
// Print the current Employee details of the organization
o.PrintAllEmployeeDetails();
// Swap employees of the Platform and Maintenance teams
// Alternatively, this could have been done by swapping the unit name instead of the employees.
// If there are additional attributes/details per Unit then the below would not work correctly,
// so this method won't be used:
/*
o.GetFirstUnit("Platform Team", true).Name = "tempName";
o.GetFirstUnit("Maintenance Team", true).Name = "Platform Team";
o.GetFirstUnit("tempName", true).Name = "Maintenance Team";
*/
// Instead we will swap the employees:
Unit platform = o.GetFirstUnit("Platform Team", true);
Unit maintenance = o.GetFirstUnit("Maintenance Team", true);
HashSet<Employee> temp = platform.Employees;
platform.Employees = maintenance.Employees;
maintenance.Employees = temp;
// Output the new structure into a JSON file
SerializeOrganizationToJson(o, destPath);
}
}
}