2 // System.Xml.XmlSerializerTests
5 // Erik LeBel <eriklebel@yahoo.ca>
6 // Hagit Yidov <hagity@mainsoft.com>
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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.
17 // Only serializations tests so far, no deserialization.
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
29 using System.Collections;
30 using System.Globalization;
35 using System.Xml.Schema;
36 using System.Xml.Serialization;
37 using System.Reflection;
39 using System.Collections.Generic;
42 using NUnit.Framework;
44 using MonoTests.System.Xml.TestClasses;
46 namespace MonoTests.System.XmlSerialization
49 public class XmlSerializerTests
51 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
52 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
53 const string ANamespace = "some:urn";
54 const string AnotherNamespace = "another:urn";
60 private void SetUpWriter ()
62 sw = new StringWriter ();
63 xtw = new XmlTextWriter (sw);
65 xtw.Formatting = Formatting.None;
68 private string WriterText
72 string val = sw.GetStringBuilder ().ToString ();
73 int offset = val.IndexOf ('>') + 1;
74 val = val.Substring (offset);
79 private void Serialize (object o)
82 xs = new XmlSerializer (o.GetType ());
83 xs.Serialize (xtw, o);
86 private void Serialize (object o, Type type)
89 xs = new XmlSerializer (type);
90 xs.Serialize (xtw, o);
93 private void Serialize (object o, XmlSerializerNamespaces ns)
96 xs = new XmlSerializer (o.GetType ());
97 xs.Serialize (xtw, o, ns);
100 private void Serialize (object o, XmlAttributeOverrides ao)
103 xs = new XmlSerializer (o.GetType (), ao);
104 xs.Serialize (xtw, o);
107 private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
110 xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
111 (XmlRootAttribute) null, defaultNamespace);
112 xs.Serialize (xtw, o);
115 private void Serialize (object o, XmlRootAttribute root)
118 xs = new XmlSerializer (o.GetType (), root);
119 xs.Serialize (xtw, o);
122 private void Serialize (object o, XmlTypeMapping typeMapping)
125 xs = new XmlSerializer (typeMapping);
126 xs.Serialize (xtw, o);
129 private void SerializeEncoded (object o)
131 SerializeEncoded (o, o.GetType ());
134 private void SerializeEncoded (object o, SoapAttributeOverrides ao)
136 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
138 xs = new XmlSerializer (mapping);
139 xs.Serialize (xtw, o);
142 private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
144 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
146 xs = new XmlSerializer (mapping);
147 xs.Serialize (xtw, o);
150 private void SerializeEncoded (object o, Type type)
152 XmlTypeMapping mapping = CreateSoapMapping (type);
154 xs = new XmlSerializer (mapping);
155 xs.Serialize (xtw, o);
158 private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
160 XmlTypeMapping mapping = CreateSoapMapping (type);
161 xs = new XmlSerializer (mapping);
162 xs.Serialize (xtw, o);
166 #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
168 public void TestConstructor()
170 XmlSerializer ser = new XmlSerializer (null, "");
175 // test basic types ////////////////////////////////////////////////////////
177 public void TestSerializeInt ()
180 Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
184 public void TestSerializeBool ()
187 Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
190 Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
194 public void TestSerializeString ()
197 Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
201 public void TestSerializeEmptyString ()
203 Serialize (String.Empty);
204 Assert.AreEqual (Infoset ("<string />"), WriterText);
208 public void TestSerializeNullObject ()
210 Serialize (null, typeof (object));
211 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
212 "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
213 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
217 [Ignore ("The generated XML is not exact but it is equivalent")]
218 public void TestSerializeNullString ()
220 Serialize (null, typeof (string));
221 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
222 "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
223 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
227 public void TestSerializeIntArray ()
229 Serialize (new int[] { 1, 2, 3, 4 });
230 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
231 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
232 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
236 public void TestSerializeEmptyArray ()
238 Serialize (new int[] { });
239 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
240 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
241 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
245 public void TestSerializeChar ()
248 Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
251 Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
254 Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
256 Serialize ('\uFF01');
257 Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
261 public void TestSerializeFloat ()
264 Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
267 Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
269 // FIXME test INF and other boundary conditions that may exist with floats
273 public void TestSerializeEnumeration_FromValue ()
275 Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
277 "<?xml version='1.0' encoding='utf-16'?>" +
278 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
283 [Category ("NotWorking")]
284 public void TestSerializeEnumeration_FromValue_Encoded ()
286 SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
287 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
288 "<?xml version='1.0' encoding='utf-16'?>" +
289 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
290 XmlSchema.InstanceNamespace), sw.ToString ());
294 public void TestSerializeEnumeration ()
296 Serialize (SimpleEnumeration.FIRST);
297 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
299 Serialize (SimpleEnumeration.SECOND);
300 Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
304 public void TestSerializeEnumeration_Encoded ()
306 SerializeEncoded (SimpleEnumeration.FIRST);
307 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
308 "<?xml version='1.0' encoding='utf-16'?>" +
309 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
310 XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
312 SerializeEncoded (SimpleEnumeration.SECOND);
313 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
314 "<?xml version='1.0' encoding='utf-16'?>" +
315 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
316 XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
320 public void TestSerializeEnumDefaultValue ()
322 Serialize (new EnumDefaultValue ());
323 Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
325 Serialize (new SimpleEnumeration ());
326 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
328 Serialize (3, typeof (EnumDefaultValue));
329 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
331 Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
332 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
334 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
335 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
337 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
338 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
340 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
341 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
343 Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
344 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
346 Serialize (3, typeof (FlagEnum));
347 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
349 Serialize (5, typeof (FlagEnum));
350 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
352 Serialize (FlagEnum.e4, typeof (FlagEnum));
353 Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
355 Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
356 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
358 Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
359 Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
361 Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
362 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
364 Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
365 Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
367 Serialize (3, typeof (EnumDefaultValueNF));
368 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
370 Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
371 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
373 Serialize (2, typeof (ZeroFlagEnum));
374 Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t<w>o</ZeroFlagEnum>"), WriterText, "#18");
376 Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
377 Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
381 [Category ("NotWorking")]
382 public void TestSerializeEnumDefaultValue_Encoded ()
384 SerializeEncoded (new EnumDefaultValue ());
385 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
386 "<?xml version='1.0' encoding='utf-16'?>" +
387 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
388 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
390 SerializeEncoded (new SimpleEnumeration ());
391 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
392 "<?xml version='1.0' encoding='utf-16'?>" +
393 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
394 XmlSchema.InstanceNamespace), sw.ToString (), "#2");
396 SerializeEncoded (3, typeof (EnumDefaultValue));
397 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
398 "<?xml version='1.0' encoding='utf-16'?>" +
399 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
400 XmlSchema.InstanceNamespace), sw.ToString (), "#3");
402 SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
403 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
404 "<?xml version='1.0' encoding='utf-16'?>" +
405 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
406 XmlSchema.InstanceNamespace), sw.ToString (), "#4");
408 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
409 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
410 "<?xml version='1.0' encoding='utf-16'?>" +
411 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
412 XmlSchema.InstanceNamespace), sw.ToString (), "#5");
414 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
415 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
416 "<?xml version='1.0' encoding='utf-16'?>" +
417 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
418 XmlSchema.InstanceNamespace), sw.ToString (), "#6");
420 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
421 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
422 "<?xml version='1.0' encoding='utf-16'?>" +
423 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
424 XmlSchema.InstanceNamespace), sw.ToString (), "#7");
426 SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
427 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
428 "<?xml version='1.0' encoding='utf-16'?>" +
429 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
430 XmlSchema.InstanceNamespace), sw.ToString (), "#8");
432 SerializeEncoded (3, typeof (FlagEnum));
433 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
434 "<?xml version='1.0' encoding='utf-16'?>" +
435 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
436 XmlSchema.InstanceNamespace), sw.ToString (), "#9");
438 SerializeEncoded (5, typeof (FlagEnum));
439 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
440 "<?xml version='1.0' encoding='utf-16'?>" +
441 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
442 XmlSchema.InstanceNamespace), sw.ToString (), "#10");
444 SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
445 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
446 "<?xml version='1.0' encoding='utf-16'?>" +
447 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
448 XmlSchema.InstanceNamespace), sw.ToString (), "#11");
450 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
451 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
452 "<?xml version='1.0' encoding='utf-16'?>" +
453 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
454 XmlSchema.InstanceNamespace), sw.ToString (), "#12");
456 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
457 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
458 "<?xml version='1.0' encoding='utf-16'?>" +
459 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
460 XmlSchema.InstanceNamespace), sw.ToString (), "#13");
462 SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
463 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
464 "<?xml version='1.0' encoding='utf-16'?>" +
465 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
466 XmlSchema.InstanceNamespace), sw.ToString (), "#14");
468 SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
469 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
470 "<?xml version='1.0' encoding='utf-16'?>" +
471 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
472 XmlSchema.InstanceNamespace), sw.ToString (), "#15");
474 SerializeEncoded (3, typeof (EnumDefaultValueNF));
475 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
476 "<?xml version='1.0' encoding='utf-16'?>" +
477 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
478 XmlSchema.InstanceNamespace), sw.ToString (), "#16");
480 SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
481 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
482 "<?xml version='1.0' encoding='utf-16'?>" +
483 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
484 XmlSchema.InstanceNamespace), sw.ToString (), "#17");
486 SerializeEncoded (2, typeof (ZeroFlagEnum));
487 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
488 "<?xml version='1.0' encoding='utf-16'?>" +
489 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
490 XmlSchema.InstanceNamespace), sw.ToString (), "#18");
492 SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
493 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
494 "<?xml version='1.0' encoding='utf-16'?>" +
495 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
496 XmlSchema.InstanceNamespace), sw.ToString (), "#19");
500 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
503 Serialize ("b", typeof (EnumDefaultValue));
505 } catch (InvalidOperationException ex) {
506 Assert.IsNotNull (ex.InnerException, "#A2");
507 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
511 Serialize ("e1", typeof (EnumDefaultValue));
513 } catch (InvalidOperationException ex) {
514 Assert.IsNotNull (ex.InnerException, "#B2");
515 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
519 Serialize ("e1,e2", typeof (EnumDefaultValue));
521 } catch (InvalidOperationException ex) {
522 Assert.IsNotNull (ex.InnerException, "#C2");
523 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
527 Serialize (string.Empty, typeof (EnumDefaultValue));
529 } catch (InvalidOperationException ex) {
530 Assert.IsNotNull (ex.InnerException, "#D2");
531 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
535 Serialize ("1", typeof (EnumDefaultValue));
537 } catch (InvalidOperationException ex) {
538 Assert.IsNotNull (ex.InnerException, "#E2");
539 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
543 Serialize ("0", typeof (EnumDefaultValue));
545 } catch (InvalidOperationException ex) {
546 Assert.IsNotNull (ex.InnerException, "#F2");
547 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
551 Serialize (new SimpleClass (), typeof (EnumDefaultValue));
553 } catch (InvalidOperationException ex) {
554 Assert.IsNotNull (ex.InnerException, "#G2");
555 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
560 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
564 Serialize (5, typeof (EnumDefaultValue));
566 } catch (InvalidOperationException ex) {
567 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
568 Assert.IsNotNull (ex.InnerException, "#3");
569 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
570 Assert.IsNotNull (ex.InnerException.Message, "#5");
571 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
572 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
575 Serialize (5, typeof (EnumDefaultValue));
576 Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
581 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
585 Serialize (new EnumDefaultValueNF ());
587 } catch (InvalidOperationException ex) {
588 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
589 Assert.IsNotNull (ex.InnerException, "#3");
590 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
591 Assert.IsNotNull (ex.InnerException.Message, "#5");
592 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
593 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
596 Serialize (new EnumDefaultValueNF ());
597 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
602 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
606 Serialize (15, typeof (EnumDefaultValueNF));
608 } catch (InvalidOperationException ex) {
609 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
610 Assert.IsNotNull (ex.InnerException, "#3");
611 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
612 Assert.IsNotNull (ex.InnerException.Message, "#5");
613 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
614 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
617 Serialize (15, typeof (EnumDefaultValueNF));
618 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
623 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
626 Serialize ("b", typeof (EnumDefaultValueNF));
628 } catch (InvalidOperationException ex) {
629 Assert.IsNotNull (ex.InnerException, "#A2");
630 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
634 Serialize ("e2", typeof (EnumDefaultValueNF));
636 } catch (InvalidOperationException ex) {
637 Assert.IsNotNull (ex.InnerException, "#B2");
638 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
642 Serialize (string.Empty, typeof (EnumDefaultValueNF));
644 } catch (InvalidOperationException ex) {
645 Assert.IsNotNull (ex.InnerException, "#C2");
646 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
650 Serialize ("1", typeof (EnumDefaultValueNF));
652 } catch (InvalidOperationException ex) {
653 Assert.IsNotNull (ex.InnerException, "#D2");
654 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
658 Serialize ("0", typeof (EnumDefaultValueNF));
660 } catch (InvalidOperationException ex) {
661 Assert.IsNotNull (ex.InnerException, "#E2");
662 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
667 public void TestSerializeField ()
669 Field f = new Field ();
670 Serialize (f, typeof (Field));
671 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
672 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
673 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
674 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
676 f.Flags1 = FlagEnum.e1;
677 f.Flags2 = FlagEnum.e1;
678 f.Flags3 = FlagEnum.e2;
679 f.Modifiers = MapModifiers.Protected;
680 f.Modifiers2 = MapModifiers.Public;
681 f.Modifiers3 = MapModifiers.Public;
682 f.Modifiers4 = MapModifiers.Protected;
683 f.Modifiers5 = MapModifiers.Public;
684 Serialize (f, typeof (Field));
685 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
686 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
687 " modifiers='protected' modifiers2='public' />",
688 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
690 f.Flags1 = (FlagEnum) 1;
691 f.Flags1 = FlagEnum.e2;
692 f.Flags2 = FlagEnum.e2;
693 f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
694 f.Modifiers = MapModifiers.Public;
695 f.Modifiers2 = MapModifiers.Protected;
696 f.Modifiers3 = MapModifiers.Protected;
697 f.Modifiers4 = MapModifiers.Public;
698 f.Modifiers5 = MapModifiers.Protected;
699 Serialize (f, typeof (Field));
700 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
701 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
702 " flag4='' modifiers='public' modifiers2='protected'" +
703 " modifiers3='protected' modifiers4='public'" +
704 " modifiers5='protected' />",
705 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
707 f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
708 f.Flags2 = FlagEnum.e2;
709 f.Flags3 = FlagEnum.e4;
710 f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
711 f.Modifiers3 = MapModifiers.Public;
712 f.Modifiers4 = MapModifiers.Protected;
713 f.Modifiers5 = MapModifiers.Public;
714 f.Names = new string[] { "a", "b" };
715 Serialize (f, typeof (Field));
716 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
717 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
718 " flag3='four' flag4='one two four' modifiers='public'" +
719 " modifiers2='protected' names='a b' />",
720 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
722 f.Flags2 = (FlagEnum) 444;
723 f.Flags3 = (FlagEnum) 555;
724 f.Modifiers = (MapModifiers) 666;
725 f.Modifiers2 = (MapModifiers) 777;
726 f.Modifiers3 = (MapModifiers) 0;
727 f.Modifiers4 = (MapModifiers) 888;
728 f.Modifiers5 = (MapModifiers) 999;
731 Serialize (f, typeof (Field));
733 } catch (InvalidOperationException ex) {
734 // There was an error generating the XML document
735 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
736 Assert.IsNotNull (ex.Message, "#E3");
737 Assert.IsNotNull (ex.InnerException, "#E4");
739 // Instance validation error: '444' is not a valid value for
740 // MonoTests.System.Xml.TestClasses.FlagEnum
741 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
742 Assert.IsNotNull (ex.InnerException.Message, "#E6");
743 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
744 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
745 Assert.IsNull (ex.InnerException.InnerException, "#E9");
748 Serialize (f, typeof (Field));
749 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
750 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
751 " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
752 " modifiers4='888' modifiers5='999' names='a b' />",
753 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
758 [Category ("NotDotNet")] // MS bug
759 public void TestSerializeField_Encoded ()
761 Field_Encoded f = new Field_Encoded ();
762 SerializeEncoded (f, typeof (Field_Encoded));
763 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
764 "<?xml version='1.0' encoding='utf-16'?>" +
766 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
767 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
768 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
770 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
771 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
772 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
774 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
775 sw.GetStringBuilder ().ToString (), "#A");
777 f.Flags1 = FlagEnum_Encoded.e1;
778 f.Flags2 = FlagEnum_Encoded.e1;
779 f.Flags3 = FlagEnum_Encoded.e2;
780 f.Modifiers = MapModifiers.Protected;
781 f.Modifiers2 = MapModifiers.Public;
782 f.Modifiers3 = MapModifiers.Public;
783 f.Modifiers4 = MapModifiers.Protected;
784 f.Modifiers5 = MapModifiers.Public;
785 SerializeEncoded (f, typeof (Field_Encoded));
786 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
787 "<?xml version='1.0' encoding='utf-16'?>" +
789 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
790 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
791 " xmlns:q1='some:urn' />",
793 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
794 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
795 " xmlns:q1='some:urn' />",
797 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
798 sw.GetStringBuilder ().ToString (), "#B");
800 f.Flags1 = FlagEnum_Encoded.e2;
801 f.Flags2 = FlagEnum_Encoded.e2;
802 f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
803 f.Modifiers = MapModifiers.Public;
804 f.Modifiers2 = MapModifiers.Protected;
805 f.Modifiers3 = MapModifiers.Protected;
806 f.Modifiers4 = MapModifiers.Public;
807 f.Modifiers5 = MapModifiers.Protected;
808 SerializeEncoded (f, typeof (Field_Encoded));
809 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
810 "<?xml version='1.0' encoding='utf-16'?>" +
812 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
813 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
814 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
815 " xmlns:q1='some:urn' />",
817 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
818 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
819 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
820 " xmlns:q1='some:urn' />",
822 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
823 sw.GetStringBuilder ().ToString (), "#C");
825 f.Flags1 = (FlagEnum_Encoded) 1;
826 f.Flags2 = (FlagEnum_Encoded) 444;
827 f.Flags3 = (FlagEnum_Encoded) 555;
828 f.Modifiers = (MapModifiers) 666;
829 f.Modifiers2 = (MapModifiers) 777;
830 f.Modifiers3 = (MapModifiers) 0;
831 f.Modifiers4 = (MapModifiers) 888;
832 f.Modifiers5 = (MapModifiers) 999;
836 SerializeEncoded (f, typeof (Field_Encoded));
839 } catch (InvalidOperationException ex) {
840 // There was an error generating the XML document
841 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
842 Assert.IsNotNull (ex.Message, "#D3");
843 Assert.IsNotNull (ex.InnerException, "#D4");
845 // Instance validation error: '444' is not a valid value for
846 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
847 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
848 Assert.IsNotNull (ex.InnerException.Message, "#D6");
849 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
850 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
851 Assert.IsNull (ex.InnerException.InnerException, "#D9");
854 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
855 "<?xml version='1.0' encoding='utf-16'?>" +
856 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
857 " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
858 " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
859 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
860 sw.GetStringBuilder ().ToString (), "#D");
865 public void TestSerializeGroup ()
867 Group myGroup = new Group ();
868 myGroup.GroupName = ".NET";
870 Byte[] hexByte = new Byte[] { 0x64, 0x32 };
871 myGroup.GroupNumber = hexByte;
873 DateTime myDate = new DateTime (2002, 5, 2);
874 myGroup.Today = myDate;
875 myGroup.PostitiveInt = "10000";
876 myGroup.IgnoreThis = true;
877 Car thisCar = (Car) myGroup.myCar ("1234566");
878 myGroup.MyVehicle = thisCar;
881 xtw.WriteStartDocument (true);
882 xtw.WriteStartElement ("Wrapper");
883 SerializeEncoded (xtw, myGroup, typeof (Group));
884 xtw.WriteEndElement ();
887 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
889 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
890 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
891 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
892 "<MyVehicle href='#id2' />" +
894 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
895 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
896 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
899 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
902 myGroup.GroupName = null;
903 myGroup.Grouptype = GroupType.B;
904 myGroup.MyVehicle.licenseNumber = null;
905 myGroup.MyVehicle.weight = "450";
908 xtw.WriteStartDocument (true);
909 xtw.WriteStartElement ("Wrapper");
910 SerializeEncoded (xtw, myGroup, typeof (Group));
911 xtw.WriteEndElement ();
914 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
916 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
917 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
918 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
919 "<MyVehicle href='#id2' />" +
921 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
922 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
923 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
926 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
931 public void TestSerializeZeroFlagEnum_InvalidValue ()
935 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
937 } catch (InvalidOperationException ex) {
938 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
939 Assert.IsNotNull (ex.InnerException, "#3");
940 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
941 Assert.IsNotNull (ex.InnerException.Message, "#5");
942 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
943 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
946 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
947 Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
952 public void TestSerializeQualifiedName ()
954 Serialize (new XmlQualifiedName ("me", "home.urn"));
955 Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
959 public void TestSerializeBytes ()
961 Serialize ((byte) 0xAB);
962 Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
964 Serialize ((byte) 15);
965 Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
969 public void TestSerializeByteArrays ()
971 Serialize (new byte[] { });
972 Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
974 Serialize (new byte[] { 0xAB, 0xCD });
975 Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
979 public void TestSerializeDateTime ()
981 DateTime d = new DateTime ();
984 TimeZone tz = TimeZone.CurrentTimeZone;
985 TimeSpan off = tz.GetUtcOffset (d);
986 string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
988 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
990 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
1001 public void TestSerialize()
1004 Assert.AreEqual (WriterText, "");
1008 // test basic class serialization /////////////////////////////////////
1010 public void TestSerializeSimpleClass ()
1012 SimpleClass simple = new SimpleClass ();
1014 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1016 simple.something = "hello";
1019 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
1023 public void TestSerializeStringCollection ()
1025 StringCollection strings = new StringCollection ();
1026 Serialize (strings);
1027 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1029 strings.Add ("hello");
1030 strings.Add ("goodbye");
1031 Serialize (strings);
1032 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
1036 public void TestSerializeOptionalValueTypeContainer ()
1038 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1040 OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
1042 Serialize (optionalValue);
1043 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1044 "<?xml version='1.0' encoding='utf-16'?>" +
1046 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
1048 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
1050 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
1051 sw.ToString (), "#1");
1053 attr = new XmlAttributes ();
1055 // remove the DefaultValue attribute on the Flags member
1056 overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
1057 // remove the DefaultValue attribute on the Attributes member
1058 overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
1060 Serialize (optionalValue, overrides);
1061 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1062 "<?xml version='1.0' encoding='utf-16'?>" +
1064 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1066 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1068 "<Attributes xmlns='{3}'>one four</Attributes>" +
1069 "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1070 AnotherNamespace, ANamespace), sw.ToString (), "#2");
1072 optionalValue.FlagsSpecified = true;
1073 Serialize (optionalValue, overrides);
1074 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1075 "<?xml version='1.0' encoding='utf-16'?>" +
1077 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1079 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1081 "<Attributes xmlns='{3}'>one four</Attributes>" +
1082 "<Flags xmlns='{3}'>one</Flags>" +
1084 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
1085 ANamespace), sw.ToString (), "#3");
1089 public void TestSerializePlainContainer ()
1091 StringCollectionContainer container = new StringCollectionContainer ();
1092 Serialize (container);
1093 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
1095 container.Messages.Add ("hello");
1096 container.Messages.Add ("goodbye");
1097 Serialize (container);
1098 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
1102 public void TestSerializeArrayContainer ()
1104 ArrayContainer container = new ArrayContainer ();
1105 Serialize (container);
1106 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1108 container.items = new object[] { 10, 20 };
1109 Serialize (container);
1110 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
1112 container.items = new object[] { 10, "hello" };
1113 Serialize (container);
1114 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
1118 public void TestSerializeClassArrayContainer ()
1120 ClassArrayContainer container = new ClassArrayContainer ();
1121 Serialize (container);
1122 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1124 SimpleClass simple1 = new SimpleClass ();
1125 simple1.something = "hello";
1126 SimpleClass simple2 = new SimpleClass ();
1127 simple2.something = "hello";
1128 container.items = new SimpleClass[2];
1129 container.items[0] = simple1;
1130 container.items[1] = simple2;
1131 Serialize (container);
1132 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
1135 // test basic attributes ///////////////////////////////////////////////
1137 public void TestSerializeSimpleClassWithXmlAttributes ()
1139 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1141 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1143 simple.something = "hello";
1145 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
1148 // test overrides ///////////////////////////////////////////////////////
1150 public void TestSerializeSimpleClassWithOverrides ()
1152 // Also tests XmlIgnore
1153 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1155 XmlAttributes attr = new XmlAttributes ();
1156 attr.XmlIgnore = true;
1157 overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
1159 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1160 simple.something = "hello";
1161 Serialize (simple, overrides);
1162 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1166 public void TestSerializeSchema ()
1168 XmlSchema schema = new XmlSchema ();
1169 schema.Items.Add (new XmlSchemaAttribute ());
1170 schema.Items.Add (new XmlSchemaAttributeGroup ());
1171 schema.Items.Add (new XmlSchemaComplexType ());
1172 schema.Items.Add (new XmlSchemaNotation ());
1173 schema.Items.Add (new XmlSchemaSimpleType ());
1174 schema.Items.Add (new XmlSchemaGroup ());
1175 schema.Items.Add (new XmlSchemaElement ());
1177 StringWriter sw = new StringWriter ();
1178 XmlTextWriter xtw = new XmlTextWriter (sw);
1179 xtw.QuoteChar = '\'';
1180 xtw.Formatting = Formatting.Indented;
1181 XmlSerializer xs = new XmlSerializer (schema.GetType ());
1182 xs.Serialize (xtw, schema);
1184 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1185 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1186 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1187 " <xsd:attribute />{0}" +
1188 " <xsd:attributeGroup />{0}" +
1189 " <xsd:complexType />{0}" +
1190 " <xsd:notation />{0}" +
1191 " <xsd:simpleType />{0}" +
1192 " <xsd:group />{0}" +
1193 " <xsd:element />{0}" +
1194 "</xsd:schema>", Environment.NewLine), sw.ToString ());
1197 // test xmlText //////////////////////////////////////////////////////////
1199 public void TestSerializeXmlTextAttribute ()
1201 SimpleClass simple = new SimpleClass ();
1202 simple.something = "hello";
1204 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1205 XmlAttributes attr = new XmlAttributes ();
1206 overrides.Add (typeof (SimpleClass), "something", attr);
1208 attr.XmlText = new XmlTextAttribute ();
1209 Serialize (simple, overrides);
1210 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
1212 attr.XmlText = new XmlTextAttribute (typeof (string));
1213 Serialize (simple, overrides);
1214 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
1217 attr.XmlText = new XmlTextAttribute (typeof (byte[]));
1218 Serialize (simple, overrides);
1219 Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1220 } catch (InvalidOperationException ex) {
1221 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1222 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1223 Assert.IsNotNull (ex.Message, "#A3");
1224 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
1225 Assert.IsNotNull (ex.InnerException, "#A5");
1227 // there was an error reflecting field 'something'.
1228 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
1229 Assert.IsNotNull (ex.InnerException.Message, "#A7");
1230 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
1231 Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
1233 // the type for XmlText may not be specified for primitive types.
1234 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
1235 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
1236 Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
1240 attr.XmlText = new XmlTextAttribute ();
1241 attr.XmlText.DataType = "sometype";
1242 Serialize (simple, overrides);
1243 Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1244 } catch (InvalidOperationException ex) {
1245 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1246 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1247 Assert.IsNotNull (ex.Message, "#B3");
1248 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
1249 Assert.IsNotNull (ex.InnerException, "#B5");
1251 // There was an error reflecting field 'something'.
1252 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
1253 Assert.IsNotNull (ex.InnerException.Message, "#B7");
1254 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
1255 Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
1259 // There was an error reflecting type 'System.String'.
1260 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1261 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1262 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1263 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1265 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
1266 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1267 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1268 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1269 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1270 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1275 // test xmlRoot //////////////////////////////////////////////////////////
1277 public void TestSerializeXmlRootAttribute ()
1279 // constructor override & element name
1280 XmlRootAttribute root = new XmlRootAttribute ();
1281 root.ElementName = "renamed";
1283 SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
1284 Serialize (simpleWithAttributes, root);
1285 Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1287 SimpleClass simple = null;
1288 root.IsNullable = false;
1290 Serialize (simple, root);
1291 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1292 } catch (NullReferenceException) {
1295 root.IsNullable = true;
1297 Serialize (simple, root);
1298 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1299 } catch (NullReferenceException) {
1302 simple = new SimpleClass ();
1303 root.ElementName = null;
1304 root.Namespace = "some.urn";
1305 Serialize (simple, root);
1306 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
1310 public void TestSerializeXmlRootAttributeOnMember ()
1313 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1314 XmlAttributes childAttr = new XmlAttributes ();
1315 childAttr.XmlRoot = new XmlRootAttribute ("simple");
1316 overrides.Add (typeof (SimpleClass), childAttr);
1318 XmlAttributes attr = new XmlAttributes ();
1319 attr.XmlRoot = new XmlRootAttribute ("simple");
1320 overrides.Add (typeof (ClassArrayContainer), attr);
1322 ClassArrayContainer container = new ClassArrayContainer ();
1323 container.items = new SimpleClass[1];
1324 container.items[0] = new SimpleClass ();
1325 Serialize (container, overrides);
1326 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
1328 // FIXME test data type
1331 // test XmlAttribute /////////////////////////////////////////////////////
1333 public void TestSerializeXmlAttributeAttribute ()
1336 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1337 XmlAttributes attr = new XmlAttributes ();
1338 attr.XmlAttribute = new XmlAttributeAttribute ();
1339 overrides.Add (typeof (SimpleClass), "something", attr);
1341 SimpleClass simple = new SimpleClass (); ;
1342 Serialize (simple, overrides);
1343 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1346 simple.something = "hello";
1347 Serialize (simple, overrides);
1348 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
1351 attr.XmlAttribute.AttributeName = "somethingelse";
1352 Serialize (simple, overrides);
1353 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
1356 // FIXME this should work, shouldnt it?
1357 // attr.XmlAttribute.Type = typeof(string);
1358 // Serialize(simple, overrides);
1359 // Assert(WriterText.EndsWith(" something='hello' />"));
1362 attr.XmlAttribute.Namespace = "some:urn";
1363 Serialize (simple, overrides);
1364 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
1367 // FIXME XmlSchemaForm Form
1369 // FIXME write XmlQualifiedName as attribute
1372 // test XmlElement ///////////////////////////////////////////////////////
1374 public void TestSerializeXmlElementAttribute ()
1376 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1377 XmlAttributes attr = new XmlAttributes ();
1378 XmlElementAttribute element = new XmlElementAttribute ();
1379 attr.XmlElements.Add (element);
1380 overrides.Add (typeof (SimpleClass), "something", attr);
1383 SimpleClass simple = new SimpleClass (); ;
1384 Serialize (simple, overrides);
1385 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1388 simple.something = "hello";
1389 Serialize (simple, overrides);
1390 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
1393 element.ElementName = "saying";
1394 Serialize (simple, overrides);
1395 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
1398 element.IsNullable = false;
1399 simple.something = null;
1400 Serialize (simple, overrides);
1401 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
1403 element.IsNullable = true;
1404 simple.something = null;
1405 Serialize (simple, overrides);
1406 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
1409 element.ElementName = null;
1410 element.IsNullable = false;
1411 element.Namespace = "some:urn";
1412 simple.something = "hello";
1413 Serialize (simple, overrides);
1414 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
1421 // test XmlElementAttribute with arrays and collections //////////////////
1423 public void TestSerializeCollectionWithXmlElementAttribute ()
1426 // if no type is specified or the specified type
1427 // matches the contents of the collection,
1428 // serialize each element in an element named after the member.
1429 // if the type does not match, or matches the collection itself,
1430 // create a base wrapping element for the member, and then
1431 // wrap each collection item in its own wrapping element based on type.
1433 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1434 XmlAttributes attr = new XmlAttributes ();
1435 XmlElementAttribute element = new XmlElementAttribute ();
1436 attr.XmlElements.Add (element);
1437 overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
1439 // empty collection & no type info in XmlElementAttribute
1440 StringCollectionContainer container = new StringCollectionContainer ();
1441 Serialize (container, overrides);
1442 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1444 // non-empty collection & no type info in XmlElementAttribute
1445 container.Messages.Add ("hello");
1446 Serialize (container, overrides);
1447 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
1449 // non-empty collection & only type info in XmlElementAttribute
1450 element.Type = typeof (StringCollection);
1451 Serialize (container, overrides);
1452 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
1454 // non-empty collection & only type info in XmlElementAttribute
1455 element.Type = typeof (string);
1456 Serialize (container, overrides);
1457 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4");
1460 container.Messages.Add ("goodbye");
1461 element.Type = null;
1462 Serialize (container, overrides);
1463 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText, "#5");
1466 // test DefaultValue /////////////////////////////////////////////////////
1468 public void TestSerializeDefaultValueAttribute ()
1470 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1472 XmlAttributes attr = new XmlAttributes ();
1473 string defaultValueInstance = "nothing";
1474 attr.XmlDefaultValue = defaultValueInstance;
1475 overrides.Add (typeof (SimpleClass), "something", attr);
1478 SimpleClass simple = new SimpleClass ();
1479 Serialize (simple, overrides);
1480 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1482 // same value as default
1483 simple.something = defaultValueInstance;
1484 Serialize (simple, overrides);
1485 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1488 simple.something = "hello";
1489 Serialize (simple, overrides);
1490 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3");
1492 overrides = new XmlAttributeOverrides ();
1493 attr = new XmlAttributes ();
1494 attr.XmlAttribute = new XmlAttributeAttribute ();
1495 attr.XmlDefaultValue = defaultValueInstance;
1496 overrides.Add (typeof (SimpleClass), "something", attr);
1499 simple = new SimpleClass ();
1500 Serialize (simple, overrides);
1501 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1503 // same value as default
1504 simple.something = defaultValueInstance;
1505 Serialize (simple, overrides);
1506 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
1509 simple.something = "hello";
1510 Serialize (simple, overrides);
1511 Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
1513 overrides = new XmlAttributeOverrides ();
1514 attr = new XmlAttributes ();
1515 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1516 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1519 TestDefault testDefault = new TestDefault ();
1520 Serialize (testDefault);
1521 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
1523 // use the default with overrides
1524 Serialize (testDefault, overrides);
1525 Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
1527 overrides = new XmlAttributeOverrides ();
1528 attr = new XmlAttributes ();
1529 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1530 attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
1531 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1533 // use the default with overrides
1534 Serialize (testDefault, overrides);
1535 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
1537 // use the default with overrides and default namspace
1538 Serialize (testDefault, overrides, AnotherNamespace);
1539 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
1541 // non-default values
1542 testDefault.strDefault = "Some Text";
1543 testDefault.boolT = false;
1544 testDefault.boolF = true;
1545 testDefault.decimalval = 20m;
1546 testDefault.flag = FlagEnum.e2;
1547 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1548 Serialize (testDefault);
1549 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1550 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1551 " <strDefault>Some Text</strDefault>" +
1552 " <boolT>false</boolT>" +
1553 " <boolF>true</boolF>" +
1554 " <decimalval>20</decimalval>" +
1555 " <flag>two</flag>" +
1556 " <flagencoded>e1 e2</flagencoded>" +
1557 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1560 Serialize (testDefault, overrides);
1561 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1562 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1563 " <strDefault>Some Text</strDefault>" +
1564 " <boolT>false</boolT>" +
1565 " <boolF>true</boolF>" +
1566 " <decimalval>20</decimalval>" +
1567 " <flag>two</flag>" +
1568 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1571 Serialize (testDefault, overrides, AnotherNamespace);
1572 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1573 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1574 " <strDefault>Some Text</strDefault>" +
1575 " <boolT>false</boolT>" +
1576 " <boolF>true</boolF>" +
1577 " <decimalval>20</decimalval>" +
1578 " <flag>two</flag>" +
1579 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1582 attr = new XmlAttributes ();
1583 XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
1584 xmlType.Namespace = "yetanother:urn";
1585 attr.XmlType = xmlType;
1586 overrides.Add (typeof (FlagEnum_Encoded), attr);
1588 Serialize (testDefault, overrides, AnotherNamespace);
1589 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1590 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1591 " <strDefault>Some Text</strDefault>" +
1592 " <boolT>false</boolT>" +
1593 " <boolF>true</boolF>" +
1594 " <decimalval>20</decimalval>" +
1595 " <flag>two</flag>" +
1596 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1599 attr = new XmlAttributes ();
1600 attr.XmlType = new XmlTypeAttribute ("testDefault");
1601 overrides.Add (typeof (TestDefault), attr);
1603 Serialize (testDefault, overrides, AnotherNamespace);
1604 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1605 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1606 " <strDefault>Some Text</strDefault>" +
1607 " <boolT>false</boolT>" +
1608 " <boolF>true</boolF>" +
1609 " <decimalval>20</decimalval>" +
1610 " <flag>two</flag>" +
1611 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1612 AnotherNamespace)), WriterText, "#C9");
1616 [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
1617 public void TestSerializeDefaultValueAttribute_Encoded ()
1619 SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
1620 SoapAttributes attr = new SoapAttributes ();
1621 attr.SoapAttribute = new SoapAttributeAttribute ();
1622 string defaultValueInstance = "nothing";
1623 attr.SoapDefaultValue = defaultValueInstance;
1624 overrides.Add (typeof (SimpleClass), "something", attr);
1627 SimpleClass simple = new SimpleClass ();
1628 SerializeEncoded (simple, overrides);
1629 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1631 // same value as default
1632 simple.something = defaultValueInstance;
1633 SerializeEncoded (simple, overrides);
1634 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1637 simple.something = "hello";
1638 SerializeEncoded (simple, overrides);
1639 Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
1641 attr.SoapAttribute = null;
1642 attr.SoapElement = new SoapElementAttribute ();
1645 simple = new SimpleClass ();
1646 SerializeEncoded (simple, overrides);
1647 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1649 // same value as default
1650 simple.something = defaultValueInstance;
1651 SerializeEncoded (simple, overrides);
1652 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
1655 simple.something = "hello";
1656 SerializeEncoded (simple, overrides);
1657 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
1659 overrides = new SoapAttributeOverrides ();
1660 attr = new SoapAttributes ();
1661 attr.SoapElement = new SoapElementAttribute ("flagenc");
1662 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1664 // use the default (from MS KB325691)
1665 TestDefault testDefault = new TestDefault ();
1666 SerializeEncoded (testDefault);
1667 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1668 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1669 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1670 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1671 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1672 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1673 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1674 " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1675 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1678 SerializeEncoded (testDefault, overrides);
1679 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1680 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1681 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1682 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1683 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1684 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1685 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1686 " <flagenc xsi:type='flagenum'>one four</flagenc>" +
1687 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1690 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1691 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1692 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1693 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1694 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1695 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1696 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1697 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1698 " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1699 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1700 AnotherNamespace)), WriterText, "#C3");
1702 // non-default values
1703 testDefault.strDefault = "Some Text";
1704 testDefault.boolT = false;
1705 testDefault.boolF = true;
1706 testDefault.decimalval = 20m;
1707 testDefault.flag = FlagEnum.e2;
1708 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1709 SerializeEncoded (testDefault);
1710 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1711 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1712 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1713 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1714 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1715 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1716 " <flag xsi:type='FlagEnum'>e2</flag>" +
1717 " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1718 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1721 SerializeEncoded (testDefault, overrides);
1722 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1723 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1724 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1725 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1726 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1727 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1728 " <flag xsi:type='FlagEnum'>e2</flag>" +
1729 " <flagenc xsi:type='flagenum'>one two</flagenc>" +
1730 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1733 attr = new SoapAttributes ();
1734 attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
1735 overrides.Add (typeof (FlagEnum_Encoded), attr);
1737 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1738 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1739 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1740 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1741 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1742 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1743 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1744 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1745 " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1746 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1747 AnotherNamespace)), WriterText, "#C6");
1749 attr = new SoapAttributes ();
1750 attr.SoapType = new SoapTypeAttribute ("testDefault");
1751 overrides.Add (typeof (TestDefault), attr);
1753 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1754 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1755 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1756 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1757 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1758 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1759 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1760 " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1761 " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1762 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1763 AnotherNamespace)), WriterText, "#C7");
1766 // test XmlEnum //////////////////////////////////////////////////////////
1768 public void TestSerializeXmlEnumAttribute ()
1770 Serialize (XmlSchemaForm.Qualified);
1771 Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
1773 Serialize (XmlSchemaForm.Unqualified);
1774 Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
1778 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1780 // technically XmlSchemaForm.None has an XmlIgnore attribute,
1781 // but it is not being serialized as a member.
1785 Serialize (XmlSchemaForm.None);
1787 } catch (InvalidOperationException ex) {
1788 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1789 Assert.IsNotNull (ex.InnerException, "#3");
1790 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
1791 Assert.IsNotNull (ex.InnerException.Message, "#5");
1792 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
1793 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
1796 Serialize (XmlSchemaForm.None);
1797 Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
1802 public void TestSerializeXmlNodeArray ()
1804 XmlDocument doc = new XmlDocument ();
1805 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1806 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1810 public void TestSerializeXmlNodeArray2 ()
1812 XmlDocument doc = new XmlDocument ();
1813 Serialize (new XmlNode[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1814 Assert.AreEqual (Infoset (String.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
1818 [ExpectedException (typeof (InvalidOperationException))]
1819 [Category ("NotWorking")]
1820 public void TestSerializeXmlNodeArrayIncludesAttribute ()
1822 XmlDocument doc = new XmlDocument ();
1823 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1827 public void TestSerializeXmlElementArray ()
1829 XmlDocument doc = new XmlDocument ();
1830 Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1831 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1836 [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
1837 public void TestSerializeGenericListOfNode ()
1839 XmlDocument doc = new XmlDocument ();
1840 Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1841 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1845 [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
1846 public void TestSerializeGenericListOfElement ()
1848 XmlDocument doc = new XmlDocument ();
1849 Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1850 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1854 public void TestSerializeXmlDocument ()
1856 XmlDocument doc = new XmlDocument ();
1857 doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
1858 Serialize (doc, typeof (XmlDocument));
1859 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
1860 sw.GetStringBuilder ().ToString ());
1864 public void TestSerializeXmlElement ()
1866 XmlDocument doc = new XmlDocument ();
1867 Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
1868 Assert.AreEqual (Infoset ("<elem/>"), WriterText);
1872 public void TestSerializeXmlElementSubclass ()
1874 XmlDocument doc = new XmlDocument ();
1875 Serialize (new MyElem (doc), typeof (XmlElement));
1876 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
1878 Serialize (new MyElem (doc), typeof (MyElem));
1879 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
1883 public void TestSerializeXmlCDataSection ()
1885 XmlDocument doc = new XmlDocument ();
1886 CDataContainer c = new CDataContainer ();
1887 c.cdata = doc.CreateCDataSection ("data section contents");
1889 Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
1893 public void TestSerializeXmlNode ()
1895 XmlDocument doc = new XmlDocument ();
1896 NodeContainer c = new NodeContainer ();
1897 c.node = doc.CreateTextNode ("text");
1899 Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
1903 public void TestSerializeChoice ()
1905 Choices ch = new Choices ();
1906 ch.MyChoice = "choice text";
1907 ch.ItemType = ItemChoiceType.ChoiceZero;
1909 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText, "#1");
1910 ch.ItemType = ItemChoiceType.StrangeOne;
1912 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText, "#2");
1913 ch.ItemType = ItemChoiceType.ChoiceTwo;
1915 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText, "#3");
1919 public void TestSerializeNamesWithSpaces ()
1921 TestSpace ts = new TestSpace ();
1925 Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
1929 public void TestSerializeReadOnlyProps ()
1931 ReadOnlyProperties ts = new ReadOnlyProperties ();
1933 Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1937 public void TestSerializeReadOnlyListProp ()
1939 ReadOnlyListProperty ts = new ReadOnlyListProperty ();
1941 Assert.AreEqual (Infoset ("<ReadOnlyListProperty xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><StrList><string>listString1</string><string>listString2</string></StrList></ReadOnlyListProperty>"), WriterText);
1946 public void TestSerializeIList ()
1948 clsPerson k = new clsPerson ();
1949 k.EmailAccounts = new ArrayList ();
1950 k.EmailAccounts.Add ("a");
1951 k.EmailAccounts.Add ("b");
1953 Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
1957 public void TestSerializeArrayEnc ()
1959 SoapReflectionImporter imp = new SoapReflectionImporter ();
1960 XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1961 XmlSerializer ser = new XmlSerializer (map);
1962 StringWriter sw = new StringWriter ();
1963 XmlTextWriter tw = new XmlTextWriter (sw);
1964 tw.WriteStartElement ("aa");
1965 ser.Serialize (tw, new ArrayClass ());
1966 tw.WriteEndElement ();
1969 [Test] // bug #76049
1970 public void TestIncludeType ()
1972 XmlReflectionImporter imp = new XmlReflectionImporter ();
1973 XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1974 imp.IncludeType (typeof (TestSpace));
1975 XmlSerializer ser = new XmlSerializer (map);
1976 ser.Serialize (new StringWriter (), new TestSpace ());
1980 public void TestSerializeChoiceArray ()
1982 CompositeValueType v = new CompositeValueType ();
1985 Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
1989 public void TestArrayAttributeWithDataType ()
1991 Serialize (new ArrayAttributeWithType ());
1992 string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1993 res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1994 Assert.AreEqual (Infoset (res), WriterText);
1998 public void TestSubclassElementType ()
2000 SubclassTestContainer c = new SubclassTestContainer ();
2001 c.data = new SubclassTestSub ();
2004 string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
2005 res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
2006 Assert.AreEqual (Infoset (res), WriterText);
2010 [ExpectedException (typeof (InvalidOperationException))]
2011 public void TestArrayAttributeWithWrongDataType ()
2013 Serialize (new ArrayAttributeWithWrongType ());
2017 [Category ("NotWorking")]
2018 public void TestSerializePrimitiveTypesContainer ()
2020 Serialize (new PrimitiveTypesContainer ());
2021 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2022 "<?xml version='1.0' encoding='utf-16'?>" +
2024 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
2026 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
2028 "<Number>2004</Number>" +
2029 "<Name>some name</Name>" +
2030 "<Index>56</Index>" +
2031 "<Password>8w8=</Password>" +
2032 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2033 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2034 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2036 SerializeEncoded (new PrimitiveTypesContainer ());
2037 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2038 "<?xml version='1.0' encoding='utf-16'?>" +
2040 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2042 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2044 "<Number xsi:type='xsd:int'>2004</Number>" +
2045 "<Name xsi:type='xsd:string'>some name</Name>" +
2046 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2047 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2048 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2049 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2050 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2051 sw.ToString (), "#2");
2055 public void TestSchemaForm ()
2057 TestSchemaForm1 t1 = new TestSchemaForm1 ();
2058 t1.p1 = new PrintTypeResponse ();
2060 t1.p2 = new PrintTypeResponse ();
2063 TestSchemaForm2 t2 = new TestSchemaForm2 ();
2064 t2.p1 = new PrintTypeResponse ();
2066 t2.p2 = new PrintTypeResponse ();
2071 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2072 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2075 res += " <data>data1</data>";
2076 res += " </result>";
2077 res += " <intern xmlns=\"urn:responseTypes\">";
2078 res += " <result xmlns=\"\">";
2079 res += " <data>data2</data>";
2080 res += " </result>";
2081 res += " </intern>";
2083 res += " <p2 xmlns=\"urn:oo\">";
2084 res += " <result xmlns=\"\">";
2085 res += " <data>data1</data>";
2086 res += " </result>";
2087 res += " <intern xmlns=\"urn:responseTypes\">";
2088 res += " <result xmlns=\"\">";
2089 res += " <data>data2</data>";
2090 res += " </result>";
2091 res += " </intern>";
2093 res += "</TestSchemaForm1>";
2094 Assert.AreEqual (Infoset (res), WriterText);
2098 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2099 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2100 res += " <p1 xmlns=\"urn:testForm\">";
2101 res += " <result xmlns=\"\">";
2102 res += " <data>data1</data>";
2103 res += " </result>";
2104 res += " <intern xmlns=\"urn:responseTypes\">";
2105 res += " <result xmlns=\"\">";
2106 res += " <data>data2</data>";
2107 res += " </result>";
2108 res += " </intern>";
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>";
2120 res += "</TestSchemaForm2>";
2121 Assert.AreEqual (Infoset (res), WriterText);
2123 XmlReflectionImporter imp = new XmlReflectionImporter ();
2124 XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2125 Serialize (t1, map);
2127 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2128 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
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>";
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>";
2149 res += "</TestSchemaForm1>";
2150 Assert.AreEqual (Infoset (res), WriterText);
2152 imp = new XmlReflectionImporter ();
2153 map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2154 Serialize (t2, map);
2156 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2157 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2158 res += " <p1 xmlns=\"urn:testForm\">";
2159 res += " <result xmlns=\"\">";
2160 res += " <data>data1</data>";
2161 res += " </result>";
2162 res += " <intern xmlns=\"urn:responseTypes\">";
2163 res += " <result xmlns=\"\">";
2164 res += " <data>data2</data>";
2165 res += " </result>";
2166 res += " </intern>";
2168 res += " <p2 xmlns=\"urn:oo\">";
2169 res += " <result xmlns=\"\">";
2170 res += " <data>data1</data>";
2171 res += " </result>";
2172 res += " <intern xmlns=\"urn:responseTypes\">";
2173 res += " <result xmlns=\"\">";
2174 res += " <data>data2</data>";
2175 res += " </result>";
2176 res += " </intern>";
2178 res += "</TestSchemaForm2>";
2179 Assert.AreEqual (Infoset (res), WriterText);
2182 [Test] // bug #78536
2183 public void CDataTextNodes ()
2185 XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2186 ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2187 string xml = @"<CDataTextNodesType>
2189 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2190 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2191 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2193 </CDataTextNodesType>";
2194 ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2199 public void GenerateSerializerGenerics ()
2201 XmlReflectionImporter imp = new XmlReflectionImporter ();
2202 Type type = typeof (List<int>);
2203 XmlSerializer.GenerateSerializer (
2205 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2210 public void Nullable ()
2212 XmlSerializer ser = new XmlSerializer (typeof (int?));
2213 int? nullableType = 5;
2214 sw = new StringWriter ();
2215 xtw = new XmlTextWriter (sw);
2216 ser.Serialize (xtw, nullableType);
2218 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2219 Assert.AreEqual (Infoset (expected), WriterText);
2220 int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2221 Assert.AreEqual (5, i);
2225 public void NullableEnums ()
2227 WithNulls w = new WithNulls ();
2228 XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2229 StringWriter tw = new StringWriter ();
2230 ser.Serialize (tw, w);
2232 string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2233 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2234 "<nint xsi:nil='true' />" +
2235 "<nenum xsi:nil='true' />" +
2236 "<ndate xsi:nil='true' />" +
2239 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2241 StringReader sr = new StringReader (tw.ToString ());
2242 w = (WithNulls) ser.Deserialize (sr);
2244 Assert.IsFalse (w.nint.HasValue);
2245 Assert.IsFalse (w.nenum.HasValue);
2246 Assert.IsFalse (w.ndate.HasValue);
2248 DateTime t = new DateTime (2008,4,1);
2251 w.nenum = TestEnumWithNulls.bb;
2253 tw = new StringWriter ();
2254 ser.Serialize (tw, w);
2256 expected = "<?xml version='1.0' encoding='utf-16'?>" +
2257 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2259 "<nenum>bb</nenum>" +
2260 "<ndate>2008-04-01T00:00:00</ndate>" +
2263 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2265 sr = new StringReader (tw.ToString ());
2266 w = (WithNulls) ser.Deserialize (sr);
2268 Assert.IsTrue (w.nint.HasValue);
2269 Assert.IsTrue (w.nenum.HasValue);
2270 Assert.IsTrue (w.ndate.HasValue);
2271 Assert.AreEqual (4, w.nint.Value);
2272 Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2273 Assert.AreEqual (t, w.ndate.Value);
2277 public void SerializeBase64Binary()
2279 XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2280 sw = new StringWriter ();
2281 XmlTextWriter xtw = new XmlTextWriter (sw);
2282 ser.Serialize (xtw, new Base64Binary ());
2284 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
2285 Assert.AreEqual (Infoset (expected), WriterText);
2286 Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2287 Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2290 [Test] // bug #79989, #79990
2291 public void SerializeHexBinary ()
2293 XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2294 sw = new StringWriter ();
2295 XmlTextWriter xtw = new XmlTextWriter (sw);
2296 ser.Serialize (xtw, new HexBinary ());
2298 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
2299 Assert.AreEqual (Infoset (expected), WriterText);
2300 HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2301 Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2305 [ExpectedException (typeof (InvalidOperationException))]
2306 public void XmlArrayAttributeOnInt ()
2308 new XmlSerializer (typeof (XmlArrayOnInt));
2312 [ExpectedException (typeof (InvalidOperationException))]
2313 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2315 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2319 [ExpectedException (typeof (InvalidOperationException))]
2320 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2322 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2325 [Test] // bug #78042
2326 public void XmlArrayAttributeOnArray ()
2328 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2329 sw = new StringWriter ();
2330 XmlTextWriter xtw = new XmlTextWriter (sw);
2331 ser.Serialize (xtw, new XmlArrayOnArray ());
2333 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
2334 Assert.AreEqual (Infoset (expected), WriterText);
2338 public void XmlArrayAttributeOnCollection ()
2340 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2341 XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2342 inst.Sane.Add ("abc");
2344 sw = new StringWriter ();
2345 XmlTextWriter xtw = new XmlTextWriter (sw);
2346 ser.Serialize (xtw, inst);
2348 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
2349 Assert.AreEqual (Infoset (expected), WriterText);
2352 [Test] // bug #338705
2353 public void SerializeTimeSpan ()
2355 // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2356 XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2357 ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2361 public void SerializeDurationToString ()
2363 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2364 ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2368 [ExpectedException (typeof (InvalidOperationException))]
2369 public void SerializeDurationToTimeSpan ()
2371 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2372 ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2376 [ExpectedException (typeof (InvalidOperationException))]
2377 public void SerializeInvalidDataType ()
2379 XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2380 ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2385 [ExpectedException (typeof (ApplicationException))]
2387 public void SerializeErrorneousIXmlSerializable ()
2389 Serialize (new ErrorneousGetSchema ());
2390 Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2394 public void DateTimeRoundtrip ()
2397 XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2398 StringWriter sw = new StringWriter ();
2399 ser.Serialize (sw, DateTime.UtcNow);
2400 DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2401 Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2406 public void SupportIXmlSerializableImplicitlyConvertible ()
2408 XmlAttributes attrs = new XmlAttributes ();
2409 XmlElementAttribute attr = new XmlElementAttribute ();
2410 attr.ElementName = "XmlSerializable";
2411 attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2412 attrs.XmlElements.Add (attr);
2413 XmlAttributeOverrides attrOverrides = new
2414 XmlAttributeOverrides ();
2415 attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2417 XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2418 new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2421 [Test] // bug #566370
2422 public void SerializeEnumWithCSharpKeyword ()
2424 var ser = new XmlSerializer (typeof (DoxCompoundKind));
2425 for (int i = 0; i < 100; i++) // test serialization code generator
2426 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2429 public enum DoxCompoundKind
2436 [XmlEnum("interface")]
2442 [XmlEnum("namespace")]
2450 #region GenericsSeralizationTests
2454 public void TestSerializeGenSimpleClassString ()
2456 GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2458 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2460 simple.something = "hello";
2463 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
2467 public void TestSerializeGenSimpleClassBool ()
2469 GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2471 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
2473 simple.something = true;
2476 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
2480 public void TestSerializeGenSimpleStructInt ()
2482 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2484 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
2486 simple.something = 123;
2489 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
2493 public void TestSerializeGenListClassString ()
2495 GenListClass<string> genlist = new GenListClass<string> ();
2496 Serialize (genlist);
2497 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
2499 genlist.somelist.Add ("Value1");
2500 genlist.somelist.Add ("Value2");
2502 Serialize (genlist);
2503 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
2507 public void TestSerializeGenListClassFloat ()
2509 GenListClass<float> genlist = new GenListClass<float> ();
2510 Serialize (genlist);
2511 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
2513 genlist.somelist.Add (1);
2514 genlist.somelist.Add (2.2F);
2516 Serialize (genlist);
2517 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
2521 public void TestSerializeGenListClassList ()
2523 GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2524 Serialize (genlist);
2525 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2527 GenListClass<int> inlist1 = new GenListClass<int> ();
2528 inlist1.somelist.Add (1);
2529 inlist1.somelist.Add (2);
2530 GenListClass<int> inlist2 = new GenListClass<int> ();
2531 inlist2.somelist.Add (10);
2532 inlist2.somelist.Add (20);
2533 genlist.somelist.Add (inlist1);
2534 genlist.somelist.Add (inlist2);
2536 Serialize (genlist);
2537 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2541 public void TestSerializeGenListClassArray ()
2543 GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2544 Serialize (genlist);
2545 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2547 GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2548 genarr1.arr[0] = 'a';
2549 genarr1.arr[1] = 'b';
2550 genlist.somelist.Add (genarr1);
2551 GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2552 genarr2.arr[0] = 'd';
2553 genarr2.arr[1] = 'e';
2554 genarr2.arr[2] = 'f';
2555 genlist.somelist.Add (genarr2);
2557 Serialize (genlist);
2558 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2562 public void TestSerializeGenTwoClassCharDouble ()
2564 GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2566 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
2568 gentwo.something1 = 'a';
2569 gentwo.something2 = 2.2;
2572 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
2576 public void TestSerializeGenDerivedClassDecimalShort ()
2578 GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2579 Serialize (derived);
2580 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2582 derived.something1 = "Value1";
2583 derived.something2 = 1;
2584 derived.another1 = 1.1M;
2585 derived.another2 = -22;
2587 Serialize (derived);
2588 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2592 public void TestSerializeGenDerivedSecondClassByteUlong ()
2594 GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2595 Serialize (derived2);
2596 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2598 derived2.something1 = 1;
2599 derived2.something2 = 222;
2600 derived2.another1 = 111;
2601 derived2.another2 = 222222;
2603 Serialize (derived2);
2604 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2608 public void TestSerializeGenNestedClass ()
2610 GenNestedClass<string, int>.InnerClass<bool> nested =
2611 new GenNestedClass<string, int>.InnerClass<bool> ();
2613 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
2616 nested.something = true;
2619 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
2623 public void TestSerializeGenListClassListNested ()
2625 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2626 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2627 Serialize (genlist);
2628 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2630 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2631 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2632 GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2634 inval1.something = "ONE";
2635 inlist1.somelist.Add (inval1);
2636 GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2638 inval2.something = "TWO";
2639 inlist1.somelist.Add (inval2);
2640 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2641 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2642 GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2644 inval3.something = "THIRTY";
2645 inlist2.somelist.Add (inval3);
2646 genlist.somelist.Add (inlist1);
2647 genlist.somelist.Add (inlist2);
2649 Serialize (genlist);
2650 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2653 public enum Myenum { one, two, three, four, five, six };
2655 public void TestSerializeGenArrayClassEnum ()
2657 GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2659 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2661 genarr.arr[0] = Myenum.one;
2662 genarr.arr[1] = Myenum.three;
2663 genarr.arr[2] = Myenum.five;
2666 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2670 public void TestSerializeGenArrayStruct ()
2672 GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2674 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
2676 GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2677 genstruct.something = 111;
2678 genarr.arr[0] = genstruct;
2679 genstruct.something = 222;
2680 genarr.arr[1] = genstruct;
2681 genstruct.something = 333;
2682 genarr.arr[2] = genstruct;
2685 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
2689 public void TestSerializeGenArrayList ()
2691 GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2693 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
2695 GenListClass<string> genlist1 = new GenListClass<string> ();
2696 genlist1.somelist.Add ("list1-val1");
2697 genlist1.somelist.Add ("list1-val2");
2698 genarr.arr[0] = genlist1;
2699 GenListClass<string> genlist2 = new GenListClass<string> ();
2700 genlist2.somelist.Add ("list2-val1");
2701 genlist2.somelist.Add ("list2-val2");
2702 genlist2.somelist.Add ("list2-val3");
2703 genlist2.somelist.Add ("list2-val4");
2704 genarr.arr[1] = genlist2;
2705 GenListClass<string> genlist3 = new GenListClass<string> ();
2706 genlist3.somelist.Add ("list3val");
2707 genarr.arr[2] = genlist3;
2710 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
2714 public void TestSerializeGenComplexStruct ()
2716 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2717 Serialize (complex);
2718 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
2720 complex.something = 123;
2721 complex.simpleclass.something = 456;
2722 complex.simplestruct.something = 789;
2723 GenListClass<int> genlist = new GenListClass<int> ();
2724 genlist.somelist.Add (100);
2725 genlist.somelist.Add (200);
2726 complex.listclass = genlist;
2727 GenArrayClass<int> genarr = new GenArrayClass<int> ();
2731 complex.arrayclass = genarr;
2732 complex.twoclass.something1 = 10;
2733 complex.twoclass.something2 = "Ten";
2734 complex.derivedclass.another1 = 1;
2735 complex.derivedclass.another2 = "one";
2736 complex.derivedclass.something1 = "two";
2737 complex.derivedclass.something2 = 2;
2738 complex.derived2.another1 = 3;
2739 complex.derived2.another2 = "three";
2740 complex.derived2.something1 = 4;
2741 complex.derived2.something2 = "four";
2742 complex.nestedouter.outer = 5;
2743 complex.nestedinner.inner = "six";
2744 complex.nestedinner.something = 6;
2746 Serialize (complex);
2747 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
2750 [Test] // bug #80759
2751 public void HasNullableField ()
2753 Bug80759 foo = new Bug80759 ();
2755 foo.NullableInt = 10;
2757 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2759 MemoryStream stream = new MemoryStream ();
2761 serializer.Serialize (stream, foo);
2762 stream.Position = 0;
2763 foo = (Bug80759) serializer.Deserialize (stream);
2766 [Test] // bug #80759, with fieldSpecified.
2767 [ExpectedException (typeof (InvalidOperationException))]
2768 [Category ("NotWorking")]
2769 public void HasFieldSpecifiedButIrrelevant ()
2771 Bug80759_2 foo = new Bug80759_2 ();
2773 foo.NullableInt = 10;
2775 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2777 MemoryStream stream = new MemoryStream ();
2779 serializer.Serialize (stream, foo);
2780 stream.Position = 0;
2781 foo = (Bug80759_2) serializer.Deserialize (stream);
2785 public void HasNullableField2 ()
2787 Bug80759 foo = new Bug80759 ();
2789 foo.NullableInt = 10;
2791 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2793 MemoryStream stream = new MemoryStream ();
2795 serializer.Serialize (stream, foo);
2796 stream.Position = 0;
2797 foo = (Bug80759) serializer.Deserialize (stream);
2799 Assert.AreEqual ("BAR", foo.Test, "#1");
2800 Assert.AreEqual (10, foo.NullableInt, "#2");
2802 foo.NullableInt = null;
2803 stream = new MemoryStream ();
2804 serializer.Serialize (stream, foo);
2805 stream.Position = 0;
2806 foo = (Bug80759) serializer.Deserialize (stream);
2808 Assert.AreEqual ("BAR", foo.Test, "#3");
2809 Assert.IsNull (foo.NullableInt, "#4");
2813 public void SupportPrivateCtorOnly ()
2816 new XmlSerializer (typeof (PrivateCtorOnly));
2817 StringWriter sw = new StringWriter ();
2818 xs.Serialize (sw, PrivateCtorOnly.Instance);
2819 xs.Deserialize (new StringReader (sw.ToString ()));
2823 public void XmlSchemaProviderQNameBecomesRootName ()
2825 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2826 Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2827 Assert.AreEqual (Infoset ("<foo />"), WriterText);
2828 xs.Deserialize (new StringReader ("<foo/>"));
2832 public void XmlSchemaProviderQNameBecomesRootName2 ()
2834 string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2835 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2836 Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2837 Assert.AreEqual (Infoset (xml), WriterText);
2838 xs.Deserialize (new StringReader (xml));
2842 public void XmlAnyElementForObjects () // bug #553032
2844 new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2848 [ExpectedException (typeof (InvalidOperationException))]
2849 public void XmlAnyElementForObjects2 () // bug #553032-2
2851 new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2855 public class Bug2893 {
2858 Contents = new XmlDataDocument();
2861 [XmlAnyElement("Contents")]
2862 public XmlNode Contents;
2865 // Bug Xamarin #2893
2867 public void XmlAnyElementForXmlNode ()
2869 var obj = new Bug2893 ();
2870 XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
2871 XmlWriterSettings settings = new XmlWriterSettings();
2873 var xsn = new XmlSerializerNamespaces();
2874 xsn.Add(string.Empty, string.Empty);
2876 byte[] buffer = new byte[2048];
2877 var ms = new MemoryStream(buffer);
2878 using (var xw = XmlWriter.Create(ms, settings))
2880 mySerializer.Serialize(xw, obj, xsn);
2884 mySerializer.Serialize(ms, obj);
2888 public void XmlRootOverridesSchemaProviderQName ()
2890 var obj = new XmlRootOverridesSchemaProviderQNameType ();
2892 XmlSerializer xs = new XmlSerializer (obj.GetType ());
2894 var sw = new StringWriter ();
2895 using (XmlWriter xw = XmlWriter.Create (sw))
2896 xs.Serialize (xw, obj);
2897 Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2900 public class AnotherArrayListType
2903 public string one = "aaa";
2905 public string another = "bbb";
2908 public class DerivedArrayListType : AnotherArrayListType
2913 public class ClassWithArrayList
2915 [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2916 [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2917 [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2918 [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2919 public ArrayList list;
2922 public class ClassWithArray
2924 [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2925 [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2926 [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2927 [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2928 public object[] list;
2933 public void MultipleXmlElementAttributesOnArrayList()
2935 var test = new ClassWithArrayList();
2937 test.list = new ArrayList();
2939 test.list.Add("apepe");
2940 test.list.Add(new AnotherArrayListType());
2941 test.list.Add(new DerivedArrayListType());
2944 var expected_text = "<:ClassWithArrayList http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2946 Assert.AreEqual(WriterText, expected_text, WriterText);
2950 public void MultipleXmlElementAttributesOnArray()
2952 var test = new ClassWithArray();
2954 test.list = new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() };
2957 var expected_text = "<:ClassWithArray http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2959 Assert.AreEqual(WriterText, expected_text, WriterText);
2964 #endregion //GenericsSeralizationTests
2966 public class XmlArrayOnInt
2972 public class XmlArrayUnqualifiedWithNamespace
2974 [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2975 public ArrayList Sane = new ArrayList ();
2978 public class XmlArrayItemUnqualifiedWithNamespace
2980 [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2981 public ArrayList Sane = new ArrayList ();
2984 [XmlRoot (Namespace = "urn:foo")]
2985 public class XmlArrayOnArrayList
2987 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2988 public ArrayList Sane = new ArrayList ();
2991 [XmlRoot (Namespace = "urn:foo")]
2992 public class XmlArrayOnArray
2994 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2995 public string[] Sane = new string[] { "foo", "bar" };
2997 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2998 public ArrayItemInXmlArray[] Mids =
2999 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
3002 [XmlType (Namespace = "urn:gyabo")]
3003 public class ArrayItemInXmlArray
3005 [XmlArray (Form = XmlSchemaForm.Unqualified)]
3006 public string[] Whee = new string[] { "foo", "bar" };
3009 [XmlRoot ("Base64Binary")]
3010 public class Base64Binary
3012 [XmlAttribute (DataType = "base64Binary")]
3013 public byte [] Data = new byte [] {1, 2, 3};
3016 [XmlRoot ("HexBinary")]
3017 public class HexBinary
3019 [XmlAttribute (DataType = "hexBinary")]
3020 public byte[] Data = new byte[] { 1, 2, 3 };
3023 [XmlRoot ("PrivateCtorOnly")]
3024 public class PrivateCtorOnly
3026 public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
3027 private PrivateCtorOnly ()
3032 public class CDataTextNodesType
3034 public CDataTextNodesInternal foo;
3037 public class CDataTextNodesInternal
3040 public string Value;
3043 public class InvalidTypeContainer
3045 [XmlElement (DataType = "invalid")]
3046 public string InvalidTypeItem = "aaa";
3049 public class TimeSpanContainer1
3051 [XmlElement (DataType = "duration")]
3052 public string StringDuration = "aaa";
3055 public class TimeSpanContainer2
3057 [XmlElement (DataType = "duration")]
3058 public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
3062 public class Bug80759
3065 public int? NullableInt;
3068 public class Bug80759_2
3071 public int? NullableInt;
3074 public bool NullableIntSpecified {
3075 get { return NullableInt.HasValue; }
3079 [XmlSchemaProvider ("GetXsdType")]
3080 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
3082 public XmlSchema GetSchema ()
3087 public void ReadXml (XmlReader reader)
3092 public void WriteXml (XmlWriter writer)
3096 public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
3098 if (xss.Count == 0) {
3099 XmlSchema xs = new XmlSchema ();
3100 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3105 return new XmlQualifiedName ("foo");
3109 public class XmlSchemaProviderQNameBecomesRootNameType2
3111 [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3112 public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3115 public class XmlAnyElementForObjectsType
3118 public object [] arr = new object [] {3,4,5};
3122 [XmlSchemaProvider ("GetSchema")]
3123 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3125 public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3127 var xs = new XmlSchema ();
3128 var xse = new XmlSchemaComplexType () { Name = "bar" };
3131 return new XmlQualifiedName ("bar");
3134 XmlSchema IXmlSerializable.GetSchema ()
3139 void IXmlSerializable.ReadXml (XmlReader reader)
3142 void IXmlSerializable.WriteXml (XmlWriter writer)
3149 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3156 public static string Infoset (string sx)
3158 XmlDocument doc = new XmlDocument ();
3160 StringBuilder sb = new StringBuilder ();
3161 GetInfoset (doc.DocumentElement, sb);
3162 return sb.ToString ();
3165 public static string Infoset (XmlNode nod)
3167 StringBuilder sb = new StringBuilder ();
3168 GetInfoset (nod, sb);
3169 return sb.ToString ();
3172 static void GetInfoset (XmlNode nod, StringBuilder sb)
3174 switch (nod.NodeType) {
3175 case XmlNodeType.Attribute:
3176 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3177 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3180 case XmlNodeType.Element:
3181 XmlElement elem = (XmlElement) nod;
3182 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3184 ArrayList ats = new ArrayList ();
3185 foreach (XmlAttribute at in elem.Attributes)
3186 ats.Add (at.LocalName + " " + at.NamespaceURI);
3190 foreach (string name in ats) {
3191 string[] nn = name.Split (' ');
3192 GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3196 foreach (XmlNode cn in elem.ChildNodes)
3197 GetInfoset (cn, sb);
3202 sb.Append (nod.OuterXml);
3207 static XmlTypeMapping CreateSoapMapping (Type type)
3209 SoapReflectionImporter importer = new SoapReflectionImporter ();
3210 return importer.ImportTypeMapping (type);
3213 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3215 SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3216 return importer.ImportTypeMapping (type);
3219 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3221 SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3222 return importer.ImportTypeMapping (type);
3225 [XmlSchemaProvider (null, IsAny = true)]
3226 public class AnySchemaProviderClass : IXmlSerializable {
3230 void IXmlSerializable.WriteXml (XmlWriter writer)
3232 writer.WriteElementString ("text", Text);
3235 void IXmlSerializable.ReadXml (XmlReader reader)
3237 Text = reader.ReadElementString ("text");
3240 XmlSchema IXmlSerializable.GetSchema ()
3247 public void SerializeAnySchemaProvider ()
3249 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3250 Environment.NewLine + "<text>test</text>";
3252 var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
3254 var obj = new AnySchemaProviderClass {
3258 using (var t = new StringWriter ()) {
3259 ser.Serialize (t, obj);
3260 Assert.AreEqual (expected, t.ToString ());
3265 public void DeserializeAnySchemaProvider ()
3267 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3268 Environment.NewLine + "<text>test</text>";
3270 var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
3272 using (var t = new StringReader (expected)) {
3273 var obj = (AnySchemaProviderClass) ser.Deserialize (t);
3274 Assert.AreEqual ("test", obj.Text);
3278 public class SubNoParameterlessConstructor : NoParameterlessConstructor
3280 public SubNoParameterlessConstructor ()
3286 public class NoParameterlessConstructor
3288 [XmlElement ("Text")]
3291 public NoParameterlessConstructor (string parameter)
3297 public void BaseClassWithoutParameterlessConstructor ()
3299 var ser = new XmlSerializer (typeof (SubNoParameterlessConstructor));
3301 var obj = new SubNoParameterlessConstructor {
3305 using (var w = new StringWriter ()) {
3306 ser.Serialize (w, obj);
3307 using (var r = new StringReader ( w.ToString ())) {
3308 var desObj = (SubNoParameterlessConstructor) ser.Deserialize (r);
3309 Assert.AreEqual (obj.Text, desObj.Text);
3314 public class ClassWithXmlAnyElement
3316 [XmlAnyElement ("Contents")]
3317 public XmlNode Contents;
3321 public void TestClassWithXmlAnyElement ()
3323 var d = new XmlDocument ();
3324 var e = d.CreateElement ("Contents");
3325 e.AppendChild (d.CreateElement ("SomeElement"));
3327 var c = new ClassWithXmlAnyElement {
3331 var ser = new XmlSerializer (typeof (ClassWithXmlAnyElement));
3332 using (var sw = new StringWriter ())
3333 ser.Serialize (sw, c);
3337 public void ClassWithImplicitlyConvertibleElement ()
3339 var ser = new XmlSerializer (typeof (ObjectWithElementRequiringImplicitCast));
3341 var obj = new ObjectWithElementRequiringImplicitCast ("test");
3343 using (var w = new StringWriter ()) {
3344 ser.Serialize (w, obj);
3345 using (var r = new StringReader ( w.ToString ())) {
3346 var desObj = (ObjectWithElementRequiringImplicitCast) ser.Deserialize (r);
3347 Assert.AreEqual (obj.Object.Text, desObj.Object.Text);
3352 public class ClassWithOptionalMethods
3354 private readonly bool shouldSerializeX;
3355 private readonly bool xSpecified;
3358 public int X { get; set; }
3360 public bool ShouldSerializeX () { return shouldSerializeX; }
3362 public bool XSpecified
3364 get { return xSpecified; }
3367 public ClassWithOptionalMethods ()
3371 public ClassWithOptionalMethods (int x, bool shouldSerializeX, bool xSpecified)
3374 this.shouldSerializeX = shouldSerializeX;
3375 this.xSpecified = xSpecified;
3380 public void OptionalMethods ()
3382 var ser = new XmlSerializer (typeof (ClassWithOptionalMethods));
3384 var expectedValueWithoutX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3385 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />");
3387 var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3388 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3390 using (var t = new StringWriter ()) {
3391 var obj = new ClassWithOptionalMethods (11, false, false);
3392 ser.Serialize (t, obj);
3393 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3396 using (var t = new StringWriter ()) {
3397 var obj = new ClassWithOptionalMethods (11, true, false);
3398 ser.Serialize (t, obj);
3399 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3402 using (var t = new StringWriter ()) {
3403 var obj = new ClassWithOptionalMethods (11, false, true);
3404 ser.Serialize (t, obj);
3405 Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
3408 using (var t = new StringWriter ()) {
3409 var obj = new ClassWithOptionalMethods (11, true, true);
3410 ser.Serialize (t, obj);
3411 Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
3415 public class ClassWithShouldSerializeGeneric
3418 public int X { get; set; }
3420 public bool ShouldSerializeX<T> () { return false; }
3424 [Category("NotDotNet")]
3425 public void ShouldSerializeGeneric ()
3427 var ser = new XmlSerializer (typeof (ClassWithShouldSerializeGeneric));
3429 var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
3430 "<ClassWithShouldSerializeGeneric xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3432 using (var t = new StringWriter ()) {
3433 var obj = new ClassWithShouldSerializeGeneric { X = 11 };
3434 ser.Serialize (t, obj);
3435 Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
3440 public void NullableArrayItems ()
3442 var ser = new XmlSerializer (typeof (ObjectWithNullableArrayItems));
3444 var obj = new ObjectWithNullableArrayItems ();
3445 obj.Elements = new List <SimpleClass> ();
3446 obj.Elements.Add (new SimpleClass { something = "Hello" });
3447 obj.Elements.Add (null);
3448 obj.Elements.Add (new SimpleClass { something = "World" });
3450 using (var w = new StringWriter ()) {
3451 ser.Serialize (w, obj);
3452 using (var r = new StringReader ( w.ToString ())) {
3453 var desObj = (ObjectWithNullableArrayItems) ser.Deserialize (r);
3454 Assert.IsNull (desObj.Elements [1]);
3460 public void NonNullableArrayItems ()
3462 var ser = new XmlSerializer (typeof (ObjectWithNonNullableArrayItems));
3464 var obj = new ObjectWithNonNullableArrayItems ();
3465 obj.Elements = new List <SimpleClass> ();
3466 obj.Elements.Add (new SimpleClass { something = "Hello" });
3467 obj.Elements.Add (null);
3468 obj.Elements.Add (new SimpleClass { something = "World" });
3470 using (var w = new StringWriter ()) {
3471 ser.Serialize (w, obj);
3472 using (var r = new StringReader ( w.ToString ())) {
3473 var desObj = (ObjectWithNonNullableArrayItems) ser.Deserialize (r);
3474 Assert.IsNotNull (desObj.Elements [1]);
3480 public void NotSpecifiedNullableArrayItems ()
3482 var ser = new XmlSerializer (typeof (ObjectWithNotSpecifiedNullableArrayItems));
3484 var obj = new ObjectWithNotSpecifiedNullableArrayItems ();
3485 obj.Elements = new List <SimpleClass> ();
3486 obj.Elements.Add (new SimpleClass { something = "Hello" });
3487 obj.Elements.Add (null);
3488 obj.Elements.Add (new SimpleClass { something = "World" });
3490 using (var w = new StringWriter ()) {
3491 ser.Serialize (w, obj);
3492 using (var r = new StringReader ( w.ToString ())) {
3493 var desObj = (ObjectWithNotSpecifiedNullableArrayItems) ser.Deserialize (r);
3494 Assert.IsNull (desObj.Elements [1]);
3499 private static void TestClassWithDefaultTextNotNullAux (string value, string expected)
3501 var obj = new ClassWithDefaultTextNotNull (value);
3502 var ser = new XmlSerializer (typeof (ClassWithDefaultTextNotNull));
3504 using (var mstream = new MemoryStream ())
3505 using (var writer = new XmlTextWriter (mstream, Encoding.ASCII)) {
3506 ser.Serialize (writer, obj);
3508 mstream.Seek (0, SeekOrigin.Begin);
3509 using (var reader = new XmlTextReader (mstream)) {
3510 var result = (ClassWithDefaultTextNotNull) ser.Deserialize (reader);
3511 Assert.AreEqual (expected, result.Value);
3517 public void TestClassWithDefaultTextNotNull ()
3519 TestClassWithDefaultTextNotNullAux ("my_text", "my_text");
3520 TestClassWithDefaultTextNotNullAux ("", ClassWithDefaultTextNotNull.DefaultValue);
3521 TestClassWithDefaultTextNotNullAux (null, ClassWithDefaultTextNotNull.DefaultValue);
3525 // Test generated serialization code.
3526 public class XmlSerializerGeneratorTests : XmlSerializerTests {
3528 private FieldInfo backgroundGeneration;
3529 private FieldInfo generationThreshold;
3530 private FieldInfo generatorFallback;
3532 private bool backgroundGenerationOld;
3533 private int generationThresholdOld;
3534 private bool generatorFallbackOld;
3537 public void SetUp ()
3539 // Make sure XmlSerializer static constructor is called
3540 XmlSerializer.FromTypes (new Type [] {});
3542 const BindingFlags binding = BindingFlags.Static | BindingFlags.NonPublic;
3543 backgroundGeneration = typeof (XmlSerializer).GetField ("backgroundGeneration", binding);
3544 generationThreshold = typeof (XmlSerializer).GetField ("generationThreshold", binding);
3545 generatorFallback = typeof (XmlSerializer).GetField ("generatorFallback", binding);
3547 if (backgroundGeneration == null)
3548 Assert.Ignore ("Unable to access field backgroundGeneration");
3549 if (generationThreshold == null)
3550 Assert.Ignore ("Unable to access field generationThreshold");
3551 if (generatorFallback == null)
3552 Assert.Ignore ("Unable to access field generatorFallback");
3554 backgroundGenerationOld = (bool) backgroundGeneration.GetValue (null);
3555 generationThresholdOld = (int) generationThreshold.GetValue (null);
3556 generatorFallbackOld = (bool) generatorFallback.GetValue (null);
3558 backgroundGeneration.SetValue (null, false);
3559 generationThreshold.SetValue (null, 0);
3560 generatorFallback.SetValue (null, false);
3564 public void TearDown ()
3566 if (backgroundGeneration == null || generationThreshold == null || generatorFallback == null)
3569 backgroundGeneration.SetValue (null, backgroundGenerationOld);
3570 generationThreshold.SetValue (null, generationThresholdOld);
3571 generatorFallback.SetValue (null, generatorFallbackOld);