1
1
using System . Xml . Serialization ;
2
-
2
+ using static System . Reflection . Metadata . BlobBuilder ;
3
+
3
4
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 = """
12
13
<Person>
13
14
<Name>John Doe</Name>
14
15
<Age>30</Age>
15
16
</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 = """
28
26
<Library>
29
27
<Books>
30
28
<Book>
@@ -37,38 +35,32 @@ static void Main(string[] args)
37
35
</Book>
38
36
</Books>
39
37
</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 = """
59
51
<PersonRecord>
60
52
<Name>John Wick</Name>
61
53
<Age>35</Age>
62
54
</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 = """
72
64
<LibraryRecord>
73
65
<Books>
74
66
<BookRecord>
@@ -81,25 +73,24 @@ static void Main(string[] args)
81
73
</BookRecord>
82
74
</Books>
83
75
</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
+ }
105
96
}
0 commit comments