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