Skip to content

Commit 20c2083

Browse files
Read XML: net10.0, MSTest 4.5.2, read people.xml from file (#2334)
Retarget ReadXML and Tests to net10.0 and move the test packages to Microsoft.NET.Test.Sdk 18.11.0, MSTest 4.5.2 and coverlet.collector 10.1.0 (Assert.ThrowsException becomes Assert.ThrowsExactly). Add people.xml with a copy-to-output item, ReadXmlFromFile (XDocument.Load and XmlReader.Create) and ReadXmlUsingXmlDocument, with four tests. Rename ReadNamesAndAges to ReadNamesAndSurnames and drop the stray block after the using declaration. Program.cs gains the file-reading demos.
1 parent 16be384 commit 20c2083

9 files changed

Lines changed: 193 additions & 34 deletions

File tree

‎xml-csharp/ReadXML/ReadXML/Program.cs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,33 @@
5151
Console.ReadKey();
5252

5353
Console.Clear();
54-
Console.WriteLine("XmlReader: Read names and ages from XML document with people data:\n");
54+
Console.WriteLine("XmlReader: Read names and surnames from XML document with people data:\n");
5555
Console.WriteLine("\n---\n");
56-
ReadXmlUsingXmlReader.ReadNamesAndAges(
56+
ReadXmlUsingXmlReader.ReadNamesAndSurnames(
5757
CreateXmlUsingXmlWriter.CreateAnArrayOfPeople(people)
5858
)
5959
.ToList()
6060
.ForEach(Console.WriteLine);
61-
Console.ReadKey();
61+
Console.ReadKey();
62+
63+
Console.Clear();
64+
Console.WriteLine("Read an XML file from the output folder:\n");
65+
Console.WriteLine("\n---\n");
66+
ReadXmlFromFile.ReadEmailsUsingXDocument(ReadXmlFromFile.FilePath)
67+
.ToList()
68+
.ForEach(Console.WriteLine);
69+
ReadXmlFromFile.ReadEmailsUsingXmlReader(ReadXmlFromFile.FilePath)
70+
.ToList()
71+
.ForEach(Console.WriteLine);
72+
ReadXmlUsingXmlDocument.ReadEmails(ReadXmlFromFile.FilePath)
73+
.ToList()
74+
.ForEach(Console.WriteLine);
75+
Console.ReadKey();
76+
77+
Console.Clear();
78+
Console.WriteLine("XmlReader: Read names and surnames from people.xml:\n");
79+
Console.WriteLine("\n---\n");
80+
var xml = File.ReadAllText(ReadXmlFromFile.FilePath);
81+
82+
foreach (var row in ReadXmlUsingXmlReader.ReadNamesAndSurnames(xml))
83+
Console.WriteLine(row);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Xml;
2+
using System.Xml.Linq;
3+
4+
namespace ReadXml.Read
5+
{
6+
public static class ReadXmlFromFile
7+
{
8+
public static readonly string FilePath =
9+
Path.Combine(AppContext.BaseDirectory, "people.xml");
10+
11+
public static IEnumerable<string> ReadEmailsUsingXDocument(string path)
12+
{
13+
var xmlDoc = XDocument.Load(path);
14+
15+
return xmlDoc.Root!
16+
.Elements("person")
17+
.Select(person => person.Element("email")!.Value)
18+
.ToList();
19+
}
20+
21+
public static IEnumerable<string> ReadEmailsUsingXmlReader(string path)
22+
{
23+
using var reader = XmlReader.Create(path);
24+
25+
List<string> result = [];
26+
while (reader.Read())
27+
{
28+
if (reader.NodeType == XmlNodeType.Element && reader.Name == "email")
29+
result.Add(reader.ReadElementContentAsString());
30+
}
31+
32+
return result;
33+
}
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Xml;
2+
3+
namespace ReadXml.Read
4+
{
5+
public static class ReadXmlUsingXmlDocument
6+
{
7+
public static IEnumerable<string> ReadEmails(string path)
8+
{
9+
var xmlDoc = new XmlDocument();
10+
xmlDoc.Load(path);
11+
12+
return xmlDoc.GetElementsByTagName("email")
13+
.Cast<XmlNode>()
14+
.Select(node => node.InnerText)
15+
.ToList();
16+
}
17+
}
18+
}

‎xml-csharp/ReadXML/ReadXML/Read/ReadXmlUsingXmlReader.cs‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,38 @@ public static IEnumerable<string> ReadXmlWithoutWhiteSpace(string xml)
3333
return result;
3434
}
3535

36-
public static IEnumerable<string> ReadNamesAndAges(string xml)
36+
public static IEnumerable<string> ReadNamesAndSurnames(string xml)
3737
{
3838
var settings = new XmlReaderSettings
3939
{
4040
IgnoreWhitespace = true
4141
};
4242

43-
List<string> result = [];
4443
using var reader = XmlReader.Create(new StringReader(xml), settings);
44+
45+
List<string> result = [];
46+
var personData = new PersonData();
47+
var numberOfPersons = 0;
48+
49+
while (reader.Read())
4550
{
46-
var personData = new PersonData();
47-
var numberOfPersons = 0;
51+
if (reader.NodeType == XmlNodeType.Element)
52+
{
53+
if (reader.Name == "person")
54+
personData.Init();
55+
56+
if (reader.Name == "firstName")
57+
personData.FirstName = reader.ReadElementContentAsString();
58+
59+
if (reader.Name == "lastName")
60+
personData.LastName = reader.ReadElementContentAsString();
61+
}
4862

49-
while (reader.Read())
63+
if (reader.NodeType == XmlNodeType.EndElement)
5064
{
51-
if (reader.NodeType == XmlNodeType.Element)
52-
{
53-
if (reader.Name == "person")
54-
personData.Init();
55-
56-
if (reader.Name == "firstName")
57-
personData.FirstName = reader.ReadElementContentAsString();
58-
59-
if (reader.Name == "lastName")
60-
personData.LastName = reader.ReadElementContentAsString();
61-
}
62-
63-
if (reader.NodeType == XmlNodeType.EndElement)
64-
{
65-
if (reader.Name == "person")
66-
result.Add($"#{++numberOfPersons,3} | " +
67-
$"{personData.FirstName,-15} | {personData.LastName,-15}");
68-
}
65+
if (reader.Name == "person")
66+
result.Add($"#{++numberOfPersons,3} | " +
67+
$"{personData.FirstName,-15} | {personData.LastName,-15}");
6968
}
7069
}
7170

‎xml-csharp/ReadXML/ReadXML/ReadXML.csproj‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<None Update="people.xml" CopyToOutputDirectory="PreserveNewest" />
12+
</ItemGroup>
13+
1014
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<people>
3+
<person>
4+
<name>
5+
<firstName>Olivia</firstName>
6+
<lastName>Davis</lastName>
7+
</name>
8+
<email>olivia.davis@code-maze.com</email>
9+
<age>34</age>
10+
</person>
11+
<person>
12+
<name>
13+
<firstName>John</firstName>
14+
<lastName>Davis</lastName>
15+
</name>
16+
<email>john.davis@code-maze.com</email>
17+
<age>41</age>
18+
</person>
19+
<person>
20+
<name>
21+
<firstName>Sarah</firstName>
22+
<lastName>Moore</lastName>
23+
</name>
24+
<email>sarah.moore@code-maze.com</email>
25+
<age>27</age>
26+
</person>
27+
<person>
28+
<name>
29+
<firstName>Sarah</firstName>
30+
<lastName>Smith</lastName>
31+
</name>
32+
<email>sarah.smith@code-maze.com</email>
33+
<age>62</age>
34+
</person>
35+
</people>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Tests.Read
2+
{
3+
[TestClass]
4+
public class ReadXmlFromFileTests
5+
{
6+
private static readonly string[] ExpectedEmails =
7+
[
8+
"olivia.davis@code-maze.com",
9+
"john.davis@code-maze.com",
10+
"sarah.moore@code-maze.com",
11+
"sarah.smith@code-maze.com"
12+
];
13+
14+
[TestMethod]
15+
public void GivenPeopleFile_WhenCallingReadEmailsUsingXDocument_ThenAllEmailsAreReturned()
16+
{
17+
var result = ReadXmlFromFile.ReadEmailsUsingXDocument(ReadXmlFromFile.FilePath);
18+
19+
CollectionAssert.AreEqual(ExpectedEmails, result.ToArray());
20+
}
21+
22+
[TestMethod]
23+
public void GivenPeopleFile_WhenCallingReadEmailsUsingXmlReader_ThenAllEmailsAreReturned()
24+
{
25+
var result = ReadXmlFromFile.ReadEmailsUsingXmlReader(ReadXmlFromFile.FilePath);
26+
27+
CollectionAssert.AreEqual(ExpectedEmails, result.ToArray());
28+
}
29+
30+
[TestMethod]
31+
public void GivenPeopleFile_WhenCallingXmlDocumentReadEmails_ThenAllEmailsAreReturned()
32+
{
33+
var result = ReadXmlUsingXmlDocument.ReadEmails(ReadXmlFromFile.FilePath);
34+
35+
CollectionAssert.AreEqual(ExpectedEmails, result.ToArray());
36+
}
37+
38+
[TestMethod]
39+
public void GivenMissingFile_WhenCallingXDocumentLoad_ThenFileNotFoundExceptionIsThrown()
40+
{
41+
var missing = Path.Combine(AppContext.BaseDirectory, "missing.xml");
42+
43+
Assert.ThrowsExactly<FileNotFoundException>(() => ReadXmlFromFile.ReadEmailsUsingXDocument(missing));
44+
}
45+
}
46+
}

‎xml-csharp/ReadXML/Tests/Read/ReadXmlUsingXmlReaderTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void GivenValidXmlString_WhenCallingReadXml_ThenWhitespacesAreIsReturned(
1616
[TestMethod]
1717
public void GivenInvalidXmlString_WhenCallingReadXml_ThenExceptionIsThrown()
1818
{
19-
Assert.ThrowsException<XmlException>(() => ReadXmlUsingXmlReader.ReadXml(CreateInvalidXmlString()));
19+
Assert.ThrowsExactly<XmlException>(() => ReadXmlUsingXmlReader.ReadXml(CreateInvalidXmlString()));
2020
}
2121

2222

@@ -41,7 +41,7 @@ public void GivenNumberOfPersons_WhenCallingCreateAnArrayOfPeople_ThenSameNumber
4141
var people = People.Get(numberOfPersons);
4242
var arrayOfPeople = CreateXmlUsingXmlWriter.CreateAnArrayOfPeople(people);
4343

44-
var result = ReadXmlUsingXmlReader.ReadNamesAndAges(arrayOfPeople);
44+
var result = ReadXmlUsingXmlReader.ReadNamesAndSurnames(arrayOfPeople);
4545

4646
Assert.AreEqual(numberOfPersons, result.Count());
4747
}

‎xml-csharp/ReadXML/Tests/Tests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -10,10 +10,10 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14-
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
15-
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
16-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
14+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
15+
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
16+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)