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