Demonstrating Serialization in .Net
I’m trying to prepare a brown-bag meeting to demonstrate the power and usefulness of XML Serialization to my co-workers. The problem is that it is too easy to get carried away with this stuff. I keep wanting to optimize everything and make it reusable and programmer-friendly. The more I make things generic, the less helpful it becomes in understanding how things work during a tutorial.
Here is a little helper that I made to assist me in serializing and deserializing objects with XML, Soap, and as Binary formats.
1 using System;
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.Runtime.Serialization.Formatters.Soap;
6 using System.Text;
7 using System.Xml.Serialization;
8
9 namespace Demo
10 {
11 public class Serializer
12 {
13 public class Serialize
14 {
15 public static void XmlFile(object o, string path)
16 {
17 StreamWriter writer = null;
18 try
19 {
20 XmlSerializer formatter = new XmlSerializer(o.GetType());
21 writer = new StreamWriter(path);
22 formatter.Serialize(writer, o);
23 }
24 finally
25 {
26 if(writer != null) writer.Close();
27 }
28 }
29 public static string XmlString(object o)
30 {
31 StringWriter writer = null;
32 try
33 {
34 XmlSerializer formatter = new XmlSerializer(o.GetType());
35 writer = new StringWriter();
36 formatter.Serialize(writer, o);
37 return writer.ToString();
38 }
39 finally
40 {
41 if(writer != null) writer.Close();
42 }
43 }
44 public static void CustomFile(object o, string path, IFormatter formatter)
45 {
46 FileStream writer = null;
47 try
48 {
49 writer = File.Create(path);
50 formatter.Serialize(writer, o);
51 }
52 finally
53 {
54 if(writer != null) writer.Close();
55 }
56 }
57 public static string CustomString(object o, IFormatter formatter)
58 {
59 byte[] buffer = CustomArray(o, formatter);
60 return Encoding.Default.GetString(buffer);
61 }
62 public static byte[] CustomArray(object o, IFormatter formatter)
63 {
64 MemoryStream writer = null;
65 try
66 {
67 writer = new MemoryStream();
68 formatter.Serialize(writer, o);
69 return writer.ToArray();
70 }
71 finally
72 {
73 if(writer != null) writer.Close();
74 }
75 }
76 public static byte[] SoapArray(object o)
77 {
78 return CustomArray(o, new SoapFormatter());
79 }
80 public static void SoapFile(object o, string path)
81 {
82 CustomFile(o, path, new SoapFormatter());
83 }
84 public static string SoapString(object o)
85 {
86 return CustomString(o, new SoapFormatter());
87 }
88 public static byte[] BinaryArray(object o)
89 {
90 return CustomArray(o, new BinaryFormatter());
91 }
92 public static void BinaryFile(object o, string path)
93 {
94 CustomFile(o, path, new BinaryFormatter());
95 }
96 }
97 public class Deserialize
98 {
99 public static object XmlFile(string path, Type type)
100 {
101 StreamReader reader = null;
102 try
103 {
104 XmlSerializer formatter = new XmlSerializer(type);
105 reader = new StreamReader(path);
106 return formatter.Deserialize(reader);
107 }
108 finally
109 {
110 if(reader != null) reader.Close();
111 }
112 }
113 public static object XmlString(string xml, Type type)
114 {
115 StringReader reader = null;
116 try
117 {
118 XmlSerializer formatter = new XmlSerializer(type);
119 reader = new StringReader(xml);
120 return formatter.Deserialize(reader);
121 }
122 finally
123 {
124 if(reader != null) reader.Close();
125 }
126 }
127 public static object CustomFile(string path, IFormatter formatter)
128 {
129 FileStream reader = null;
130 try
131 {
132 reader = File.OpenRead(path);
133 return formatter.Deserialize(reader);
134 }
135 finally
136 {
137 if(reader != null) reader.Close();
138 }
139 }
140 public static object CustomArray(byte[] buffer, IFormatter formatter)
141 {
142 MemoryStream reader = null;
143 try
144 {
145 reader = new MemoryStream(buffer);
146 return formatter.Deserialize(reader);
147 }
148 finally
149 {
150 if(reader != null) reader.Close();
151 }
152 }
153 public static object CustomString(string text, IFormatter formatter)
154 {
155 byte[] buffer = Encoding.Default.GetBytes(text);
156 return CustomArray(buffer, formatter);
157 }
158 public static object SoapArray(byte[] buffer)
159 {
160 return CustomArray(buffer, new SoapFormatter());
161 }
162 public static object SoapFile(string path)
163 {
164 return CustomFile(path, new SoapFormatter());
165 }
166 public static object SoapString(string soap)
167 {
168 return CustomString(soap, new SoapFormatter());
169 }
170 public static object BinaryArray(byte[] buffer)
171 {
172 return CustomArray(buffer, new BinaryFormatter());
173 }
174 public static object BinaryFile(string path)
175 {
176 return CustomFile(path, new BinaryFormatter());
177 }
178 }
179 }
180 }
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.Runtime.Serialization.Formatters.Soap;
6 using System.Text;
7 using System.Xml.Serialization;
8
9 namespace Demo
10 {
11 public class Serializer
12 {
13 public class Serialize
14 {
15 public static void XmlFile(object o, string path)
16 {
17 StreamWriter writer = null;
18 try
19 {
20 XmlSerializer formatter = new XmlSerializer(o.GetType());
21 writer = new StreamWriter(path);
22 formatter.Serialize(writer, o);
23 }
24 finally
25 {
26 if(writer != null) writer.Close();
27 }
28 }
29 public static string XmlString(object o)
30 {
31 StringWriter writer = null;
32 try
33 {
34 XmlSerializer formatter = new XmlSerializer(o.GetType());
35 writer = new StringWriter();
36 formatter.Serialize(writer, o);
37 return writer.ToString();
38 }
39 finally
40 {
41 if(writer != null) writer.Close();
42 }
43 }
44 public static void CustomFile(object o, string path, IFormatter formatter)
45 {
46 FileStream writer = null;
47 try
48 {
49 writer = File.Create(path);
50 formatter.Serialize(writer, o);
51 }
52 finally
53 {
54 if(writer != null) writer.Close();
55 }
56 }
57 public static string CustomString(object o, IFormatter formatter)
58 {
59 byte[] buffer = CustomArray(o, formatter);
60 return Encoding.Default.GetString(buffer);
61 }
62 public static byte[] CustomArray(object o, IFormatter formatter)
63 {
64 MemoryStream writer = null;
65 try
66 {
67 writer = new MemoryStream();
68 formatter.Serialize(writer, o);
69 return writer.ToArray();
70 }
71 finally
72 {
73 if(writer != null) writer.Close();
74 }
75 }
76 public static byte[] SoapArray(object o)
77 {
78 return CustomArray(o, new SoapFormatter());
79 }
80 public static void SoapFile(object o, string path)
81 {
82 CustomFile(o, path, new SoapFormatter());
83 }
84 public static string SoapString(object o)
85 {
86 return CustomString(o, new SoapFormatter());
87 }
88 public static byte[] BinaryArray(object o)
89 {
90 return CustomArray(o, new BinaryFormatter());
91 }
92 public static void BinaryFile(object o, string path)
93 {
94 CustomFile(o, path, new BinaryFormatter());
95 }
96 }
97 public class Deserialize
98 {
99 public static object XmlFile(string path, Type type)
100 {
101 StreamReader reader = null;
102 try
103 {
104 XmlSerializer formatter = new XmlSerializer(type);
105 reader = new StreamReader(path);
106 return formatter.Deserialize(reader);
107 }
108 finally
109 {
110 if(reader != null) reader.Close();
111 }
112 }
113 public static object XmlString(string xml, Type type)
114 {
115 StringReader reader = null;
116 try
117 {
118 XmlSerializer formatter = new XmlSerializer(type);
119 reader = new StringReader(xml);
120 return formatter.Deserialize(reader);
121 }
122 finally
123 {
124 if(reader != null) reader.Close();
125 }
126 }
127 public static object CustomFile(string path, IFormatter formatter)
128 {
129 FileStream reader = null;
130 try
131 {
132 reader = File.OpenRead(path);
133 return formatter.Deserialize(reader);
134 }
135 finally
136 {
137 if(reader != null) reader.Close();
138 }
139 }
140 public static object CustomArray(byte[] buffer, IFormatter formatter)
141 {
142 MemoryStream reader = null;
143 try
144 {
145 reader = new MemoryStream(buffer);
146 return formatter.Deserialize(reader);
147 }
148 finally
149 {
150 if(reader != null) reader.Close();
151 }
152 }
153 public static object CustomString(string text, IFormatter formatter)
154 {
155 byte[] buffer = Encoding.Default.GetBytes(text);
156 return CustomArray(buffer, formatter);
157 }
158 public static object SoapArray(byte[] buffer)
159 {
160 return CustomArray(buffer, new SoapFormatter());
161 }
162 public static object SoapFile(string path)
163 {
164 return CustomFile(path, new SoapFormatter());
165 }
166 public static object SoapString(string soap)
167 {
168 return CustomString(soap, new SoapFormatter());
169 }
170 public static object BinaryArray(byte[] buffer)
171 {
172 return CustomArray(buffer, new BinaryFormatter());
173 }
174 public static object BinaryFile(string path)
175 {
176 return CustomFile(path, new BinaryFormatter());
177 }
178 }
179 }
180 }