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