Skip to content

Commit 11fe13a

Browse files
committed
Fixes to the XML deserialization article
1 parent e57c01d commit 11fe13a

File tree

11 files changed

+205
-179
lines changed

11 files changed

+205
-179
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System.Xml.Serialization;
22

33
namespace XMLDeserializationInCsharp
4-
{
5-
public class Book
6-
{
7-
[XmlElement("Title")]
8-
public string Title { get; set; }=string.Empty;
9-
10-
[XmlElement("Author")]
11-
public string Author { get; set; }= string.Empty;
12-
}
4+
{
5+
public class Book
6+
{
7+
[XmlElement("Title")]
8+
public string Title { get; set; } = string.Empty;
9+
10+
[XmlElement("Author")]
11+
public string Author { get; set; } = string.Empty;
12+
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Xml;
22
using System.Xml.Serialization;
3-
4-
public record BookRecord([property: XmlElement("Title")] string Title, [property: XmlElement("Author")] string Author)
5-
{
6-
private BookRecord() : this("", "")
7-
{
8-
9-
}
3+
4+
namespace XMLDeserializationInCsharp
5+
{
6+
public record BookRecord([property: XmlElement("Title")] string Title, [property: XmlElement("Author")] string Author)
7+
{
8+
private BookRecord() : this("", "")
9+
{
10+
11+
}
12+
}
1013
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
public record LibraryRecord()
2-
{
3-
public List<BookRecord> Books { get; init; } = new();
4-
private LibraryRecord(List<BookRecord> books):this()
5-
{
6-
Books = books;
7-
}
1+
using XMLDeserializationInCsharp;
2+
3+
public record LibraryRecord()
4+
{
5+
public List<BookRecord> Books { get; init; } = new();
6+
private LibraryRecord(List<BookRecord> books) : this()
7+
{
8+
Books = books;
9+
}
810
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
namespace XMLDeserializationInCsharp
2-
{
3-
public class Person
4-
{
5-
public string Name { get; set; } = string.Empty;
6-
public int Age { get; set; }=int.MinValue;
7-
}
1+
using System.Xml.Serialization;
2+
3+
namespace XMLDeserializationInCsharp
4+
{
5+
[XmlRoot("Person")]
6+
public class Person
7+
{
8+
[XmlElement("Name")]
9+
public string Name { get; set; } = string.Empty;
10+
[XmlElement("Age")]
11+
public int Age { get; set; } = int.MinValue;
12+
}
813
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
public record PersonRecord(string Name, int Age)
2-
{
3-
public PersonRecord() : this("", int.MinValue)
4-
{
5-
6-
}
1+
public record PersonRecord(string Name, int Age)
2+
{
3+
public PersonRecord() : this("", int.MinValue)
4+
{
5+
6+
}
77
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
using System.Xml.Serialization;
2-
2+
using static System.Reflection.Metadata.BlobBuilder;
3+
34
namespace XMLDeserializationInCsharp
4-
{
5-
public class Program
6-
{
7-
static void Main(string[] args)
8-
{
9-
#region Deserialization_Of_Class
10-
#region Simple Deserialization
11-
var xmlData = """
5+
{
6+
public class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var xmlData2 = """<PersonRecord> <Name>John Doe</Name> <Age>30</Age> </PersonRecord>"""; var person2 = DeserializeXmlData<PersonRecord>(xmlData2); Console.WriteLine($"Name: {person2.Name}, Age: {person2.Age}");
11+
12+
var xmlData = """
1213
<Person>
1314
<Name>John Doe</Name>
1415
<Age>30</Age>
1516
</Person>
16-
""";
17-
18-
19-
var person = DeserializeXmlData<Person>(xmlData);
20-
if (person != null)
21-
{
22-
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
23-
}
24-
#endregion
25-
26-
#region Complex Deserialization
27-
var complexXML = """
17+
""";
18+
19+
var person = DeserializeXmlData<Person>(xmlData);
20+
if (person != null)
21+
{
22+
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
23+
}
24+
25+
var complexXML = """
2826
<Library>
2927
<Books>
3028
<Book>
@@ -37,38 +35,32 @@ static void Main(string[] args)
3735
</Book>
3836
</Books>
3937
</Library>
40-
""";
41-
42-
43-
var library = DeserializeXmlData<Library>(complexXML);
44-
if (library != null)
45-
{
46-
foreach (Book book in library.Books)
47-
{
48-
Console.WriteLine($"Title: {book.Title}, Author: {book.Author}");
49-
}
50-
}
51-
#endregion
52-
#endregion
53-
54-
55-
#region Deserialization_Of_Records
56-
57-
//Simple Deserialization
58-
var personXML = """
38+
""";
39+
40+
var library = DeserializeXmlData<Library>(complexXML);
41+
if (library != null)
42+
{
43+
foreach (Book book in library.Books)
44+
{
45+
Console.WriteLine($"Title: {book.Title}, Author: {book.Author}");
46+
}
47+
}
48+
49+
//Simple Deserialization
50+
var personXML = """
5951
<PersonRecord>
6052
<Name>John Wick</Name>
6153
<Age>35</Age>
6254
</PersonRecord>
63-
""";
64-
var personRecord = DeserializeXmlData<PersonRecord>(personXML);
65-
if (personRecord != null)
66-
{
67-
Console.WriteLine($"Name: {personRecord.Name}, Age: {personRecord.Age}");
68-
}
69-
70-
//Complex Deserialization
71-
var libraryXML = """
55+
""";
56+
var personRecord = DeserializeXmlData<PersonRecord>(personXML);
57+
if (personRecord != null)
58+
{
59+
Console.WriteLine($"Name: {personRecord.Name}, Age: {personRecord.Age}");
60+
}
61+
62+
//Complex Deserialization
63+
var libraryXML = """
7264
<LibraryRecord>
7365
<Books>
7466
<BookRecord>
@@ -81,25 +73,24 @@ static void Main(string[] args)
8173
</BookRecord>
8274
</Books>
8375
</LibraryRecord>
84-
""";
85-
var libraryRecord = DeserializeXmlData<LibraryRecord>(libraryXML);
86-
if (libraryRecord != null)
87-
{
88-
foreach (BookRecord book in libraryRecord.Books)
89-
{
90-
Console.WriteLine($"Title: {book.Title}, Author: {book.Author}");
91-
}
92-
}
93-
#endregion
94-
}
95-
96-
#region Deserialization Method
97-
public static T? DeserializeXmlData<T>(string xmlData)
98-
{
99-
var serializer = new XmlSerializer(typeof(T));
100-
using var reader = new StringReader(xmlData);
101-
return (T?)serializer.Deserialize(reader);
102-
}
103-
#endregion
104-
}
76+
""";
77+
78+
var libraryRecord = DeserializeXmlData<LibraryRecord>(libraryXML);
79+
if (libraryRecord != null)
80+
{
81+
foreach (BookRecord book in libraryRecord.Books)
82+
{
83+
Console.WriteLine($"Title: {book.Title}, Author: {book.Author}");
84+
}
85+
}
86+
}
87+
88+
public static T? DeserializeXmlData<T>(string xmlData)
89+
{
90+
var serializer = new XmlSerializer(typeof(T));
91+
using var reader = new StringReader(xmlData);
92+
93+
return (T?)serializer.Deserialize(reader);
94+
}
95+
}
10596
}

xml-csharp/XMLDeserializationInCsharp/XMLDeserializationInCsharp/XMLDeserializationInCsharp.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<None Update="library.xml">
12+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
13+
</None>
14+
<None Update="person.xml">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
1019
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Library>
2+
<Books>
3+
<Book>
4+
<Title> Book 1 </Title>
5+
<Author> Author 1 </Author>
6+
</Book>
7+
<Book>
8+
<Title> Book 2 </Title>
9+
<Author> Author 2 </Author>
10+
</Book>
11+
</Books>
12+
</Library>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Person>
2+
<Name>Jane Smith</Name>
3+
<Age>25</Age>
4+
</Person>

xml-csharp/XMLDeserializationInCsharp/XMLDeserializationTests/Usings.cs

-1
This file was deleted.

0 commit comments

Comments
 (0)