Skip to content

Commit 71810cc

Browse files
authored
CM-164:XML Serialization in C# (#185)
1 parent 9881c5e commit 71810cc

14 files changed

+616
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XMLSerializationInCsharp", "XMLSerializationInCsharp\XMLSerializationInCsharp.csproj", "{D247A4D6-B023-4161-B617-1CE98F9B5BD3}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XMLSerializationInCsharpTests", "XMLSerializationInCsharpTests\XMLSerializationInCsharpTests.csproj", "{E6641E28-3C87-4998-A5CE-1E96A67744DD}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{D247A4D6-B023-4161-B617-1CE98F9B5BD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{D247A4D6-B023-4161-B617-1CE98F9B5BD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{D247A4D6-B023-4161-B617-1CE98F9B5BD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{D247A4D6-B023-4161-B617-1CE98F9B5BD3}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E6641E28-3C87-4998-A5CE-1E96A67744DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E6641E28-3C87-4998-A5CE-1E96A67744DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E6641E28-3C87-4998-A5CE-1E96A67744DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E6641E28-3C87-4998-A5CE-1E96A67744DD}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {8F1395AE-90A0-4A75-9835-65170177E627}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace XMLSerializationInCsharp
4+
{
5+
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Console.Write("hello world");
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace XMLSerializationInCsharp_v1
4+
{
5+
public class Patient
6+
{
7+
public int ID { get; set; }
8+
public string FirstName { get; set; }
9+
public string LastName { get; set; }
10+
public DateTime Birthday { get; set; }
11+
public int RoomNo { get; set; }
12+
}
13+
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace XMLSerializationInCsharp_v2
4+
{
5+
public class Patient
6+
{
7+
public int ID { get; set; }
8+
public string FirstName { get; set; }
9+
public string LastName { get; set; }
10+
public DateTime Birthday { get; set; }
11+
public int RoomNo { get; set; }
12+
public Address HomeAddress { get; set; }
13+
}
14+
15+
public class Address
16+
{
17+
public string Street { get; set; }
18+
public string Zip { get; set; }
19+
public string City { get; set; }
20+
public string Country { get; set; }
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace XMLSerializationInCsharp_v3
5+
{
6+
public class Patient
7+
{
8+
public int ID { get; set; }
9+
public string FirstName { get; set; }
10+
public string LastName { get; set; }
11+
public DateTime Birthday { get; set; }
12+
public int RoomNo { get; set; }
13+
public Address HomeAddress { get; set; }
14+
public List<Measurement> Measurements {get; set;}
15+
}
16+
17+
public class Address
18+
{
19+
public string Street { get; set; }
20+
public string Zip { get; set; }
21+
public string City { get; set; }
22+
public string Country { get; set; }
23+
}
24+
25+
public class Measurement
26+
{
27+
public DateTime TimeTaken { get; set; }
28+
public decimal Temp { get; set; }
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Xml.Serialization;
4+
5+
namespace XMLSerializationInCsharp_v4
6+
{
7+
[XmlRoot("PatientInfo")]
8+
public class Patient
9+
{
10+
[XmlAttribute ("PatientID")]
11+
public int ID { get; set; }
12+
public string FirstName { get; set; }
13+
public string LastName { get; set; }
14+
[XmlIgnore]
15+
public DateTime Birthday { get; set; }
16+
[XmlElement("RoomNumber")]
17+
public int RoomNo { get; set; }
18+
public Address HomeAddress { get; set; }
19+
[XmlArray("TemperatureReadings")]
20+
[XmlArrayItem("TemperatureReading")]
21+
public List<Measurement> Measurements {get; set;}
22+
}
23+
24+
public class Address
25+
{
26+
public string Street { get; set; }
27+
public string Zip { get; set; }
28+
public string City { get; set; }
29+
[XmlText]
30+
public string Country { get; set; }
31+
}
32+
33+
public class Measurement
34+
{
35+
public DateTime TimeTaken { get; set; }
36+
public decimal Temp { get; set; }
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Xml.Serialization;
3+
4+
namespace XMLSerializationInCsharp_v5
5+
{
6+
[XmlInclude(typeof(Doctor))]
7+
[XmlInclude(typeof(Patient))]
8+
public class Person
9+
{
10+
public int ID { get; set; }
11+
public string FirstName { get; set; }
12+
public string LastName { get; set; }
13+
public DateTime Birthday { get; set; }
14+
}
15+
16+
public class Doctor: Person
17+
{
18+
public string Specialization { get; set; }
19+
}
20+
21+
public class Patient: Person
22+
{
23+
public int RoomNo { get; set; }
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
11+
<PackageReference Include="NUnit" Version="3.13.1" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
13+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\XMLSerializationInCsharp\XMLSerializationInCsharp.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.IO;
4+
using System.Text;
5+
using System.Xml.Serialization;
6+
using XMLSerializationInCsharp_v1;
7+
8+
namespace XMLSerializationInCsharpTests
9+
{
10+
public class XMLSerializationUnitTest_v1
11+
{
12+
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
13+
<Patient xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
14+
<ID>232323</ID>
15+
<FirstName>John</FirstName>
16+
<LastName>Doe</LastName>
17+
<Birthday>1990-12-30T00:00:00</Birthday>
18+
<RoomNo>310</RoomNo>
19+
</Patient>";
20+
21+
[SetUp]
22+
public void Setup()
23+
{
24+
25+
}
26+
27+
[Test]
28+
public void WhenSerializingASimpleClass_ThenCorrectXML()
29+
{
30+
var patient = new Patient()
31+
{
32+
ID = 232323,
33+
FirstName = "John",
34+
LastName = "Doe",
35+
Birthday = new DateTime(1990, 12, 30),
36+
RoomNo = 310
37+
};
38+
39+
using (var stream = new MemoryStream())
40+
using (var writer = new StreamWriter(stream, Encoding.UTF8))
41+
{
42+
var serializer = new XmlSerializer(typeof(Patient));
43+
serializer.Serialize(writer, patient);
44+
Assert.AreEqual(xml, Encoding.UTF8.GetString(stream.ToArray()));
45+
}
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.IO;
4+
using System.Text;
5+
using System.Xml.Serialization;
6+
using XMLSerializationInCsharp_v2;
7+
8+
namespace XMLSerializationInCsharpTests
9+
{
10+
public class XMLSerializationUnitTest_v2
11+
{
12+
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
13+
<Patient xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
14+
<ID>232323</ID>
15+
<FirstName>John</FirstName>
16+
<LastName>Doe</LastName>
17+
<Birthday>1990-12-30T00:00:00</Birthday>
18+
<RoomNo>310</RoomNo>
19+
<HomeAddress>
20+
<Street>12 Main str.</Street>
21+
<Zip>23322</Zip>
22+
<City>Boston</City>
23+
<Country>USA</Country>
24+
</HomeAddress>
25+
</Patient>";
26+
27+
string xmlArr = @"<?xml version=""1.0"" encoding=""utf-8""?>
28+
<ArrayOfPatient xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
29+
<Patient>
30+
<ID>232323</ID>
31+
<FirstName>John</FirstName>
32+
<LastName>Doe</LastName>
33+
<Birthday>1990-12-30T00:00:00</Birthday>
34+
<RoomNo>310</RoomNo>
35+
<HomeAddress>
36+
<Street>12 Main str.</Street>
37+
<Zip>23322</Zip>
38+
<City>Boston</City>
39+
<Country>USA</Country>
40+
</HomeAddress>
41+
</Patient>
42+
<Patient>
43+
<ID>454545</ID>
44+
<FirstName>Jane</FirstName>
45+
<LastName>Doe</LastName>
46+
<Birthday>1985-01-02T00:00:00</Birthday>
47+
<RoomNo>232</RoomNo>
48+
<HomeAddress>
49+
<Street>8 Market str.</Street>
50+
<Zip>XS45E</Zip>
51+
<City>London</City>
52+
<Country>UK</Country>
53+
</HomeAddress>
54+
</Patient>
55+
</ArrayOfPatient>";
56+
57+
[SetUp]
58+
public void Setup()
59+
{
60+
61+
}
62+
63+
[Test]
64+
public void WhenSerializingAClassWithComplexObject_ThenCorrectXML()
65+
{
66+
var patient = new Patient()
67+
{
68+
ID = 232323,
69+
FirstName = "John",
70+
LastName = "Doe",
71+
Birthday = new DateTime(1990, 12, 30),
72+
RoomNo = 310,
73+
HomeAddress = new Address()
74+
{
75+
Street = "12 Main str.",
76+
Zip = "23322",
77+
City = "Boston",
78+
Country = "USA",
79+
}
80+
};
81+
82+
using (MemoryStream stream = new MemoryStream())
83+
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
84+
{
85+
XmlSerializer serializer = new XmlSerializer(typeof(Patient));
86+
serializer.Serialize(writer, patient);
87+
Assert.AreEqual(xml, Encoding.UTF8.GetString(stream.ToArray()));
88+
}
89+
}
90+
91+
[Test]
92+
public void WhenSerializingAnArrayOfObjects_ThenCorrectXML()
93+
{
94+
var arr = new Patient[]
95+
{
96+
new Patient()
97+
{
98+
ID = 232323,
99+
FirstName = "John",
100+
LastName = "Doe",
101+
Birthday = new DateTime(1990, 12, 30),
102+
RoomNo = 310,
103+
HomeAddress = new Address()
104+
{
105+
Street = "12 Main str.",
106+
Zip = "23322",
107+
City = "Boston",
108+
Country = "USA",
109+
}
110+
},
111+
new Patient()
112+
{
113+
ID = 454545,
114+
FirstName = "Jane",
115+
LastName = "Doe",
116+
Birthday = new DateTime(1985, 1, 2),
117+
RoomNo = 232,
118+
HomeAddress = new Address()
119+
{
120+
Street = "8 Market str.",
121+
Zip = "XS45E",
122+
City = "London",
123+
Country = "UK",
124+
}
125+
}
126+
};
127+
128+
using (var stream = new MemoryStream())
129+
using (var writer = new StreamWriter(stream, Encoding.UTF8))
130+
{
131+
var serializer = new XmlSerializer(typeof(Patient[]));
132+
serializer.Serialize(writer, arr);
133+
Assert.AreEqual(xmlArr, Encoding.UTF8.GetString(stream.ToArray()));
134+
}
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)