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