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;
34 using System.Xml.Schema;
35 using System.Xml.Serialization;
37 using System.Collections.Generic;
40 using NUnit.Framework;
42 using MonoTests.System.Xml.TestClasses;
44 namespace MonoTests.System.XmlSerialization
47 public class XmlSerializerTests
49 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
50 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
51 const string ANamespace = "some:urn";
52 const string AnotherNamespace = "another:urn";
58 private void SetUpWriter ()
60 sw = new StringWriter ();
61 xtw = new XmlTextWriter (sw);
63 xtw.Formatting = Formatting.None;
66 private string WriterText
70 string val = sw.GetStringBuilder ().ToString ();
71 int offset = val.IndexOf ('>') + 1;
72 val = val.Substring (offset);
77 private void Serialize (object o)
80 xs = new XmlSerializer (o.GetType ());
81 xs.Serialize (xtw, o);
84 private void Serialize (object o, Type type)
87 xs = new XmlSerializer (type);
88 xs.Serialize (xtw, o);
91 private void Serialize (object o, XmlSerializerNamespaces ns)
94 xs = new XmlSerializer (o.GetType ());
95 xs.Serialize (xtw, o, ns);
98 private void Serialize (object o, XmlAttributeOverrides ao)
101 xs = new XmlSerializer (o.GetType (), ao);
102 xs.Serialize (xtw, o);
105 private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
108 xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
109 (XmlRootAttribute) null, defaultNamespace);
110 xs.Serialize (xtw, o);
113 private void Serialize (object o, XmlRootAttribute root)
116 xs = new XmlSerializer (o.GetType (), root);
117 xs.Serialize (xtw, o);
120 private void Serialize (object o, XmlTypeMapping typeMapping)
123 xs = new XmlSerializer (typeMapping);
124 xs.Serialize (xtw, o);
127 private void SerializeEncoded (object o)
129 SerializeEncoded (o, o.GetType ());
132 private void SerializeEncoded (object o, SoapAttributeOverrides ao)
134 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
136 xs = new XmlSerializer (mapping);
137 xs.Serialize (xtw, o);
140 private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
142 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
144 xs = new XmlSerializer (mapping);
145 xs.Serialize (xtw, o);
148 private void SerializeEncoded (object o, Type type)
150 XmlTypeMapping mapping = CreateSoapMapping (type);
152 xs = new XmlSerializer (mapping);
153 xs.Serialize (xtw, o);
156 private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
158 XmlTypeMapping mapping = CreateSoapMapping (type);
159 xs = new XmlSerializer (mapping);
160 xs.Serialize (xtw, o);
164 #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
166 public void TestConstructor()
168 XmlSerializer ser = new XmlSerializer (null, "");
173 // test basic types ////////////////////////////////////////////////////////
175 public void TestSerializeInt ()
178 Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
182 public void TestSerializeBool ()
185 Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
188 Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
192 public void TestSerializeString ()
195 Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
199 public void TestSerializeEmptyString ()
201 Serialize (String.Empty);
202 Assert.AreEqual (Infoset ("<string />"), WriterText);
206 public void TestSerializeNullObject ()
208 Serialize (null, typeof (object));
209 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
210 "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
211 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
215 [Ignore ("The generated XML is not exact but it is equivalent")]
216 public void TestSerializeNullString ()
218 Serialize (null, typeof (string));
219 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
220 "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
221 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
225 public void TestSerializeIntArray ()
227 Serialize (new int[] { 1, 2, 3, 4 });
228 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
229 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
230 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
234 public void TestSerializeEmptyArray ()
236 Serialize (new int[] { });
237 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
238 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
239 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
244 [Ignore ("TD #7464")]
246 public void TestSerializeChar ()
249 Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
252 Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
255 Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
257 Serialize ('\uFF01');
258 Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
262 public void TestSerializeFloat ()
265 Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
268 Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
270 // FIXME test INF and other boundary conditions that may exist with floats
274 public void TestSerializeEnumeration_FromValue ()
276 Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
278 "<?xml version='1.0' encoding='utf-16'?>" +
279 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
284 [Category ("NotWorking")]
285 public void TestSerializeEnumeration_FromValue_Encoded ()
287 SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
288 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
289 "<?xml version='1.0' encoding='utf-16'?>" +
290 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
291 XmlSchema.InstanceNamespace), sw.ToString ());
295 public void TestSerializeEnumeration ()
297 Serialize (SimpleEnumeration.FIRST);
298 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
300 Serialize (SimpleEnumeration.SECOND);
301 Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
305 public void TestSerializeEnumeration_Encoded ()
307 SerializeEncoded (SimpleEnumeration.FIRST);
308 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
309 "<?xml version='1.0' encoding='utf-16'?>" +
310 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
311 XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
313 SerializeEncoded (SimpleEnumeration.SECOND);
314 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
315 "<?xml version='1.0' encoding='utf-16'?>" +
316 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
317 XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
321 public void TestSerializeEnumDefaultValue ()
323 Serialize (new EnumDefaultValue ());
324 Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
326 Serialize (new SimpleEnumeration ());
327 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
329 Serialize (3, typeof (EnumDefaultValue));
330 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
332 Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
333 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
335 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
336 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
338 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
339 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
341 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
342 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
344 Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
345 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
347 Serialize (3, typeof (FlagEnum));
348 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
350 Serialize (5, typeof (FlagEnum));
351 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
353 Serialize (FlagEnum.e4, typeof (FlagEnum));
354 Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
356 Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
357 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
359 Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
360 Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
362 Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
363 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
365 Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
366 Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
368 Serialize (3, typeof (EnumDefaultValueNF));
369 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
371 Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
372 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
374 Serialize (2, typeof (ZeroFlagEnum));
375 Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t<w>o</ZeroFlagEnum>"), WriterText, "#18");
377 Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
378 Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
382 [Category ("NotWorking")]
383 public void TestSerializeEnumDefaultValue_Encoded ()
385 SerializeEncoded (new EnumDefaultValue ());
386 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
387 "<?xml version='1.0' encoding='utf-16'?>" +
388 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
389 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
391 SerializeEncoded (new SimpleEnumeration ());
392 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
393 "<?xml version='1.0' encoding='utf-16'?>" +
394 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
395 XmlSchema.InstanceNamespace), sw.ToString (), "#2");
397 SerializeEncoded (3, typeof (EnumDefaultValue));
398 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
399 "<?xml version='1.0' encoding='utf-16'?>" +
400 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
401 XmlSchema.InstanceNamespace), sw.ToString (), "#3");
403 SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
404 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
405 "<?xml version='1.0' encoding='utf-16'?>" +
406 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
407 XmlSchema.InstanceNamespace), sw.ToString (), "#4");
409 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
410 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
411 "<?xml version='1.0' encoding='utf-16'?>" +
412 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
413 XmlSchema.InstanceNamespace), sw.ToString (), "#5");
415 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
416 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
417 "<?xml version='1.0' encoding='utf-16'?>" +
418 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
419 XmlSchema.InstanceNamespace), sw.ToString (), "#6");
421 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
422 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
423 "<?xml version='1.0' encoding='utf-16'?>" +
424 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
425 XmlSchema.InstanceNamespace), sw.ToString (), "#7");
427 SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
428 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
429 "<?xml version='1.0' encoding='utf-16'?>" +
430 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
431 XmlSchema.InstanceNamespace), sw.ToString (), "#8");
433 SerializeEncoded (3, typeof (FlagEnum));
434 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
435 "<?xml version='1.0' encoding='utf-16'?>" +
436 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
437 XmlSchema.InstanceNamespace), sw.ToString (), "#9");
439 SerializeEncoded (5, typeof (FlagEnum));
440 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
441 "<?xml version='1.0' encoding='utf-16'?>" +
442 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
443 XmlSchema.InstanceNamespace), sw.ToString (), "#10");
445 SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
446 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
447 "<?xml version='1.0' encoding='utf-16'?>" +
448 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
449 XmlSchema.InstanceNamespace), sw.ToString (), "#11");
451 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
452 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
453 "<?xml version='1.0' encoding='utf-16'?>" +
454 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
455 XmlSchema.InstanceNamespace), sw.ToString (), "#12");
457 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
458 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
459 "<?xml version='1.0' encoding='utf-16'?>" +
460 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
461 XmlSchema.InstanceNamespace), sw.ToString (), "#13");
463 SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
464 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
465 "<?xml version='1.0' encoding='utf-16'?>" +
466 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
467 XmlSchema.InstanceNamespace), sw.ToString (), "#14");
469 SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
470 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
471 "<?xml version='1.0' encoding='utf-16'?>" +
472 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
473 XmlSchema.InstanceNamespace), sw.ToString (), "#15");
475 SerializeEncoded (3, typeof (EnumDefaultValueNF));
476 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
477 "<?xml version='1.0' encoding='utf-16'?>" +
478 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
479 XmlSchema.InstanceNamespace), sw.ToString (), "#16");
481 SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
482 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
483 "<?xml version='1.0' encoding='utf-16'?>" +
484 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
485 XmlSchema.InstanceNamespace), sw.ToString (), "#17");
487 SerializeEncoded (2, typeof (ZeroFlagEnum));
488 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
489 "<?xml version='1.0' encoding='utf-16'?>" +
490 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
491 XmlSchema.InstanceNamespace), sw.ToString (), "#18");
493 SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
494 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
495 "<?xml version='1.0' encoding='utf-16'?>" +
496 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
497 XmlSchema.InstanceNamespace), sw.ToString (), "#19");
501 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
504 Serialize ("b", typeof (EnumDefaultValue));
506 } catch (InvalidOperationException ex) {
507 Assert.IsNotNull (ex.InnerException, "#A2");
508 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
512 Serialize ("e1", typeof (EnumDefaultValue));
514 } catch (InvalidOperationException ex) {
515 Assert.IsNotNull (ex.InnerException, "#B2");
516 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
520 Serialize ("e1,e2", typeof (EnumDefaultValue));
522 } catch (InvalidOperationException ex) {
523 Assert.IsNotNull (ex.InnerException, "#C2");
524 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
528 Serialize (string.Empty, typeof (EnumDefaultValue));
530 } catch (InvalidOperationException ex) {
531 Assert.IsNotNull (ex.InnerException, "#D2");
532 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
536 Serialize ("1", typeof (EnumDefaultValue));
538 } catch (InvalidOperationException ex) {
539 Assert.IsNotNull (ex.InnerException, "#E2");
540 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
544 Serialize ("0", typeof (EnumDefaultValue));
546 } catch (InvalidOperationException ex) {
547 Assert.IsNotNull (ex.InnerException, "#F2");
548 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
552 Serialize (new SimpleClass (), typeof (EnumDefaultValue));
554 } catch (InvalidOperationException ex) {
555 Assert.IsNotNull (ex.InnerException, "#G2");
556 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
561 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
565 Serialize (5, typeof (EnumDefaultValue));
567 } catch (InvalidOperationException ex) {
568 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
569 Assert.IsNotNull (ex.InnerException, "#3");
570 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
571 Assert.IsNotNull (ex.InnerException.Message, "#5");
572 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
573 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
576 Serialize (5, typeof (EnumDefaultValue));
577 Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
582 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
586 Serialize (new EnumDefaultValueNF ());
588 } catch (InvalidOperationException ex) {
589 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
590 Assert.IsNotNull (ex.InnerException, "#3");
591 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
592 Assert.IsNotNull (ex.InnerException.Message, "#5");
593 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
594 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
597 Serialize (new EnumDefaultValueNF ());
598 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
603 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
607 Serialize (15, typeof (EnumDefaultValueNF));
609 } catch (InvalidOperationException ex) {
610 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
611 Assert.IsNotNull (ex.InnerException, "#3");
612 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
613 Assert.IsNotNull (ex.InnerException.Message, "#5");
614 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
615 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
618 Serialize (15, typeof (EnumDefaultValueNF));
619 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
624 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
627 Serialize ("b", typeof (EnumDefaultValueNF));
629 } catch (InvalidOperationException ex) {
630 Assert.IsNotNull (ex.InnerException, "#A2");
631 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
635 Serialize ("e2", typeof (EnumDefaultValueNF));
637 } catch (InvalidOperationException ex) {
638 Assert.IsNotNull (ex.InnerException, "#B2");
639 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
643 Serialize (string.Empty, typeof (EnumDefaultValueNF));
645 } catch (InvalidOperationException ex) {
646 Assert.IsNotNull (ex.InnerException, "#C2");
647 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
651 Serialize ("1", typeof (EnumDefaultValueNF));
653 } catch (InvalidOperationException ex) {
654 Assert.IsNotNull (ex.InnerException, "#D2");
655 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
659 Serialize ("0", typeof (EnumDefaultValueNF));
661 } catch (InvalidOperationException ex) {
662 Assert.IsNotNull (ex.InnerException, "#E2");
663 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
668 public void TestSerializeField ()
670 Field f = new Field ();
671 Serialize (f, typeof (Field));
672 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
673 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
674 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
675 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
677 f.Flags1 = FlagEnum.e1;
678 f.Flags2 = FlagEnum.e1;
679 f.Flags3 = FlagEnum.e2;
680 f.Modifiers = MapModifiers.Protected;
681 f.Modifiers2 = MapModifiers.Public;
682 f.Modifiers3 = MapModifiers.Public;
683 f.Modifiers4 = MapModifiers.Protected;
684 f.Modifiers5 = MapModifiers.Public;
685 Serialize (f, typeof (Field));
686 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
687 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
688 " modifiers='protected' modifiers2='public' />",
689 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
691 f.Flags1 = (FlagEnum) 1;
692 f.Flags1 = FlagEnum.e2;
693 f.Flags2 = FlagEnum.e2;
694 f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
695 f.Modifiers = MapModifiers.Public;
696 f.Modifiers2 = MapModifiers.Protected;
697 f.Modifiers3 = MapModifiers.Protected;
698 f.Modifiers4 = MapModifiers.Public;
699 f.Modifiers5 = MapModifiers.Protected;
700 Serialize (f, typeof (Field));
701 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
702 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
703 " flag4='' modifiers='public' modifiers2='protected'" +
704 " modifiers3='protected' modifiers4='public'" +
705 " modifiers5='protected' />",
706 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
708 f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
709 f.Flags2 = FlagEnum.e2;
710 f.Flags3 = FlagEnum.e4;
711 f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
712 f.Modifiers3 = MapModifiers.Public;
713 f.Modifiers4 = MapModifiers.Protected;
714 f.Modifiers5 = MapModifiers.Public;
715 f.Names = new string[] { "a", "b" };
716 Serialize (f, typeof (Field));
717 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
718 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
719 " flag3='four' flag4='one two four' modifiers='public'" +
720 " modifiers2='protected' names='a b' />",
721 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
723 f.Flags2 = (FlagEnum) 444;
724 f.Flags3 = (FlagEnum) 555;
725 f.Modifiers = (MapModifiers) 666;
726 f.Modifiers2 = (MapModifiers) 777;
727 f.Modifiers3 = (MapModifiers) 0;
728 f.Modifiers4 = (MapModifiers) 888;
729 f.Modifiers5 = (MapModifiers) 999;
732 Serialize (f, typeof (Field));
734 } catch (InvalidOperationException ex) {
735 // There was an error generating the XML document
736 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
737 Assert.IsNotNull (ex.Message, "#E3");
738 Assert.IsNotNull (ex.InnerException, "#E4");
740 // Instance validation error: '444' is not a valid value for
741 // MonoTests.System.Xml.TestClasses.FlagEnum
742 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
743 Assert.IsNotNull (ex.InnerException.Message, "#E6");
744 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
745 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
746 Assert.IsNull (ex.InnerException.InnerException, "#E9");
749 Serialize (f, typeof (Field));
750 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
751 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
752 " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
753 " modifiers4='888' modifiers5='999' names='a b' />",
754 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
759 [Category ("NotDotNet")] // MS bug
761 [Ignore ("TD #7458")]
763 public void TestSerializeField_Encoded ()
765 Field_Encoded f = new Field_Encoded ();
766 SerializeEncoded (f, typeof (Field_Encoded));
767 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
768 "<?xml version='1.0' encoding='utf-16'?>" +
770 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
771 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
772 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
774 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
775 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
776 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
778 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
779 sw.GetStringBuilder ().ToString (), "#A");
781 f.Flags1 = FlagEnum_Encoded.e1;
782 f.Flags2 = FlagEnum_Encoded.e1;
783 f.Flags3 = FlagEnum_Encoded.e2;
784 f.Modifiers = MapModifiers.Protected;
785 f.Modifiers2 = MapModifiers.Public;
786 f.Modifiers3 = MapModifiers.Public;
787 f.Modifiers4 = MapModifiers.Protected;
788 f.Modifiers5 = MapModifiers.Public;
789 SerializeEncoded (f, typeof (Field_Encoded));
790 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
791 "<?xml version='1.0' encoding='utf-16'?>" +
793 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
794 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
795 " xmlns:q1='some:urn' />",
797 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
798 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
799 " xmlns:q1='some:urn' />",
801 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
802 sw.GetStringBuilder ().ToString (), "#B");
804 f.Flags1 = FlagEnum_Encoded.e2;
805 f.Flags2 = FlagEnum_Encoded.e2;
806 f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
807 f.Modifiers = MapModifiers.Public;
808 f.Modifiers2 = MapModifiers.Protected;
809 f.Modifiers3 = MapModifiers.Protected;
810 f.Modifiers4 = MapModifiers.Public;
811 f.Modifiers5 = MapModifiers.Protected;
812 SerializeEncoded (f, typeof (Field_Encoded));
813 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
814 "<?xml version='1.0' encoding='utf-16'?>" +
816 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
817 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
818 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
819 " xmlns:q1='some:urn' />",
821 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
822 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
823 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
824 " xmlns:q1='some:urn' />",
826 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
827 sw.GetStringBuilder ().ToString (), "#C");
829 f.Flags1 = (FlagEnum_Encoded) 1;
830 f.Flags2 = (FlagEnum_Encoded) 444;
831 f.Flags3 = (FlagEnum_Encoded) 555;
832 f.Modifiers = (MapModifiers) 666;
833 f.Modifiers2 = (MapModifiers) 777;
834 f.Modifiers3 = (MapModifiers) 0;
835 f.Modifiers4 = (MapModifiers) 888;
836 f.Modifiers5 = (MapModifiers) 999;
840 SerializeEncoded (f, typeof (Field_Encoded));
843 } catch (InvalidOperationException ex) {
844 // There was an error generating the XML document
845 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
846 Assert.IsNotNull (ex.Message, "#D3");
847 Assert.IsNotNull (ex.InnerException, "#D4");
849 // Instance validation error: '444' is not a valid value for
850 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
851 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
852 Assert.IsNotNull (ex.InnerException.Message, "#D6");
853 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
854 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
855 Assert.IsNull (ex.InnerException.InnerException, "#D9");
858 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
859 "<?xml version='1.0' encoding='utf-16'?>" +
860 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
861 " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
862 " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
863 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
864 sw.GetStringBuilder ().ToString (), "#D");
870 [Ignore ("JVM returns fields in different order")]
872 public void TestSerializeGroup ()
874 Group myGroup = new Group ();
875 myGroup.GroupName = ".NET";
877 Byte[] hexByte = new Byte[] { 0x64, 0x32 };
878 myGroup.GroupNumber = hexByte;
880 DateTime myDate = new DateTime (2002, 5, 2);
881 myGroup.Today = myDate;
882 myGroup.PostitiveInt = "10000";
883 myGroup.IgnoreThis = true;
884 Car thisCar = (Car) myGroup.myCar ("1234566");
885 myGroup.MyVehicle = thisCar;
888 xtw.WriteStartDocument (true);
889 xtw.WriteStartElement ("Wrapper");
890 SerializeEncoded (xtw, myGroup, typeof (Group));
891 xtw.WriteEndElement ();
894 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
896 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
897 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
898 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
899 "<MyVehicle href='#id2' />" +
901 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
902 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
903 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
906 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
909 myGroup.GroupName = null;
910 myGroup.Grouptype = GroupType.B;
911 myGroup.MyVehicle.licenseNumber = null;
912 myGroup.MyVehicle.weight = "450";
915 xtw.WriteStartDocument (true);
916 xtw.WriteStartElement ("Wrapper");
917 SerializeEncoded (xtw, myGroup, typeof (Group));
918 xtw.WriteEndElement ();
921 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
923 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
924 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
925 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
926 "<MyVehicle href='#id2' />" +
928 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
929 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
930 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
933 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
938 public void TestSerializeZeroFlagEnum_InvalidValue ()
942 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
944 } catch (InvalidOperationException ex) {
945 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
946 Assert.IsNotNull (ex.InnerException, "#3");
947 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
948 Assert.IsNotNull (ex.InnerException.Message, "#5");
949 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
950 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
953 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
954 Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
959 public void TestSerializeQualifiedName ()
961 Serialize (new XmlQualifiedName ("me", "home.urn"));
962 Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
966 public void TestSerializeBytes ()
968 Serialize ((byte) 0xAB);
969 Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
971 Serialize ((byte) 15);
972 Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
976 public void TestSerializeByteArrays ()
978 Serialize (new byte[] { });
979 Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
981 Serialize (new byte[] { 0xAB, 0xCD });
982 Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
986 public void TestSerializeDateTime ()
988 DateTime d = new DateTime ();
991 TimeZone tz = TimeZone.CurrentTimeZone;
992 TimeSpan off = tz.GetUtcOffset (d);
993 string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
994 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
1004 public void TestSerialize()
1007 Assert.AreEqual (WriterText, "");
1011 // test basic class serialization /////////////////////////////////////
1013 public void TestSerializeSimpleClass ()
1015 SimpleClass simple = new SimpleClass ();
1017 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1019 simple.something = "hello";
1022 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
1026 public void TestSerializeStringCollection ()
1028 StringCollection strings = new StringCollection ();
1029 Serialize (strings);
1030 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1032 strings.Add ("hello");
1033 strings.Add ("goodbye");
1034 Serialize (strings);
1035 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
1039 public void TestSerializeOptionalValueTypeContainer ()
1041 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1043 OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
1045 Serialize (optionalValue);
1046 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1047 "<?xml version='1.0' encoding='utf-16'?>" +
1049 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
1051 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
1053 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
1054 sw.ToString (), "#1");
1056 attr = new XmlAttributes ();
1058 // remove the DefaultValue attribute on the Flags member
1059 overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
1060 // remove the DefaultValue attribute on the Attributes member
1061 overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
1063 Serialize (optionalValue, overrides);
1064 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1065 "<?xml version='1.0' encoding='utf-16'?>" +
1067 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1069 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1071 "<Attributes xmlns='{3}'>one four</Attributes>" +
1072 "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1073 AnotherNamespace, ANamespace), sw.ToString (), "#2");
1075 optionalValue.FlagsSpecified = true;
1076 Serialize (optionalValue, overrides);
1077 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1078 "<?xml version='1.0' encoding='utf-16'?>" +
1080 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1082 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1084 "<Attributes xmlns='{3}'>one four</Attributes>" +
1085 "<Flags xmlns='{3}'>one</Flags>" +
1087 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
1088 ANamespace), sw.ToString (), "#3");
1092 public void TestSerializePlainContainer ()
1094 StringCollectionContainer container = new StringCollectionContainer ();
1095 Serialize (container);
1096 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
1098 container.Messages.Add ("hello");
1099 container.Messages.Add ("goodbye");
1100 Serialize (container);
1101 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
1105 public void TestSerializeArrayContainer ()
1107 ArrayContainer container = new ArrayContainer ();
1108 Serialize (container);
1109 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1111 container.items = new object[] { 10, 20 };
1112 Serialize (container);
1113 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
1115 container.items = new object[] { 10, "hello" };
1116 Serialize (container);
1117 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
1121 public void TestSerializeClassArrayContainer ()
1123 ClassArrayContainer container = new ClassArrayContainer ();
1124 Serialize (container);
1125 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1127 SimpleClass simple1 = new SimpleClass ();
1128 simple1.something = "hello";
1129 SimpleClass simple2 = new SimpleClass ();
1130 simple2.something = "hello";
1131 container.items = new SimpleClass[2];
1132 container.items[0] = simple1;
1133 container.items[1] = simple2;
1134 Serialize (container);
1135 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
1138 // test basic attributes ///////////////////////////////////////////////
1140 public void TestSerializeSimpleClassWithXmlAttributes ()
1142 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1144 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1146 simple.something = "hello";
1148 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
1151 // test overrides ///////////////////////////////////////////////////////
1153 public void TestSerializeSimpleClassWithOverrides ()
1155 // Also tests XmlIgnore
1156 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1158 XmlAttributes attr = new XmlAttributes ();
1159 attr.XmlIgnore = true;
1160 overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
1162 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1163 simple.something = "hello";
1164 Serialize (simple, overrides);
1165 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1169 public void TestSerializeSchema ()
1171 XmlSchema schema = new XmlSchema ();
1172 schema.Items.Add (new XmlSchemaAttribute ());
1173 schema.Items.Add (new XmlSchemaAttributeGroup ());
1174 schema.Items.Add (new XmlSchemaComplexType ());
1175 schema.Items.Add (new XmlSchemaNotation ());
1176 schema.Items.Add (new XmlSchemaSimpleType ());
1177 schema.Items.Add (new XmlSchemaGroup ());
1178 schema.Items.Add (new XmlSchemaElement ());
1180 StringWriter sw = new StringWriter ();
1181 XmlTextWriter xtw = new XmlTextWriter (sw);
1182 xtw.QuoteChar = '\'';
1183 xtw.Formatting = Formatting.Indented;
1184 XmlSerializer xs = new XmlSerializer (schema.GetType ());
1185 xs.Serialize (xtw, schema);
1187 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1188 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1189 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1190 " <xsd:attribute />{0}" +
1191 " <xsd:attributeGroup />{0}" +
1192 " <xsd:complexType />{0}" +
1193 " <xsd:notation />{0}" +
1194 " <xsd:simpleType />{0}" +
1195 " <xsd:group />{0}" +
1196 " <xsd:element />{0}" +
1197 "</xsd:schema>", Environment.NewLine), sw.ToString ());
1200 // test xmlText //////////////////////////////////////////////////////////
1202 public void TestSerializeXmlTextAttribute ()
1204 SimpleClass simple = new SimpleClass ();
1205 simple.something = "hello";
1207 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1208 XmlAttributes attr = new XmlAttributes ();
1209 overrides.Add (typeof (SimpleClass), "something", attr);
1211 attr.XmlText = new XmlTextAttribute ();
1212 Serialize (simple, overrides);
1213 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
1215 attr.XmlText = new XmlTextAttribute (typeof (string));
1216 Serialize (simple, overrides);
1217 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
1220 attr.XmlText = new XmlTextAttribute (typeof (byte[]));
1221 Serialize (simple, overrides);
1222 Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1223 } catch (InvalidOperationException ex) {
1224 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1225 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1226 Assert.IsNotNull (ex.Message, "#A3");
1227 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
1228 Assert.IsNotNull (ex.InnerException, "#A5");
1230 // there was an error reflecting field 'something'.
1231 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
1232 Assert.IsNotNull (ex.InnerException.Message, "#A7");
1233 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
1234 Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
1236 // the type for XmlText may not be specified for primitive types.
1237 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
1238 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
1239 Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
1243 attr.XmlText = new XmlTextAttribute ();
1244 attr.XmlText.DataType = "sometype";
1245 Serialize (simple, overrides);
1246 Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1247 } catch (InvalidOperationException ex) {
1248 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1249 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1250 Assert.IsNotNull (ex.Message, "#B3");
1251 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
1252 Assert.IsNotNull (ex.InnerException, "#B5");
1254 // There was an error reflecting field 'something'.
1255 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
1256 Assert.IsNotNull (ex.InnerException.Message, "#B7");
1257 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
1258 Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
1262 // There was an error reflecting type 'System.String'.
1263 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1264 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1265 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1266 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1268 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
1269 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1270 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1271 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1272 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1273 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1278 // test xmlRoot //////////////////////////////////////////////////////////
1280 public void TestSerializeXmlRootAttribute ()
1282 // constructor override & element name
1283 XmlRootAttribute root = new XmlRootAttribute ();
1284 root.ElementName = "renamed";
1286 SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
1287 Serialize (simpleWithAttributes, root);
1288 Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1290 SimpleClass simple = null;
1291 root.IsNullable = false;
1293 Serialize (simple, root);
1294 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1295 } catch (NullReferenceException) {
1298 root.IsNullable = true;
1300 Serialize (simple, root);
1301 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1302 } catch (NullReferenceException) {
1305 simple = new SimpleClass ();
1306 root.ElementName = null;
1307 root.Namespace = "some.urn";
1308 Serialize (simple, root);
1309 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
1313 public void TestSerializeXmlRootAttributeOnMember ()
1316 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1317 XmlAttributes childAttr = new XmlAttributes ();
1318 childAttr.XmlRoot = new XmlRootAttribute ("simple");
1319 overrides.Add (typeof (SimpleClass), childAttr);
1321 XmlAttributes attr = new XmlAttributes ();
1322 attr.XmlRoot = new XmlRootAttribute ("simple");
1323 overrides.Add (typeof (ClassArrayContainer), attr);
1325 ClassArrayContainer container = new ClassArrayContainer ();
1326 container.items = new SimpleClass[1];
1327 container.items[0] = new SimpleClass ();
1328 Serialize (container, overrides);
1329 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
1331 // FIXME test data type
1334 // test XmlAttribute /////////////////////////////////////////////////////
1336 public void TestSerializeXmlAttributeAttribute ()
1339 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1340 XmlAttributes attr = new XmlAttributes ();
1341 attr.XmlAttribute = new XmlAttributeAttribute ();
1342 overrides.Add (typeof (SimpleClass), "something", attr);
1344 SimpleClass simple = new SimpleClass (); ;
1345 Serialize (simple, overrides);
1346 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1349 simple.something = "hello";
1350 Serialize (simple, overrides);
1351 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
1354 attr.XmlAttribute.AttributeName = "somethingelse";
1355 Serialize (simple, overrides);
1356 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
1359 // FIXME this should work, shouldnt it?
1360 // attr.XmlAttribute.Type = typeof(string);
1361 // Serialize(simple, overrides);
1362 // Assert(WriterText.EndsWith(" something='hello' />"));
1365 attr.XmlAttribute.Namespace = "some:urn";
1366 Serialize (simple, overrides);
1367 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
1370 // FIXME XmlSchemaForm Form
1372 // FIXME write XmlQualifiedName as attribute
1375 // test XmlElement ///////////////////////////////////////////////////////
1377 public void TestSerializeXmlElementAttribute ()
1379 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1380 XmlAttributes attr = new XmlAttributes ();
1381 XmlElementAttribute element = new XmlElementAttribute ();
1382 attr.XmlElements.Add (element);
1383 overrides.Add (typeof (SimpleClass), "something", attr);
1386 SimpleClass simple = new SimpleClass (); ;
1387 Serialize (simple, overrides);
1388 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1391 simple.something = "hello";
1392 Serialize (simple, overrides);
1393 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
1396 element.ElementName = "saying";
1397 Serialize (simple, overrides);
1398 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
1401 element.IsNullable = false;
1402 simple.something = null;
1403 Serialize (simple, overrides);
1404 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
1406 element.IsNullable = true;
1407 simple.something = null;
1408 Serialize (simple, overrides);
1409 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
1412 element.ElementName = null;
1413 element.IsNullable = false;
1414 element.Namespace = "some:urn";
1415 simple.something = "hello";
1416 Serialize (simple, overrides);
1417 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
1424 // test XmlElementAttribute with arrays and collections //////////////////
1426 public void TestSerializeCollectionWithXmlElementAttribute ()
1429 // if no type is specified or the specified type
1430 // matches the contents of the collection,
1431 // serialize each element in an element named after the member.
1432 // if the type does not match, or matches the collection itself,
1433 // create a base wrapping element for the member, and then
1434 // wrap each collection item in its own wrapping element based on type.
1436 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1437 XmlAttributes attr = new XmlAttributes ();
1438 XmlElementAttribute element = new XmlElementAttribute ();
1439 attr.XmlElements.Add (element);
1440 overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
1442 // empty collection & no type info in XmlElementAttribute
1443 StringCollectionContainer container = new StringCollectionContainer ();
1444 Serialize (container, overrides);
1445 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1447 // non-empty collection & no type info in XmlElementAttribute
1448 container.Messages.Add ("hello");
1449 Serialize (container, overrides);
1450 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
1452 // non-empty collection & only type info in XmlElementAttribute
1453 element.Type = typeof (StringCollection);
1454 Serialize (container, overrides);
1455 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
1457 // non-empty collection & only type info in XmlElementAttribute
1458 element.Type = typeof (string);
1459 Serialize (container, overrides);
1460 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4");
1463 container.Messages.Add ("goodbye");
1464 element.Type = null;
1465 Serialize (container, overrides);
1466 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText, "#5");
1469 // test DefaultValue /////////////////////////////////////////////////////
1472 [Ignore ("TD #7458")]
1474 public void TestSerializeDefaultValueAttribute ()
1476 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1478 XmlAttributes attr = new XmlAttributes ();
1479 string defaultValueInstance = "nothing";
1480 attr.XmlDefaultValue = defaultValueInstance;
1481 overrides.Add (typeof (SimpleClass), "something", attr);
1484 SimpleClass simple = new SimpleClass ();
1485 Serialize (simple, overrides);
1486 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1488 // same value as default
1489 simple.something = defaultValueInstance;
1490 Serialize (simple, overrides);
1491 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1494 simple.something = "hello";
1495 Serialize (simple, overrides);
1496 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3");
1498 overrides = new XmlAttributeOverrides ();
1499 attr = new XmlAttributes ();
1500 attr.XmlAttribute = new XmlAttributeAttribute ();
1501 attr.XmlDefaultValue = defaultValueInstance;
1502 overrides.Add (typeof (SimpleClass), "something", attr);
1505 simple = new SimpleClass ();
1506 Serialize (simple, overrides);
1507 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1509 // same value as default
1510 simple.something = defaultValueInstance;
1511 Serialize (simple, overrides);
1512 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
1515 simple.something = "hello";
1516 Serialize (simple, overrides);
1517 Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
1519 overrides = new XmlAttributeOverrides ();
1520 attr = new XmlAttributes ();
1521 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1522 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1525 TestDefault testDefault = new TestDefault ();
1526 Serialize (testDefault);
1527 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
1529 // use the default with overrides
1530 Serialize (testDefault, overrides);
1531 Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
1533 overrides = new XmlAttributeOverrides ();
1534 attr = new XmlAttributes ();
1535 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1536 attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
1537 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1539 // use the default with overrides
1540 Serialize (testDefault, overrides);
1541 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
1543 // use the default with overrides and default namspace
1544 Serialize (testDefault, overrides, AnotherNamespace);
1545 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
1547 // non-default values
1548 testDefault.strDefault = "Some Text";
1549 testDefault.boolT = false;
1550 testDefault.boolF = true;
1551 testDefault.decimalval = 20m;
1552 testDefault.flag = FlagEnum.e2;
1553 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1554 Serialize (testDefault);
1555 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1556 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1557 " <strDefault>Some Text</strDefault>" +
1558 " <boolT>false</boolT>" +
1559 " <boolF>true</boolF>" +
1560 " <decimalval>20</decimalval>" +
1561 " <flag>two</flag>" +
1562 " <flagencoded>e1 e2</flagencoded>" +
1563 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1566 Serialize (testDefault, overrides);
1567 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1568 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1569 " <strDefault>Some Text</strDefault>" +
1570 " <boolT>false</boolT>" +
1571 " <boolF>true</boolF>" +
1572 " <decimalval>20</decimalval>" +
1573 " <flag>two</flag>" +
1574 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1577 Serialize (testDefault, overrides, AnotherNamespace);
1578 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1579 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1580 " <strDefault>Some Text</strDefault>" +
1581 " <boolT>false</boolT>" +
1582 " <boolF>true</boolF>" +
1583 " <decimalval>20</decimalval>" +
1584 " <flag>two</flag>" +
1585 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1588 attr = new XmlAttributes ();
1589 XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
1590 xmlType.Namespace = "yetanother:urn";
1591 attr.XmlType = xmlType;
1592 overrides.Add (typeof (FlagEnum_Encoded), attr);
1594 Serialize (testDefault, overrides, AnotherNamespace);
1595 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1596 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1597 " <strDefault>Some Text</strDefault>" +
1598 " <boolT>false</boolT>" +
1599 " <boolF>true</boolF>" +
1600 " <decimalval>20</decimalval>" +
1601 " <flag>two</flag>" +
1602 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1605 attr = new XmlAttributes ();
1606 attr.XmlType = new XmlTypeAttribute ("testDefault");
1607 overrides.Add (typeof (TestDefault), attr);
1609 Serialize (testDefault, overrides, AnotherNamespace);
1610 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1611 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1612 " <strDefault>Some Text</strDefault>" +
1613 " <boolT>false</boolT>" +
1614 " <boolF>true</boolF>" +
1615 " <decimalval>20</decimalval>" +
1616 " <flag>two</flag>" +
1617 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1618 AnotherNamespace)), WriterText, "#C9");
1622 [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
1623 public void TestSerializeDefaultValueAttribute_Encoded ()
1625 SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
1626 SoapAttributes attr = new SoapAttributes ();
1627 attr.SoapAttribute = new SoapAttributeAttribute ();
1628 string defaultValueInstance = "nothing";
1629 attr.SoapDefaultValue = defaultValueInstance;
1630 overrides.Add (typeof (SimpleClass), "something", attr);
1633 SimpleClass simple = new SimpleClass ();
1634 SerializeEncoded (simple, overrides);
1635 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1637 // same value as default
1638 simple.something = defaultValueInstance;
1639 SerializeEncoded (simple, overrides);
1640 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1643 simple.something = "hello";
1644 SerializeEncoded (simple, overrides);
1645 Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
1647 attr.SoapAttribute = null;
1648 attr.SoapElement = new SoapElementAttribute ();
1651 simple = new SimpleClass ();
1652 SerializeEncoded (simple, overrides);
1653 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1655 // same value as default
1656 simple.something = defaultValueInstance;
1657 SerializeEncoded (simple, overrides);
1658 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
1661 simple.something = "hello";
1662 SerializeEncoded (simple, overrides);
1663 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
1665 overrides = new SoapAttributeOverrides ();
1666 attr = new SoapAttributes ();
1667 attr.SoapElement = new SoapElementAttribute ("flagenc");
1668 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1670 // use the default (from MS KB325691)
1671 TestDefault testDefault = new TestDefault ();
1672 SerializeEncoded (testDefault);
1673 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1674 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1675 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1676 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1677 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1678 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1679 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1680 " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1681 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1684 SerializeEncoded (testDefault, overrides);
1685 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1686 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1687 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1688 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1689 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1690 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1691 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1692 " <flagenc xsi:type='flagenum'>one four</flagenc>" +
1693 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1696 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1697 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1698 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1699 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1700 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1701 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1702 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1703 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1704 " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1705 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1706 AnotherNamespace)), WriterText, "#C3");
1708 // non-default values
1709 testDefault.strDefault = "Some Text";
1710 testDefault.boolT = false;
1711 testDefault.boolF = true;
1712 testDefault.decimalval = 20m;
1713 testDefault.flag = FlagEnum.e2;
1714 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1715 SerializeEncoded (testDefault);
1716 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1717 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1718 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1719 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1720 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1721 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1722 " <flag xsi:type='FlagEnum'>e2</flag>" +
1723 " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1724 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1727 SerializeEncoded (testDefault, overrides);
1728 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1729 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1730 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1731 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1732 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1733 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1734 " <flag xsi:type='FlagEnum'>e2</flag>" +
1735 " <flagenc xsi:type='flagenum'>one two</flagenc>" +
1736 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1739 attr = new SoapAttributes ();
1740 attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
1741 overrides.Add (typeof (FlagEnum_Encoded), attr);
1743 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1744 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1745 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1746 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1747 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1748 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1749 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1750 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1751 " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1752 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1753 AnotherNamespace)), WriterText, "#C6");
1755 attr = new SoapAttributes ();
1756 attr.SoapType = new SoapTypeAttribute ("testDefault");
1757 overrides.Add (typeof (TestDefault), attr);
1759 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1760 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1761 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1762 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1763 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1764 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1765 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1766 " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1767 " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1768 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1769 AnotherNamespace)), WriterText, "#C7");
1772 // test XmlEnum //////////////////////////////////////////////////////////
1774 public void TestSerializeXmlEnumAttribute ()
1776 Serialize (XmlSchemaForm.Qualified);
1777 Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
1779 Serialize (XmlSchemaForm.Unqualified);
1780 Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
1784 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1786 // technically XmlSchemaForm.None has an XmlIgnore attribute,
1787 // but it is not being serialized as a member.
1791 Serialize (XmlSchemaForm.None);
1793 } catch (InvalidOperationException ex) {
1794 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1795 Assert.IsNotNull (ex.InnerException, "#3");
1796 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
1797 Assert.IsNotNull (ex.InnerException.Message, "#5");
1798 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
1799 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
1802 Serialize (XmlSchemaForm.None);
1803 Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
1808 public void TestSerializeXmlNodeArray ()
1810 XmlDocument doc = new XmlDocument ();
1811 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1812 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1816 public void TestSerializeXmlElementArray ()
1818 XmlDocument doc = new XmlDocument ();
1819 Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1820 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1825 [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
1826 public void TestSerializeGenericListOfNode ()
1828 XmlDocument doc = new XmlDocument ();
1829 Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1830 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1834 [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
1835 public void TestSerializeGenericListOfElement ()
1837 XmlDocument doc = new XmlDocument ();
1838 Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1839 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1844 public void TestSerializeXmlElement ()
1846 XmlDocument doc = new XmlDocument ();
1847 Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
1848 Assert.AreEqual (Infoset ("<elem/>"), WriterText);
1852 public void TestSerializeXmlElementSubclass ()
1854 XmlDocument doc = new XmlDocument ();
1855 Serialize (new MyElem (doc), typeof (XmlElement));
1856 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
1858 Serialize (new MyElem (doc), typeof (MyElem));
1859 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
1863 public void TestSerializeXmlCDataSection ()
1865 XmlDocument doc = new XmlDocument ();
1866 CDataContainer c = new CDataContainer ();
1867 c.cdata = doc.CreateCDataSection ("data section contents");
1869 Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
1873 public void TestSerializeXmlNode ()
1875 XmlDocument doc = new XmlDocument ();
1876 NodeContainer c = new NodeContainer ();
1877 c.node = doc.CreateTextNode ("text");
1879 Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
1883 public void TestSerializeChoice ()
1885 Choices ch = new Choices ();
1886 ch.MyChoice = "choice text";
1887 ch.ItemType = ItemChoiceType.ChoiceZero;
1889 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText, "#1");
1890 ch.ItemType = ItemChoiceType.StrangeOne;
1892 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText, "#2");
1893 ch.ItemType = ItemChoiceType.ChoiceTwo;
1895 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText, "#3");
1899 public void TestSerializeNamesWithSpaces ()
1901 TestSpace ts = new TestSpace ();
1905 Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
1909 public void TestSerializeReadOnlyProps ()
1911 ReadOnlyProperties ts = new ReadOnlyProperties ();
1913 Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1917 public void TestSerializeIList ()
1919 clsPerson k = new clsPerson ();
1920 k.EmailAccounts = new ArrayList ();
1921 k.EmailAccounts.Add ("a");
1922 k.EmailAccounts.Add ("b");
1924 Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
1928 public void TestSerializeArrayEnc ()
1930 SoapReflectionImporter imp = new SoapReflectionImporter ();
1931 XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1932 XmlSerializer ser = new XmlSerializer (map);
1933 StringWriter sw = new StringWriter ();
1934 XmlTextWriter tw = new XmlTextWriter (sw);
1935 tw.WriteStartElement ("aa");
1936 ser.Serialize (tw, new ArrayClass ());
1937 tw.WriteEndElement ();
1940 [Test] // bug #76049
1941 public void TestIncludeType ()
1943 XmlReflectionImporter imp = new XmlReflectionImporter ();
1944 XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1945 imp.IncludeType (typeof (TestSpace));
1946 XmlSerializer ser = new XmlSerializer (map);
1947 ser.Serialize (new StringWriter (), new TestSpace ());
1951 public void TestSerializeChoiceArray ()
1953 CompositeValueType v = new CompositeValueType ();
1956 Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
1960 public void TestArrayAttributeWithDataType ()
1962 Serialize (new ArrayAttributeWithType ());
1963 string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1964 res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1965 Assert.AreEqual (Infoset (res), WriterText);
1969 public void TestSubclassElementType ()
1971 SubclassTestContainer c = new SubclassTestContainer ();
1972 c.data = new SubclassTestSub ();
1975 string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1976 res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1977 Assert.AreEqual (Infoset (res), WriterText);
1981 [ExpectedException (typeof (InvalidOperationException))]
1982 public void TestArrayAttributeWithWrongDataType ()
1984 Serialize (new ArrayAttributeWithWrongType ());
1988 [Category ("NotWorking")]
1989 public void TestSerializePrimitiveTypesContainer ()
1991 Serialize (new PrimitiveTypesContainer ());
1992 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1993 "<?xml version='1.0' encoding='utf-16'?>" +
1995 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
1997 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
1999 "<Number>2004</Number>" +
2000 "<Name>some name</Name>" +
2001 "<Index>56</Index>" +
2002 "<Password>8w8=</Password>" +
2003 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2004 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2005 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2007 SerializeEncoded (new PrimitiveTypesContainer ());
2008 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2009 "<?xml version='1.0' encoding='utf-16'?>" +
2011 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2013 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2015 "<Number xsi:type='xsd:int'>2004</Number>" +
2016 "<Name xsi:type='xsd:string'>some name</Name>" +
2017 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2018 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2019 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2020 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2021 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2022 sw.ToString (), "#2");
2026 public void TestSchemaForm ()
2028 TestSchemaForm1 t1 = new TestSchemaForm1 ();
2029 t1.p1 = new PrintTypeResponse ();
2031 t1.p2 = new PrintTypeResponse ();
2034 TestSchemaForm2 t2 = new TestSchemaForm2 ();
2035 t2.p1 = new PrintTypeResponse ();
2037 t2.p2 = new PrintTypeResponse ();
2042 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2043 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2046 res += " <data>data1</data>";
2047 res += " </result>";
2048 res += " <intern xmlns=\"urn:responseTypes\">";
2049 res += " <result xmlns=\"\">";
2050 res += " <data>data2</data>";
2051 res += " </result>";
2052 res += " </intern>";
2054 res += " <p2 xmlns=\"urn:oo\">";
2055 res += " <result xmlns=\"\">";
2056 res += " <data>data1</data>";
2057 res += " </result>";
2058 res += " <intern xmlns=\"urn:responseTypes\">";
2059 res += " <result xmlns=\"\">";
2060 res += " <data>data2</data>";
2061 res += " </result>";
2062 res += " </intern>";
2064 res += "</TestSchemaForm1>";
2065 Assert.AreEqual (Infoset (res), WriterText);
2069 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2070 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2071 res += " <p1 xmlns=\"urn:testForm\">";
2072 res += " <result xmlns=\"\">";
2073 res += " <data>data1</data>";
2074 res += " </result>";
2075 res += " <intern xmlns=\"urn:responseTypes\">";
2076 res += " <result xmlns=\"\">";
2077 res += " <data>data2</data>";
2078 res += " </result>";
2079 res += " </intern>";
2081 res += " <p2 xmlns=\"urn:oo\">";
2082 res += " <result xmlns=\"\">";
2083 res += " <data>data1</data>";
2084 res += " </result>";
2085 res += " <intern xmlns=\"urn:responseTypes\">";
2086 res += " <result xmlns=\"\">";
2087 res += " <data>data2</data>";
2088 res += " </result>";
2089 res += " </intern>";
2091 res += "</TestSchemaForm2>";
2092 Assert.AreEqual (Infoset (res), WriterText);
2094 XmlReflectionImporter imp = new XmlReflectionImporter ();
2095 XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2096 Serialize (t1, map);
2098 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2099 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
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 += "</TestSchemaForm1>";
2121 Assert.AreEqual (Infoset (res), WriterText);
2123 imp = new XmlReflectionImporter ();
2124 map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2125 Serialize (t2, map);
2127 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2128 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2129 res += " <p1 xmlns=\"urn:testForm\">";
2130 res += " <result xmlns=\"\">";
2131 res += " <data>data1</data>";
2132 res += " </result>";
2133 res += " <intern xmlns=\"urn:responseTypes\">";
2134 res += " <result xmlns=\"\">";
2135 res += " <data>data2</data>";
2136 res += " </result>";
2137 res += " </intern>";
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 += "</TestSchemaForm2>";
2150 Assert.AreEqual (Infoset (res), WriterText);
2153 [Test] // bug #78536
2154 public void CDataTextNodes ()
2156 XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2157 ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2158 string xml = @"<CDataTextNodesType>
2160 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2161 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2162 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2164 </CDataTextNodesType>";
2165 ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2171 public void GenerateSerializerGenerics ()
2173 XmlReflectionImporter imp = new XmlReflectionImporter ();
2174 Type type = typeof (List<int>);
2175 XmlSerializer.GenerateSerializer (
2177 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2182 public void Nullable ()
2184 XmlSerializer ser = new XmlSerializer (typeof (int?));
2185 int? nullableType = 5;
2186 sw = new StringWriter ();
2187 xtw = new XmlTextWriter (sw);
2188 ser.Serialize (xtw, nullableType);
2190 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2191 Assert.AreEqual (Infoset (expected), WriterText);
2192 int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2193 Assert.AreEqual (5, i);
2198 public void SerializeBase64Binary()
2200 XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2201 sw = new StringWriter ();
2202 XmlTextWriter xtw = new XmlTextWriter (sw);
2203 ser.Serialize (xtw, new Base64Binary ());
2205 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
2206 Assert.AreEqual (Infoset (expected), WriterText);
2207 Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2208 Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2211 [Test] // bug #79989, #79990
2212 public void SerializeHexBinary ()
2214 XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2215 sw = new StringWriter ();
2216 XmlTextWriter xtw = new XmlTextWriter (sw);
2217 ser.Serialize (xtw, new HexBinary ());
2219 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
2220 Assert.AreEqual (Infoset (expected), WriterText);
2221 HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2222 Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2226 [ExpectedException (typeof (InvalidOperationException))]
2227 public void XmlArrayAttributeOnInt ()
2229 new XmlSerializer (typeof (XmlArrayOnInt));
2233 [ExpectedException (typeof (InvalidOperationException))]
2234 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2236 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2240 [ExpectedException (typeof (InvalidOperationException))]
2241 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2243 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2246 [Test] // bug #78042
2247 public void XmlArrayAttributeOnArray ()
2249 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2250 sw = new StringWriter ();
2251 XmlTextWriter xtw = new XmlTextWriter (sw);
2252 ser.Serialize (xtw, new XmlArrayOnArray ());
2254 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
2255 Assert.AreEqual (Infoset (expected), WriterText);
2259 public void XmlArrayAttributeOnCollection ()
2261 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2262 XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2263 inst.Sane.Add ("abc");
2265 sw = new StringWriter ();
2266 XmlTextWriter xtw = new XmlTextWriter (sw);
2267 ser.Serialize (xtw, inst);
2269 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
2270 Assert.AreEqual (Infoset (expected), WriterText);
2273 #region GenericsSeralizationTests
2277 public void TestSerializeGenSimpleClassString ()
2279 GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2281 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2283 simple.something = "hello";
2286 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
2290 public void TestSerializeGenSimpleClassBool ()
2292 GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2294 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
2296 simple.something = true;
2299 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
2303 public void TestSerializeGenSimpleStructInt ()
2305 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2307 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
2309 simple.something = 123;
2312 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
2316 public void TestSerializeGenListClassString ()
2318 GenListClass<string> genlist = new GenListClass<string> ();
2319 Serialize (genlist);
2320 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
2322 genlist.somelist.Add ("Value1");
2323 genlist.somelist.Add ("Value2");
2325 Serialize (genlist);
2326 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
2330 public void TestSerializeGenListClassFloat ()
2332 GenListClass<float> genlist = new GenListClass<float> ();
2333 Serialize (genlist);
2334 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
2336 genlist.somelist.Add (1);
2337 genlist.somelist.Add (2.2F);
2339 Serialize (genlist);
2340 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
2344 public void TestSerializeGenListClassList ()
2346 GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2347 Serialize (genlist);
2348 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2350 GenListClass<int> inlist1 = new GenListClass<int> ();
2351 inlist1.somelist.Add (1);
2352 inlist1.somelist.Add (2);
2353 GenListClass<int> inlist2 = new GenListClass<int> ();
2354 inlist2.somelist.Add (10);
2355 inlist2.somelist.Add (20);
2356 genlist.somelist.Add (inlist1);
2357 genlist.somelist.Add (inlist2);
2359 Serialize (genlist);
2360 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2364 public void TestSerializeGenListClassArray ()
2366 GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2367 Serialize (genlist);
2368 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2370 GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2371 genarr1.arr[0] = 'a';
2372 genarr1.arr[1] = 'b';
2373 genlist.somelist.Add (genarr1);
2374 GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2375 genarr2.arr[0] = 'd';
2376 genarr2.arr[1] = 'e';
2377 genarr2.arr[2] = 'f';
2378 genlist.somelist.Add (genarr2);
2380 Serialize (genlist);
2381 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2385 public void TestSerializeGenTwoClassCharDouble ()
2387 GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2389 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
2391 gentwo.something1 = 'a';
2392 gentwo.something2 = 2.2;
2395 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
2399 public void TestSerializeGenDerivedClassDecimalShort ()
2401 GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2402 Serialize (derived);
2403 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2405 derived.something1 = "Value1";
2406 derived.something2 = 1;
2407 derived.another1 = 1.1M;
2408 derived.another2 = -22;
2410 Serialize (derived);
2411 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2415 public void TestSerializeGenDerivedSecondClassByteUlong ()
2417 GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2418 Serialize (derived2);
2419 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2421 derived2.something1 = 1;
2422 derived2.something2 = 222;
2423 derived2.another1 = 111;
2424 derived2.another2 = 222222;
2426 Serialize (derived2);
2427 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2431 public void TestSerializeGenNestedClass ()
2433 GenNestedClass<string, int>.InnerClass<bool> nested =
2434 new GenNestedClass<string, int>.InnerClass<bool> ();
2436 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
2439 nested.something = true;
2442 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
2446 public void TestSerializeGenListClassListNested ()
2448 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2449 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2450 Serialize (genlist);
2451 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2453 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2454 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2455 GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2457 inval1.something = "ONE";
2458 inlist1.somelist.Add (inval1);
2459 GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2461 inval2.something = "TWO";
2462 inlist1.somelist.Add (inval2);
2463 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2464 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2465 GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2467 inval3.something = "THIRTY";
2468 inlist2.somelist.Add (inval3);
2469 genlist.somelist.Add (inlist1);
2470 genlist.somelist.Add (inlist2);
2472 Serialize (genlist);
2473 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2476 public enum Myenum { one, two, three, four, five, six };
2478 public void TestSerializeGenArrayClassEnum ()
2480 GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2482 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2484 genarr.arr[0] = Myenum.one;
2485 genarr.arr[1] = Myenum.three;
2486 genarr.arr[2] = Myenum.five;
2489 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2493 public void TestSerializeGenArrayStruct ()
2495 GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2497 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
2499 GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2500 genstruct.something = 111;
2501 genarr.arr[0] = genstruct;
2502 genstruct.something = 222;
2503 genarr.arr[1] = genstruct;
2504 genstruct.something = 333;
2505 genarr.arr[2] = genstruct;
2508 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
2512 public void TestSerializeGenArrayList ()
2514 GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2516 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
2518 GenListClass<string> genlist1 = new GenListClass<string> ();
2519 genlist1.somelist.Add ("list1-val1");
2520 genlist1.somelist.Add ("list1-val2");
2521 genarr.arr[0] = genlist1;
2522 GenListClass<string> genlist2 = new GenListClass<string> ();
2523 genlist2.somelist.Add ("list2-val1");
2524 genlist2.somelist.Add ("list2-val2");
2525 genlist2.somelist.Add ("list2-val3");
2526 genlist2.somelist.Add ("list2-val4");
2527 genarr.arr[1] = genlist2;
2528 GenListClass<string> genlist3 = new GenListClass<string> ();
2529 genlist3.somelist.Add ("list3val");
2530 genarr.arr[2] = genlist3;
2533 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
2537 public void TestSerializeGenComplexStruct ()
2539 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2540 Serialize (complex);
2541 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
2543 complex.something = 123;
2544 complex.simpleclass.something = 456;
2545 complex.simplestruct.something = 789;
2546 GenListClass<int> genlist = new GenListClass<int> ();
2547 genlist.somelist.Add (100);
2548 genlist.somelist.Add (200);
2549 complex.listclass = genlist;
2550 GenArrayClass<int> genarr = new GenArrayClass<int> ();
2554 complex.arrayclass = genarr;
2555 complex.twoclass.something1 = 10;
2556 complex.twoclass.something2 = "Ten";
2557 complex.derivedclass.another1 = 1;
2558 complex.derivedclass.another2 = "one";
2559 complex.derivedclass.something1 = "two";
2560 complex.derivedclass.something2 = 2;
2561 complex.derived2.another1 = 3;
2562 complex.derived2.another2 = "three";
2563 complex.derived2.something1 = 4;
2564 complex.derived2.something2 = "four";
2565 complex.nestedouter.outer = 5;
2566 complex.nestedinner.inner = "six";
2567 complex.nestedinner.something = 6;
2569 Serialize (complex);
2570 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
2573 [Test] // bug #80759
2574 public void HasFieldSpecifiedButIrrelevant ()
2576 Bug80759 foo = new Bug80759 ();
2578 foo.NullableInt = 10;
2580 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2582 MemoryStream stream = new MemoryStream ();
2584 serializer.Serialize (stream, foo);
2585 stream.Position = 0;
2586 foo = (Bug80759) serializer.Deserialize (stream);
2590 #endregion //GenericsSeralizationTests
2592 public class XmlArrayOnInt
2598 public class XmlArrayUnqualifiedWithNamespace
2600 [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2601 public ArrayList Sane = new ArrayList ();
2604 public class XmlArrayItemUnqualifiedWithNamespace
2606 [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2607 public ArrayList Sane = new ArrayList ();
2610 [XmlRoot (Namespace = "urn:foo")]
2611 public class XmlArrayOnArrayList
2613 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2614 public ArrayList Sane = new ArrayList ();
2617 [XmlRoot (Namespace = "urn:foo")]
2618 public class XmlArrayOnArray
2620 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2621 public string[] Sane = new string[] { "foo", "bar" };
2623 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2624 public ArrayItemInXmlArray[] Mids =
2625 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
2628 [XmlType (Namespace = "urn:gyabo")]
2629 public class ArrayItemInXmlArray
2631 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2632 public string[] Whee = new string[] { "foo", "bar" };
2635 [XmlRoot ("Base64Binary")]
2636 public class Base64Binary
2638 [XmlAttribute (DataType = "base64Binary")]
2639 public byte [] Data = new byte [] {1, 2, 3};
2642 [XmlRoot ("HexBinary")]
2643 public class HexBinary
2645 [XmlAttribute (DataType = "hexBinary")]
2646 public byte[] Data = new byte[] { 1, 2, 3 };
2649 public class CDataTextNodesType
2651 public CDataTextNodesInternal foo;
2654 public class CDataTextNodesInternal
2657 public string Value;
2661 public class Bug80759
2664 public int? NullableInt;
2667 public bool NullableIntSpecified {
2668 get { return NullableInt.HasValue; }
2673 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
2680 public static string Infoset (string sx)
2682 XmlDocument doc = new XmlDocument ();
2684 StringBuilder sb = new StringBuilder ();
2685 GetInfoset (doc.DocumentElement, sb);
2686 return sb.ToString ();
2689 public static string Infoset (XmlNode nod)
2691 StringBuilder sb = new StringBuilder ();
2692 GetInfoset (nod, sb);
2693 return sb.ToString ();
2696 static void GetInfoset (XmlNode nod, StringBuilder sb)
2698 switch (nod.NodeType) {
2699 case XmlNodeType.Attribute:
2700 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
2701 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
2704 case XmlNodeType.Element:
2705 XmlElement elem = (XmlElement) nod;
2706 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
2708 ArrayList ats = new ArrayList ();
2709 foreach (XmlAttribute at in elem.Attributes)
2710 ats.Add (at.LocalName + " " + at.NamespaceURI);
2714 foreach (string name in ats) {
2715 string[] nn = name.Split (' ');
2716 GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
2720 foreach (XmlNode cn in elem.ChildNodes)
2721 GetInfoset (cn, sb);
2726 sb.Append (nod.OuterXml);
2731 static XmlTypeMapping CreateSoapMapping (Type type)
2733 SoapReflectionImporter importer = new SoapReflectionImporter ();
2734 return importer.ImportTypeMapping (type);
2737 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
2739 SoapReflectionImporter importer = new SoapReflectionImporter (ao);
2740 return importer.ImportTypeMapping (type);
2743 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
2745 SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
2746 return importer.ImportTypeMapping (type);