24c8f106523b44cf958527f49371d9414ba3d042
[mono.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTests.cs
1 //
2 // System.Xml.XmlSerializerTests
3 //
4 // Author:
5 //   Erik LeBel <eriklebel@yahoo.ca>
6 //   Hagit Yidov <hagity@mainsoft.com>
7 //
8 // (C) 2003 Erik LeBel
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
10 //
11 //
12 // NOTES:
13 //  Where possible, these tests avoid testing the order of
14 //  an object's members serialization. Mono and .NET do not
15 //  reflect members in the same order.
16 //
17 //  Only serializations tests so far, no deserialization.
18 //
19 // FIXME
20 //  test XmlArrayAttribute
21 //  test XmlArrayItemAttribute
22 //  test serialization of decimal type
23 //  test serialization of Guid type
24 //  test XmlNode serialization with and without modifying attributes.
25 //  test deserialization
26 //  FIXMEs found in this file
27
28 using System;
29 using System.Collections;
30 using System.Globalization;
31 using System.IO;
32 using System.Text;
33 using System.Xml;
34 using System.Data;
35 using System.Xml.Schema;
36 using System.Xml.Serialization;
37 using System.Reflection;
38 using System.Collections.Generic;
39
40 using NUnit.Framework;
41
42 using MonoTests.System.Xml.TestClasses;
43
44 namespace MonoTests.System.XmlSerialization
45 {
46         [TestFixture]
47         public class XmlSerializerTests
48         {
49                 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
50                 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
51                 const string ANamespace = "some:urn";
52                 const string AnotherNamespace = "another:urn";
53
54                 StringWriter sw;
55                 XmlTextWriter xtw;
56                 XmlSerializer xs;
57
58                 private void SetUpWriter ()
59                 {
60                         sw = new StringWriter ();
61                         xtw = new XmlTextWriter (sw);
62                         xtw.QuoteChar = '\'';
63                         xtw.Formatting = Formatting.None;
64                 }
65
66                 private string WriterText
67                 {
68                         get
69                         {
70                                 string val = sw.GetStringBuilder ().ToString ();
71                                 int offset = val.IndexOf ('>') + 1;
72                                 val = val.Substring (offset);
73                                 return Infoset (val);
74                         }
75                 }
76
77                 private void Serialize (object o)
78                 {
79                         SetUpWriter ();
80                         xs = new XmlSerializer (o.GetType ());
81                         xs.Serialize (xtw, o);
82                 }
83
84                 private void Serialize (object o, Type type)
85                 {
86                         SetUpWriter ();
87                         xs = new XmlSerializer (type);
88                         xs.Serialize (xtw, o);
89                 }
90
91                 private void Serialize (object o, XmlSerializerNamespaces ns)
92                 {
93                         SetUpWriter ();
94                         xs = new XmlSerializer (o.GetType ());
95                         xs.Serialize (xtw, o, ns);
96                 }
97
98                 private void Serialize (object o, XmlAttributeOverrides ao)
99                 {
100                         SetUpWriter ();
101                         xs = new XmlSerializer (o.GetType (), ao);
102                         xs.Serialize (xtw, o);
103                 }
104
105                 private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
106                 {
107                         SetUpWriter ();
108                         xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
109                                 (XmlRootAttribute) null, defaultNamespace);
110                         xs.Serialize (xtw, o);
111                 }
112
113                 private void Serialize (object o, XmlRootAttribute root)
114                 {
115                         SetUpWriter ();
116                         xs = new XmlSerializer (o.GetType (), root);
117                         xs.Serialize (xtw, o);
118                 }
119
120                 private void Serialize (object o, XmlTypeMapping typeMapping)
121                 {
122                         SetUpWriter ();
123                         xs = new XmlSerializer (typeMapping);
124                         xs.Serialize (xtw, o);
125                 }
126
127                 private void SerializeEncoded (object o)
128                 {
129                         SerializeEncoded (o, o.GetType ());
130                 }
131
132                 private void SerializeEncoded (object o, SoapAttributeOverrides ao)
133                 {
134                         XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
135                         SetUpWriter ();
136                         xs = new XmlSerializer (mapping);
137                         xs.Serialize (xtw, o);
138                 }
139
140                 private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
141                 {
142                         XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
143                         SetUpWriter ();
144                         xs = new XmlSerializer (mapping);
145                         xs.Serialize (xtw, o);
146                 }
147
148                 private void SerializeEncoded (object o, Type type)
149                 {
150                         XmlTypeMapping mapping = CreateSoapMapping (type);
151                         SetUpWriter ();
152                         xs = new XmlSerializer (mapping);
153                         xs.Serialize (xtw, o);
154                 }
155
156                 private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
157                 {
158                         XmlTypeMapping mapping = CreateSoapMapping (type);
159                         xs = new XmlSerializer (mapping);
160                         xs.Serialize (xtw, o);
161                 }
162
163                 // test constructors
164 #if USE_VERSION_1_1     // It doesn't pass on MS.NET 1.1.
165                 [Test]
166                 public void TestConstructor()
167                 {
168                         XmlSerializer ser = new XmlSerializer (null, "");
169                 }
170 #else
171 #endif
172
173                 // test basic types ////////////////////////////////////////////////////////
174                 [Test]
175                 public void TestSerializeInt ()
176                 {
177                         Serialize (10);
178                         Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
179                 }
180
181                 [Test]
182                 public void TestSerializeBool ()
183                 {
184                         Serialize (true);
185                         Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
186
187                         Serialize (false);
188                         Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
189                 }
190
191                 [Test]
192                 public void TestSerializeString ()
193                 {
194                         Serialize ("hello");
195                         Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
196                 }
197
198                 [Test]
199                 public void TestSerializeEmptyString ()
200                 {
201                         Serialize (String.Empty);
202                         Assert.AreEqual (Infoset ("<string />"), WriterText);
203                 }
204
205                 [Test]
206                 public void TestSerializeNullObject ()
207                 {
208                         Serialize (null, typeof (object));
209                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
210                                 "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
211                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
212                 }
213
214                 [Test]
215                 [Ignore ("The generated XML is not exact but it is equivalent")]
216                 public void TestSerializeNullString ()
217                 {
218                         Serialize (null, typeof (string));
219                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
220                                 "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
221                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
222                 }
223
224                 [Test]
225                 public void TestSerializeIntArray ()
226                 {
227                         Serialize (new int[] { 1, 2, 3, 4 });
228                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
229                                 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
230                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
231                 }
232
233                 [Test]
234                 public void TestSerializeEmptyArray ()
235                 {
236                         Serialize (new int[] { });
237                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
238                                 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
239                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
240                 }
241
242                 [Test]
243                 public void TestSerializeChar ()
244                 {
245                         Serialize ('A');
246                         Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
247
248                         Serialize ('\0');
249                         Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
250
251                         Serialize ('\n');
252                         Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
253
254                         Serialize ('\uFF01');
255                         Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
256                 }
257
258                 [Test]
259                 public void TestSerializeFloat ()
260                 {
261                         Serialize (10.78);
262                         Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
263
264                         Serialize (-1e8);
265                         Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
266
267                         // FIXME test INF and other boundary conditions that may exist with floats
268                 }
269
270                 [Test]
271                 public void TestSerializeEnumeration_FromValue ()
272                 {
273                         Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
274                         Assert.AreEqual (
275                                 "<?xml version='1.0' encoding='utf-16'?>" +
276                                 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
277                                 sw.ToString ());
278                 }
279
280                 [Test]
281                 [Category ("MobileNotWorking")]
282                 public void TestSerializeEnumeration_FromValue_Encoded ()
283                 {
284                         SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
285                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
286                                 "<?xml version='1.0' encoding='utf-16'?>" +
287                                 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
288                                 XmlSchema.InstanceNamespace), sw.ToString ());
289                 }
290
291                 [Test]
292                 public void TestSerializeEnumeration ()
293                 {
294                         Serialize (SimpleEnumeration.FIRST);
295                         Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
296
297                         Serialize (SimpleEnumeration.SECOND);
298                         Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
299                 }
300
301                 [Test]
302                 public void TestSerializeEnumeration_Encoded ()
303                 {
304                         SerializeEncoded (SimpleEnumeration.FIRST);
305                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
306                                 "<?xml version='1.0' encoding='utf-16'?>" +
307                                 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
308                                 XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
309
310                         SerializeEncoded (SimpleEnumeration.SECOND);
311                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
312                                 "<?xml version='1.0' encoding='utf-16'?>" +
313                                 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
314                                 XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
315                 }
316
317                 [Test]
318                 public void TestSerializeEnumDefaultValue ()
319                 {
320                         Serialize (new EnumDefaultValue ());
321                         Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
322
323                         Serialize (new SimpleEnumeration ());
324                         Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
325
326                         Serialize (3, typeof (EnumDefaultValue));
327                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
328
329                         Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
330                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
331
332                         Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
333                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
334
335                         Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
336                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
337
338                         Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
339                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
340
341                         Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
342                         Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
343
344                         Serialize (3, typeof (FlagEnum));
345                         Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
346
347                         Serialize (5, typeof (FlagEnum));
348                         Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
349
350                         Serialize (FlagEnum.e4, typeof (FlagEnum));
351                         Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
352
353                         Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
354                         Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
355
356                         Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
357                         Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
358
359                         Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
360                         Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
361
362                         Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
363                         Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
364
365                         Serialize (3, typeof (EnumDefaultValueNF));
366                         Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
367
368                         Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
369                         Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
370
371                         Serialize (2, typeof (ZeroFlagEnum));
372                         Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
373
374                         Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
375                         Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
376                 }
377
378                 [Test]
379                 [Category ("MobileNotWorking")]
380                 public void TestSerializeEnumDefaultValue_Encoded ()
381                 {
382                         SerializeEncoded (new EnumDefaultValue ());
383                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
384                                 "<?xml version='1.0' encoding='utf-16'?>" +
385                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
386                                 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
387
388                         SerializeEncoded (new SimpleEnumeration ());
389                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
390                                 "<?xml version='1.0' encoding='utf-16'?>" +
391                                 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
392                                 XmlSchema.InstanceNamespace), sw.ToString (), "#2");
393
394                         SerializeEncoded (3, typeof (EnumDefaultValue));
395                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
396                                 "<?xml version='1.0' encoding='utf-16'?>" +
397                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
398                                 XmlSchema.InstanceNamespace), sw.ToString (), "#3");
399
400                         SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
401                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
402                                 "<?xml version='1.0' encoding='utf-16'?>" +
403                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
404                                 XmlSchema.InstanceNamespace), sw.ToString (), "#4");
405
406                         SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
407                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
408                                 "<?xml version='1.0' encoding='utf-16'?>" +
409                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
410                                 XmlSchema.InstanceNamespace), sw.ToString (), "#5");
411
412                         SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
413                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
414                                 "<?xml version='1.0' encoding='utf-16'?>" +
415                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
416                                 XmlSchema.InstanceNamespace), sw.ToString (), "#6");
417
418                         SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
419                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
420                                 "<?xml version='1.0' encoding='utf-16'?>" +
421                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
422                                 XmlSchema.InstanceNamespace), sw.ToString (), "#7");
423
424                         SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
425                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
426                                 "<?xml version='1.0' encoding='utf-16'?>" +
427                                 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
428                                 XmlSchema.InstanceNamespace), sw.ToString (), "#8");
429
430                         SerializeEncoded (3, typeof (FlagEnum));
431                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
432                                 "<?xml version='1.0' encoding='utf-16'?>" +
433                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
434                                 XmlSchema.InstanceNamespace), sw.ToString (), "#9");
435
436                         SerializeEncoded (5, typeof (FlagEnum));
437                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
438                                 "<?xml version='1.0' encoding='utf-16'?>" +
439                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
440                                 XmlSchema.InstanceNamespace), sw.ToString (), "#10");
441
442                         SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
443                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
444                                 "<?xml version='1.0' encoding='utf-16'?>" +
445                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
446                                 XmlSchema.InstanceNamespace), sw.ToString (), "#11");
447
448                         SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
449                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
450                                 "<?xml version='1.0' encoding='utf-16'?>" +
451                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
452                                 XmlSchema.InstanceNamespace), sw.ToString (), "#12");
453
454                         SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
455                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
456                                 "<?xml version='1.0' encoding='utf-16'?>" +
457                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
458                                 XmlSchema.InstanceNamespace), sw.ToString (), "#13");
459
460                         SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
461                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
462                                 "<?xml version='1.0' encoding='utf-16'?>" +
463                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
464                                 XmlSchema.InstanceNamespace), sw.ToString (), "#14");
465
466                         SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
467                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
468                                 "<?xml version='1.0' encoding='utf-16'?>" +
469                                 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
470                                 XmlSchema.InstanceNamespace), sw.ToString (), "#15");
471
472                         SerializeEncoded (3, typeof (EnumDefaultValueNF));
473                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
474                                 "<?xml version='1.0' encoding='utf-16'?>" +
475                                 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
476                                 XmlSchema.InstanceNamespace), sw.ToString (), "#16");
477
478                         SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
479                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
480                                 "<?xml version='1.0' encoding='utf-16'?>" +
481                                 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
482                                 XmlSchema.InstanceNamespace), sw.ToString (), "#17");
483
484                         SerializeEncoded (2, typeof (ZeroFlagEnum));
485                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
486                                 "<?xml version='1.0' encoding='utf-16'?>" +
487                                 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
488                                 XmlSchema.InstanceNamespace), sw.ToString (), "#18");
489
490                         SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
491                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
492                                 "<?xml version='1.0' encoding='utf-16'?>" +
493                                 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
494                                 XmlSchema.InstanceNamespace), sw.ToString (), "#19");
495                 }
496
497                 [Test]
498                 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
499                 {
500                         try {
501                                 Serialize ("b", typeof (EnumDefaultValue));
502                                 Assert.Fail ("#A1");
503                         } catch (InvalidOperationException ex) {
504                                 Assert.IsNotNull (ex.InnerException, "#A2");
505                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
506                         }
507
508                         try {
509                                 Serialize ("e1", typeof (EnumDefaultValue));
510                                 Assert.Fail ("#B1");
511                         } catch (InvalidOperationException ex) {
512                                 Assert.IsNotNull (ex.InnerException, "#B2");
513                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
514                         }
515
516                         try {
517                                 Serialize ("e1,e2", typeof (EnumDefaultValue));
518                                 Assert.Fail ("#C1");
519                         } catch (InvalidOperationException ex) {
520                                 Assert.IsNotNull (ex.InnerException, "#C2");
521                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
522                         }
523
524                         try {
525                                 Serialize (string.Empty, typeof (EnumDefaultValue));
526                                 Assert.Fail ("#D1");
527                         } catch (InvalidOperationException ex) {
528                                 Assert.IsNotNull (ex.InnerException, "#D2");
529                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
530                         }
531
532                         try {
533                                 Serialize ("1", typeof (EnumDefaultValue));
534                                 Assert.Fail ("#E1");
535                         } catch (InvalidOperationException ex) {
536                                 Assert.IsNotNull (ex.InnerException, "#E2");
537                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
538                         }
539
540                         try {
541                                 Serialize ("0", typeof (EnumDefaultValue));
542                                 Assert.Fail ("#F1");
543                         } catch (InvalidOperationException ex) {
544                                 Assert.IsNotNull (ex.InnerException, "#F2");
545                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
546                         }
547
548                         try {
549                                 Serialize (new SimpleClass (), typeof (EnumDefaultValue));
550                                 Assert.Fail ("#G1");
551                         } catch (InvalidOperationException ex) {
552                                 Assert.IsNotNull (ex.InnerException, "#G2");
553                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
554                         }
555                 }
556
557                 [Test]
558                 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
559                 {
560                         try {
561                                 Serialize (5, typeof (EnumDefaultValue));
562                                 Assert.Fail ("#1");
563                         } catch (InvalidOperationException ex) {
564                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
565                                 Assert.IsNotNull (ex.InnerException, "#3");
566                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
567                                 Assert.IsNotNull (ex.InnerException.Message, "#5");
568                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
569                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
570                         }
571                 }
572
573                 [Test]
574                 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
575                 {
576                         try {
577                                 Serialize (new EnumDefaultValueNF ());
578                                 Assert.Fail ("#1");
579                         } catch (InvalidOperationException ex) {
580                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
581                                 Assert.IsNotNull (ex.InnerException, "#3");
582                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
583                                 Assert.IsNotNull (ex.InnerException.Message, "#5");
584                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
585                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
586                         }
587                 }
588
589                 [Test]
590                 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
591                 {
592                         try {
593                                 Serialize (15, typeof (EnumDefaultValueNF));
594                                 Assert.Fail ("#1");
595                         } catch (InvalidOperationException ex) {
596                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
597                                 Assert.IsNotNull (ex.InnerException, "#3");
598                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
599                                 Assert.IsNotNull (ex.InnerException.Message, "#5");
600                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
601                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
602                         }
603                 }
604
605                 [Test]
606                 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
607                 {
608                         try {
609                                 Serialize ("b", typeof (EnumDefaultValueNF));
610                                 Assert.Fail ("#A1");
611                         } catch (InvalidOperationException ex) {
612                                 Assert.IsNotNull (ex.InnerException, "#A2");
613                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
614                         }
615
616                         try {
617                                 Serialize ("e2", typeof (EnumDefaultValueNF));
618                                 Assert.Fail ("#B1");
619                         } catch (InvalidOperationException ex) {
620                                 Assert.IsNotNull (ex.InnerException, "#B2");
621                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
622                         }
623
624                         try {
625                                 Serialize (string.Empty, typeof (EnumDefaultValueNF));
626                                 Assert.Fail ("#C1");
627                         } catch (InvalidOperationException ex) {
628                                 Assert.IsNotNull (ex.InnerException, "#C2");
629                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
630                         }
631
632                         try {
633                                 Serialize ("1", typeof (EnumDefaultValueNF));
634                                 Assert.Fail ("#D1");
635                         } catch (InvalidOperationException ex) {
636                                 Assert.IsNotNull (ex.InnerException, "#D2");
637                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
638                         }
639
640                         try {
641                                 Serialize ("0", typeof (EnumDefaultValueNF));
642                                 Assert.Fail ("#E1");
643                         } catch (InvalidOperationException ex) {
644                                 Assert.IsNotNull (ex.InnerException, "#E2");
645                                 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
646                         }
647                 }
648
649                 [Test]
650                 public void TestSerializeField ()
651                 {
652                         Field f = new Field ();
653                         Serialize (f, typeof (Field));
654                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
655                                 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
656                                 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
657                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
658
659                         f.Flags1 = FlagEnum.e1;
660                         f.Flags2 = FlagEnum.e1;
661                         f.Flags3 = FlagEnum.e2;
662                         f.Modifiers = MapModifiers.Protected;
663                         f.Modifiers2 = MapModifiers.Public;
664                         f.Modifiers3 = MapModifiers.Public;
665                         f.Modifiers4 = MapModifiers.Protected;
666                         f.Modifiers5 = MapModifiers.Public;
667                         Serialize (f, typeof (Field));
668                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
669                                 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
670                                 " modifiers='protected' modifiers2='public' />",
671                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
672
673                         f.Flags1 = (FlagEnum) 1;
674                         f.Flags1 = FlagEnum.e2;
675                         f.Flags2 = FlagEnum.e2;
676                         f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
677                         f.Modifiers = MapModifiers.Public;
678                         f.Modifiers2 = MapModifiers.Protected;
679                         f.Modifiers3 = MapModifiers.Protected;
680                         f.Modifiers4 = MapModifiers.Public;
681                         f.Modifiers5 = MapModifiers.Protected;
682                         Serialize (f, typeof (Field));
683                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
684                                 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
685                                 " flag4='' modifiers='public' modifiers2='protected'" +
686                                 " modifiers3='protected' modifiers4='public'" +
687                                 " modifiers5='protected' />",
688                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
689
690                         f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
691                         f.Flags2 = FlagEnum.e2;
692                         f.Flags3 = FlagEnum.e4;
693                         f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
694                         f.Modifiers3 = MapModifiers.Public;
695                         f.Modifiers4 = MapModifiers.Protected;
696                         f.Modifiers5 = MapModifiers.Public;
697                         f.Names = new string[] { "a", "b" };
698                         Serialize (f, typeof (Field));
699                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
700                                 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
701                                 " flag3='four' flag4='one two four' modifiers='public'" +
702                                 " modifiers2='protected' names='a b' />",
703                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
704
705                         f.Flags2 = (FlagEnum) 444;
706                         f.Flags3 = (FlagEnum) 555;
707                         f.Modifiers = (MapModifiers) 666;
708                         f.Modifiers2 = (MapModifiers) 777;
709                         f.Modifiers3 = (MapModifiers) 0;
710                         f.Modifiers4 = (MapModifiers) 888;
711                         f.Modifiers5 = (MapModifiers) 999;
712                         try {
713                                 Serialize (f, typeof (Field));
714                                 Assert.Fail ("#E1");
715                         } catch (InvalidOperationException ex) {
716                                 // There was an error generating the XML document
717                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
718                                 Assert.IsNotNull (ex.Message, "#E3");
719                                 Assert.IsNotNull (ex.InnerException, "#E4");
720
721                                 // Instance validation error: '444' is not a valid value for
722                                 // MonoTests.System.Xml.TestClasses.FlagEnum
723                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
724                                 Assert.IsNotNull (ex.InnerException.Message, "#E6");
725                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
726                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
727                                 Assert.IsNull (ex.InnerException.InnerException, "#E9");
728                         }
729                 }
730
731                 [Test]
732                 [Category ("NotWorking")] // MS bug
733                 public void TestSerializeField_Encoded ()
734                 {
735                         Field_Encoded f = new Field_Encoded ();
736                         SerializeEncoded (f, typeof (Field_Encoded));
737                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
738                                 "<?xml version='1.0' encoding='utf-16'?>" +
739                                 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
740                                 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
741                                 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
742                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
743                                 sw.GetStringBuilder ().ToString (), "#A");
744
745                         f.Flags1 = FlagEnum_Encoded.e1;
746                         f.Flags2 = FlagEnum_Encoded.e1;
747                         f.Flags3 = FlagEnum_Encoded.e2;
748                         f.Modifiers = MapModifiers.Protected;
749                         f.Modifiers2 = MapModifiers.Public;
750                         f.Modifiers3 = MapModifiers.Public;
751                         f.Modifiers4 = MapModifiers.Protected;
752                         f.Modifiers5 = MapModifiers.Public;
753                         SerializeEncoded (f, typeof (Field_Encoded));
754                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
755                                 "<?xml version='1.0' encoding='utf-16'?>" +
756                                 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
757                                 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
758                                 " xmlns:q1='some:urn' />",
759                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
760                                 sw.GetStringBuilder ().ToString (), "#B");
761
762                         f.Flags1 = FlagEnum_Encoded.e2;
763                         f.Flags2 = FlagEnum_Encoded.e2;
764                         f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
765                         f.Modifiers = MapModifiers.Public;
766                         f.Modifiers2 = MapModifiers.Protected;
767                         f.Modifiers3 = MapModifiers.Protected;
768                         f.Modifiers4 = MapModifiers.Public;
769                         f.Modifiers5 = MapModifiers.Protected;
770                         SerializeEncoded (f, typeof (Field_Encoded));
771                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
772                                 "<?xml version='1.0' encoding='utf-16'?>" +
773                                 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
774                                 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
775                                 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
776                                 " xmlns:q1='some:urn' />",
777                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
778                                 sw.GetStringBuilder ().ToString (), "#C");
779
780                         f.Flags1 = (FlagEnum_Encoded) 1;
781                         f.Flags2 = (FlagEnum_Encoded) 444;
782                         f.Flags3 = (FlagEnum_Encoded) 555;
783                         f.Modifiers = (MapModifiers) 666;
784                         f.Modifiers2 = (MapModifiers) 777;
785                         f.Modifiers3 = (MapModifiers) 0;
786                         f.Modifiers4 = (MapModifiers) 888;
787                         f.Modifiers5 = (MapModifiers) 999;
788                         try {
789                         SerializeEncoded (f, typeof (Field_Encoded));
790                                 Assert.Fail ("#D1");
791                         } catch (InvalidOperationException ex) {
792                                 // There was an error generating the XML document
793                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
794                                 Assert.IsNotNull (ex.Message, "#D3");
795                                 Assert.IsNotNull (ex.InnerException, "#D4");
796
797                                 // Instance validation error: '444' is not a valid value for
798                                 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
799                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
800                                 Assert.IsNotNull (ex.InnerException.Message, "#D6");
801                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
802                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
803                                 Assert.IsNull (ex.InnerException.InnerException, "#D9");
804                         }
805                 }
806
807                 [Test]
808                 public void TestSerializeGroup ()
809                 {
810                         Group myGroup = new Group ();
811                         myGroup.GroupName = ".NET";
812
813                         Byte[] hexByte = new Byte[] { 0x64, 0x32 };
814                         myGroup.GroupNumber = hexByte;
815
816                         DateTime myDate = new DateTime (2002, 5, 2);
817                         myGroup.Today = myDate;
818                         myGroup.PostitiveInt = "10000";
819                         myGroup.IgnoreThis = true;
820                         Car thisCar = (Car) myGroup.myCar ("1234566");
821                         myGroup.MyVehicle = thisCar;
822
823                         SetUpWriter ();
824                         xtw.WriteStartDocument (true);
825                         xtw.WriteStartElement ("Wrapper");
826                         SerializeEncoded (xtw, myGroup, typeof (Group));
827                         xtw.WriteEndElement ();
828                         xtw.Close ();
829
830                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
831                                 "<Wrapper>" +
832                                 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
833                                 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
834                                 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
835                                 "<MyVehicle href='#id2' />" +
836                                 "</Group>" +
837                                 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
838                                 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
839                                 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
840                                 "</Car>" +
841                                 "</Wrapper>",
842                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
843                                 WriterText, "#1");
844
845                         myGroup.GroupName = null;
846                         myGroup.Grouptype = GroupType.B;
847                         myGroup.MyVehicle.licenseNumber = null;
848                         myGroup.MyVehicle.weight = "450";
849
850                         SetUpWriter ();
851                         xtw.WriteStartDocument (true);
852                         xtw.WriteStartElement ("Wrapper");
853                         SerializeEncoded (xtw, myGroup, typeof (Group));
854                         xtw.WriteEndElement ();
855                         xtw.Close ();
856
857                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
858                                 "<Wrapper>" +
859                                 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
860                                 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
861                                 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
862                                 "<MyVehicle href='#id2' />" +
863                                 "</Group>" +
864                                 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
865                                 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
866                                 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
867                                 "</Car>" +
868                                 "</Wrapper>",
869                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
870                                 WriterText, "#2");
871                 }
872
873                 [Test]
874                 public void TestSerializeZeroFlagEnum_InvalidValue ()
875                 {
876                         try {
877                                 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
878                                 Assert.Fail ("#1");
879                         } catch (InvalidOperationException ex) {
880                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
881                                 Assert.IsNotNull (ex.InnerException, "#3");
882                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
883                                 Assert.IsNotNull (ex.InnerException.Message, "#5");
884                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
885                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
886                         }
887                 }
888
889                 [Test]
890                 public void TestSerializeQualifiedName ()
891                 {
892                         Serialize (new XmlQualifiedName ("me", "home.urn"));
893                         Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
894                 }
895
896                 [Test]
897                 public void TestSerializeBytes ()
898                 {
899                         Serialize ((byte) 0xAB);
900                         Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
901
902                         Serialize ((byte) 15);
903                         Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
904                 }
905
906                 [Test]
907                 public void TestSerializeByteArrays ()
908                 {
909                         Serialize (new byte[] { });
910                         Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
911
912                         Serialize (new byte[] { 0xAB, 0xCD });
913                         Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
914                 }
915
916                 [Test]
917                 public void TestSerializeDateTime ()
918                 {
919                         DateTime d = new DateTime ();
920                         Serialize (d);
921
922                         TimeZone tz = TimeZone.CurrentTimeZone;
923                         TimeSpan off = tz.GetUtcOffset (d);
924                         string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
925                         Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
926                 }
927
928                 /*
929                 FIXME
930                  - decimal
931                  - Guid
932                  - XmlNode objects
933                 
934                 [Test]
935                 public void TestSerialize()
936                 {
937                         Serialize();
938                         Assert.AreEqual (WriterText, "");
939                 }
940                 */
941
942                 // test basic class serialization /////////////////////////////////////         
943                 [Test]
944                 public void TestSerializeSimpleClass ()
945                 {
946                         SimpleClass simple = new SimpleClass ();
947                         Serialize (simple);
948                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
949
950                         simple.something = "hello";
951
952                         Serialize (simple);
953                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
954                 }
955
956                 [Test]
957                 public void TestSerializeStringCollection ()
958                 {
959                         StringCollection strings = new StringCollection ();
960                         Serialize (strings);
961                         Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
962
963                         strings.Add ("hello");
964                         strings.Add ("goodbye");
965                         Serialize (strings);
966                         Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
967                 }
968
969                 [Test]
970                 public void TestSerializeOptionalValueTypeContainer ()
971                 {
972                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
973                         XmlAttributes attr;
974                         OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
975
976                         Serialize (optionalValue);
977                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
978                                 "<?xml version='1.0' encoding='utf-16'?>" +
979                                 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
980                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
981                                 sw.ToString (), "#1");
982
983                         attr = new XmlAttributes ();
984
985                         // remove the DefaultValue attribute on the Flags member
986                         overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
987                         // remove the DefaultValue attribute on the Attributes member
988                         overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
989
990                         Serialize (optionalValue, overrides);
991                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
992                                 "<?xml version='1.0' encoding='utf-16'?>" +
993                                 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
994                                 "<Attributes xmlns='{3}'>one four</Attributes>" +
995                                 "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
996                                 AnotherNamespace, ANamespace), sw.ToString (), "#2");
997
998                         optionalValue.FlagsSpecified = true;
999                         Serialize (optionalValue, overrides);
1000                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1001                                 "<?xml version='1.0' encoding='utf-16'?>" +
1002                                 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1003                                 "<Attributes xmlns='{3}'>one four</Attributes>" +
1004                                 "<Flags xmlns='{3}'>one</Flags>" +
1005                                 "</optionalValue>",
1006                                 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
1007                                 ANamespace), sw.ToString (), "#3");
1008                 }
1009                 
1010                 [Test]
1011                 public void TestRoundTripSerializeOptionalValueTypeContainer ()
1012                 {
1013                         var source = new OptionalValueTypeContainer ();
1014                         source.IsEmpty = true;
1015                         source.IsEmptySpecified = true;
1016                         var ser = new XmlSerializer (typeof (OptionalValueTypeContainer));
1017                         string xml;
1018                         using (var t = new StringWriter ()) {
1019                                 ser.Serialize (t, source);
1020                                 xml = t.ToString();
1021                         }
1022                         using (var s = new StringReader (xml)) {
1023                                 var obj = (OptionalValueTypeContainer) ser.Deserialize(s);
1024                                 Assert.AreEqual (source.IsEmpty, obj.IsEmpty, "#1");
1025                                 Assert.AreEqual (source.IsEmptySpecified, obj.IsEmptySpecified, "#2");
1026                         }
1027                 }
1028                 
1029                 [Test]
1030                 public void TestSerializePlainContainer ()
1031                 {
1032                         StringCollectionContainer container = new StringCollectionContainer ();
1033                         Serialize (container);
1034                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
1035
1036                         container.Messages.Add ("hello");
1037                         container.Messages.Add ("goodbye");
1038                         Serialize (container);
1039                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
1040                 }
1041
1042                 [Test]
1043                 public void TestSerializeArrayContainer ()
1044                 {
1045                         ArrayContainer container = new ArrayContainer ();
1046                         Serialize (container);
1047                         Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1048
1049                         container.items = new object[] { 10, 20 };
1050                         Serialize (container);
1051                         Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
1052
1053                         container.items = new object[] { 10, "hello" };
1054                         Serialize (container);
1055                         Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
1056                 }
1057
1058                 [Test]
1059                 public void TestSerializeClassArrayContainer ()
1060                 {
1061                         ClassArrayContainer container = new ClassArrayContainer ();
1062                         Serialize (container);
1063                         Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1064
1065                         SimpleClass simple1 = new SimpleClass ();
1066                         simple1.something = "hello";
1067                         SimpleClass simple2 = new SimpleClass ();
1068                         simple2.something = "hello";
1069                         container.items = new SimpleClass[2];
1070                         container.items[0] = simple1;
1071                         container.items[1] = simple2;
1072                         Serialize (container);
1073                         Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
1074                 }
1075
1076                 // test basic attributes ///////////////////////////////////////////////
1077                 [Test]
1078                 public void TestSerializeSimpleClassWithXmlAttributes ()
1079                 {
1080                         SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1081                         Serialize (simple);
1082                         Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1083
1084                         simple.something = "hello";
1085                         Serialize (simple);
1086                         Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
1087                 }
1088
1089                 // test overrides ///////////////////////////////////////////////////////
1090                 [Test]
1091                 public void TestSerializeSimpleClassWithOverrides ()
1092                 {
1093                         // Also tests XmlIgnore
1094                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1095
1096                         XmlAttributes attr = new XmlAttributes ();
1097                         attr.XmlIgnore = true;
1098                         overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
1099
1100                         SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1101                         simple.something = "hello";
1102                         Serialize (simple, overrides);
1103                         Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1104                 }
1105
1106                 [Test]
1107                 public void TestSerializeSchema ()
1108                 {
1109                         XmlSchema schema = new XmlSchema ();
1110                         schema.Items.Add (new XmlSchemaAttribute ());
1111                         schema.Items.Add (new XmlSchemaAttributeGroup ());
1112                         schema.Items.Add (new XmlSchemaComplexType ());
1113                         schema.Items.Add (new XmlSchemaNotation ());
1114                         schema.Items.Add (new XmlSchemaSimpleType ());
1115                         schema.Items.Add (new XmlSchemaGroup ());
1116                         schema.Items.Add (new XmlSchemaElement ());
1117
1118                         StringWriter sw = new StringWriter ();
1119                         XmlTextWriter xtw = new XmlTextWriter (sw);
1120                         xtw.QuoteChar = '\'';
1121                         xtw.Formatting = Formatting.Indented;
1122                         XmlSerializer xs = new XmlSerializer (schema.GetType ());
1123                         xs.Serialize (xtw, schema);
1124
1125                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1126                                 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1127                                 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1128                                 "  <xsd:attribute />{0}" +
1129                                 "  <xsd:attributeGroup />{0}" +
1130                                 "  <xsd:complexType />{0}" +
1131                                 "  <xsd:notation />{0}" +
1132                                 "  <xsd:simpleType />{0}" +
1133                                 "  <xsd:group />{0}" +
1134                                 "  <xsd:element />{0}" +
1135                                 "</xsd:schema>", Environment.NewLine), sw.ToString ());
1136                 }
1137
1138                 // test xmlText //////////////////////////////////////////////////////////
1139                 [Test]
1140                 public void TestSerializeXmlTextAttribute ()
1141                 {
1142                         SimpleClass simple = new SimpleClass ();
1143                         simple.something = "hello";
1144
1145                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1146                         XmlAttributes attr = new XmlAttributes ();
1147                         overrides.Add (typeof (SimpleClass), "something", attr);
1148
1149                         attr.XmlText = new XmlTextAttribute ();
1150                         Serialize (simple, overrides);
1151                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
1152
1153                         attr.XmlText = new XmlTextAttribute (typeof (string));
1154                         Serialize (simple, overrides);
1155                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
1156
1157                         try {
1158                                 attr.XmlText = new XmlTextAttribute (typeof (byte[]));
1159                                 Serialize (simple, overrides);
1160                                 Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1161                         } catch (InvalidOperationException ex) {
1162                                 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1163                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1164                                 Assert.IsNotNull (ex.Message, "#A3");
1165                                 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
1166                                 Assert.IsNotNull (ex.InnerException, "#A5");
1167
1168                                 // there was an error reflecting field 'something'.
1169                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
1170                                 Assert.IsNotNull (ex.InnerException.Message, "#A7");
1171                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
1172                                 Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
1173
1174                                 // the type for XmlText may not be specified for primitive types.
1175                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
1176                                 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
1177                                 Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
1178                         }
1179
1180                         try {
1181                                 attr.XmlText = new XmlTextAttribute ();
1182                                 attr.XmlText.DataType = "sometype";
1183                                 Serialize (simple, overrides);
1184                                 Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1185                         } catch (InvalidOperationException ex) {
1186                                 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1187                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1188                                 Assert.IsNotNull (ex.Message, "#B3");
1189                                 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
1190                                 Assert.IsNotNull (ex.InnerException, "#B5");
1191
1192                                 // There was an error reflecting field 'something'.
1193                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
1194                                 Assert.IsNotNull (ex.InnerException.Message, "#B7");
1195                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
1196                                 Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
1197
1198                                 //FIXME
1199                                 /*
1200                                 // There was an error reflecting type 'System.String'.
1201                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1202                                 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1203                                 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1204                                 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1205
1206                                 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property. 
1207                                 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1208                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1209                                 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1210                                 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1211                                 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1212                                 */
1213                         }
1214                 }
1215
1216                 // test xmlRoot //////////////////////////////////////////////////////////
1217                 [Test]
1218                 public void TestSerializeXmlRootAttribute ()
1219                 {
1220                         // constructor override & element name
1221                         XmlRootAttribute root = new XmlRootAttribute ();
1222                         root.ElementName = "renamed";
1223
1224                         SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
1225                         Serialize (simpleWithAttributes, root);
1226                         Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1227
1228                         SimpleClass simple = null;
1229                         root.IsNullable = false;
1230                         try {
1231                                 Serialize (simple, root);
1232                                 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1233                         } catch (NullReferenceException) {
1234                         }
1235
1236                         root.IsNullable = true;
1237                         try {
1238                                 Serialize (simple, root);
1239                                 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1240                         } catch (NullReferenceException) {
1241                         }
1242
1243                         simple = new SimpleClass ();
1244                         root.ElementName = null;
1245                         root.Namespace = "some.urn";
1246                         Serialize (simple, root);
1247                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
1248                 }
1249
1250                 [Test]
1251                 public void TestSerializeXmlRootAttributeOnMember ()
1252                 {
1253                         // nested root
1254                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1255                         XmlAttributes childAttr = new XmlAttributes ();
1256                         childAttr.XmlRoot = new XmlRootAttribute ("simple");
1257                         overrides.Add (typeof (SimpleClass), childAttr);
1258
1259                         XmlAttributes attr = new XmlAttributes ();
1260                         attr.XmlRoot = new XmlRootAttribute ("simple");
1261                         overrides.Add (typeof (ClassArrayContainer), attr);
1262
1263                         ClassArrayContainer container = new ClassArrayContainer ();
1264                         container.items = new SimpleClass[1];
1265                         container.items[0] = new SimpleClass ();
1266                         Serialize (container, overrides);
1267                         Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
1268
1269                         // FIXME test data type
1270                 }
1271
1272                 // test XmlAttribute /////////////////////////////////////////////////////
1273                 [Test]
1274                 public void TestSerializeXmlAttributeAttribute ()
1275                 {
1276                         // null
1277                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1278                         XmlAttributes attr = new XmlAttributes ();
1279                         attr.XmlAttribute = new XmlAttributeAttribute ();
1280                         overrides.Add (typeof (SimpleClass), "something", attr);
1281
1282                         SimpleClass simple = new SimpleClass (); ;
1283                         Serialize (simple, overrides);
1284                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1285
1286                         // regular
1287                         simple.something = "hello";
1288                         Serialize (simple, overrides);
1289                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
1290
1291                         // AttributeName
1292                         attr.XmlAttribute.AttributeName = "somethingelse";
1293                         Serialize (simple, overrides);
1294                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
1295
1296                         // Type
1297                         // FIXME this should work, shouldnt it?
1298                         // attr.XmlAttribute.Type = typeof(string);
1299                         // Serialize(simple, overrides);
1300                         // Assert(WriterText.EndsWith(" something='hello' />"));
1301
1302                         // Namespace
1303                         attr.XmlAttribute.Namespace = "some:urn";
1304                         Serialize (simple, overrides);
1305                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
1306
1307                         // FIXME DataType
1308                         // FIXME XmlSchemaForm Form
1309
1310                         // FIXME write XmlQualifiedName as attribute
1311                 }
1312
1313                 // test XmlElement ///////////////////////////////////////////////////////
1314                 [Test]
1315                 public void TestSerializeXmlElementAttribute ()
1316                 {
1317                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1318                         XmlAttributes attr = new XmlAttributes ();
1319                         XmlElementAttribute element = new XmlElementAttribute ();
1320                         attr.XmlElements.Add (element);
1321                         overrides.Add (typeof (SimpleClass), "something", attr);
1322
1323                         // null
1324                         SimpleClass simple = new SimpleClass (); ;
1325                         Serialize (simple, overrides);
1326                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1327
1328                         // not null
1329                         simple.something = "hello";
1330                         Serialize (simple, overrides);
1331                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
1332
1333                         //ElementName
1334                         element.ElementName = "saying";
1335                         Serialize (simple, overrides);
1336                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
1337
1338                         //IsNullable
1339                         element.IsNullable = false;
1340                         simple.something = null;
1341                         Serialize (simple, overrides);
1342                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
1343
1344                         element.IsNullable = true;
1345                         simple.something = null;
1346                         Serialize (simple, overrides);
1347                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
1348
1349                         //Namespace
1350                         element.ElementName = null;
1351                         element.IsNullable = false;
1352                         element.Namespace = "some:urn";
1353                         simple.something = "hello";
1354                         Serialize (simple, overrides);
1355                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
1356
1357                         //FIXME DataType
1358                         //FIXME Form
1359                         //FIXME Type
1360                 }
1361
1362                 // test XmlElementAttribute with arrays and collections //////////////////
1363                 [Test]
1364                 public void TestSerializeCollectionWithXmlElementAttribute ()
1365                 {
1366                         // the rule is:
1367                         // if no type is specified or the specified type 
1368                         //    matches the contents of the collection, 
1369                         //    serialize each element in an element named after the member.
1370                         // if the type does not match, or matches the collection itself,
1371                         //    create a base wrapping element for the member, and then
1372                         //    wrap each collection item in its own wrapping element based on type.
1373
1374                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1375                         XmlAttributes attr = new XmlAttributes ();
1376                         XmlElementAttribute element = new XmlElementAttribute ();
1377                         attr.XmlElements.Add (element);
1378                         overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
1379
1380                         // empty collection & no type info in XmlElementAttribute
1381                         StringCollectionContainer container = new StringCollectionContainer ();
1382                         Serialize (container, overrides);
1383                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1384
1385                         // non-empty collection & no type info in XmlElementAttribute
1386                         container.Messages.Add ("hello");
1387                         Serialize (container, overrides);
1388                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
1389
1390                         // non-empty collection & only type info in XmlElementAttribute
1391                         element.Type = typeof (StringCollection);
1392                         Serialize (container, overrides);
1393                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
1394
1395                         // non-empty collection & only type info in XmlElementAttribute
1396                         element.Type = typeof (string);
1397                         Serialize (container, overrides);
1398                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4");
1399
1400                         // two elements
1401                         container.Messages.Add ("goodbye");
1402                         element.Type = null;
1403                         Serialize (container, overrides);
1404                         Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText, "#5");
1405                 }
1406
1407                 // test DefaultValue /////////////////////////////////////////////////////
1408                 [Test]
1409                 public void TestSerializeDefaultValueAttribute ()
1410                 {
1411                         XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1412
1413                         XmlAttributes attr = new XmlAttributes ();
1414                         string defaultValueInstance = "nothing";
1415                         attr.XmlDefaultValue = defaultValueInstance;
1416                         overrides.Add (typeof (SimpleClass), "something", attr);
1417
1418                         // use the default
1419                         SimpleClass simple = new SimpleClass ();
1420                         Serialize (simple, overrides);
1421                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1422
1423                         // same value as default
1424                         simple.something = defaultValueInstance;
1425                         Serialize (simple, overrides);
1426                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1427
1428                         // some other value
1429                         simple.something = "hello";
1430                         Serialize (simple, overrides);
1431                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3");
1432
1433                         overrides = new XmlAttributeOverrides ();
1434                         attr = new XmlAttributes ();
1435                         attr.XmlAttribute = new XmlAttributeAttribute ();
1436                         attr.XmlDefaultValue = defaultValueInstance;
1437                         overrides.Add (typeof (SimpleClass), "something", attr);
1438
1439                         // use the default
1440                         simple = new SimpleClass ();
1441                         Serialize (simple, overrides);
1442                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1443
1444                         // same value as default
1445                         simple.something = defaultValueInstance;
1446                         Serialize (simple, overrides);
1447                         Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
1448
1449                         // some other value
1450                         simple.something = "hello";
1451                         Serialize (simple, overrides);
1452                         Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
1453
1454                         overrides = new XmlAttributeOverrides ();
1455                         attr = new XmlAttributes ();
1456                         attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1457                         overrides.Add (typeof (TestDefault), "flagencoded", attr);
1458
1459                         // use the default
1460                         TestDefault testDefault = new TestDefault ();
1461                         Serialize (testDefault);
1462                         Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
1463
1464                         // use the default with overrides
1465                         Serialize (testDefault, overrides);
1466                         Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
1467
1468                         overrides = new XmlAttributeOverrides ();
1469                         attr = new XmlAttributes ();
1470                         attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1471                         attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
1472                         overrides.Add (typeof (TestDefault), "flagencoded", attr);
1473
1474                         // use the default with overrides
1475                         Serialize (testDefault, overrides);
1476                         Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
1477
1478                         // use the default with overrides and default namspace
1479                         Serialize (testDefault, overrides, AnotherNamespace);
1480                         Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
1481
1482                         // non-default values
1483                         testDefault.strDefault = "Some Text";
1484                         testDefault.boolT = false;
1485                         testDefault.boolF = true;
1486                         testDefault.decimalval = 20m;
1487                         testDefault.flag = FlagEnum.e2;
1488                         testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1489                         Serialize (testDefault);
1490                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1491                                 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1492                                 "    <strDefault>Some Text</strDefault>" +
1493                                 "    <boolT>false</boolT>" +
1494                                 "    <boolF>true</boolF>" +
1495                                 "    <decimalval>20</decimalval>" +
1496                                 "    <flag>two</flag>" +
1497                                 "    <flagencoded>e1 e2</flagencoded>" +
1498                                 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1499                                 WriterText, "#C5");
1500
1501                         Serialize (testDefault, overrides);
1502                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1503                                 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1504                                 "    <strDefault>Some Text</strDefault>" +
1505                                 "    <boolT>false</boolT>" +
1506                                 "    <boolF>true</boolF>" +
1507                                 "    <decimalval>20</decimalval>" +
1508                                 "    <flag>two</flag>" +
1509                                 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1510                                 WriterText, "#C6");
1511
1512                         Serialize (testDefault, overrides, AnotherNamespace);
1513                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1514                                 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1515                                 "    <strDefault>Some Text</strDefault>" +
1516                                 "    <boolT>false</boolT>" +
1517                                 "    <boolF>true</boolF>" +
1518                                 "    <decimalval>20</decimalval>" +
1519                                 "    <flag>two</flag>" +
1520                                 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1521                                 WriterText, "#C7");
1522
1523                         attr = new XmlAttributes ();
1524                         XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
1525                         xmlType.Namespace = "yetanother:urn";
1526                         attr.XmlType = xmlType;
1527                         overrides.Add (typeof (FlagEnum_Encoded), attr);
1528
1529                         Serialize (testDefault, overrides, AnotherNamespace);
1530                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1531                                 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1532                                 "    <strDefault>Some Text</strDefault>" +
1533                                 "    <boolT>false</boolT>" +
1534                                 "    <boolF>true</boolF>" +
1535                                 "    <decimalval>20</decimalval>" +
1536                                 "    <flag>two</flag>" +
1537                                 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1538                                 WriterText, "#C8");
1539
1540                         attr = new XmlAttributes ();
1541                         attr.XmlType = new XmlTypeAttribute ("testDefault");
1542                         overrides.Add (typeof (TestDefault), attr);
1543
1544                         Serialize (testDefault, overrides, AnotherNamespace);
1545                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1546                                 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1547                                 "    <strDefault>Some Text</strDefault>" +
1548                                 "    <boolT>false</boolT>" +
1549                                 "    <boolF>true</boolF>" +
1550                                 "    <decimalval>20</decimalval>" +
1551                                 "    <flag>two</flag>" +
1552                                 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1553                                 AnotherNamespace)), WriterText, "#C9");
1554                 }
1555
1556                 [Test]
1557                 public void TestSerializeDefaultValueAttribute_Encoded ()
1558                 {
1559                         SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
1560                         SoapAttributes attr = new SoapAttributes ();
1561                         attr.SoapAttribute = new SoapAttributeAttribute ();
1562                         string defaultValueInstance = "nothing";
1563                         attr.SoapDefaultValue = defaultValueInstance;
1564                         overrides.Add (typeof (SimpleClass), "something", attr);
1565
1566                         // use the default
1567                         SimpleClass simple = new SimpleClass ();
1568                         SerializeEncoded (simple, overrides);
1569                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1570
1571                         // same value as default
1572                         simple.something = defaultValueInstance;
1573                         SerializeEncoded (simple, overrides);
1574                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1575
1576                         // some other value
1577                         simple.something = "hello";
1578                         SerializeEncoded (simple, overrides);
1579                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
1580
1581                         attr.SoapAttribute = null;
1582                         attr.SoapElement = new SoapElementAttribute ();
1583
1584                         // use the default
1585                         simple = new SimpleClass ();
1586                         SerializeEncoded (simple, overrides);
1587                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1588
1589                         // same value as default
1590                         simple.something = defaultValueInstance;
1591                         SerializeEncoded (simple, overrides);
1592                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
1593
1594                         // some other value
1595                         simple.something = "hello";
1596                         SerializeEncoded (simple, overrides);
1597                         Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
1598
1599                         overrides = new SoapAttributeOverrides ();
1600                         attr = new SoapAttributes ();
1601                         attr.SoapElement = new SoapElementAttribute ("flagenc");
1602                         overrides.Add (typeof (TestDefault), "flagencoded", attr);
1603
1604                         // use the default (from MS KB325691)
1605                         TestDefault testDefault = new TestDefault ();
1606                         SerializeEncoded (testDefault);
1607                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1608                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1609                                 "    <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1610                                 "    <boolT xsi:type='xsd:boolean'>true</boolT>" +
1611                                 "    <boolF xsi:type='xsd:boolean'>false</boolF>" +
1612                                 "    <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1613                                 "    <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1614                                 "    <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1615                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1616                                 WriterText, "#C1");
1617
1618                         SerializeEncoded (testDefault, overrides);
1619                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1620                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1621                                 "    <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1622                                 "    <boolT xsi:type='xsd:boolean'>true</boolT>" +
1623                                 "    <boolF xsi:type='xsd:boolean'>false</boolF>" +
1624                                 "    <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1625                                 "    <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1626                                 "    <flagenc xsi:type='flagenum'>one four</flagenc>" +
1627                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1628                                 WriterText, "#C2");
1629
1630                         SerializeEncoded (testDefault, overrides, AnotherNamespace);
1631                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1632                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1633                                 "    <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1634                                 "    <boolT xsi:type='xsd:boolean'>true</boolT>" +
1635                                 "    <boolF xsi:type='xsd:boolean'>false</boolF>" +
1636                                 "    <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1637                                 "    <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1638                                 "    <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1639                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1640                                 AnotherNamespace)), WriterText, "#C3");
1641
1642                         // non-default values
1643                         testDefault.strDefault = "Some Text";
1644                         testDefault.boolT = false;
1645                         testDefault.boolF = true;
1646                         testDefault.decimalval = 20m;
1647                         testDefault.flag = FlagEnum.e2;
1648                         testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1649                         SerializeEncoded (testDefault);
1650                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1651                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1652                                 "    <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1653                                 "    <boolT xsi:type='xsd:boolean'>false</boolT>" +
1654                                 "    <boolF xsi:type='xsd:boolean'>true</boolF>" +
1655                                 "    <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1656                                 "    <flag xsi:type='FlagEnum'>e2</flag>" +
1657                                 "    <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1658                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1659                                 WriterText, "#C4");
1660
1661                         SerializeEncoded (testDefault, overrides);
1662                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1663                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1664                                 "    <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1665                                 "    <boolT xsi:type='xsd:boolean'>false</boolT>" +
1666                                 "    <boolF xsi:type='xsd:boolean'>true</boolF>" +
1667                                 "    <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1668                                 "    <flag xsi:type='FlagEnum'>e2</flag>" +
1669                                 "    <flagenc xsi:type='flagenum'>one two</flagenc>" +
1670                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1671                                 WriterText, "#C5");
1672
1673                         attr = new SoapAttributes ();
1674                         attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
1675                         overrides.Add (typeof (FlagEnum_Encoded), attr);
1676
1677                         SerializeEncoded (testDefault, overrides, AnotherNamespace);
1678                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1679                                 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1680                                 "    <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1681                                 "    <boolT xsi:type='xsd:boolean'>false</boolT>" +
1682                                 "    <boolF xsi:type='xsd:boolean'>true</boolF>" +
1683                                 "    <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1684                                 "    <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1685                                 "    <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1686                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1687                                 AnotherNamespace)), WriterText, "#C6");
1688
1689                         attr = new SoapAttributes ();
1690                         attr.SoapType = new SoapTypeAttribute ("testDefault");
1691                         overrides.Add (typeof (TestDefault), attr);
1692
1693                         SerializeEncoded (testDefault, overrides, AnotherNamespace);
1694                         Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1695                                 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1696                                 "    <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1697                                 "    <boolT xsi:type='xsd:boolean'>false</boolT>" +
1698                                 "    <boolF xsi:type='xsd:boolean'>true</boolF>" +
1699                                 "    <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1700                                 "    <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1701                                 "    <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1702                                 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1703                                 AnotherNamespace)), WriterText, "#C7");
1704                 }
1705
1706                 // test XmlEnum //////////////////////////////////////////////////////////
1707                 [Test]
1708                 public void TestSerializeXmlEnumAttribute ()
1709                 {
1710                         Serialize (XmlSchemaForm.Qualified);
1711                         Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
1712
1713                         Serialize (XmlSchemaForm.Unqualified);
1714                         Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
1715                 }
1716
1717                 [Test]
1718                 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1719                 {
1720                         // technically XmlSchemaForm.None has an XmlIgnore attribute,
1721                         // but it is not being serialized as a member.
1722
1723                         try {
1724                                 Serialize (XmlSchemaForm.None);
1725                                 Assert.Fail ("#1");
1726                         } catch (InvalidOperationException ex) {
1727                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1728                                 Assert.IsNotNull (ex.InnerException, "#3");
1729                                 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
1730                                 Assert.IsNotNull (ex.InnerException.Message, "#5");
1731                                 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
1732                                 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
1733                         }
1734                 }
1735
1736                 [Test]
1737                 public void TestSerializeXmlNodeArray ()
1738                 {
1739                         XmlDocument doc = new XmlDocument ();
1740                         Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1741                         Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1742                 }
1743
1744                 [Test]
1745                 public void TestSerializeXmlNodeArray2 ()
1746                 {
1747                         XmlDocument doc = new XmlDocument ();
1748                         Serialize (new XmlNode[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1749                         Assert.AreEqual (Infoset (String.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
1750                 }
1751
1752                 [Test]
1753                 [ExpectedException (typeof (InvalidOperationException))]
1754                 [Category ("MobileNotWorking")]
1755                 public void TestSerializeXmlNodeArrayIncludesAttribute ()
1756                 {
1757                         XmlDocument doc = new XmlDocument ();
1758                         Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1759                 }
1760
1761                 [Test]
1762                 public void TestSerializeXmlElementArray ()
1763                 {
1764                         XmlDocument doc = new XmlDocument ();
1765                         Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1766                         Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1767                 }
1768
1769                 [Test]
1770                 [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
1771                 public void TestSerializeGenericListOfNode ()
1772                 {
1773                         XmlDocument doc = new XmlDocument ();
1774                         Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1775                         Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1776                 }
1777
1778                 [Test]
1779                 [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
1780                 public void TestSerializeGenericListOfElement ()
1781                 {
1782                         XmlDocument doc = new XmlDocument ();
1783                         Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1784                         Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1785                 }
1786                 [Test]
1787                 public void TestSerializeXmlDocument ()
1788                 {
1789                         XmlDocument doc = new XmlDocument ();
1790                         doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
1791                         Serialize (doc, typeof (XmlDocument));
1792                         Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
1793                                 sw.GetStringBuilder ().ToString ());
1794                 }
1795
1796                 [Test]
1797                 public void TestSerializeXmlElement ()
1798                 {
1799                         XmlDocument doc = new XmlDocument ();
1800                         Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
1801                         Assert.AreEqual (Infoset ("<elem/>"), WriterText);
1802                 }
1803
1804                 [Test]
1805                 public void TestSerializeXmlElementSubclass ()
1806                 {
1807                         XmlDocument doc = new XmlDocument ();
1808                         Serialize (new MyElem (doc), typeof (XmlElement));
1809                         Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
1810
1811                         Serialize (new MyElem (doc), typeof (MyElem));
1812                         Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
1813                 }
1814
1815                 [Test]
1816                 public void TestSerializeXmlCDataSection ()
1817                 {
1818                         XmlDocument doc = new XmlDocument ();
1819                         CDataContainer c = new CDataContainer ();
1820                         c.cdata = doc.CreateCDataSection ("data section contents");
1821                         Serialize (c);
1822                         Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
1823                 }
1824
1825                 [Test]
1826                 public void TestSerializeXmlNode ()
1827                 {
1828                         XmlDocument doc = new XmlDocument ();
1829                         NodeContainer c = new NodeContainer ();
1830                         c.node = doc.CreateTextNode ("text");
1831                         Serialize (c);
1832                         Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
1833                 }
1834
1835                 [Test]
1836                 public void TestSerializeChoice ()
1837                 {
1838                         Choices ch = new Choices ();
1839                         ch.MyChoice = "choice text";
1840                         ch.ItemType = ItemChoiceType.ChoiceZero;
1841                         Serialize (ch);
1842                         Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText, "#1");
1843                         ch.ItemType = ItemChoiceType.StrangeOne;
1844                         Serialize (ch);
1845                         Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText, "#2");
1846                         ch.ItemType = ItemChoiceType.ChoiceTwo;
1847                         Serialize (ch);
1848                         Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText, "#3");
1849                 }
1850
1851                 [Test]
1852                 public void TestSerializeNamesWithSpaces ()
1853                 {
1854                         TestSpace ts = new TestSpace ();
1855                         ts.elem = 4;
1856                         ts.attr = 5;
1857                         Serialize (ts);
1858                         Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
1859                 }
1860
1861                 [Test]
1862                 public void TestSerializeReadOnlyProps ()
1863                 {
1864                         ReadOnlyProperties ts = new ReadOnlyProperties ();
1865                         Serialize (ts);
1866                         Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1867                 }
1868
1869                 [Test]
1870                 public void TestSerializeReadOnlyListProp ()
1871                 {
1872                         ReadOnlyListProperty ts = new ReadOnlyListProperty ();
1873                         Serialize (ts);
1874                         Assert.AreEqual (Infoset ("<ReadOnlyListProperty xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><StrList><string>listString1</string><string>listString2</string></StrList></ReadOnlyListProperty>"), WriterText);
1875                 }
1876
1877
1878                 [Test]
1879                 public void TestSerializeIList ()
1880                 {
1881                         clsPerson k = new clsPerson ();
1882                         k.EmailAccounts = new ArrayList ();
1883                         k.EmailAccounts.Add ("a");
1884                         k.EmailAccounts.Add ("b");
1885                         Serialize (k);
1886                         Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
1887                 }
1888
1889                 [Test]
1890                 public void TestSerializeArrayEnc ()
1891                 {
1892                         SoapReflectionImporter imp = new SoapReflectionImporter ();
1893                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1894                         XmlSerializer ser = new XmlSerializer (map);
1895                         StringWriter sw = new StringWriter ();
1896                         XmlTextWriter tw = new XmlTextWriter (sw);
1897                         tw.WriteStartElement ("aa");
1898                         ser.Serialize (tw, new ArrayClass ());
1899                         tw.WriteEndElement ();
1900                 }
1901
1902                 [Test] // bug #76049
1903                 public void TestIncludeType ()
1904                 {
1905                         XmlReflectionImporter imp = new XmlReflectionImporter ();
1906                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1907                         imp.IncludeType (typeof (TestSpace));
1908                         XmlSerializer ser = new XmlSerializer (map);
1909                         ser.Serialize (new StringWriter (), new TestSpace ());
1910                 }
1911
1912                 [Test]
1913                 public void TestSerializeChoiceArray ()
1914                 {
1915                         CompositeValueType v = new CompositeValueType ();
1916                         v.Init ();
1917                         Serialize (v);
1918                         Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
1919                 }
1920
1921                 [Test]
1922                 public void TestArrayAttributeWithDataType ()
1923                 {
1924                         Serialize (new ArrayAttributeWithType ());
1925                         string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1926                         res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1927                         Assert.AreEqual (Infoset (res), WriterText);
1928                 }
1929
1930                 [Test]
1931                 public void TestSubclassElementType ()
1932                 {
1933                         SubclassTestContainer c = new SubclassTestContainer ();
1934                         c.data = new SubclassTestSub ();
1935                         Serialize (c);
1936
1937                         string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1938                         res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1939                         Assert.AreEqual (Infoset (res), WriterText);
1940                 }
1941
1942                 [Test] // Covers #36829
1943                 public void TestSubclassElementList ()
1944                 {
1945                         var o = new SubclassTestList () { Items = new List<object> () { new SubclassTestSub () } };
1946                         Serialize (o);
1947
1948                         string res = "<SubclassTestList xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1949                         res += "<b xsi:type=\"SubclassTestSub\"/></SubclassTestList>";
1950                         Assert.AreEqual (Infoset (res), WriterText);
1951                 }
1952
1953                 [Test]
1954                 [ExpectedException (typeof (InvalidOperationException))]
1955                 public void TestArrayAttributeWithWrongDataType ()
1956                 {
1957                         Serialize (new ArrayAttributeWithWrongType ());
1958                 }
1959
1960                 [Test]
1961                 [Category ("NotWorking")]
1962                 public void TestSerializePrimitiveTypesContainer ()
1963                 {
1964                         Serialize (new PrimitiveTypesContainer ());
1965                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1966                                 "<?xml version='1.0' encoding='utf-16'?>" +
1967                                 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
1968                                 "<Number>2004</Number>" +
1969                                 "<Name>some name</Name>" +
1970                                 "<Index>56</Index>" +
1971                                 "<Password>8w8=</Password>" +
1972                                 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
1973                                 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
1974                                 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
1975
1976                         SerializeEncoded (new PrimitiveTypesContainer ());
1977                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1978                                 "<?xml version='1.0' encoding='utf-16'?>" +
1979                                 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
1980                                 "<Number xsi:type='xsd:int'>2004</Number>" +
1981                                 "<Name xsi:type='xsd:string'>some name</Name>" +
1982                                 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1983                                 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1984                                 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
1985                                 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
1986                                 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
1987                                 sw.ToString (), "#2");
1988                 }
1989
1990                 [Test]
1991                 public void TestSchemaForm ()
1992                 {
1993                         TestSchemaForm1 t1 = new TestSchemaForm1 ();
1994                         t1.p1 = new PrintTypeResponse ();
1995                         t1.p1.Init ();
1996                         t1.p2 = new PrintTypeResponse ();
1997                         t1.p2.Init ();
1998
1999                         TestSchemaForm2 t2 = new TestSchemaForm2 ();
2000                         t2.p1 = new PrintTypeResponse ();
2001                         t2.p1.Init ();
2002                         t2.p2 = new PrintTypeResponse ();
2003                         t2.p2.Init ();
2004
2005                         Serialize (t1);
2006                         string res = "";
2007                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2008                         res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2009                         res += "  <p1>";
2010                         res += "    <result>";
2011                         res += "      <data>data1</data>";
2012                         res += "    </result>";
2013                         res += "    <intern xmlns=\"urn:responseTypes\">";
2014                         res += "      <result xmlns=\"\">";
2015                         res += "        <data>data2</data>";
2016                         res += "      </result>";
2017                         res += "    </intern>";
2018                         res += "  </p1>";
2019                         res += "  <p2 xmlns=\"urn:oo\">";
2020                         res += "    <result xmlns=\"\">";
2021                         res += "      <data>data1</data>";
2022                         res += "    </result>";
2023                         res += "    <intern xmlns=\"urn:responseTypes\">";
2024                         res += "      <result xmlns=\"\">";
2025                         res += "        <data>data2</data>";
2026                         res += "      </result>";
2027                         res += "    </intern>";
2028                         res += "  </p2>";
2029                         res += "</TestSchemaForm1>";
2030                         Assert.AreEqual (Infoset (res), WriterText);
2031
2032                         Serialize (t2);
2033                         res = "";
2034                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2035                         res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2036                         res += "  <p1 xmlns=\"urn:testForm\">";
2037                         res += "    <result xmlns=\"\">";
2038                         res += "      <data>data1</data>";
2039                         res += "    </result>";
2040                         res += "    <intern xmlns=\"urn:responseTypes\">";
2041                         res += "      <result xmlns=\"\">";
2042                         res += "        <data>data2</data>";
2043                         res += "      </result>";
2044                         res += "    </intern>";
2045                         res += "  </p1>";
2046                         res += "  <p2 xmlns=\"urn:oo\">";
2047                         res += "    <result xmlns=\"\">";
2048                         res += "      <data>data1</data>";
2049                         res += "    </result>";
2050                         res += "    <intern xmlns=\"urn:responseTypes\">";
2051                         res += "      <result xmlns=\"\">";
2052                         res += "        <data>data2</data>";
2053                         res += "      </result>";
2054                         res += "    </intern>";
2055                         res += "  </p2>";
2056                         res += "</TestSchemaForm2>";
2057                         Assert.AreEqual (Infoset (res), WriterText);
2058
2059                         XmlReflectionImporter imp = new XmlReflectionImporter ();
2060                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2061                         Serialize (t1, map);
2062                         res = "";
2063                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2064                         res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2065                         res += "  <p1>";
2066                         res += "    <result xmlns=\"\">";
2067                         res += "      <data>data1</data>";
2068                         res += "    </result>";
2069                         res += "    <intern xmlns=\"urn:responseTypes\">";
2070                         res += "      <result xmlns=\"\">";
2071                         res += "        <data>data2</data>";
2072                         res += "      </result>";
2073                         res += "    </intern>";
2074                         res += "  </p1>";
2075                         res += "  <p2 xmlns=\"urn:oo\">";
2076                         res += "    <result xmlns=\"\">";
2077                         res += "      <data>data1</data>";
2078                         res += "    </result>";
2079                         res += "    <intern xmlns=\"urn:responseTypes\">";
2080                         res += "      <result xmlns=\"\">";
2081                         res += "        <data>data2</data>";
2082                         res += "      </result>";
2083                         res += "    </intern>";
2084                         res += "  </p2>";
2085                         res += "</TestSchemaForm1>";
2086                         Assert.AreEqual (Infoset (res), WriterText);
2087
2088                         imp = new XmlReflectionImporter ();
2089                         map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2090                         Serialize (t2, map);
2091                         res = "";
2092                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2093                         res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2094                         res += "  <p1 xmlns=\"urn:testForm\">";
2095                         res += "    <result xmlns=\"\">";
2096                         res += "      <data>data1</data>";
2097                         res += "    </result>";
2098                         res += "    <intern xmlns=\"urn:responseTypes\">";
2099                         res += "      <result xmlns=\"\">";
2100                         res += "        <data>data2</data>";
2101                         res += "      </result>";
2102                         res += "    </intern>";
2103                         res += "  </p1>";
2104                         res += "  <p2 xmlns=\"urn:oo\">";
2105                         res += "    <result xmlns=\"\">";
2106                         res += "      <data>data1</data>";
2107                         res += "    </result>";
2108                         res += "    <intern xmlns=\"urn:responseTypes\">";
2109                         res += "      <result xmlns=\"\">";
2110                         res += "        <data>data2</data>";
2111                         res += "      </result>";
2112                         res += "    </intern>";
2113                         res += "  </p2>";
2114                         res += "</TestSchemaForm2>";
2115                         Assert.AreEqual (Infoset (res), WriterText);
2116                 }
2117
2118                 [Test] // bug #78536
2119                 public void CDataTextNodes ()
2120                 {
2121                         XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2122                         ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2123                         string xml = @"<CDataTextNodesType>
2124   <foo><![CDATA[
2125 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2126 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2127 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2128         ]]></foo>
2129 </CDataTextNodesType>";
2130                         ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2131                 }
2132
2133 #if !MOBILE
2134                 [Test]
2135                 public void GenerateSerializerGenerics ()
2136                 {
2137                         XmlReflectionImporter imp = new XmlReflectionImporter ();
2138                         Type type = typeof (List<int>);
2139                         XmlSerializer.GenerateSerializer (
2140                                 new Type [] {type},
2141                                 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2142                 }
2143 #endif
2144
2145                 [Test]
2146                 public void Nullable ()
2147                 {
2148                         XmlSerializer ser = new XmlSerializer (typeof (int?));
2149                         int? nullableType = 5;
2150                         sw = new StringWriter ();
2151                         xtw = new XmlTextWriter (sw);
2152                         ser.Serialize (xtw, nullableType);
2153                         xtw.Close ();
2154                         string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2155                         Assert.AreEqual (Infoset (expected), WriterText);
2156                         int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2157                         Assert.AreEqual (5, i);
2158                 }
2159
2160                 [Test]
2161                 public void NullableEnums ()
2162                 {
2163                         WithNulls w = new WithNulls ();
2164                         XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2165                         StringWriter tw = new StringWriter ();
2166                         ser.Serialize (tw, w);
2167
2168                         string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2169                                 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2170                                         "<nint xsi:nil='true' />" +
2171                                         "<nenum xsi:nil='true' />" +
2172                                         "<ndate xsi:nil='true' />" +
2173                                         "</WithNulls>";
2174                         
2175                         Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2176                         
2177                         StringReader sr = new StringReader (tw.ToString ());
2178                         w = (WithNulls) ser.Deserialize (sr);
2179                         
2180                         Assert.IsFalse (w.nint.HasValue);
2181                         Assert.IsFalse (w.nenum.HasValue);
2182                         Assert.IsFalse (w.ndate.HasValue);
2183                         
2184                         DateTime t = new DateTime (2008,4,1);
2185                         w.nint = 4;
2186                         w.ndate = t;
2187                         w.nenum = TestEnumWithNulls.bb;
2188                         
2189                         tw = new StringWriter ();
2190                         ser.Serialize (tw, w);
2191                         
2192                         expected = "<?xml version='1.0' encoding='utf-16'?>" +
2193                                 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2194                                         "<nint>4</nint>" +
2195                                         "<nenum>bb</nenum>" +
2196                                         "<ndate>2008-04-01T00:00:00</ndate>" +
2197                                         "</WithNulls>";
2198                         
2199                         Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2200                         
2201                         sr = new StringReader (tw.ToString ());
2202                         w = (WithNulls) ser.Deserialize (sr);
2203                         
2204                         Assert.IsTrue (w.nint.HasValue);
2205                         Assert.IsTrue (w.nenum.HasValue);
2206                         Assert.IsTrue (w.ndate.HasValue);
2207                         Assert.AreEqual (4, w.nint.Value);
2208                         Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2209                         Assert.AreEqual (t, w.ndate.Value);
2210                 }
2211
2212                 [Test]
2213                 public void SerializeBase64Binary()
2214                 {
2215                         XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2216                         sw = new StringWriter ();
2217                         XmlTextWriter xtw = new XmlTextWriter (sw);
2218                         ser.Serialize (xtw, new Base64Binary ());
2219                         xtw.Close ();
2220                         string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
2221                         Assert.AreEqual (Infoset (expected), WriterText);
2222                         Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2223                         Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2224                 }
2225
2226                 [Test] // bug #79989, #79990
2227                 public void SerializeHexBinary ()
2228                 {
2229                         XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2230                         sw = new StringWriter ();
2231                         XmlTextWriter xtw = new XmlTextWriter (sw);
2232                         ser.Serialize (xtw, new HexBinary ());
2233                         xtw.Close ();
2234                         string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
2235                         Assert.AreEqual (Infoset (expected), WriterText);
2236                         HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2237                         Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2238                 }
2239
2240                 [Test]
2241                 [ExpectedException (typeof (InvalidOperationException))]
2242                 public void XmlArrayAttributeOnInt ()
2243                 {
2244                         new XmlSerializer (typeof (XmlArrayOnInt));
2245                 }
2246
2247                 [Test]
2248                 [Category ("MobileNotWorking")]
2249                 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2250                 {
2251                         new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2252                 }
2253
2254                 [Test]
2255                 [Category ("MobileNotWorking")]
2256                 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2257                 {
2258                         new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2259                 }
2260
2261                 [Test] // bug #78042
2262                 public void XmlArrayAttributeOnArray ()
2263                 {
2264                         XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2265                         sw = new StringWriter ();
2266                         XmlTextWriter xtw = new XmlTextWriter (sw);
2267                         ser.Serialize (xtw, new XmlArrayOnArray ());
2268                         xtw.Close ();
2269                         string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
2270                         Assert.AreEqual (Infoset (expected), WriterText);
2271                 }
2272
2273                 [Test]
2274                 public void XmlArrayAttributeOnCollection ()
2275                 {
2276                         XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2277                         XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2278                         inst.Sane.Add ("abc");
2279                         inst.Sane.Add (1);
2280                         sw = new StringWriter ();
2281                         XmlTextWriter xtw = new XmlTextWriter (sw);
2282                         ser.Serialize (xtw, inst);
2283                         xtw.Close ();
2284                         string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
2285                         Assert.AreEqual (Infoset (expected), WriterText);
2286                 }
2287
2288                 [Test] // bug #338705
2289                 public void SerializeTimeSpan ()
2290                 {
2291                         // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2292                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2293                         ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2294                 }
2295
2296                 [Test]
2297                 public void SerializeDurationToString ()
2298                 {
2299                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2300                         ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2301                 }
2302
2303                 [Test]
2304                 [ExpectedException (typeof (InvalidOperationException))]
2305                 public void SerializeDurationToTimeSpan ()
2306                 {
2307                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2308                         ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2309                 }
2310
2311                 [Test]
2312                 [ExpectedException (typeof (InvalidOperationException))]
2313                 public void SerializeInvalidDataType ()
2314                 {
2315                         XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2316                         ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2317                 }
2318
2319                 [Test]
2320                 public void SerializeErrorneousIXmlSerializable ()
2321                 {
2322                         Serialize (new ErrorneousGetSchema ());
2323                         Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2324                 }
2325
2326                 [Test]
2327                 public void DateTimeRoundtrip ()
2328                 {
2329                         // bug #337729
2330                         XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2331                         StringWriter sw = new StringWriter ();
2332                         ser.Serialize (sw, DateTime.UtcNow);
2333                         DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2334                         Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2335                 }
2336
2337                 [Test]
2338                 public void SupportIXmlSerializableImplicitlyConvertible ()
2339                 {
2340                         XmlAttributes attrs = new XmlAttributes ();
2341                         XmlElementAttribute attr = new XmlElementAttribute ();
2342                         attr.ElementName = "XmlSerializable";
2343                         attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2344                         attrs.XmlElements.Add (attr);
2345                         XmlAttributeOverrides attrOverrides = new
2346                         XmlAttributeOverrides ();
2347                         attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2348
2349                         XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2350                         new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2351                 }
2352
2353                 [Test] // bug #566370
2354                 public void SerializeEnumWithCSharpKeyword ()
2355                 {
2356                         var ser = new XmlSerializer (typeof (DoxCompoundKind));
2357                         for (int i = 0; i < 100; i++) // test serialization code generator
2358                                 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2359                 }
2360
2361                 public enum DoxCompoundKind
2362                 {
2363                         [XmlEnum("class")]
2364                         @class,
2365                         [XmlEnum("struct")]
2366                         @struct,
2367                         union,
2368                         [XmlEnum("interface")]
2369                         @interface,
2370                         protocol,
2371                         category,
2372                         exception,
2373                         file,
2374                         [XmlEnum("namespace")]
2375                         @namespace,
2376                         group,
2377                         page,
2378                         example,
2379                         dir
2380                 }
2381
2382                 #region GenericsSeralizationTests
2383
2384                 [Test]
2385                 public void TestSerializeGenSimpleClassString ()
2386                 {
2387                         GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2388                         Serialize (simple);
2389                         Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2390
2391                         simple.something = "hello";
2392
2393                         Serialize (simple);
2394                         Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
2395                 }
2396
2397                 [Test]
2398                 public void TestSerializeGenSimpleClassBool ()
2399                 {
2400                         GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2401                         Serialize (simple);
2402                         Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
2403
2404                         simple.something = true;
2405
2406                         Serialize (simple);
2407                         Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
2408                 }
2409
2410                 [Test]
2411                 public void TestSerializeGenSimpleStructInt ()
2412                 {
2413                         GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2414                         Serialize (simple);
2415                         Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
2416
2417                         simple.something = 123;
2418
2419                         Serialize (simple);
2420                         Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
2421                 }
2422
2423                 [Test]
2424                 public void TestSerializeGenListClassString ()
2425                 {
2426                         GenListClass<string> genlist = new GenListClass<string> ();
2427                         Serialize (genlist);
2428                         Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
2429
2430                         genlist.somelist.Add ("Value1");
2431                         genlist.somelist.Add ("Value2");
2432
2433                         Serialize (genlist);
2434                         Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
2435                 }
2436
2437                 [Test]
2438                 public void TestSerializeGenListClassFloat ()
2439                 {
2440                         GenListClass<float> genlist = new GenListClass<float> ();
2441                         Serialize (genlist);
2442                         Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
2443
2444                         genlist.somelist.Add (1);
2445                         genlist.somelist.Add (2.2F);
2446
2447                         Serialize (genlist);
2448                         Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
2449                 }
2450
2451                 [Test]
2452                 public void TestSerializeGenListClassList ()
2453                 {
2454                         GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2455                         Serialize (genlist);
2456                         Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2457
2458                         GenListClass<int> inlist1 = new GenListClass<int> ();
2459                         inlist1.somelist.Add (1);
2460                         inlist1.somelist.Add (2);
2461                         GenListClass<int> inlist2 = new GenListClass<int> ();
2462                         inlist2.somelist.Add (10);
2463                         inlist2.somelist.Add (20);
2464                         genlist.somelist.Add (inlist1);
2465                         genlist.somelist.Add (inlist2);
2466
2467                         Serialize (genlist);
2468                         Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2469                 }
2470
2471                 [Test]
2472                 public void TestSerializeGenListClassArray ()
2473                 {
2474                         GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2475                         Serialize (genlist);
2476                         Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2477
2478                         GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2479                         genarr1.arr[0] = 'a';
2480                         genarr1.arr[1] = 'b';
2481                         genlist.somelist.Add (genarr1);
2482                         GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2483                         genarr2.arr[0] = 'd';
2484                         genarr2.arr[1] = 'e';
2485                         genarr2.arr[2] = 'f';
2486                         genlist.somelist.Add (genarr2);
2487
2488                         Serialize (genlist);
2489                         Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2490                 }
2491
2492                 [Test]
2493                 public void TestSerializeGenTwoClassCharDouble ()
2494                 {
2495                         GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2496                         Serialize (gentwo);
2497                         Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
2498
2499                         gentwo.something1 = 'a';
2500                         gentwo.something2 = 2.2;
2501
2502                         Serialize (gentwo);
2503                         Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
2504                 }
2505
2506                 [Test]
2507                 public void TestSerializeGenDerivedClassDecimalShort ()
2508                 {
2509                         GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2510                         Serialize (derived);
2511                         Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2512
2513                         derived.something1 = "Value1";
2514                         derived.something2 = 1;
2515                         derived.another1 = 1.1M;
2516                         derived.another2 = -22;
2517
2518                         Serialize (derived);
2519                         Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2520                 }
2521
2522                 [Test]
2523                 public void TestSerializeGenDerivedSecondClassByteUlong ()
2524                 {
2525                         GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2526                         Serialize (derived2);
2527                         Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2528
2529                         derived2.something1 = 1;
2530                         derived2.something2 = 222;
2531                         derived2.another1 = 111;
2532                         derived2.another2 = 222222;
2533
2534                         Serialize (derived2);
2535                         Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2536                 }
2537
2538                 [Test]
2539                 public void TestSerializeGenNestedClass ()
2540                 {
2541                         GenNestedClass<string, int>.InnerClass<bool> nested =
2542                                 new GenNestedClass<string, int>.InnerClass<bool> ();
2543                         Serialize (nested);
2544                         Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
2545
2546                         nested.inner = 5;
2547                         nested.something = true;
2548
2549                         Serialize (nested);
2550                         Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
2551                 }
2552
2553                 [Test]
2554                 public void TestSerializeGenListClassListNested ()
2555                 {
2556                         GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2557                                 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2558                         Serialize (genlist);
2559                         Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2560
2561                         GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2562                                 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2563                         GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2564                         inval1.inner = 1;
2565                         inval1.something = "ONE";
2566                         inlist1.somelist.Add (inval1);
2567                         GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2568                         inval2.inner = 2;
2569                         inval2.something = "TWO";
2570                         inlist1.somelist.Add (inval2);
2571                         GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2572                                 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2573                         GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2574                         inval3.inner = 30;
2575                         inval3.something = "THIRTY";
2576                         inlist2.somelist.Add (inval3);
2577                         genlist.somelist.Add (inlist1);
2578                         genlist.somelist.Add (inlist2);
2579
2580                         Serialize (genlist);
2581                         Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2582                 }
2583
2584                 public enum Myenum { one, two, three, four, five, six };
2585                 [Test]
2586                 public void TestSerializeGenArrayClassEnum ()
2587                 {
2588                         GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2589                         Serialize (genarr);
2590                         Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2591
2592                         genarr.arr[0] = Myenum.one;
2593                         genarr.arr[1] = Myenum.three;
2594                         genarr.arr[2] = Myenum.five;
2595
2596                         Serialize (genarr);
2597                         Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2598                 }
2599
2600                 [Test]
2601                 public void TestSerializeGenArrayStruct ()
2602                 {
2603                         GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2604                         Serialize (genarr);
2605                         Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
2606
2607                         GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2608                         genstruct.something = 111;
2609                         genarr.arr[0] = genstruct;
2610                         genstruct.something = 222;
2611                         genarr.arr[1] = genstruct;
2612                         genstruct.something = 333;
2613                         genarr.arr[2] = genstruct;
2614
2615                         Serialize (genarr);
2616                         Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
2617                 }
2618
2619                 [Test]
2620                 public void TestSerializeGenArrayList ()
2621                 {
2622                         GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2623                         Serialize (genarr);
2624                         Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
2625
2626                         GenListClass<string> genlist1 = new GenListClass<string> ();
2627                         genlist1.somelist.Add ("list1-val1");
2628                         genlist1.somelist.Add ("list1-val2");
2629                         genarr.arr[0] = genlist1;
2630                         GenListClass<string> genlist2 = new GenListClass<string> ();
2631                         genlist2.somelist.Add ("list2-val1");
2632                         genlist2.somelist.Add ("list2-val2");
2633                         genlist2.somelist.Add ("list2-val3");
2634                         genlist2.somelist.Add ("list2-val4");
2635                         genarr.arr[1] = genlist2;
2636                         GenListClass<string> genlist3 = new GenListClass<string> ();
2637                         genlist3.somelist.Add ("list3val");
2638                         genarr.arr[2] = genlist3;
2639
2640                         Serialize (genarr);
2641                         Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
2642                 }
2643
2644                 [Test]
2645                 public void TestSerializeGenComplexStruct ()
2646                 {
2647                         GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2648                         Serialize (complex);
2649                         Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
2650
2651                         complex.something = 123;
2652                         complex.simpleclass.something = 456;
2653                         complex.simplestruct.something = 789;
2654                         GenListClass<int> genlist = new GenListClass<int> ();
2655                         genlist.somelist.Add (100);
2656                         genlist.somelist.Add (200);
2657                         complex.listclass = genlist;
2658                         GenArrayClass<int> genarr = new GenArrayClass<int> ();
2659                         genarr.arr[0] = 11;
2660                         genarr.arr[1] = 22;
2661                         genarr.arr[2] = 33;
2662                         complex.arrayclass = genarr;
2663                         complex.twoclass.something1 = 10;
2664                         complex.twoclass.something2 = "Ten";
2665                         complex.derivedclass.another1 = 1;
2666                         complex.derivedclass.another2 = "one";
2667                         complex.derivedclass.something1 = "two";
2668                         complex.derivedclass.something2 = 2;
2669                         complex.derived2.another1 = 3;
2670                         complex.derived2.another2 = "three";
2671                         complex.derived2.something1 = 4;
2672                         complex.derived2.something2 = "four";
2673                         complex.nestedouter.outer = 5;
2674                         complex.nestedinner.inner = "six";
2675                         complex.nestedinner.something = 6;
2676
2677                         Serialize (complex);
2678                         Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
2679                 }
2680
2681                 [Test] // bug #80759
2682                 public void HasNullableField ()
2683                 {
2684                         Bug80759 foo = new Bug80759 ();
2685                         foo.Test = "BAR";
2686                         foo.NullableInt = 10;
2687
2688                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2689
2690                         MemoryStream stream = new MemoryStream ();
2691
2692                         serializer.Serialize (stream, foo);
2693                         stream.Position = 0;
2694                         foo = (Bug80759) serializer.Deserialize (stream);
2695                 }
2696
2697                 [Test] // bug #80759, with fieldSpecified.
2698                 [ExpectedException (typeof (InvalidOperationException))]
2699                 [Category ("NotWorking")]
2700                 public void HasFieldSpecifiedButIrrelevant ()
2701                 {
2702                         Bug80759_2 foo = new Bug80759_2 ();
2703                         foo.Test = "BAR";
2704                         foo.NullableInt = 10;
2705
2706                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2707
2708                         MemoryStream stream = new MemoryStream ();
2709
2710                         serializer.Serialize (stream, foo);
2711                         stream.Position = 0;
2712                         foo = (Bug80759_2) serializer.Deserialize (stream);
2713                 }
2714
2715                 [Test]
2716                 public void HasNullableField2 ()
2717                 {
2718                         Bug80759 foo = new Bug80759 ();
2719                         foo.Test = "BAR";
2720                         foo.NullableInt = 10;
2721
2722                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2723
2724                         MemoryStream stream = new MemoryStream ();
2725
2726                         serializer.Serialize (stream, foo);
2727                         stream.Position = 0;
2728                         foo = (Bug80759) serializer.Deserialize (stream);
2729
2730                         Assert.AreEqual ("BAR", foo.Test, "#1");
2731                         Assert.AreEqual (10, foo.NullableInt, "#2");
2732
2733                         foo.NullableInt = null;
2734                         stream = new MemoryStream ();
2735                         serializer.Serialize (stream, foo);
2736                         stream.Position = 0;
2737                         foo = (Bug80759) serializer.Deserialize (stream);
2738
2739                         Assert.AreEqual ("BAR", foo.Test, "#3");
2740                         Assert.IsNull (foo.NullableInt, "#4");
2741                 }
2742
2743                 [Test]
2744                 public void SupportPrivateCtorOnly ()
2745                 {
2746                         XmlSerializer xs =
2747                                 new XmlSerializer (typeof (PrivateCtorOnly));
2748                         StringWriter sw = new StringWriter ();
2749                         xs.Serialize (sw, PrivateCtorOnly.Instance);
2750                         xs.Deserialize (new StringReader (sw.ToString ()));
2751                 }
2752
2753                 [Test]
2754                 public void XmlSchemaProviderQNameBecomesRootName ()
2755                 {
2756                         xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2757                         Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2758                         Assert.AreEqual (Infoset ("<foo />"), WriterText);
2759                         xs.Deserialize (new StringReader ("<foo/>"));
2760                 }
2761
2762                 [Test]
2763                 public void XmlSchemaProviderQNameBecomesRootName2 ()
2764                 {
2765                         string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2766                         xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2767                         Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2768                         Assert.AreEqual (Infoset (xml), WriterText);
2769                         xs.Deserialize (new StringReader (xml));
2770                 }
2771
2772                 [Test]
2773                 public void XmlAnyElementForObjects () // bug #553032
2774                 {
2775                         new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2776                 }
2777
2778                 [Test]
2779                 [ExpectedException (typeof (InvalidOperationException))]
2780                 public void XmlAnyElementForObjects2 () // bug #553032-2
2781                 {
2782                         new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2783                 }
2784
2785
2786                 public class Bug2893 {
2787                         public Bug2893 ()
2788                         {                       
2789                                 Contents = new XmlDataDocument();
2790                         }
2791                         
2792                         [XmlAnyElement("Contents")]
2793                         public XmlNode Contents;
2794                 }
2795
2796                 // Bug Xamarin #2893
2797                 [Test]
2798                 public void XmlAnyElementForXmlNode ()
2799                 {
2800                         var obj = new Bug2893 ();
2801                         XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
2802                         XmlWriterSettings settings = new XmlWriterSettings();
2803
2804                         var xsn = new XmlSerializerNamespaces();
2805                         xsn.Add(string.Empty, string.Empty);
2806
2807                         byte[] buffer = new byte[2048];
2808                         var ms = new MemoryStream(buffer);
2809                         using (var xw = XmlWriter.Create(ms, settings))
2810                         {
2811                                 mySerializer.Serialize(xw, obj, xsn);
2812                                 xw.Flush();
2813                         }
2814
2815                         mySerializer.Serialize(ms, obj);
2816                 }
2817
2818                 [Test]
2819                 public void XmlRootOverridesSchemaProviderQName ()
2820                 {
2821                         var obj = new XmlRootOverridesSchemaProviderQNameType ();
2822
2823                         XmlSerializer xs = new XmlSerializer (obj.GetType ());
2824
2825                         var sw = new StringWriter ();
2826                         using (XmlWriter xw = XmlWriter.Create (sw))
2827                                 xs.Serialize (xw, obj);
2828                         Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2829                 }
2830
2831                 public class AnotherArrayListType
2832                 {
2833                         [XmlAttribute]
2834                         public string one = "aaa";
2835                         [XmlAttribute]
2836                         public string another = "bbb";
2837                 }
2838
2839                 public class DerivedArrayListType : AnotherArrayListType
2840                 {
2841
2842                 }
2843
2844                 public class ClassWithArrayList
2845                 {
2846                         [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2847                         [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2848                         [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2849                         [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2850                         public ArrayList list;
2851                 }
2852
2853                 public class ClassWithArray
2854                 {
2855                         [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2856                         [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2857                         [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2858                         [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2859                         public object[] list;
2860
2861                 }
2862
2863                 [Test]
2864                 public void MultipleXmlElementAttributesOnArrayList()
2865                 {
2866                         var test = new ClassWithArrayList();
2867
2868                         test.list = new ArrayList();
2869                         test.list.Add(3);
2870                         test.list.Add("apepe");
2871                         test.list.Add(new AnotherArrayListType());
2872                         test.list.Add(new DerivedArrayListType());
2873
2874                         Serialize(test);
2875                         var expected_text = "<:ClassWithArrayList http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2876
2877                         Assert.AreEqual(WriterText, expected_text, WriterText);
2878                 }
2879
2880                 [Test]
2881                 public void MultipleXmlElementAttributesOnArray()
2882                 {
2883                         var test = new ClassWithArray();
2884
2885                         test.list = new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() };
2886
2887                         Serialize(test);
2888                         var expected_text = "<:ClassWithArray http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2889
2890                         Assert.AreEqual(WriterText, expected_text, WriterText);
2891                 }
2892
2893
2894                 #endregion //GenericsSeralizationTests
2895                 #region XmlInclude on abstract class tests (Bug #18558)
2896                 [Test]
2897                 public void TestSerializeIntermediateType ()
2898                 {
2899                         string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlIntermediateType intermediate=\"false\"/></ContainerTypeForTest>";
2900                         var obj = new ContainerTypeForTest();
2901                         obj.MemberToUseInclude = new IntermediateTypeForTest ();
2902                         Serialize (obj);
2903                         Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
2904                 }
2905
2906                 [Test]
2907                 public void TestSerializeSecondType ()
2908                 {
2909                         string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlSecondType intermediate=\"false\"/></ContainerTypeForTest>";
2910                         var obj = new ContainerTypeForTest();
2911                         obj.MemberToUseInclude = new SecondDerivedTypeForTest ();
2912                         Serialize (obj);
2913                         Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
2914                 }
2915                 #endregion
2916                 public class XmlArrayOnInt
2917                 {
2918                         [XmlArray]
2919                         public int Bogus;
2920                 }
2921
2922                 public class XmlArrayUnqualifiedWithNamespace
2923                 {
2924                         [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2925                         public ArrayList Sane = new ArrayList ();
2926                 }
2927
2928                 public class XmlArrayItemUnqualifiedWithNamespace
2929                 {
2930                         [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2931                         public ArrayList Sane = new ArrayList ();
2932                 }
2933
2934                 [XmlRoot (Namespace = "urn:foo")]
2935                 public class XmlArrayOnArrayList
2936                 {
2937                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2938                         public ArrayList Sane = new ArrayList ();
2939                 }
2940
2941                 [XmlRoot (Namespace = "urn:foo")]
2942                 public class XmlArrayOnArray
2943                 {
2944                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2945                         public string[] Sane = new string[] { "foo", "bar" };
2946
2947                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2948                         public ArrayItemInXmlArray[] Mids =
2949                                 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
2950                 }
2951
2952                 [XmlType (Namespace = "urn:gyabo")]
2953                 public class ArrayItemInXmlArray
2954                 {
2955                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2956                         public string[] Whee = new string[] { "foo", "bar" };
2957                 }
2958
2959                 [XmlRoot ("Base64Binary")]
2960                 public class Base64Binary
2961                 {
2962                         [XmlAttribute (DataType = "base64Binary")]
2963                         public byte [] Data = new byte [] {1, 2, 3};
2964                 }
2965
2966                 [XmlRoot ("HexBinary")]
2967                 public class HexBinary
2968                 {
2969                         [XmlAttribute (DataType = "hexBinary")]
2970                         public byte[] Data = new byte[] { 1, 2, 3 };
2971                 }
2972
2973                 [XmlRoot ("PrivateCtorOnly")]
2974                 public class PrivateCtorOnly
2975                 {
2976                         public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
2977                         private PrivateCtorOnly ()
2978                         {
2979                         }
2980                 }
2981
2982                 public class CDataTextNodesType
2983                 {
2984                         public CDataTextNodesInternal foo;
2985                 }
2986
2987                 public class CDataTextNodesInternal
2988                 {
2989                         [XmlText]
2990                         public string Value;
2991                 }
2992
2993                 public class InvalidTypeContainer
2994                 {
2995                         [XmlElement (DataType = "invalid")]
2996                         public string InvalidTypeItem = "aaa";
2997                 }
2998
2999                 public class TimeSpanContainer1
3000                 {
3001                         [XmlElement (DataType = "duration")]
3002                         public string StringDuration = "aaa";
3003                 }
3004
3005                 public class TimeSpanContainer2
3006                 {
3007                         [XmlElement (DataType = "duration")]
3008                         public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
3009                 }
3010
3011                 public class Bug80759
3012                 {
3013                         public string Test;
3014                         public int? NullableInt;
3015                 }
3016
3017                 public class Bug80759_2
3018                 {
3019                         public string Test;
3020                         public int? NullableInt;
3021
3022                         [XmlIgnore]
3023                         public bool NullableIntSpecified {
3024                                 get { return NullableInt.HasValue; }
3025                         }
3026                 }
3027
3028                 [XmlSchemaProvider ("GetXsdType")]
3029                 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
3030                 {
3031                         public XmlSchema GetSchema ()
3032                         {
3033                                 return null;
3034                         }
3035
3036                         public void ReadXml (XmlReader reader)
3037                         {
3038                                 reader.Skip ();
3039                         }
3040
3041                         public void WriteXml (XmlWriter writer)
3042                         {
3043                         }
3044
3045                         public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
3046                         {
3047                                 if (xss.Count == 0) {
3048                                         XmlSchema xs = new XmlSchema ();
3049                                         XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3050                                         ct.Name = "foo";
3051                                         xs.Items.Add (ct);
3052                                         xss.Add (xs);
3053                                 }
3054                                 return new XmlQualifiedName ("foo");
3055                         }
3056                 }
3057
3058                 public class XmlSchemaProviderQNameBecomesRootNameType2
3059                 {
3060                         [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3061                         public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3062                 }
3063
3064                 public class XmlAnyElementForObjectsType
3065                 {
3066                         [XmlAnyElement]
3067                         public object [] arr = new object [] {3,4,5};
3068                 }
3069
3070                 [XmlRoot ("foo")]
3071                 [XmlSchemaProvider ("GetSchema")]
3072                 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3073                 {
3074                         public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3075                         {
3076                                 var xs = new XmlSchema ();
3077                                 var xse = new XmlSchemaComplexType () { Name = "bar" };
3078                                 xs.Items.Add (xse);
3079                                 xss.Add (xs);
3080                                 return new XmlQualifiedName ("bar");
3081                         }
3082
3083                         XmlSchema IXmlSerializable.GetSchema ()
3084                         {
3085                                 return null;
3086                         }
3087
3088                         void IXmlSerializable.ReadXml (XmlReader reader)
3089                         {
3090                         }
3091                         void IXmlSerializable.WriteXml (XmlWriter writer)
3092                         {
3093                         }
3094                 }
3095
3096
3097                 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3098                 {
3099                         Assert.Fail ();
3100                 }
3101
3102                 // Helper methods
3103
3104                 public static string Infoset (string sx)
3105                 {
3106                         XmlDocument doc = new XmlDocument ();
3107                         doc.LoadXml (sx);
3108                         StringBuilder sb = new StringBuilder ();
3109                         GetInfoset (doc.DocumentElement, sb);
3110                         return sb.ToString ();
3111                 }
3112
3113                 public static string Infoset (XmlNode nod)
3114                 {
3115                         StringBuilder sb = new StringBuilder ();
3116                         GetInfoset (nod, sb);
3117                         return sb.ToString ();
3118                 }
3119
3120                 static void GetInfoset (XmlNode nod, StringBuilder sb)
3121                 {
3122                         switch (nod.NodeType) {
3123                         case XmlNodeType.Attribute:
3124                                 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3125                                 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3126                                 break;
3127
3128                         case XmlNodeType.Element:
3129                                 XmlElement elem = (XmlElement) nod;
3130                                 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3131
3132                                 ArrayList ats = new ArrayList ();
3133                                 foreach (XmlAttribute at in elem.Attributes)
3134                                         ats.Add (at.LocalName + " " + at.NamespaceURI);
3135
3136                                 ats.Sort ();
3137
3138                                 foreach (string name in ats) {
3139                                         string[] nn = name.Split (' ');
3140                                         GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3141                                 }
3142
3143                                 sb.Append (">");
3144                                 foreach (XmlNode cn in elem.ChildNodes)
3145                                         GetInfoset (cn, sb);
3146                                 sb.Append ("</>");
3147                                 break;
3148
3149                         default:
3150                                 sb.Append (nod.OuterXml);
3151                                 break;
3152                         }
3153                 }
3154
3155                 static XmlTypeMapping CreateSoapMapping (Type type)
3156                 {
3157                         SoapReflectionImporter importer = new SoapReflectionImporter ();
3158                         return importer.ImportTypeMapping (type);
3159                 }
3160
3161                 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3162                 {
3163                         SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3164                         return importer.ImportTypeMapping (type);
3165                 }
3166
3167                 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3168                 {
3169                         SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3170                         return importer.ImportTypeMapping (type);
3171                 }
3172
3173                 [XmlSchemaProvider (null, IsAny = true)]
3174                 public class AnySchemaProviderClass : IXmlSerializable {
3175
3176                         public string Text;
3177
3178                         void IXmlSerializable.WriteXml (XmlWriter writer)
3179                         {
3180                                 writer.WriteElementString ("text", Text);
3181                         }
3182
3183                         void IXmlSerializable.ReadXml (XmlReader reader)
3184                         {
3185                                 Text = reader.ReadElementString ("text");
3186                         }
3187
3188                         XmlSchema IXmlSerializable.GetSchema ()
3189                         {
3190                                 return null;
3191                         }
3192                 }
3193
3194                 [Test]
3195                 public void SerializeAnySchemaProvider ()
3196                 {
3197                         string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3198                                 Environment.NewLine + "<text>test</text>";
3199
3200                         var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
3201
3202                         var obj = new AnySchemaProviderClass {
3203                                 Text = "test",
3204                         };
3205
3206                         using (var t = new StringWriter ()) {
3207                                 ser.Serialize (t, obj);
3208                                 Assert.AreEqual (expected, t.ToString ());
3209                         }
3210                 }
3211
3212                 [Test]
3213                 public void DeserializeAnySchemaProvider ()
3214                 {
3215                         string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3216                                 Environment.NewLine + "<text>test</text>";
3217
3218                         var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
3219
3220                         using (var t = new StringReader (expected)) {
3221                                 var obj = (AnySchemaProviderClass) ser.Deserialize (t);
3222                                 Assert.AreEqual ("test", obj.Text);
3223                         }
3224                 }
3225
3226                 public class SubNoParameterlessConstructor : NoParameterlessConstructor
3227                 {
3228                         public SubNoParameterlessConstructor ()
3229                                 : base ("")
3230                         {
3231                         }
3232                 }
3233
3234                 public class NoParameterlessConstructor
3235                 {
3236                         [XmlElement ("Text")]
3237                         public string Text;
3238
3239                         public NoParameterlessConstructor (string parameter)
3240                         {
3241                         }
3242                 }
3243
3244                 [Test]
3245                 public void BaseClassWithoutParameterlessConstructor ()
3246                 {
3247                         var ser = new XmlSerializer (typeof (SubNoParameterlessConstructor));
3248
3249                         var obj = new SubNoParameterlessConstructor {
3250                                 Text = "test",
3251                         };
3252
3253                         using (var w = new StringWriter ()) {
3254                                 ser.Serialize (w, obj);
3255                                 using (var r = new StringReader ( w.ToString ())) {
3256                                         var desObj = (SubNoParameterlessConstructor) ser.Deserialize (r);
3257                                         Assert.AreEqual (obj.Text, desObj.Text);
3258                                 }
3259                         }
3260                 }
3261
3262                 public class ClassWithXmlAnyElement
3263                 {
3264                         [XmlAnyElement ("Contents")]
3265                         public XmlNode Contents;
3266                 }
3267
3268                 [Test] // bug #3211
3269                 public void TestClassWithXmlAnyElement ()
3270                 {
3271                         var d = new XmlDocument ();
3272                         var e = d.CreateElement ("Contents");
3273                         e.AppendChild (d.CreateElement ("SomeElement"));
3274
3275                         var c = new ClassWithXmlAnyElement {
3276                                 Contents = e,
3277                         };
3278
3279                         var ser = new XmlSerializer (typeof (ClassWithXmlAnyElement));
3280                         using (var sw = new StringWriter ())
3281                                 ser.Serialize (sw, c);
3282                 }
3283
3284                 [Test]
3285                 public void ClassWithImplicitlyConvertibleElement ()
3286                 {
3287                         var ser = new XmlSerializer (typeof (ObjectWithElementRequiringImplicitCast));
3288
3289                         var obj = new ObjectWithElementRequiringImplicitCast ("test");
3290
3291                         using (var w = new StringWriter ()) {
3292                                 ser.Serialize (w, obj);
3293                                 using (var r = new StringReader ( w.ToString ())) {
3294                                         var desObj = (ObjectWithElementRequiringImplicitCast) ser.Deserialize (r);
3295                                         Assert.AreEqual (obj.Object.Text, desObj.Object.Text);
3296                                 }
3297                         }
3298                 }
3299
3300                 public class ClassWithOptionalMethods
3301                 {
3302                         private readonly bool shouldSerializeX;
3303                         private readonly bool xSpecified;
3304
3305                         [XmlAttribute]
3306                         public int X { get; set; }
3307
3308                         public bool ShouldSerializeX () { return shouldSerializeX; }
3309
3310                         public bool XSpecified
3311                         {
3312                                 get { return xSpecified; }
3313                         }
3314
3315                         public ClassWithOptionalMethods ()
3316                         {
3317                         }
3318
3319                         public ClassWithOptionalMethods (int x, bool shouldSerializeX, bool xSpecified)
3320                         {
3321                                 this.X = x;
3322                                 this.shouldSerializeX = shouldSerializeX;
3323                                 this.xSpecified = xSpecified;
3324                         }
3325                 }
3326
3327                 [Test]
3328                 public void OptionalMethods ()
3329                 {
3330                         var ser = new XmlSerializer (typeof (ClassWithOptionalMethods));
3331
3332                         var expectedValueWithoutX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3333                                 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />");
3334
3335                         var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3336                                 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3337
3338                         using (var t = new StringWriter ()) {
3339                                 var obj = new ClassWithOptionalMethods (11, false, false);
3340                                 ser.Serialize (t, obj);
3341                                 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3342                         }
3343
3344                         using (var t = new StringWriter ()) {
3345                                 var obj = new ClassWithOptionalMethods (11, true, false);
3346                                 ser.Serialize (t, obj);
3347                                 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3348                         }
3349
3350                         using (var t = new StringWriter ()) {
3351                                 var obj = new ClassWithOptionalMethods (11, false, true);
3352                                 ser.Serialize (t, obj);
3353                                 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3354                         }
3355
3356                         using (var t = new StringWriter ()) {
3357                                 var obj = new ClassWithOptionalMethods (11, true, true);
3358                                 ser.Serialize (t, obj);
3359                                 Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
3360                         }
3361                 }
3362
3363                 public class ClassWithShouldSerializeGeneric
3364                 {
3365                         [XmlAttribute]
3366                         public int X { get; set; }
3367
3368                         public bool ShouldSerializeX<T> () { return false; }
3369                 }
3370
3371                 [Test]
3372                 [Category("NotWorking")]
3373                 public void ShouldSerializeGeneric ()
3374                 {
3375                         var ser = new XmlSerializer (typeof (ClassWithShouldSerializeGeneric));
3376
3377                         var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3378                                 "<ClassWithShouldSerializeGeneric xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3379
3380                         using (var t = new StringWriter ()) {
3381                                 var obj = new ClassWithShouldSerializeGeneric { X = 11 };
3382                                 ser.Serialize (t, obj);
3383                                 Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
3384                         }
3385                 }
3386
3387                 [Test]
3388                 public void NullableArrayItems ()
3389                 {
3390                         var ser = new XmlSerializer (typeof (ObjectWithNullableArrayItems));
3391
3392                         var obj = new ObjectWithNullableArrayItems ();
3393                         obj.Elements = new List <SimpleClass> ();
3394                         obj.Elements.Add (new SimpleClass { something = "Hello" });
3395                         obj.Elements.Add (null);
3396                         obj.Elements.Add (new SimpleClass { something = "World" });
3397
3398                         using (var w = new StringWriter ()) {
3399                                 ser.Serialize (w, obj);
3400                                 using (var r = new StringReader ( w.ToString ())) {
3401                                         var desObj = (ObjectWithNullableArrayItems) ser.Deserialize (r);
3402                                         Assert.IsNull (desObj.Elements [1]);
3403                                 }
3404                         }
3405                 }
3406
3407                 [Test]
3408                 public void NonNullableArrayItems ()
3409                 {
3410                         var ser = new XmlSerializer (typeof (ObjectWithNonNullableArrayItems));
3411
3412                         var obj = new ObjectWithNonNullableArrayItems ();
3413                         obj.Elements = new List <SimpleClass> ();
3414                         obj.Elements.Add (new SimpleClass { something = "Hello" });
3415                         obj.Elements.Add (null);
3416                         obj.Elements.Add (new SimpleClass { something = "World" });
3417
3418                         using (var w = new StringWriter ()) {
3419                                 ser.Serialize (w, obj);
3420                                 using (var r = new StringReader ( w.ToString ())) {
3421                                         var desObj = (ObjectWithNonNullableArrayItems) ser.Deserialize (r);
3422                                         Assert.IsNotNull (desObj.Elements [1]);
3423                                 }
3424                         }
3425                 }
3426
3427                 [Test]
3428                 public void NotSpecifiedNullableArrayItems ()
3429                 {
3430                         var ser = new XmlSerializer (typeof (ObjectWithNotSpecifiedNullableArrayItems));
3431
3432                         var obj = new ObjectWithNotSpecifiedNullableArrayItems ();
3433                         obj.Elements = new List <SimpleClass> ();
3434                         obj.Elements.Add (new SimpleClass { something = "Hello" });
3435                         obj.Elements.Add (null);
3436                         obj.Elements.Add (new SimpleClass { something = "World" });
3437
3438                         using (var w = new StringWriter ()) {
3439                                 ser.Serialize (w, obj);
3440                                 using (var r = new StringReader ( w.ToString ())) {
3441                                         var desObj = (ObjectWithNotSpecifiedNullableArrayItems) ser.Deserialize (r);
3442                                         Assert.IsNull (desObj.Elements [1]);
3443                                 }
3444                         }
3445                 }
3446
3447                 private static void TestClassWithDefaultTextNotNullAux (string value, string expected)
3448                 {
3449                         var obj = new ClassWithDefaultTextNotNull (value);
3450                         var ser = new XmlSerializer (typeof (ClassWithDefaultTextNotNull));
3451
3452                         using (var mstream = new MemoryStream ())
3453                         using (var writer = new XmlTextWriter (mstream, Encoding.ASCII)) {
3454                                 ser.Serialize (writer, obj);
3455
3456                                 mstream.Seek (0, SeekOrigin.Begin);
3457                                 using (var reader = new XmlTextReader (mstream)) {
3458                                         var result = (ClassWithDefaultTextNotNull) ser.Deserialize (reader);
3459                                         Assert.AreEqual (expected, result.Value);
3460                                 }
3461                         }
3462                 }
3463
3464                 [Test]
3465                 public void TestClassWithDefaultTextNotNull ()
3466                 {
3467                         TestClassWithDefaultTextNotNullAux ("my_text", "my_text");
3468                         TestClassWithDefaultTextNotNullAux ("", ClassWithDefaultTextNotNull.DefaultValue);
3469                         TestClassWithDefaultTextNotNullAux (null, ClassWithDefaultTextNotNull.DefaultValue);
3470                 }
3471         }
3472
3473         // Test generated serialization code.
3474         public class XmlSerializerGeneratorTests : XmlSerializerTests {
3475
3476                 private FieldInfo backgroundGeneration;
3477                 private FieldInfo generationThreshold;
3478                 private FieldInfo generatorFallback;
3479
3480                 private bool backgroundGenerationOld;
3481                 private int generationThresholdOld;
3482                 private bool generatorFallbackOld;
3483
3484                 [SetUp]
3485                 public void SetUp ()
3486                 {
3487                         // Make sure XmlSerializer static constructor is called
3488                         XmlSerializer.FromTypes (new Type [] {});
3489
3490                         const BindingFlags binding = BindingFlags.Static | BindingFlags.NonPublic;
3491                         backgroundGeneration = typeof (XmlSerializer).GetField ("backgroundGeneration", binding);
3492                         generationThreshold = typeof (XmlSerializer).GetField ("generationThreshold", binding);
3493                         generatorFallback = typeof (XmlSerializer).GetField ("generatorFallback", binding);
3494
3495                         if (backgroundGeneration == null)
3496                                 Assert.Ignore ("Unable to access field backgroundGeneration");
3497                         if (generationThreshold == null)
3498                                 Assert.Ignore ("Unable to access field generationThreshold");
3499                         if (generatorFallback == null)
3500                                 Assert.Ignore ("Unable to access field generatorFallback");
3501
3502                         backgroundGenerationOld = (bool) backgroundGeneration.GetValue (null);
3503                         generationThresholdOld = (int) generationThreshold.GetValue (null);
3504                         generatorFallbackOld = (bool) generatorFallback.GetValue (null);
3505
3506                         backgroundGeneration.SetValue (null, false);
3507                         generationThreshold.SetValue (null, 0);
3508                         generatorFallback.SetValue (null, false);
3509                 }
3510
3511                 [TearDown]
3512                 public void TearDown ()
3513                 {
3514                         if (backgroundGeneration == null || generationThreshold == null || generatorFallback == null)
3515                                 return;
3516
3517                         backgroundGeneration.SetValue (null, backgroundGenerationOld);
3518                         generationThreshold.SetValue (null, generationThresholdOld);
3519                         generatorFallback.SetValue (null, generatorFallbackOld);
3520                 }
3521         }
3522
3523 #region XmlInclude on abstract class test classes
3524
3525         [XmlType]
3526         public class ContainerTypeForTest
3527         {
3528                 [XmlElement ("XmlSecondType", typeof (SecondDerivedTypeForTest))]
3529                 [XmlElement ("XmlIntermediateType", typeof (IntermediateTypeForTest))]
3530                 [XmlElement ("XmlFirstType", typeof (FirstDerivedTypeForTest))]
3531                 public AbstractTypeForTest MemberToUseInclude { get; set; }
3532         }
3533
3534         [XmlInclude (typeof (SecondDerivedTypeForTest))]
3535         [XmlInclude (typeof (IntermediateTypeForTest))]
3536         [XmlInclude (typeof (FirstDerivedTypeForTest))]
3537         public abstract class AbstractTypeForTest
3538         {
3539         }
3540
3541         public class IntermediateTypeForTest : AbstractTypeForTest
3542         {
3543                 [XmlAttribute (AttributeName = "intermediate")]
3544                 public bool IntermediateMember { get; set; }
3545         }
3546
3547         public class FirstDerivedTypeForTest : AbstractTypeForTest
3548         {
3549                 public string FirstMember { get; set; }
3550         }
3551
3552         public class SecondDerivedTypeForTest : IntermediateTypeForTest
3553         {
3554                 public string SecondMember { get; set; }
3555         }
3556 #endregion
3557 }