Fix object::GetType when remoting is enabled.
[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 TestSerializeIList ()
1940                 {
1941                         clsPerson k = new clsPerson ();
1942                         k.EmailAccounts = new ArrayList ();
1943                         k.EmailAccounts.Add ("a");
1944                         k.EmailAccounts.Add ("b");
1945                         Serialize (k);
1946                         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);
1947                 }
1948
1949                 [Test]
1950                 public void TestSerializeArrayEnc ()
1951                 {
1952                         SoapReflectionImporter imp = new SoapReflectionImporter ();
1953                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1954                         XmlSerializer ser = new XmlSerializer (map);
1955                         StringWriter sw = new StringWriter ();
1956                         XmlTextWriter tw = new XmlTextWriter (sw);
1957                         tw.WriteStartElement ("aa");
1958                         ser.Serialize (tw, new ArrayClass ());
1959                         tw.WriteEndElement ();
1960                 }
1961
1962                 [Test] // bug #76049
1963                 public void TestIncludeType ()
1964                 {
1965                         XmlReflectionImporter imp = new XmlReflectionImporter ();
1966                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1967                         imp.IncludeType (typeof (TestSpace));
1968                         XmlSerializer ser = new XmlSerializer (map);
1969                         ser.Serialize (new StringWriter (), new TestSpace ());
1970                 }
1971
1972                 [Test]
1973                 public void TestSerializeChoiceArray ()
1974                 {
1975                         CompositeValueType v = new CompositeValueType ();
1976                         v.Init ();
1977                         Serialize (v);
1978                         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);
1979                 }
1980
1981                 [Test]
1982                 public void TestArrayAttributeWithDataType ()
1983                 {
1984                         Serialize (new ArrayAttributeWithType ());
1985                         string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1986                         res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1987                         Assert.AreEqual (Infoset (res), WriterText);
1988                 }
1989
1990                 [Test]
1991                 public void TestSubclassElementType ()
1992                 {
1993                         SubclassTestContainer c = new SubclassTestContainer ();
1994                         c.data = new SubclassTestSub ();
1995                         Serialize (c);
1996
1997                         string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1998                         res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1999                         Assert.AreEqual (Infoset (res), WriterText);
2000                 }
2001
2002                 [Test]
2003                 [ExpectedException (typeof (InvalidOperationException))]
2004                 public void TestArrayAttributeWithWrongDataType ()
2005                 {
2006                         Serialize (new ArrayAttributeWithWrongType ());
2007                 }
2008
2009                 [Test]
2010                 [Category ("NotWorking")]
2011                 public void TestSerializePrimitiveTypesContainer ()
2012                 {
2013                         Serialize (new PrimitiveTypesContainer ());
2014                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2015                                 "<?xml version='1.0' encoding='utf-16'?>" +
2016 #if NET_2_0
2017                                 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
2018 #else
2019                                 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
2020 #endif
2021                                 "<Number>2004</Number>" +
2022                                 "<Name>some name</Name>" +
2023                                 "<Index>56</Index>" +
2024                                 "<Password>8w8=</Password>" +
2025                                 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2026                                 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2027                                 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2028
2029                         SerializeEncoded (new PrimitiveTypesContainer ());
2030                         Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2031                                 "<?xml version='1.0' encoding='utf-16'?>" +
2032 #if NET_2_0
2033                                 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2034 #else
2035                                 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2036 #endif
2037                                 "<Number xsi:type='xsd:int'>2004</Number>" +
2038                                 "<Name xsi:type='xsd:string'>some name</Name>" +
2039                                 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2040                                 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2041                                 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2042                                 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2043                                 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2044                                 sw.ToString (), "#2");
2045                 }
2046
2047                 [Test]
2048                 public void TestSchemaForm ()
2049                 {
2050                         TestSchemaForm1 t1 = new TestSchemaForm1 ();
2051                         t1.p1 = new PrintTypeResponse ();
2052                         t1.p1.Init ();
2053                         t1.p2 = new PrintTypeResponse ();
2054                         t1.p2.Init ();
2055
2056                         TestSchemaForm2 t2 = new TestSchemaForm2 ();
2057                         t2.p1 = new PrintTypeResponse ();
2058                         t2.p1.Init ();
2059                         t2.p2 = new PrintTypeResponse ();
2060                         t2.p2.Init ();
2061
2062                         Serialize (t1);
2063                         string res = "";
2064                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2065                         res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2066                         res += "  <p1>";
2067                         res += "    <result>";
2068                         res += "      <data>data1</data>";
2069                         res += "    </result>";
2070                         res += "    <intern xmlns=\"urn:responseTypes\">";
2071                         res += "      <result xmlns=\"\">";
2072                         res += "        <data>data2</data>";
2073                         res += "      </result>";
2074                         res += "    </intern>";
2075                         res += "  </p1>";
2076                         res += "  <p2 xmlns=\"urn:oo\">";
2077                         res += "    <result xmlns=\"\">";
2078                         res += "      <data>data1</data>";
2079                         res += "    </result>";
2080                         res += "    <intern xmlns=\"urn:responseTypes\">";
2081                         res += "      <result xmlns=\"\">";
2082                         res += "        <data>data2</data>";
2083                         res += "      </result>";
2084                         res += "    </intern>";
2085                         res += "  </p2>";
2086                         res += "</TestSchemaForm1>";
2087                         Assert.AreEqual (Infoset (res), WriterText);
2088
2089                         Serialize (t2);
2090                         res = "";
2091                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2092                         res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2093                         res += "  <p1 xmlns=\"urn:testForm\">";
2094                         res += "    <result xmlns=\"\">";
2095                         res += "      <data>data1</data>";
2096                         res += "    </result>";
2097                         res += "    <intern xmlns=\"urn:responseTypes\">";
2098                         res += "      <result xmlns=\"\">";
2099                         res += "        <data>data2</data>";
2100                         res += "      </result>";
2101                         res += "    </intern>";
2102                         res += "  </p1>";
2103                         res += "  <p2 xmlns=\"urn:oo\">";
2104                         res += "    <result xmlns=\"\">";
2105                         res += "      <data>data1</data>";
2106                         res += "    </result>";
2107                         res += "    <intern xmlns=\"urn:responseTypes\">";
2108                         res += "      <result xmlns=\"\">";
2109                         res += "        <data>data2</data>";
2110                         res += "      </result>";
2111                         res += "    </intern>";
2112                         res += "  </p2>";
2113                         res += "</TestSchemaForm2>";
2114                         Assert.AreEqual (Infoset (res), WriterText);
2115
2116                         XmlReflectionImporter imp = new XmlReflectionImporter ();
2117                         XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2118                         Serialize (t1, map);
2119                         res = "";
2120                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2121                         res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2122                         res += "  <p1>";
2123                         res += "    <result xmlns=\"\">";
2124                         res += "      <data>data1</data>";
2125                         res += "    </result>";
2126                         res += "    <intern xmlns=\"urn:responseTypes\">";
2127                         res += "      <result xmlns=\"\">";
2128                         res += "        <data>data2</data>";
2129                         res += "      </result>";
2130                         res += "    </intern>";
2131                         res += "  </p1>";
2132                         res += "  <p2 xmlns=\"urn:oo\">";
2133                         res += "    <result xmlns=\"\">";
2134                         res += "      <data>data1</data>";
2135                         res += "    </result>";
2136                         res += "    <intern xmlns=\"urn:responseTypes\">";
2137                         res += "      <result xmlns=\"\">";
2138                         res += "        <data>data2</data>";
2139                         res += "      </result>";
2140                         res += "    </intern>";
2141                         res += "  </p2>";
2142                         res += "</TestSchemaForm1>";
2143                         Assert.AreEqual (Infoset (res), WriterText);
2144
2145                         imp = new XmlReflectionImporter ();
2146                         map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2147                         Serialize (t2, map);
2148                         res = "";
2149                         res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2150                         res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2151                         res += "  <p1 xmlns=\"urn:testForm\">";
2152                         res += "    <result xmlns=\"\">";
2153                         res += "      <data>data1</data>";
2154                         res += "    </result>";
2155                         res += "    <intern xmlns=\"urn:responseTypes\">";
2156                         res += "      <result xmlns=\"\">";
2157                         res += "        <data>data2</data>";
2158                         res += "      </result>";
2159                         res += "    </intern>";
2160                         res += "  </p1>";
2161                         res += "  <p2 xmlns=\"urn:oo\">";
2162                         res += "    <result xmlns=\"\">";
2163                         res += "      <data>data1</data>";
2164                         res += "    </result>";
2165                         res += "    <intern xmlns=\"urn:responseTypes\">";
2166                         res += "      <result xmlns=\"\">";
2167                         res += "        <data>data2</data>";
2168                         res += "      </result>";
2169                         res += "    </intern>";
2170                         res += "  </p2>";
2171                         res += "</TestSchemaForm2>";
2172                         Assert.AreEqual (Infoset (res), WriterText);
2173                 }
2174
2175                 [Test] // bug #78536
2176                 public void CDataTextNodes ()
2177                 {
2178                         XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2179                         ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2180                         string xml = @"<CDataTextNodesType>
2181   <foo><![CDATA[
2182 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2183 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2184 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2185         ]]></foo>
2186 </CDataTextNodesType>";
2187                         ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2188                 }
2189
2190 #if NET_2_0
2191 #if !TARGET_JVM
2192                 [Test]
2193                 public void GenerateSerializerGenerics ()
2194                 {
2195                         XmlReflectionImporter imp = new XmlReflectionImporter ();
2196                         Type type = typeof (List<int>);
2197                         XmlSerializer.GenerateSerializer (
2198                                 new Type [] {type},
2199                                 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2200                 }
2201 #endif
2202
2203                 [Test]
2204                 public void Nullable ()
2205                 {
2206                         XmlSerializer ser = new XmlSerializer (typeof (int?));
2207                         int? nullableType = 5;
2208                         sw = new StringWriter ();
2209                         xtw = new XmlTextWriter (sw);
2210                         ser.Serialize (xtw, nullableType);
2211                         xtw.Close ();
2212                         string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2213                         Assert.AreEqual (Infoset (expected), WriterText);
2214                         int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2215                         Assert.AreEqual (5, i);
2216                 }
2217
2218                 [Test]
2219                 public void NullableEnums ()
2220                 {
2221                         WithNulls w = new WithNulls ();
2222                         XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2223                         StringWriter tw = new StringWriter ();
2224                         ser.Serialize (tw, w);
2225
2226                         string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2227                                 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2228                                         "<nint xsi:nil='true' />" +
2229                                         "<nenum xsi:nil='true' />" +
2230                                         "<ndate xsi:nil='true' />" +
2231                                         "</WithNulls>";
2232                         
2233                         Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2234                         
2235                         StringReader sr = new StringReader (tw.ToString ());
2236                         w = (WithNulls) ser.Deserialize (sr);
2237                         
2238                         Assert.IsFalse (w.nint.HasValue);
2239                         Assert.IsFalse (w.nenum.HasValue);
2240                         Assert.IsFalse (w.ndate.HasValue);
2241                         
2242                         DateTime t = new DateTime (2008,4,1);
2243                         w.nint = 4;
2244                         w.ndate = t;
2245                         w.nenum = TestEnumWithNulls.bb;
2246                         
2247                         tw = new StringWriter ();
2248                         ser.Serialize (tw, w);
2249                         
2250                         expected = "<?xml version='1.0' encoding='utf-16'?>" +
2251                                 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2252                                         "<nint>4</nint>" +
2253                                         "<nenum>bb</nenum>" +
2254                                         "<ndate>2008-04-01T00:00:00</ndate>" +
2255                                         "</WithNulls>";
2256                         
2257                         Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2258                         
2259                         sr = new StringReader (tw.ToString ());
2260                         w = (WithNulls) ser.Deserialize (sr);
2261                         
2262                         Assert.IsTrue (w.nint.HasValue);
2263                         Assert.IsTrue (w.nenum.HasValue);
2264                         Assert.IsTrue (w.ndate.HasValue);
2265                         Assert.AreEqual (4, w.nint.Value);
2266                         Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2267                         Assert.AreEqual (t, w.ndate.Value);
2268                 }
2269 #endif
2270
2271                 [Test]
2272                 public void SerializeBase64Binary()
2273                 {
2274                         XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2275                         sw = new StringWriter ();
2276                         XmlTextWriter xtw = new XmlTextWriter (sw);
2277                         ser.Serialize (xtw, new Base64Binary ());
2278                         xtw.Close ();
2279                         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"" />";
2280                         Assert.AreEqual (Infoset (expected), WriterText);
2281                         Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2282                         Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2283                 }
2284
2285                 [Test] // bug #79989, #79990
2286                 public void SerializeHexBinary ()
2287                 {
2288                         XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2289                         sw = new StringWriter ();
2290                         XmlTextWriter xtw = new XmlTextWriter (sw);
2291                         ser.Serialize (xtw, new HexBinary ());
2292                         xtw.Close ();
2293                         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"" />";
2294                         Assert.AreEqual (Infoset (expected), WriterText);
2295                         HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2296                         Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2297                 }
2298
2299                 [Test]
2300                 [ExpectedException (typeof (InvalidOperationException))]
2301                 public void XmlArrayAttributeOnInt ()
2302                 {
2303                         new XmlSerializer (typeof (XmlArrayOnInt));
2304                 }
2305
2306                 [Test]
2307                 [ExpectedException (typeof (InvalidOperationException))]
2308                 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2309                 {
2310                         new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2311                 }
2312
2313                 [Test]
2314                 [ExpectedException (typeof (InvalidOperationException))]
2315                 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2316                 {
2317                         new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2318                 }
2319
2320                 [Test] // bug #78042
2321                 public void XmlArrayAttributeOnArray ()
2322                 {
2323                         XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2324                         sw = new StringWriter ();
2325                         XmlTextWriter xtw = new XmlTextWriter (sw);
2326                         ser.Serialize (xtw, new XmlArrayOnArray ());
2327                         xtw.Close ();
2328                         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>";
2329                         Assert.AreEqual (Infoset (expected), WriterText);
2330                 }
2331
2332                 [Test]
2333                 public void XmlArrayAttributeOnCollection ()
2334                 {
2335                         XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2336                         XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2337                         inst.Sane.Add ("abc");
2338                         inst.Sane.Add (1);
2339                         sw = new StringWriter ();
2340                         XmlTextWriter xtw = new XmlTextWriter (sw);
2341                         ser.Serialize (xtw, inst);
2342                         xtw.Close ();
2343                         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>";
2344                         Assert.AreEqual (Infoset (expected), WriterText);
2345                 }
2346
2347                 [Test] // bug #338705
2348                 public void SerializeTimeSpan ()
2349                 {
2350                         // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2351                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2352                         ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2353                 }
2354
2355                 [Test]
2356                 public void SerializeDurationToString ()
2357                 {
2358                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2359                         ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2360                 }
2361
2362                 [Test]
2363                 [ExpectedException (typeof (InvalidOperationException))]
2364                 public void SerializeDurationToTimeSpan ()
2365                 {
2366                         XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2367                         ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2368                 }
2369
2370                 [Test]
2371                 [ExpectedException (typeof (InvalidOperationException))]
2372                 public void SerializeInvalidDataType ()
2373                 {
2374                         XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2375                         ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2376                 }
2377
2378                 [Test]
2379 #if !NET_2_0
2380                 [ExpectedException (typeof (ApplicationException))]
2381 #endif
2382                 public void SerializeErrorneousIXmlSerializable ()
2383                 {
2384                         Serialize (new ErrorneousGetSchema ());
2385                         Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2386                 }
2387
2388 #if NET_2_0
2389                 public void DateTimeRoundtrip ()
2390                 {
2391                         // bug #337729
2392                         XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2393                         StringWriter sw = new StringWriter ();
2394                         ser.Serialize (sw, DateTime.UtcNow);
2395                         DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2396                         Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2397                 }
2398 #endif
2399
2400                 [Test]
2401                 public void SupportIXmlSerializableImplicitlyConvertible ()
2402                 {
2403                         XmlAttributes attrs = new XmlAttributes ();
2404                         XmlElementAttribute attr = new XmlElementAttribute ();
2405                         attr.ElementName = "XmlSerializable";
2406                         attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2407                         attrs.XmlElements.Add (attr);
2408                         XmlAttributeOverrides attrOverrides = new
2409                         XmlAttributeOverrides ();
2410                         attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2411
2412                         XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2413                         new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2414                 }
2415
2416                 [Test] // bug #566370
2417                 public void SerializeEnumWithCSharpKeyword ()
2418                 {
2419                         var ser = new XmlSerializer (typeof (DoxCompoundKind));
2420                         for (int i = 0; i < 100; i++) // test serialization code generator
2421                                 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2422                 }
2423
2424                 public enum DoxCompoundKind
2425                 {
2426                         [XmlEnum("class")]
2427                         @class,
2428                         [XmlEnum("struct")]
2429                         @struct,
2430                         union,
2431                         [XmlEnum("interface")]
2432                         @interface,
2433                         protocol,
2434                         category,
2435                         exception,
2436                         file,
2437                         [XmlEnum("namespace")]
2438                         @namespace,
2439                         group,
2440                         page,
2441                         example,
2442                         dir
2443                 }
2444
2445                 #region GenericsSeralizationTests
2446
2447 #if NET_2_0
2448                 [Test]
2449                 public void TestSerializeGenSimpleClassString ()
2450                 {
2451                         GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2452                         Serialize (simple);
2453                         Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2454
2455                         simple.something = "hello";
2456
2457                         Serialize (simple);
2458                         Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
2459                 }
2460
2461                 [Test]
2462                 public void TestSerializeGenSimpleClassBool ()
2463                 {
2464                         GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2465                         Serialize (simple);
2466                         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);
2467
2468                         simple.something = true;
2469
2470                         Serialize (simple);
2471                         Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
2472                 }
2473
2474                 [Test]
2475                 public void TestSerializeGenSimpleStructInt ()
2476                 {
2477                         GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2478                         Serialize (simple);
2479                         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);
2480
2481                         simple.something = 123;
2482
2483                         Serialize (simple);
2484                         Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
2485                 }
2486
2487                 [Test]
2488                 public void TestSerializeGenListClassString ()
2489                 {
2490                         GenListClass<string> genlist = new GenListClass<string> ();
2491                         Serialize (genlist);
2492                         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);
2493
2494                         genlist.somelist.Add ("Value1");
2495                         genlist.somelist.Add ("Value2");
2496
2497                         Serialize (genlist);
2498                         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);
2499                 }
2500
2501                 [Test]
2502                 public void TestSerializeGenListClassFloat ()
2503                 {
2504                         GenListClass<float> genlist = new GenListClass<float> ();
2505                         Serialize (genlist);
2506                         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);
2507
2508                         genlist.somelist.Add (1);
2509                         genlist.somelist.Add (2.2F);
2510
2511                         Serialize (genlist);
2512                         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);
2513                 }
2514
2515                 [Test]
2516                 public void TestSerializeGenListClassList ()
2517                 {
2518                         GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2519                         Serialize (genlist);
2520                         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);
2521
2522                         GenListClass<int> inlist1 = new GenListClass<int> ();
2523                         inlist1.somelist.Add (1);
2524                         inlist1.somelist.Add (2);
2525                         GenListClass<int> inlist2 = new GenListClass<int> ();
2526                         inlist2.somelist.Add (10);
2527                         inlist2.somelist.Add (20);
2528                         genlist.somelist.Add (inlist1);
2529                         genlist.somelist.Add (inlist2);
2530
2531                         Serialize (genlist);
2532                         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);
2533                 }
2534
2535                 [Test]
2536                 public void TestSerializeGenListClassArray ()
2537                 {
2538                         GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2539                         Serialize (genlist);
2540                         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);
2541
2542                         GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2543                         genarr1.arr[0] = 'a';
2544                         genarr1.arr[1] = 'b';
2545                         genlist.somelist.Add (genarr1);
2546                         GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2547                         genarr2.arr[0] = 'd';
2548                         genarr2.arr[1] = 'e';
2549                         genarr2.arr[2] = 'f';
2550                         genlist.somelist.Add (genarr2);
2551
2552                         Serialize (genlist);
2553                         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);
2554                 }
2555
2556                 [Test]
2557                 public void TestSerializeGenTwoClassCharDouble ()
2558                 {
2559                         GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2560                         Serialize (gentwo);
2561                         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);
2562
2563                         gentwo.something1 = 'a';
2564                         gentwo.something2 = 2.2;
2565
2566                         Serialize (gentwo);
2567                         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);
2568                 }
2569
2570                 [Test]
2571                 public void TestSerializeGenDerivedClassDecimalShort ()
2572                 {
2573                         GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2574                         Serialize (derived);
2575                         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);
2576
2577                         derived.something1 = "Value1";
2578                         derived.something2 = 1;
2579                         derived.another1 = 1.1M;
2580                         derived.another2 = -22;
2581
2582                         Serialize (derived);
2583                         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);
2584                 }
2585
2586                 [Test]
2587                 public void TestSerializeGenDerivedSecondClassByteUlong ()
2588                 {
2589                         GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2590                         Serialize (derived2);
2591                         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);
2592
2593                         derived2.something1 = 1;
2594                         derived2.something2 = 222;
2595                         derived2.another1 = 111;
2596                         derived2.another2 = 222222;
2597
2598                         Serialize (derived2);
2599                         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);
2600                 }
2601
2602                 [Test]
2603                 public void TestSerializeGenNestedClass ()
2604                 {
2605                         GenNestedClass<string, int>.InnerClass<bool> nested =
2606                                 new GenNestedClass<string, int>.InnerClass<bool> ();
2607                         Serialize (nested);
2608                         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);
2609
2610                         nested.inner = 5;
2611                         nested.something = true;
2612
2613                         Serialize (nested);
2614                         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);
2615                 }
2616
2617                 [Test]
2618                 public void TestSerializeGenListClassListNested ()
2619                 {
2620                         GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2621                                 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2622                         Serialize (genlist);
2623                         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);
2624
2625                         GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2626                                 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2627                         GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2628                         inval1.inner = 1;
2629                         inval1.something = "ONE";
2630                         inlist1.somelist.Add (inval1);
2631                         GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2632                         inval2.inner = 2;
2633                         inval2.something = "TWO";
2634                         inlist1.somelist.Add (inval2);
2635                         GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2636                                 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2637                         GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2638                         inval3.inner = 30;
2639                         inval3.something = "THIRTY";
2640                         inlist2.somelist.Add (inval3);
2641                         genlist.somelist.Add (inlist1);
2642                         genlist.somelist.Add (inlist2);
2643
2644                         Serialize (genlist);
2645                         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);
2646                 }
2647
2648                 public enum Myenum { one, two, three, four, five, six };
2649                 [Test]
2650                 public void TestSerializeGenArrayClassEnum ()
2651                 {
2652                         GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2653                         Serialize (genarr);
2654                         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);
2655
2656                         genarr.arr[0] = Myenum.one;
2657                         genarr.arr[1] = Myenum.three;
2658                         genarr.arr[2] = Myenum.five;
2659
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>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2662                 }
2663
2664                 [Test]
2665                 public void TestSerializeGenArrayStruct ()
2666                 {
2667                         GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2668                         Serialize (genarr);
2669                         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);
2670
2671                         GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2672                         genstruct.something = 111;
2673                         genarr.arr[0] = genstruct;
2674                         genstruct.something = 222;
2675                         genarr.arr[1] = genstruct;
2676                         genstruct.something = 333;
2677                         genarr.arr[2] = genstruct;
2678
2679                         Serialize (genarr);
2680                         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);
2681                 }
2682
2683                 [Test]
2684                 public void TestSerializeGenArrayList ()
2685                 {
2686                         GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2687                         Serialize (genarr);
2688                         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);
2689
2690                         GenListClass<string> genlist1 = new GenListClass<string> ();
2691                         genlist1.somelist.Add ("list1-val1");
2692                         genlist1.somelist.Add ("list1-val2");
2693                         genarr.arr[0] = genlist1;
2694                         GenListClass<string> genlist2 = new GenListClass<string> ();
2695                         genlist2.somelist.Add ("list2-val1");
2696                         genlist2.somelist.Add ("list2-val2");
2697                         genlist2.somelist.Add ("list2-val3");
2698                         genlist2.somelist.Add ("list2-val4");
2699                         genarr.arr[1] = genlist2;
2700                         GenListClass<string> genlist3 = new GenListClass<string> ();
2701                         genlist3.somelist.Add ("list3val");
2702                         genarr.arr[2] = genlist3;
2703
2704                         Serialize (genarr);
2705                         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);
2706                 }
2707
2708                 [Test]
2709                 public void TestSerializeGenComplexStruct ()
2710                 {
2711                         GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2712                         Serialize (complex);
2713                         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);
2714
2715                         complex.something = 123;
2716                         complex.simpleclass.something = 456;
2717                         complex.simplestruct.something = 789;
2718                         GenListClass<int> genlist = new GenListClass<int> ();
2719                         genlist.somelist.Add (100);
2720                         genlist.somelist.Add (200);
2721                         complex.listclass = genlist;
2722                         GenArrayClass<int> genarr = new GenArrayClass<int> ();
2723                         genarr.arr[0] = 11;
2724                         genarr.arr[1] = 22;
2725                         genarr.arr[2] = 33;
2726                         complex.arrayclass = genarr;
2727                         complex.twoclass.something1 = 10;
2728                         complex.twoclass.something2 = "Ten";
2729                         complex.derivedclass.another1 = 1;
2730                         complex.derivedclass.another2 = "one";
2731                         complex.derivedclass.something1 = "two";
2732                         complex.derivedclass.something2 = 2;
2733                         complex.derived2.another1 = 3;
2734                         complex.derived2.another2 = "three";
2735                         complex.derived2.something1 = 4;
2736                         complex.derived2.something2 = "four";
2737                         complex.nestedouter.outer = 5;
2738                         complex.nestedinner.inner = "six";
2739                         complex.nestedinner.something = 6;
2740
2741                         Serialize (complex);
2742                         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);
2743                 }
2744
2745                 [Test] // bug #80759
2746                 public void HasNullableField ()
2747                 {
2748                         Bug80759 foo = new Bug80759 ();
2749                         foo.Test = "BAR";
2750                         foo.NullableInt = 10;
2751
2752                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2753
2754                         MemoryStream stream = new MemoryStream ();
2755
2756                         serializer.Serialize (stream, foo);
2757                         stream.Position = 0;
2758                         foo = (Bug80759) serializer.Deserialize (stream);
2759                 }
2760
2761                 [Test] // bug #80759, with fieldSpecified.
2762                 [ExpectedException (typeof (InvalidOperationException))]
2763                 [Category ("NotWorking")]
2764                 public void HasFieldSpecifiedButIrrelevant ()
2765                 {
2766                         Bug80759_2 foo = new Bug80759_2 ();
2767                         foo.Test = "BAR";
2768                         foo.NullableInt = 10;
2769
2770                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2771
2772                         MemoryStream stream = new MemoryStream ();
2773
2774                         serializer.Serialize (stream, foo);
2775                         stream.Position = 0;
2776                         foo = (Bug80759_2) serializer.Deserialize (stream);
2777                 }
2778
2779                 [Test]
2780                 public void HasNullableField2 ()
2781                 {
2782                         Bug80759 foo = new Bug80759 ();
2783                         foo.Test = "BAR";
2784                         foo.NullableInt = 10;
2785
2786                         XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2787
2788                         MemoryStream stream = new MemoryStream ();
2789
2790                         serializer.Serialize (stream, foo);
2791                         stream.Position = 0;
2792                         foo = (Bug80759) serializer.Deserialize (stream);
2793
2794                         Assert.AreEqual ("BAR", foo.Test, "#1");
2795                         Assert.AreEqual (10, foo.NullableInt, "#2");
2796
2797                         foo.NullableInt = null;
2798                         stream = new MemoryStream ();
2799                         serializer.Serialize (stream, foo);
2800                         stream.Position = 0;
2801                         foo = (Bug80759) serializer.Deserialize (stream);
2802
2803                         Assert.AreEqual ("BAR", foo.Test, "#3");
2804                         Assert.IsNull (foo.NullableInt, "#4");
2805                 }
2806
2807                 [Test]
2808                 public void SupportPrivateCtorOnly ()
2809                 {
2810                         XmlSerializer xs =
2811                                 new XmlSerializer (typeof (PrivateCtorOnly));
2812                         StringWriter sw = new StringWriter ();
2813                         xs.Serialize (sw, PrivateCtorOnly.Instance);
2814                         xs.Deserialize (new StringReader (sw.ToString ()));
2815                 }
2816
2817                 [Test]
2818                 public void XmlSchemaProviderQNameBecomesRootName ()
2819                 {
2820                         xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2821                         Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2822                         Assert.AreEqual (Infoset ("<foo />"), WriterText);
2823                         xs.Deserialize (new StringReader ("<foo/>"));
2824                 }
2825
2826                 [Test]
2827                 public void XmlSchemaProviderQNameBecomesRootName2 ()
2828                 {
2829                         string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2830                         xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2831                         Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2832                         Assert.AreEqual (Infoset (xml), WriterText);
2833                         xs.Deserialize (new StringReader (xml));
2834                 }
2835
2836                 [Test]
2837                 public void XmlAnyElementForObjects () // bug #553032
2838                 {
2839                         new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2840                 }
2841
2842                 [Test]
2843                 [ExpectedException (typeof (InvalidOperationException))]
2844                 public void XmlAnyElementForObjects2 () // bug #553032-2
2845                 {
2846                         new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2847                 }
2848
2849
2850                 public class Bug2893 {
2851                         public Bug2893 ()
2852                         {                       
2853                                 Contents = new XmlDataDocument();
2854                         }
2855                         
2856                         [XmlAnyElement("Contents")]
2857                         public XmlNode Contents;
2858                 }
2859
2860                 // Bug Xamarin #2893
2861                 [Test]
2862                 public void XmlAnyElementForXmlNode ()
2863                 {
2864                         var obj = new Bug2893 ();
2865                         XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
2866                         XmlWriterSettings settings = new XmlWriterSettings();
2867
2868                         var xsn = new XmlSerializerNamespaces();
2869                         xsn.Add(string.Empty, string.Empty);
2870
2871                         byte[] buffer = new byte[2048];
2872                         var ms = new MemoryStream(buffer);
2873                         using (var xw = XmlWriter.Create(ms, settings))
2874                         {
2875                                 mySerializer.Serialize(xw, obj, xsn);
2876                                 xw.Flush();
2877                         }
2878
2879                         mySerializer.Serialize(ms, obj);
2880                 }
2881
2882                 [Test]
2883                 public void XmlRootOverridesSchemaProviderQName ()
2884                 {
2885                         var obj = new XmlRootOverridesSchemaProviderQNameType ();
2886
2887                         XmlSerializer xs = new XmlSerializer (obj.GetType ());
2888
2889                         var sw = new StringWriter ();
2890                         using (XmlWriter xw = XmlWriter.Create (sw))
2891                                 xs.Serialize (xw, obj);
2892                         Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2893                 }
2894 #endif
2895
2896                 #endregion //GenericsSeralizationTests
2897
2898                 public class XmlArrayOnInt
2899                 {
2900                         [XmlArray]
2901                         public int Bogus;
2902                 }
2903
2904                 public class XmlArrayUnqualifiedWithNamespace
2905                 {
2906                         [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2907                         public ArrayList Sane = new ArrayList ();
2908                 }
2909
2910                 public class XmlArrayItemUnqualifiedWithNamespace
2911                 {
2912                         [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2913                         public ArrayList Sane = new ArrayList ();
2914                 }
2915
2916                 [XmlRoot (Namespace = "urn:foo")]
2917                 public class XmlArrayOnArrayList
2918                 {
2919                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2920                         public ArrayList Sane = new ArrayList ();
2921                 }
2922
2923                 [XmlRoot (Namespace = "urn:foo")]
2924                 public class XmlArrayOnArray
2925                 {
2926                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2927                         public string[] Sane = new string[] { "foo", "bar" };
2928
2929                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2930                         public ArrayItemInXmlArray[] Mids =
2931                                 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
2932                 }
2933
2934                 [XmlType (Namespace = "urn:gyabo")]
2935                 public class ArrayItemInXmlArray
2936                 {
2937                         [XmlArray (Form = XmlSchemaForm.Unqualified)]
2938                         public string[] Whee = new string[] { "foo", "bar" };
2939                 }
2940
2941                 [XmlRoot ("Base64Binary")]
2942                 public class Base64Binary
2943                 {
2944                         [XmlAttribute (DataType = "base64Binary")]
2945                         public byte [] Data = new byte [] {1, 2, 3};
2946                 }
2947
2948                 [XmlRoot ("HexBinary")]
2949                 public class HexBinary
2950                 {
2951                         [XmlAttribute (DataType = "hexBinary")]
2952                         public byte[] Data = new byte[] { 1, 2, 3 };
2953                 }
2954
2955                 [XmlRoot ("PrivateCtorOnly")]
2956                 public class PrivateCtorOnly
2957                 {
2958                         public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
2959                         private PrivateCtorOnly ()
2960                         {
2961                         }
2962                 }
2963
2964                 public class CDataTextNodesType
2965                 {
2966                         public CDataTextNodesInternal foo;
2967                 }
2968
2969                 public class CDataTextNodesInternal
2970                 {
2971                         [XmlText]
2972                         public string Value;
2973                 }
2974
2975                 public class InvalidTypeContainer
2976                 {
2977                         [XmlElement (DataType = "invalid")]
2978                         public string InvalidTypeItem = "aaa";
2979                 }
2980
2981                 public class TimeSpanContainer1
2982                 {
2983                         [XmlElement (DataType = "duration")]
2984                         public string StringDuration = "aaa";
2985                 }
2986
2987                 public class TimeSpanContainer2
2988                 {
2989                         [XmlElement (DataType = "duration")]
2990                         public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
2991                 }
2992
2993 #if NET_2_0
2994                 public class Bug80759
2995                 {
2996                         public string Test;
2997                         public int? NullableInt;
2998                 }
2999
3000                 public class Bug80759_2
3001                 {
3002                         public string Test;
3003                         public int? NullableInt;
3004
3005                         [XmlIgnore]
3006                         public bool NullableIntSpecified {
3007                                 get { return NullableInt.HasValue; }
3008                         }
3009                 }
3010
3011                 [XmlSchemaProvider ("GetXsdType")]
3012                 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
3013                 {
3014                         public XmlSchema GetSchema ()
3015                         {
3016                                 return null;
3017                         }
3018
3019                         public void ReadXml (XmlReader reader)
3020                         {
3021                                 reader.Skip ();
3022                         }
3023
3024                         public void WriteXml (XmlWriter writer)
3025                         {
3026                         }
3027
3028                         public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
3029                         {
3030                                 if (xss.Count == 0) {
3031                                         XmlSchema xs = new XmlSchema ();
3032                                         XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3033                                         ct.Name = "foo";
3034                                         xs.Items.Add (ct);
3035                                         xss.Add (xs);
3036                                 }
3037                                 return new XmlQualifiedName ("foo");
3038                         }
3039                 }
3040
3041                 public class XmlSchemaProviderQNameBecomesRootNameType2
3042                 {
3043                         [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3044                         public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3045                 }
3046
3047                 public class XmlAnyElementForObjectsType
3048                 {
3049                         [XmlAnyElement]
3050                         public object [] arr = new object [] {3,4,5};
3051                 }
3052
3053                 [XmlRoot ("foo")]
3054                 [XmlSchemaProvider ("GetSchema")]
3055                 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3056                 {
3057                         public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3058                         {
3059                                 var xs = new XmlSchema ();
3060                                 var xse = new XmlSchemaComplexType () { Name = "bar" };
3061                                 xs.Items.Add (xse);
3062                                 xss.Add (xs);
3063                                 return new XmlQualifiedName ("bar");
3064                         }
3065
3066                         XmlSchema IXmlSerializable.GetSchema ()
3067                         {
3068                                 return null;
3069                         }
3070
3071                         void IXmlSerializable.ReadXml (XmlReader reader)
3072                         {
3073                         }
3074                         void IXmlSerializable.WriteXml (XmlWriter writer)
3075                         {
3076                         }
3077                 }
3078
3079 #endif
3080
3081                 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3082                 {
3083                         Assert.Fail ();
3084                 }
3085
3086                 // Helper methods
3087
3088                 public static string Infoset (string sx)
3089                 {
3090                         XmlDocument doc = new XmlDocument ();
3091                         doc.LoadXml (sx);
3092                         StringBuilder sb = new StringBuilder ();
3093                         GetInfoset (doc.DocumentElement, sb);
3094                         return sb.ToString ();
3095                 }
3096
3097                 public static string Infoset (XmlNode nod)
3098                 {
3099                         StringBuilder sb = new StringBuilder ();
3100                         GetInfoset (nod, sb);
3101                         return sb.ToString ();
3102                 }
3103
3104                 static void GetInfoset (XmlNode nod, StringBuilder sb)
3105                 {
3106                         switch (nod.NodeType) {
3107                         case XmlNodeType.Attribute:
3108                                 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3109                                 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3110                                 break;
3111
3112                         case XmlNodeType.Element:
3113                                 XmlElement elem = (XmlElement) nod;
3114                                 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3115
3116                                 ArrayList ats = new ArrayList ();
3117                                 foreach (XmlAttribute at in elem.Attributes)
3118                                         ats.Add (at.LocalName + " " + at.NamespaceURI);
3119
3120                                 ats.Sort ();
3121
3122                                 foreach (string name in ats) {
3123                                         string[] nn = name.Split (' ');
3124                                         GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3125                                 }
3126
3127                                 sb.Append (">");
3128                                 foreach (XmlNode cn in elem.ChildNodes)
3129                                         GetInfoset (cn, sb);
3130                                 sb.Append ("</>");
3131                                 break;
3132
3133                         default:
3134                                 sb.Append (nod.OuterXml);
3135                                 break;
3136                         }
3137                 }
3138
3139                 static XmlTypeMapping CreateSoapMapping (Type type)
3140                 {
3141                         SoapReflectionImporter importer = new SoapReflectionImporter ();
3142                         return importer.ImportTypeMapping (type);
3143                 }
3144
3145                 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3146                 {
3147                         SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3148                         return importer.ImportTypeMapping (type);
3149                 }
3150
3151                 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3152                 {
3153                         SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3154                         return importer.ImportTypeMapping (type);
3155                 }
3156         }
3157 }