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);
243 public void TestSerializeChar ()
246 Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
249 Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
252 Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
254 Serialize ('\uFF01');
255 Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
259 public void TestSerializeFloat ()
262 Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
265 Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
267 // FIXME test INF and other boundary conditions that may exist with floats
271 public void TestSerializeEnumeration_FromValue ()
273 Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
275 "<?xml version='1.0' encoding='utf-16'?>" +
276 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
281 [Category ("NotWorking")]
282 public void TestSerializeEnumeration_FromValue_Encoded ()
284 SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
285 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
286 "<?xml version='1.0' encoding='utf-16'?>" +
287 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
288 XmlSchema.InstanceNamespace), sw.ToString ());
292 public void TestSerializeEnumeration ()
294 Serialize (SimpleEnumeration.FIRST);
295 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
297 Serialize (SimpleEnumeration.SECOND);
298 Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
302 public void TestSerializeEnumeration_Encoded ()
304 SerializeEncoded (SimpleEnumeration.FIRST);
305 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
306 "<?xml version='1.0' encoding='utf-16'?>" +
307 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
308 XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
310 SerializeEncoded (SimpleEnumeration.SECOND);
311 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
312 "<?xml version='1.0' encoding='utf-16'?>" +
313 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
314 XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
318 public void TestSerializeEnumDefaultValue ()
320 Serialize (new EnumDefaultValue ());
321 Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
323 Serialize (new SimpleEnumeration ());
324 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
326 Serialize (3, typeof (EnumDefaultValue));
327 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
329 Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
330 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
332 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
333 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
335 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
336 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
338 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
339 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
341 Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
342 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
344 Serialize (3, typeof (FlagEnum));
345 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
347 Serialize (5, typeof (FlagEnum));
348 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
350 Serialize (FlagEnum.e4, typeof (FlagEnum));
351 Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
353 Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
354 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
356 Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
357 Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
359 Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
360 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
362 Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
363 Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
365 Serialize (3, typeof (EnumDefaultValueNF));
366 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
368 Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
369 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
371 Serialize (2, typeof (ZeroFlagEnum));
372 Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t<w>o</ZeroFlagEnum>"), WriterText, "#18");
374 Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
375 Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
379 [Category ("NotWorking")]
380 public void TestSerializeEnumDefaultValue_Encoded ()
382 SerializeEncoded (new EnumDefaultValue ());
383 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
384 "<?xml version='1.0' encoding='utf-16'?>" +
385 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
386 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
388 SerializeEncoded (new SimpleEnumeration ());
389 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
390 "<?xml version='1.0' encoding='utf-16'?>" +
391 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
392 XmlSchema.InstanceNamespace), sw.ToString (), "#2");
394 SerializeEncoded (3, typeof (EnumDefaultValue));
395 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
396 "<?xml version='1.0' encoding='utf-16'?>" +
397 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
398 XmlSchema.InstanceNamespace), sw.ToString (), "#3");
400 SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
401 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
402 "<?xml version='1.0' encoding='utf-16'?>" +
403 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
404 XmlSchema.InstanceNamespace), sw.ToString (), "#4");
406 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
407 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
408 "<?xml version='1.0' encoding='utf-16'?>" +
409 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
410 XmlSchema.InstanceNamespace), sw.ToString (), "#5");
412 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
413 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
414 "<?xml version='1.0' encoding='utf-16'?>" +
415 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
416 XmlSchema.InstanceNamespace), sw.ToString (), "#6");
418 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
419 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
420 "<?xml version='1.0' encoding='utf-16'?>" +
421 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
422 XmlSchema.InstanceNamespace), sw.ToString (), "#7");
424 SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
425 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
426 "<?xml version='1.0' encoding='utf-16'?>" +
427 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
428 XmlSchema.InstanceNamespace), sw.ToString (), "#8");
430 SerializeEncoded (3, typeof (FlagEnum));
431 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
432 "<?xml version='1.0' encoding='utf-16'?>" +
433 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
434 XmlSchema.InstanceNamespace), sw.ToString (), "#9");
436 SerializeEncoded (5, typeof (FlagEnum));
437 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
438 "<?xml version='1.0' encoding='utf-16'?>" +
439 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
440 XmlSchema.InstanceNamespace), sw.ToString (), "#10");
442 SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
443 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
444 "<?xml version='1.0' encoding='utf-16'?>" +
445 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
446 XmlSchema.InstanceNamespace), sw.ToString (), "#11");
448 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
449 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
450 "<?xml version='1.0' encoding='utf-16'?>" +
451 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
452 XmlSchema.InstanceNamespace), sw.ToString (), "#12");
454 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
455 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
456 "<?xml version='1.0' encoding='utf-16'?>" +
457 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
458 XmlSchema.InstanceNamespace), sw.ToString (), "#13");
460 SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
461 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
462 "<?xml version='1.0' encoding='utf-16'?>" +
463 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
464 XmlSchema.InstanceNamespace), sw.ToString (), "#14");
466 SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
467 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
468 "<?xml version='1.0' encoding='utf-16'?>" +
469 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
470 XmlSchema.InstanceNamespace), sw.ToString (), "#15");
472 SerializeEncoded (3, typeof (EnumDefaultValueNF));
473 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
474 "<?xml version='1.0' encoding='utf-16'?>" +
475 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
476 XmlSchema.InstanceNamespace), sw.ToString (), "#16");
478 SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
479 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
480 "<?xml version='1.0' encoding='utf-16'?>" +
481 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
482 XmlSchema.InstanceNamespace), sw.ToString (), "#17");
484 SerializeEncoded (2, typeof (ZeroFlagEnum));
485 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
486 "<?xml version='1.0' encoding='utf-16'?>" +
487 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
488 XmlSchema.InstanceNamespace), sw.ToString (), "#18");
490 SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
491 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
492 "<?xml version='1.0' encoding='utf-16'?>" +
493 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
494 XmlSchema.InstanceNamespace), sw.ToString (), "#19");
498 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
501 Serialize ("b", typeof (EnumDefaultValue));
503 } catch (InvalidOperationException ex) {
504 Assert.IsNotNull (ex.InnerException, "#A2");
505 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
509 Serialize ("e1", typeof (EnumDefaultValue));
511 } catch (InvalidOperationException ex) {
512 Assert.IsNotNull (ex.InnerException, "#B2");
513 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
517 Serialize ("e1,e2", typeof (EnumDefaultValue));
519 } catch (InvalidOperationException ex) {
520 Assert.IsNotNull (ex.InnerException, "#C2");
521 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
525 Serialize (string.Empty, typeof (EnumDefaultValue));
527 } catch (InvalidOperationException ex) {
528 Assert.IsNotNull (ex.InnerException, "#D2");
529 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
533 Serialize ("1", typeof (EnumDefaultValue));
535 } catch (InvalidOperationException ex) {
536 Assert.IsNotNull (ex.InnerException, "#E2");
537 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
541 Serialize ("0", typeof (EnumDefaultValue));
543 } catch (InvalidOperationException ex) {
544 Assert.IsNotNull (ex.InnerException, "#F2");
545 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
549 Serialize (new SimpleClass (), typeof (EnumDefaultValue));
551 } catch (InvalidOperationException ex) {
552 Assert.IsNotNull (ex.InnerException, "#G2");
553 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
558 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
562 Serialize (5, typeof (EnumDefaultValue));
564 } catch (InvalidOperationException ex) {
565 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
566 Assert.IsNotNull (ex.InnerException, "#3");
567 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
568 Assert.IsNotNull (ex.InnerException.Message, "#5");
569 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
570 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
573 Serialize (5, typeof (EnumDefaultValue));
574 Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
579 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
583 Serialize (new EnumDefaultValueNF ());
585 } catch (InvalidOperationException ex) {
586 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
587 Assert.IsNotNull (ex.InnerException, "#3");
588 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
589 Assert.IsNotNull (ex.InnerException.Message, "#5");
590 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
591 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
594 Serialize (new EnumDefaultValueNF ());
595 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
600 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
604 Serialize (15, typeof (EnumDefaultValueNF));
606 } catch (InvalidOperationException ex) {
607 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
608 Assert.IsNotNull (ex.InnerException, "#3");
609 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
610 Assert.IsNotNull (ex.InnerException.Message, "#5");
611 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
612 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
615 Serialize (15, typeof (EnumDefaultValueNF));
616 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
621 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
624 Serialize ("b", typeof (EnumDefaultValueNF));
626 } catch (InvalidOperationException ex) {
627 Assert.IsNotNull (ex.InnerException, "#A2");
628 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
632 Serialize ("e2", typeof (EnumDefaultValueNF));
634 } catch (InvalidOperationException ex) {
635 Assert.IsNotNull (ex.InnerException, "#B2");
636 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
640 Serialize (string.Empty, typeof (EnumDefaultValueNF));
642 } catch (InvalidOperationException ex) {
643 Assert.IsNotNull (ex.InnerException, "#C2");
644 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
648 Serialize ("1", typeof (EnumDefaultValueNF));
650 } catch (InvalidOperationException ex) {
651 Assert.IsNotNull (ex.InnerException, "#D2");
652 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
656 Serialize ("0", typeof (EnumDefaultValueNF));
658 } catch (InvalidOperationException ex) {
659 Assert.IsNotNull (ex.InnerException, "#E2");
660 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
665 public void TestSerializeField ()
667 Field f = new Field ();
668 Serialize (f, typeof (Field));
669 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
670 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
671 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
672 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
674 f.Flags1 = FlagEnum.e1;
675 f.Flags2 = FlagEnum.e1;
676 f.Flags3 = FlagEnum.e2;
677 f.Modifiers = MapModifiers.Protected;
678 f.Modifiers2 = MapModifiers.Public;
679 f.Modifiers3 = MapModifiers.Public;
680 f.Modifiers4 = MapModifiers.Protected;
681 f.Modifiers5 = MapModifiers.Public;
682 Serialize (f, typeof (Field));
683 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
684 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
685 " modifiers='protected' modifiers2='public' />",
686 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
688 f.Flags1 = (FlagEnum) 1;
689 f.Flags1 = FlagEnum.e2;
690 f.Flags2 = FlagEnum.e2;
691 f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
692 f.Modifiers = MapModifiers.Public;
693 f.Modifiers2 = MapModifiers.Protected;
694 f.Modifiers3 = MapModifiers.Protected;
695 f.Modifiers4 = MapModifiers.Public;
696 f.Modifiers5 = MapModifiers.Protected;
697 Serialize (f, typeof (Field));
698 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
699 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
700 " flag4='' modifiers='public' modifiers2='protected'" +
701 " modifiers3='protected' modifiers4='public'" +
702 " modifiers5='protected' />",
703 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
705 f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
706 f.Flags2 = FlagEnum.e2;
707 f.Flags3 = FlagEnum.e4;
708 f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
709 f.Modifiers3 = MapModifiers.Public;
710 f.Modifiers4 = MapModifiers.Protected;
711 f.Modifiers5 = MapModifiers.Public;
712 f.Names = new string[] { "a", "b" };
713 Serialize (f, typeof (Field));
714 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
715 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
716 " flag3='four' flag4='one two four' modifiers='public'" +
717 " modifiers2='protected' names='a b' />",
718 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
720 f.Flags2 = (FlagEnum) 444;
721 f.Flags3 = (FlagEnum) 555;
722 f.Modifiers = (MapModifiers) 666;
723 f.Modifiers2 = (MapModifiers) 777;
724 f.Modifiers3 = (MapModifiers) 0;
725 f.Modifiers4 = (MapModifiers) 888;
726 f.Modifiers5 = (MapModifiers) 999;
729 Serialize (f, typeof (Field));
731 } catch (InvalidOperationException ex) {
732 // There was an error generating the XML document
733 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
734 Assert.IsNotNull (ex.Message, "#E3");
735 Assert.IsNotNull (ex.InnerException, "#E4");
737 // Instance validation error: '444' is not a valid value for
738 // MonoTests.System.Xml.TestClasses.FlagEnum
739 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
740 Assert.IsNotNull (ex.InnerException.Message, "#E6");
741 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
742 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
743 Assert.IsNull (ex.InnerException.InnerException, "#E9");
746 Serialize (f, typeof (Field));
747 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
748 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
749 " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
750 " modifiers4='888' modifiers5='999' names='a b' />",
751 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
756 [Category ("NotDotNet")] // MS bug
757 public void TestSerializeField_Encoded ()
759 Field_Encoded f = new Field_Encoded ();
760 SerializeEncoded (f, typeof (Field_Encoded));
761 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
762 "<?xml version='1.0' encoding='utf-16'?>" +
764 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
765 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
766 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
768 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
769 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
770 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
772 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
773 sw.GetStringBuilder ().ToString (), "#A");
775 f.Flags1 = FlagEnum_Encoded.e1;
776 f.Flags2 = FlagEnum_Encoded.e1;
777 f.Flags3 = FlagEnum_Encoded.e2;
778 f.Modifiers = MapModifiers.Protected;
779 f.Modifiers2 = MapModifiers.Public;
780 f.Modifiers3 = MapModifiers.Public;
781 f.Modifiers4 = MapModifiers.Protected;
782 f.Modifiers5 = MapModifiers.Public;
783 SerializeEncoded (f, typeof (Field_Encoded));
784 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
785 "<?xml version='1.0' encoding='utf-16'?>" +
787 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
788 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
789 " xmlns:q1='some:urn' />",
791 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
792 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
793 " xmlns:q1='some:urn' />",
795 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
796 sw.GetStringBuilder ().ToString (), "#B");
798 f.Flags1 = FlagEnum_Encoded.e2;
799 f.Flags2 = FlagEnum_Encoded.e2;
800 f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
801 f.Modifiers = MapModifiers.Public;
802 f.Modifiers2 = MapModifiers.Protected;
803 f.Modifiers3 = MapModifiers.Protected;
804 f.Modifiers4 = MapModifiers.Public;
805 f.Modifiers5 = MapModifiers.Protected;
806 SerializeEncoded (f, typeof (Field_Encoded));
807 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
808 "<?xml version='1.0' encoding='utf-16'?>" +
810 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
811 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
812 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
813 " xmlns:q1='some:urn' />",
815 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
816 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
817 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
818 " xmlns:q1='some:urn' />",
820 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
821 sw.GetStringBuilder ().ToString (), "#C");
823 f.Flags1 = (FlagEnum_Encoded) 1;
824 f.Flags2 = (FlagEnum_Encoded) 444;
825 f.Flags3 = (FlagEnum_Encoded) 555;
826 f.Modifiers = (MapModifiers) 666;
827 f.Modifiers2 = (MapModifiers) 777;
828 f.Modifiers3 = (MapModifiers) 0;
829 f.Modifiers4 = (MapModifiers) 888;
830 f.Modifiers5 = (MapModifiers) 999;
834 SerializeEncoded (f, typeof (Field_Encoded));
837 } catch (InvalidOperationException ex) {
838 // There was an error generating the XML document
839 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
840 Assert.IsNotNull (ex.Message, "#D3");
841 Assert.IsNotNull (ex.InnerException, "#D4");
843 // Instance validation error: '444' is not a valid value for
844 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
845 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
846 Assert.IsNotNull (ex.InnerException.Message, "#D6");
847 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
848 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
849 Assert.IsNull (ex.InnerException.InnerException, "#D9");
852 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
853 "<?xml version='1.0' encoding='utf-16'?>" +
854 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
855 " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
856 " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
857 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
858 sw.GetStringBuilder ().ToString (), "#D");
864 [Ignore ("JVM returns fields in different order")]
866 public void TestSerializeGroup ()
868 Group myGroup = new Group ();
869 myGroup.GroupName = ".NET";
871 Byte[] hexByte = new Byte[] { 0x64, 0x32 };
872 myGroup.GroupNumber = hexByte;
874 DateTime myDate = new DateTime (2002, 5, 2);
875 myGroup.Today = myDate;
876 myGroup.PostitiveInt = "10000";
877 myGroup.IgnoreThis = true;
878 Car thisCar = (Car) myGroup.myCar ("1234566");
879 myGroup.MyVehicle = thisCar;
882 xtw.WriteStartDocument (true);
883 xtw.WriteStartElement ("Wrapper");
884 SerializeEncoded (xtw, myGroup, typeof (Group));
885 xtw.WriteEndElement ();
888 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
890 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
891 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
892 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
893 "<MyVehicle href='#id2' />" +
895 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
896 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
897 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
900 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
903 myGroup.GroupName = null;
904 myGroup.Grouptype = GroupType.B;
905 myGroup.MyVehicle.licenseNumber = null;
906 myGroup.MyVehicle.weight = "450";
909 xtw.WriteStartDocument (true);
910 xtw.WriteStartElement ("Wrapper");
911 SerializeEncoded (xtw, myGroup, typeof (Group));
912 xtw.WriteEndElement ();
915 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
917 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
918 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
919 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
920 "<MyVehicle href='#id2' />" +
922 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
923 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
924 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
927 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
932 public void TestSerializeZeroFlagEnum_InvalidValue ()
936 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
938 } catch (InvalidOperationException ex) {
939 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
940 Assert.IsNotNull (ex.InnerException, "#3");
941 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
942 Assert.IsNotNull (ex.InnerException.Message, "#5");
943 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
944 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
947 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
948 Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
953 public void TestSerializeQualifiedName ()
955 Serialize (new XmlQualifiedName ("me", "home.urn"));
956 Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
960 public void TestSerializeBytes ()
962 Serialize ((byte) 0xAB);
963 Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
965 Serialize ((byte) 15);
966 Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
970 public void TestSerializeByteArrays ()
972 Serialize (new byte[] { });
973 Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
975 Serialize (new byte[] { 0xAB, 0xCD });
976 Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
980 public void TestSerializeDateTime ()
982 DateTime d = new DateTime ();
985 TimeZone tz = TimeZone.CurrentTimeZone;
986 TimeSpan off = tz.GetUtcOffset (d);
987 string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
989 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
991 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
1002 public void TestSerialize()
1005 Assert.AreEqual (WriterText, "");
1009 // test basic class serialization /////////////////////////////////////
1011 public void TestSerializeSimpleClass ()
1013 SimpleClass simple = new SimpleClass ();
1015 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1017 simple.something = "hello";
1020 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
1024 public void TestSerializeStringCollection ()
1026 StringCollection strings = new StringCollection ();
1027 Serialize (strings);
1028 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1030 strings.Add ("hello");
1031 strings.Add ("goodbye");
1032 Serialize (strings);
1033 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
1037 public void TestSerializeOptionalValueTypeContainer ()
1039 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1041 OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
1043 Serialize (optionalValue);
1044 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1045 "<?xml version='1.0' encoding='utf-16'?>" +
1047 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
1049 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
1051 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
1052 sw.ToString (), "#1");
1054 attr = new XmlAttributes ();
1056 // remove the DefaultValue attribute on the Flags member
1057 overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
1058 // remove the DefaultValue attribute on the Attributes member
1059 overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
1061 Serialize (optionalValue, overrides);
1062 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1063 "<?xml version='1.0' encoding='utf-16'?>" +
1065 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1067 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1069 "<Attributes xmlns='{3}'>one four</Attributes>" +
1070 "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1071 AnotherNamespace, ANamespace), sw.ToString (), "#2");
1073 optionalValue.FlagsSpecified = true;
1074 Serialize (optionalValue, overrides);
1075 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1076 "<?xml version='1.0' encoding='utf-16'?>" +
1078 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1080 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1082 "<Attributes xmlns='{3}'>one four</Attributes>" +
1083 "<Flags xmlns='{3}'>one</Flags>" +
1085 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
1086 ANamespace), sw.ToString (), "#3");
1090 public void TestSerializePlainContainer ()
1092 StringCollectionContainer container = new StringCollectionContainer ();
1093 Serialize (container);
1094 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
1096 container.Messages.Add ("hello");
1097 container.Messages.Add ("goodbye");
1098 Serialize (container);
1099 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
1103 public void TestSerializeArrayContainer ()
1105 ArrayContainer container = new ArrayContainer ();
1106 Serialize (container);
1107 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1109 container.items = new object[] { 10, 20 };
1110 Serialize (container);
1111 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
1113 container.items = new object[] { 10, "hello" };
1114 Serialize (container);
1115 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
1119 public void TestSerializeClassArrayContainer ()
1121 ClassArrayContainer container = new ClassArrayContainer ();
1122 Serialize (container);
1123 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1125 SimpleClass simple1 = new SimpleClass ();
1126 simple1.something = "hello";
1127 SimpleClass simple2 = new SimpleClass ();
1128 simple2.something = "hello";
1129 container.items = new SimpleClass[2];
1130 container.items[0] = simple1;
1131 container.items[1] = simple2;
1132 Serialize (container);
1133 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
1136 // test basic attributes ///////////////////////////////////////////////
1138 public void TestSerializeSimpleClassWithXmlAttributes ()
1140 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1142 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1144 simple.something = "hello";
1146 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
1149 // test overrides ///////////////////////////////////////////////////////
1151 public void TestSerializeSimpleClassWithOverrides ()
1153 // Also tests XmlIgnore
1154 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1156 XmlAttributes attr = new XmlAttributes ();
1157 attr.XmlIgnore = true;
1158 overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
1160 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1161 simple.something = "hello";
1162 Serialize (simple, overrides);
1163 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1167 public void TestSerializeSchema ()
1169 XmlSchema schema = new XmlSchema ();
1170 schema.Items.Add (new XmlSchemaAttribute ());
1171 schema.Items.Add (new XmlSchemaAttributeGroup ());
1172 schema.Items.Add (new XmlSchemaComplexType ());
1173 schema.Items.Add (new XmlSchemaNotation ());
1174 schema.Items.Add (new XmlSchemaSimpleType ());
1175 schema.Items.Add (new XmlSchemaGroup ());
1176 schema.Items.Add (new XmlSchemaElement ());
1178 StringWriter sw = new StringWriter ();
1179 XmlTextWriter xtw = new XmlTextWriter (sw);
1180 xtw.QuoteChar = '\'';
1181 xtw.Formatting = Formatting.Indented;
1182 XmlSerializer xs = new XmlSerializer (schema.GetType ());
1183 xs.Serialize (xtw, schema);
1185 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1186 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1187 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1188 " <xsd:attribute />{0}" +
1189 " <xsd:attributeGroup />{0}" +
1190 " <xsd:complexType />{0}" +
1191 " <xsd:notation />{0}" +
1192 " <xsd:simpleType />{0}" +
1193 " <xsd:group />{0}" +
1194 " <xsd:element />{0}" +
1195 "</xsd:schema>", Environment.NewLine), sw.ToString ());
1198 // test xmlText //////////////////////////////////////////////////////////
1200 public void TestSerializeXmlTextAttribute ()
1202 SimpleClass simple = new SimpleClass ();
1203 simple.something = "hello";
1205 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1206 XmlAttributes attr = new XmlAttributes ();
1207 overrides.Add (typeof (SimpleClass), "something", attr);
1209 attr.XmlText = new XmlTextAttribute ();
1210 Serialize (simple, overrides);
1211 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
1213 attr.XmlText = new XmlTextAttribute (typeof (string));
1214 Serialize (simple, overrides);
1215 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
1218 attr.XmlText = new XmlTextAttribute (typeof (byte[]));
1219 Serialize (simple, overrides);
1220 Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1221 } catch (InvalidOperationException ex) {
1222 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1223 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1224 Assert.IsNotNull (ex.Message, "#A3");
1225 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
1226 Assert.IsNotNull (ex.InnerException, "#A5");
1228 // there was an error reflecting field 'something'.
1229 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
1230 Assert.IsNotNull (ex.InnerException.Message, "#A7");
1231 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
1232 Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
1234 // the type for XmlText may not be specified for primitive types.
1235 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
1236 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
1237 Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
1241 attr.XmlText = new XmlTextAttribute ();
1242 attr.XmlText.DataType = "sometype";
1243 Serialize (simple, overrides);
1244 Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1245 } catch (InvalidOperationException ex) {
1246 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1247 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1248 Assert.IsNotNull (ex.Message, "#B3");
1249 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
1250 Assert.IsNotNull (ex.InnerException, "#B5");
1252 // There was an error reflecting field 'something'.
1253 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
1254 Assert.IsNotNull (ex.InnerException.Message, "#B7");
1255 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
1256 Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
1260 // There was an error reflecting type 'System.String'.
1261 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1262 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1263 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1264 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1266 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
1267 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1268 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1269 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1270 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1271 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1276 // test xmlRoot //////////////////////////////////////////////////////////
1278 public void TestSerializeXmlRootAttribute ()
1280 // constructor override & element name
1281 XmlRootAttribute root = new XmlRootAttribute ();
1282 root.ElementName = "renamed";
1284 SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
1285 Serialize (simpleWithAttributes, root);
1286 Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1288 SimpleClass simple = null;
1289 root.IsNullable = false;
1291 Serialize (simple, root);
1292 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1293 } catch (NullReferenceException) {
1296 root.IsNullable = true;
1298 Serialize (simple, root);
1299 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1300 } catch (NullReferenceException) {
1303 simple = new SimpleClass ();
1304 root.ElementName = null;
1305 root.Namespace = "some.urn";
1306 Serialize (simple, root);
1307 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
1311 public void TestSerializeXmlRootAttributeOnMember ()
1314 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1315 XmlAttributes childAttr = new XmlAttributes ();
1316 childAttr.XmlRoot = new XmlRootAttribute ("simple");
1317 overrides.Add (typeof (SimpleClass), childAttr);
1319 XmlAttributes attr = new XmlAttributes ();
1320 attr.XmlRoot = new XmlRootAttribute ("simple");
1321 overrides.Add (typeof (ClassArrayContainer), attr);
1323 ClassArrayContainer container = new ClassArrayContainer ();
1324 container.items = new SimpleClass[1];
1325 container.items[0] = new SimpleClass ();
1326 Serialize (container, overrides);
1327 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
1329 // FIXME test data type
1332 // test XmlAttribute /////////////////////////////////////////////////////
1334 public void TestSerializeXmlAttributeAttribute ()
1337 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1338 XmlAttributes attr = new XmlAttributes ();
1339 attr.XmlAttribute = new XmlAttributeAttribute ();
1340 overrides.Add (typeof (SimpleClass), "something", attr);
1342 SimpleClass simple = new SimpleClass (); ;
1343 Serialize (simple, overrides);
1344 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1347 simple.something = "hello";
1348 Serialize (simple, overrides);
1349 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
1352 attr.XmlAttribute.AttributeName = "somethingelse";
1353 Serialize (simple, overrides);
1354 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
1357 // FIXME this should work, shouldnt it?
1358 // attr.XmlAttribute.Type = typeof(string);
1359 // Serialize(simple, overrides);
1360 // Assert(WriterText.EndsWith(" something='hello' />"));
1363 attr.XmlAttribute.Namespace = "some:urn";
1364 Serialize (simple, overrides);
1365 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
1368 // FIXME XmlSchemaForm Form
1370 // FIXME write XmlQualifiedName as attribute
1373 // test XmlElement ///////////////////////////////////////////////////////
1375 public void TestSerializeXmlElementAttribute ()
1377 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1378 XmlAttributes attr = new XmlAttributes ();
1379 XmlElementAttribute element = new XmlElementAttribute ();
1380 attr.XmlElements.Add (element);
1381 overrides.Add (typeof (SimpleClass), "something", attr);
1384 SimpleClass simple = new SimpleClass (); ;
1385 Serialize (simple, overrides);
1386 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1389 simple.something = "hello";
1390 Serialize (simple, overrides);
1391 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
1394 element.ElementName = "saying";
1395 Serialize (simple, overrides);
1396 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
1399 element.IsNullable = false;
1400 simple.something = null;
1401 Serialize (simple, overrides);
1402 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
1404 element.IsNullable = true;
1405 simple.something = null;
1406 Serialize (simple, overrides);
1407 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
1410 element.ElementName = null;
1411 element.IsNullable = false;
1412 element.Namespace = "some:urn";
1413 simple.something = "hello";
1414 Serialize (simple, overrides);
1415 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
1422 // test XmlElementAttribute with arrays and collections //////////////////
1424 public void TestSerializeCollectionWithXmlElementAttribute ()
1427 // if no type is specified or the specified type
1428 // matches the contents of the collection,
1429 // serialize each element in an element named after the member.
1430 // if the type does not match, or matches the collection itself,
1431 // create a base wrapping element for the member, and then
1432 // wrap each collection item in its own wrapping element based on type.
1434 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1435 XmlAttributes attr = new XmlAttributes ();
1436 XmlElementAttribute element = new XmlElementAttribute ();
1437 attr.XmlElements.Add (element);
1438 overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
1440 // empty collection & no type info in XmlElementAttribute
1441 StringCollectionContainer container = new StringCollectionContainer ();
1442 Serialize (container, overrides);
1443 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1445 // non-empty collection & no type info in XmlElementAttribute
1446 container.Messages.Add ("hello");
1447 Serialize (container, overrides);
1448 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
1450 // non-empty collection & only type info in XmlElementAttribute
1451 element.Type = typeof (StringCollection);
1452 Serialize (container, overrides);
1453 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
1455 // non-empty collection & only type info in XmlElementAttribute
1456 element.Type = typeof (string);
1457 Serialize (container, overrides);
1458 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4");
1461 container.Messages.Add ("goodbye");
1462 element.Type = null;
1463 Serialize (container, overrides);
1464 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText, "#5");
1467 // test DefaultValue /////////////////////////////////////////////////////
1469 public void TestSerializeDefaultValueAttribute ()
1471 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1473 XmlAttributes attr = new XmlAttributes ();
1474 string defaultValueInstance = "nothing";
1475 attr.XmlDefaultValue = defaultValueInstance;
1476 overrides.Add (typeof (SimpleClass), "something", attr);
1479 SimpleClass simple = new SimpleClass ();
1480 Serialize (simple, overrides);
1481 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1483 // same value as default
1484 simple.something = defaultValueInstance;
1485 Serialize (simple, overrides);
1486 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1489 simple.something = "hello";
1490 Serialize (simple, overrides);
1491 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3");
1493 overrides = new XmlAttributeOverrides ();
1494 attr = new XmlAttributes ();
1495 attr.XmlAttribute = new XmlAttributeAttribute ();
1496 attr.XmlDefaultValue = defaultValueInstance;
1497 overrides.Add (typeof (SimpleClass), "something", attr);
1500 simple = new SimpleClass ();
1501 Serialize (simple, overrides);
1502 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1504 // same value as default
1505 simple.something = defaultValueInstance;
1506 Serialize (simple, overrides);
1507 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
1510 simple.something = "hello";
1511 Serialize (simple, overrides);
1512 Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
1514 overrides = new XmlAttributeOverrides ();
1515 attr = new XmlAttributes ();
1516 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1517 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1520 TestDefault testDefault = new TestDefault ();
1521 Serialize (testDefault);
1522 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
1524 // use the default with overrides
1525 Serialize (testDefault, overrides);
1526 Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
1528 overrides = new XmlAttributeOverrides ();
1529 attr = new XmlAttributes ();
1530 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1531 attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
1532 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1534 // use the default with overrides
1535 Serialize (testDefault, overrides);
1536 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
1538 // use the default with overrides and default namspace
1539 Serialize (testDefault, overrides, AnotherNamespace);
1540 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
1542 // non-default values
1543 testDefault.strDefault = "Some Text";
1544 testDefault.boolT = false;
1545 testDefault.boolF = true;
1546 testDefault.decimalval = 20m;
1547 testDefault.flag = FlagEnum.e2;
1548 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1549 Serialize (testDefault);
1550 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1551 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1552 " <strDefault>Some Text</strDefault>" +
1553 " <boolT>false</boolT>" +
1554 " <boolF>true</boolF>" +
1555 " <decimalval>20</decimalval>" +
1556 " <flag>two</flag>" +
1557 " <flagencoded>e1 e2</flagencoded>" +
1558 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1561 Serialize (testDefault, overrides);
1562 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1563 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1564 " <strDefault>Some Text</strDefault>" +
1565 " <boolT>false</boolT>" +
1566 " <boolF>true</boolF>" +
1567 " <decimalval>20</decimalval>" +
1568 " <flag>two</flag>" +
1569 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1572 Serialize (testDefault, overrides, AnotherNamespace);
1573 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1574 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1575 " <strDefault>Some Text</strDefault>" +
1576 " <boolT>false</boolT>" +
1577 " <boolF>true</boolF>" +
1578 " <decimalval>20</decimalval>" +
1579 " <flag>two</flag>" +
1580 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1583 attr = new XmlAttributes ();
1584 XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
1585 xmlType.Namespace = "yetanother:urn";
1586 attr.XmlType = xmlType;
1587 overrides.Add (typeof (FlagEnum_Encoded), attr);
1589 Serialize (testDefault, overrides, AnotherNamespace);
1590 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1591 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1592 " <strDefault>Some Text</strDefault>" +
1593 " <boolT>false</boolT>" +
1594 " <boolF>true</boolF>" +
1595 " <decimalval>20</decimalval>" +
1596 " <flag>two</flag>" +
1597 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1600 attr = new XmlAttributes ();
1601 attr.XmlType = new XmlTypeAttribute ("testDefault");
1602 overrides.Add (typeof (TestDefault), attr);
1604 Serialize (testDefault, overrides, AnotherNamespace);
1605 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1606 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1607 " <strDefault>Some Text</strDefault>" +
1608 " <boolT>false</boolT>" +
1609 " <boolF>true</boolF>" +
1610 " <decimalval>20</decimalval>" +
1611 " <flag>two</flag>" +
1612 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1613 AnotherNamespace)), WriterText, "#C9");
1617 [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
1618 public void TestSerializeDefaultValueAttribute_Encoded ()
1620 SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
1621 SoapAttributes attr = new SoapAttributes ();
1622 attr.SoapAttribute = new SoapAttributeAttribute ();
1623 string defaultValueInstance = "nothing";
1624 attr.SoapDefaultValue = defaultValueInstance;
1625 overrides.Add (typeof (SimpleClass), "something", attr);
1628 SimpleClass simple = new SimpleClass ();
1629 SerializeEncoded (simple, overrides);
1630 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1632 // same value as default
1633 simple.something = defaultValueInstance;
1634 SerializeEncoded (simple, overrides);
1635 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1638 simple.something = "hello";
1639 SerializeEncoded (simple, overrides);
1640 Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
1642 attr.SoapAttribute = null;
1643 attr.SoapElement = new SoapElementAttribute ();
1646 simple = new SimpleClass ();
1647 SerializeEncoded (simple, overrides);
1648 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1650 // same value as default
1651 simple.something = defaultValueInstance;
1652 SerializeEncoded (simple, overrides);
1653 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
1656 simple.something = "hello";
1657 SerializeEncoded (simple, overrides);
1658 Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
1660 overrides = new SoapAttributeOverrides ();
1661 attr = new SoapAttributes ();
1662 attr.SoapElement = new SoapElementAttribute ("flagenc");
1663 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1665 // use the default (from MS KB325691)
1666 TestDefault testDefault = new TestDefault ();
1667 SerializeEncoded (testDefault);
1668 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1669 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1670 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1671 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1672 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1673 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1674 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1675 " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1676 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1679 SerializeEncoded (testDefault, overrides);
1680 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1681 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1682 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1683 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1684 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1685 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1686 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1687 " <flagenc xsi:type='flagenum'>one four</flagenc>" +
1688 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1691 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1692 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1693 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1694 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1695 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1696 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1697 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1698 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1699 " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1700 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1701 AnotherNamespace)), WriterText, "#C3");
1703 // non-default values
1704 testDefault.strDefault = "Some Text";
1705 testDefault.boolT = false;
1706 testDefault.boolF = true;
1707 testDefault.decimalval = 20m;
1708 testDefault.flag = FlagEnum.e2;
1709 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1710 SerializeEncoded (testDefault);
1711 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1712 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1713 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1714 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1715 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1716 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1717 " <flag xsi:type='FlagEnum'>e2</flag>" +
1718 " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1719 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1722 SerializeEncoded (testDefault, overrides);
1723 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1724 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1725 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1726 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1727 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1728 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1729 " <flag xsi:type='FlagEnum'>e2</flag>" +
1730 " <flagenc xsi:type='flagenum'>one two</flagenc>" +
1731 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1734 attr = new SoapAttributes ();
1735 attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
1736 overrides.Add (typeof (FlagEnum_Encoded), attr);
1738 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1739 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1740 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1741 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1742 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1743 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1744 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1745 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1746 " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1747 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1748 AnotherNamespace)), WriterText, "#C6");
1750 attr = new SoapAttributes ();
1751 attr.SoapType = new SoapTypeAttribute ("testDefault");
1752 overrides.Add (typeof (TestDefault), attr);
1754 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1755 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1756 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1757 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1758 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1759 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1760 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1761 " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1762 " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1763 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1764 AnotherNamespace)), WriterText, "#C7");
1767 // test XmlEnum //////////////////////////////////////////////////////////
1769 public void TestSerializeXmlEnumAttribute ()
1771 Serialize (XmlSchemaForm.Qualified);
1772 Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
1774 Serialize (XmlSchemaForm.Unqualified);
1775 Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
1779 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1781 // technically XmlSchemaForm.None has an XmlIgnore attribute,
1782 // but it is not being serialized as a member.
1786 Serialize (XmlSchemaForm.None);
1788 } catch (InvalidOperationException ex) {
1789 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1790 Assert.IsNotNull (ex.InnerException, "#3");
1791 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
1792 Assert.IsNotNull (ex.InnerException.Message, "#5");
1793 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
1794 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
1797 Serialize (XmlSchemaForm.None);
1798 Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
1803 public void TestSerializeXmlNodeArray ()
1805 XmlDocument doc = new XmlDocument ();
1806 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1807 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1811 public void TestSerializeXmlNodeArray2 ()
1813 XmlDocument doc = new XmlDocument ();
1814 Serialize (new XmlNode[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1815 Assert.AreEqual (Infoset (String.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
1819 [ExpectedException (typeof (InvalidOperationException))]
1820 [Category ("NotWorking")]
1821 public void TestSerializeXmlNodeArrayIncludesAttribute ()
1823 XmlDocument doc = new XmlDocument ();
1824 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1828 public void TestSerializeXmlElementArray ()
1830 XmlDocument doc = new XmlDocument ();
1831 Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1832 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1837 [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
1838 public void TestSerializeGenericListOfNode ()
1840 XmlDocument doc = new XmlDocument ();
1841 Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1842 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1846 [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
1847 public void TestSerializeGenericListOfElement ()
1849 XmlDocument doc = new XmlDocument ();
1850 Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1851 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1855 public void TestSerializeXmlDocument ()
1857 XmlDocument doc = new XmlDocument ();
1858 doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
1859 Serialize (doc, typeof (XmlDocument));
1860 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
1861 sw.GetStringBuilder ().ToString ());
1865 public void TestSerializeXmlElement ()
1867 XmlDocument doc = new XmlDocument ();
1868 Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
1869 Assert.AreEqual (Infoset ("<elem/>"), WriterText);
1873 public void TestSerializeXmlElementSubclass ()
1875 XmlDocument doc = new XmlDocument ();
1876 Serialize (new MyElem (doc), typeof (XmlElement));
1877 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
1879 Serialize (new MyElem (doc), typeof (MyElem));
1880 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
1884 public void TestSerializeXmlCDataSection ()
1886 XmlDocument doc = new XmlDocument ();
1887 CDataContainer c = new CDataContainer ();
1888 c.cdata = doc.CreateCDataSection ("data section contents");
1890 Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
1894 public void TestSerializeXmlNode ()
1896 XmlDocument doc = new XmlDocument ();
1897 NodeContainer c = new NodeContainer ();
1898 c.node = doc.CreateTextNode ("text");
1900 Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
1904 public void TestSerializeChoice ()
1906 Choices ch = new Choices ();
1907 ch.MyChoice = "choice text";
1908 ch.ItemType = ItemChoiceType.ChoiceZero;
1910 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText, "#1");
1911 ch.ItemType = ItemChoiceType.StrangeOne;
1913 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText, "#2");
1914 ch.ItemType = ItemChoiceType.ChoiceTwo;
1916 Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText, "#3");
1920 public void TestSerializeNamesWithSpaces ()
1922 TestSpace ts = new TestSpace ();
1926 Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
1930 public void TestSerializeReadOnlyProps ()
1932 ReadOnlyProperties ts = new ReadOnlyProperties ();
1934 Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1938 public void TestSerializeIList ()
1940 clsPerson k = new clsPerson ();
1941 k.EmailAccounts = new ArrayList ();
1942 k.EmailAccounts.Add ("a");
1943 k.EmailAccounts.Add ("b");
1945 Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
1949 public void TestSerializeArrayEnc ()
1951 SoapReflectionImporter imp = new SoapReflectionImporter ();
1952 XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1953 XmlSerializer ser = new XmlSerializer (map);
1954 StringWriter sw = new StringWriter ();
1955 XmlTextWriter tw = new XmlTextWriter (sw);
1956 tw.WriteStartElement ("aa");
1957 ser.Serialize (tw, new ArrayClass ());
1958 tw.WriteEndElement ();
1961 [Test] // bug #76049
1962 public void TestIncludeType ()
1964 XmlReflectionImporter imp = new XmlReflectionImporter ();
1965 XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1966 imp.IncludeType (typeof (TestSpace));
1967 XmlSerializer ser = new XmlSerializer (map);
1968 ser.Serialize (new StringWriter (), new TestSpace ());
1972 public void TestSerializeChoiceArray ()
1974 CompositeValueType v = new CompositeValueType ();
1977 Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
1981 public void TestArrayAttributeWithDataType ()
1983 Serialize (new ArrayAttributeWithType ());
1984 string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1985 res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1986 Assert.AreEqual (Infoset (res), WriterText);
1990 public void TestSubclassElementType ()
1992 SubclassTestContainer c = new SubclassTestContainer ();
1993 c.data = new SubclassTestSub ();
1996 string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1997 res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1998 Assert.AreEqual (Infoset (res), WriterText);
2002 [ExpectedException (typeof (InvalidOperationException))]
2003 public void TestArrayAttributeWithWrongDataType ()
2005 Serialize (new ArrayAttributeWithWrongType ());
2009 [Category ("NotWorking")]
2010 public void TestSerializePrimitiveTypesContainer ()
2012 Serialize (new PrimitiveTypesContainer ());
2013 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2014 "<?xml version='1.0' encoding='utf-16'?>" +
2016 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
2018 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
2020 "<Number>2004</Number>" +
2021 "<Name>some name</Name>" +
2022 "<Index>56</Index>" +
2023 "<Password>8w8=</Password>" +
2024 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2025 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2026 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2028 SerializeEncoded (new PrimitiveTypesContainer ());
2029 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2030 "<?xml version='1.0' encoding='utf-16'?>" +
2032 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2034 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2036 "<Number xsi:type='xsd:int'>2004</Number>" +
2037 "<Name xsi:type='xsd:string'>some name</Name>" +
2038 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2039 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2040 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2041 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2042 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2043 sw.ToString (), "#2");
2047 public void TestSchemaForm ()
2049 TestSchemaForm1 t1 = new TestSchemaForm1 ();
2050 t1.p1 = new PrintTypeResponse ();
2052 t1.p2 = new PrintTypeResponse ();
2055 TestSchemaForm2 t2 = new TestSchemaForm2 ();
2056 t2.p1 = new PrintTypeResponse ();
2058 t2.p2 = new PrintTypeResponse ();
2063 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2064 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2067 res += " <data>data1</data>";
2068 res += " </result>";
2069 res += " <intern xmlns=\"urn:responseTypes\">";
2070 res += " <result xmlns=\"\">";
2071 res += " <data>data2</data>";
2072 res += " </result>";
2073 res += " </intern>";
2075 res += " <p2 xmlns=\"urn:oo\">";
2076 res += " <result xmlns=\"\">";
2077 res += " <data>data1</data>";
2078 res += " </result>";
2079 res += " <intern xmlns=\"urn:responseTypes\">";
2080 res += " <result xmlns=\"\">";
2081 res += " <data>data2</data>";
2082 res += " </result>";
2083 res += " </intern>";
2085 res += "</TestSchemaForm1>";
2086 Assert.AreEqual (Infoset (res), WriterText);
2090 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2091 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2092 res += " <p1 xmlns=\"urn:testForm\">";
2093 res += " <result xmlns=\"\">";
2094 res += " <data>data1</data>";
2095 res += " </result>";
2096 res += " <intern xmlns=\"urn:responseTypes\">";
2097 res += " <result xmlns=\"\">";
2098 res += " <data>data2</data>";
2099 res += " </result>";
2100 res += " </intern>";
2102 res += " <p2 xmlns=\"urn:oo\">";
2103 res += " <result xmlns=\"\">";
2104 res += " <data>data1</data>";
2105 res += " </result>";
2106 res += " <intern xmlns=\"urn:responseTypes\">";
2107 res += " <result xmlns=\"\">";
2108 res += " <data>data2</data>";
2109 res += " </result>";
2110 res += " </intern>";
2112 res += "</TestSchemaForm2>";
2113 Assert.AreEqual (Infoset (res), WriterText);
2115 XmlReflectionImporter imp = new XmlReflectionImporter ();
2116 XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2117 Serialize (t1, map);
2119 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2120 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2122 res += " <result xmlns=\"\">";
2123 res += " <data>data1</data>";
2124 res += " </result>";
2125 res += " <intern xmlns=\"urn:responseTypes\">";
2126 res += " <result xmlns=\"\">";
2127 res += " <data>data2</data>";
2128 res += " </result>";
2129 res += " </intern>";
2131 res += " <p2 xmlns=\"urn:oo\">";
2132 res += " <result xmlns=\"\">";
2133 res += " <data>data1</data>";
2134 res += " </result>";
2135 res += " <intern xmlns=\"urn:responseTypes\">";
2136 res += " <result xmlns=\"\">";
2137 res += " <data>data2</data>";
2138 res += " </result>";
2139 res += " </intern>";
2141 res += "</TestSchemaForm1>";
2142 Assert.AreEqual (Infoset (res), WriterText);
2144 imp = new XmlReflectionImporter ();
2145 map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2146 Serialize (t2, map);
2148 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2149 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2150 res += " <p1 xmlns=\"urn:testForm\">";
2151 res += " <result xmlns=\"\">";
2152 res += " <data>data1</data>";
2153 res += " </result>";
2154 res += " <intern xmlns=\"urn:responseTypes\">";
2155 res += " <result xmlns=\"\">";
2156 res += " <data>data2</data>";
2157 res += " </result>";
2158 res += " </intern>";
2160 res += " <p2 xmlns=\"urn:oo\">";
2161 res += " <result xmlns=\"\">";
2162 res += " <data>data1</data>";
2163 res += " </result>";
2164 res += " <intern xmlns=\"urn:responseTypes\">";
2165 res += " <result xmlns=\"\">";
2166 res += " <data>data2</data>";
2167 res += " </result>";
2168 res += " </intern>";
2170 res += "</TestSchemaForm2>";
2171 Assert.AreEqual (Infoset (res), WriterText);
2174 [Test] // bug #78536
2175 public void CDataTextNodes ()
2177 XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2178 ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2179 string xml = @"<CDataTextNodesType>
2181 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2182 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2183 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2185 </CDataTextNodesType>";
2186 ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2192 public void GenerateSerializerGenerics ()
2194 XmlReflectionImporter imp = new XmlReflectionImporter ();
2195 Type type = typeof (List<int>);
2196 XmlSerializer.GenerateSerializer (
2198 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2203 public void Nullable ()
2205 XmlSerializer ser = new XmlSerializer (typeof (int?));
2206 int? nullableType = 5;
2207 sw = new StringWriter ();
2208 xtw = new XmlTextWriter (sw);
2209 ser.Serialize (xtw, nullableType);
2211 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2212 Assert.AreEqual (Infoset (expected), WriterText);
2213 int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2214 Assert.AreEqual (5, i);
2218 public void NullableEnums ()
2220 WithNulls w = new WithNulls ();
2221 XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2222 StringWriter tw = new StringWriter ();
2223 ser.Serialize (tw, w);
2225 string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2226 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2227 "<nint xsi:nil='true' />" +
2228 "<nenum xsi:nil='true' />" +
2229 "<ndate xsi:nil='true' />" +
2232 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2234 StringReader sr = new StringReader (tw.ToString ());
2235 w = (WithNulls) ser.Deserialize (sr);
2237 Assert.IsFalse (w.nint.HasValue);
2238 Assert.IsFalse (w.nenum.HasValue);
2239 Assert.IsFalse (w.ndate.HasValue);
2241 DateTime t = new DateTime (2008,4,1);
2244 w.nenum = TestEnumWithNulls.bb;
2246 tw = new StringWriter ();
2247 ser.Serialize (tw, w);
2249 expected = "<?xml version='1.0' encoding='utf-16'?>" +
2250 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2252 "<nenum>bb</nenum>" +
2253 "<ndate>2008-04-01T00:00:00</ndate>" +
2256 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2258 sr = new StringReader (tw.ToString ());
2259 w = (WithNulls) ser.Deserialize (sr);
2261 Assert.IsTrue (w.nint.HasValue);
2262 Assert.IsTrue (w.nenum.HasValue);
2263 Assert.IsTrue (w.ndate.HasValue);
2264 Assert.AreEqual (4, w.nint.Value);
2265 Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2266 Assert.AreEqual (t, w.ndate.Value);
2271 public void SerializeBase64Binary()
2273 XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2274 sw = new StringWriter ();
2275 XmlTextWriter xtw = new XmlTextWriter (sw);
2276 ser.Serialize (xtw, new Base64Binary ());
2278 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
2279 Assert.AreEqual (Infoset (expected), WriterText);
2280 Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2281 Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2284 [Test] // bug #79989, #79990
2285 public void SerializeHexBinary ()
2287 XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2288 sw = new StringWriter ();
2289 XmlTextWriter xtw = new XmlTextWriter (sw);
2290 ser.Serialize (xtw, new HexBinary ());
2292 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
2293 Assert.AreEqual (Infoset (expected), WriterText);
2294 HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2295 Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2299 [ExpectedException (typeof (InvalidOperationException))]
2300 public void XmlArrayAttributeOnInt ()
2302 new XmlSerializer (typeof (XmlArrayOnInt));
2306 [ExpectedException (typeof (InvalidOperationException))]
2307 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2309 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2313 [ExpectedException (typeof (InvalidOperationException))]
2314 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2316 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2319 [Test] // bug #78042
2320 public void XmlArrayAttributeOnArray ()
2322 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2323 sw = new StringWriter ();
2324 XmlTextWriter xtw = new XmlTextWriter (sw);
2325 ser.Serialize (xtw, new XmlArrayOnArray ());
2327 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
2328 Assert.AreEqual (Infoset (expected), WriterText);
2332 public void XmlArrayAttributeOnCollection ()
2334 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2335 XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2336 inst.Sane.Add ("abc");
2338 sw = new StringWriter ();
2339 XmlTextWriter xtw = new XmlTextWriter (sw);
2340 ser.Serialize (xtw, inst);
2342 string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
2343 Assert.AreEqual (Infoset (expected), WriterText);
2346 [Test] // bug #338705
2347 public void SerializeTimeSpan ()
2349 // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2350 XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2351 ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2355 public void SerializeDurationToString ()
2357 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2358 ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2362 [ExpectedException (typeof (InvalidOperationException))]
2363 public void SerializeDurationToTimeSpan ()
2365 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2366 ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2370 [ExpectedException (typeof (InvalidOperationException))]
2371 public void SerializeInvalidDataType ()
2373 XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2374 ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2379 [ExpectedException (typeof (ApplicationException))]
2381 public void SerializeErrorneousIXmlSerializable ()
2383 Serialize (new ErrorneousGetSchema ());
2384 Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2388 public void DateTimeRoundtrip ()
2391 XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2392 StringWriter sw = new StringWriter ();
2393 ser.Serialize (sw, DateTime.UtcNow);
2394 DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2395 Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2400 public void SupportIXmlSerializableImplicitlyConvertible ()
2402 XmlAttributes attrs = new XmlAttributes ();
2403 XmlElementAttribute attr = new XmlElementAttribute ();
2404 attr.ElementName = "XmlSerializable";
2405 attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2406 attrs.XmlElements.Add (attr);
2407 XmlAttributeOverrides attrOverrides = new
2408 XmlAttributeOverrides ();
2409 attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2411 XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2412 new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2415 [Test] // bug #566370
2416 public void SerializeEnumWithCSharpKeyword ()
2418 var ser = new XmlSerializer (typeof (DoxCompoundKind));
2419 for (int i = 0; i < 100; i++) // test serialization code generator
2420 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2423 public enum DoxCompoundKind
2430 [XmlEnum("interface")]
2436 [XmlEnum("namespace")]
2444 #region GenericsSeralizationTests
2448 public void TestSerializeGenSimpleClassString ()
2450 GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2452 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2454 simple.something = "hello";
2457 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
2461 public void TestSerializeGenSimpleClassBool ()
2463 GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2465 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
2467 simple.something = true;
2470 Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
2474 public void TestSerializeGenSimpleStructInt ()
2476 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2478 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
2480 simple.something = 123;
2483 Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
2487 public void TestSerializeGenListClassString ()
2489 GenListClass<string> genlist = new GenListClass<string> ();
2490 Serialize (genlist);
2491 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
2493 genlist.somelist.Add ("Value1");
2494 genlist.somelist.Add ("Value2");
2496 Serialize (genlist);
2497 Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
2501 public void TestSerializeGenListClassFloat ()
2503 GenListClass<float> genlist = new GenListClass<float> ();
2504 Serialize (genlist);
2505 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
2507 genlist.somelist.Add (1);
2508 genlist.somelist.Add (2.2F);
2510 Serialize (genlist);
2511 Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
2515 public void TestSerializeGenListClassList ()
2517 GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2518 Serialize (genlist);
2519 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2521 GenListClass<int> inlist1 = new GenListClass<int> ();
2522 inlist1.somelist.Add (1);
2523 inlist1.somelist.Add (2);
2524 GenListClass<int> inlist2 = new GenListClass<int> ();
2525 inlist2.somelist.Add (10);
2526 inlist2.somelist.Add (20);
2527 genlist.somelist.Add (inlist1);
2528 genlist.somelist.Add (inlist2);
2530 Serialize (genlist);
2531 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
2535 public void TestSerializeGenListClassArray ()
2537 GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2538 Serialize (genlist);
2539 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2541 GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2542 genarr1.arr[0] = 'a';
2543 genarr1.arr[1] = 'b';
2544 genlist.somelist.Add (genarr1);
2545 GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2546 genarr2.arr[0] = 'd';
2547 genarr2.arr[1] = 'e';
2548 genarr2.arr[2] = 'f';
2549 genlist.somelist.Add (genarr2);
2551 Serialize (genlist);
2552 Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
2556 public void TestSerializeGenTwoClassCharDouble ()
2558 GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2560 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
2562 gentwo.something1 = 'a';
2563 gentwo.something2 = 2.2;
2566 Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
2570 public void TestSerializeGenDerivedClassDecimalShort ()
2572 GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2573 Serialize (derived);
2574 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2576 derived.something1 = "Value1";
2577 derived.something2 = 1;
2578 derived.another1 = 1.1M;
2579 derived.another2 = -22;
2581 Serialize (derived);
2582 Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2586 public void TestSerializeGenDerivedSecondClassByteUlong ()
2588 GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2589 Serialize (derived2);
2590 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2592 derived2.something1 = 1;
2593 derived2.something2 = 222;
2594 derived2.another1 = 111;
2595 derived2.another2 = 222222;
2597 Serialize (derived2);
2598 Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2602 public void TestSerializeGenNestedClass ()
2604 GenNestedClass<string, int>.InnerClass<bool> nested =
2605 new GenNestedClass<string, int>.InnerClass<bool> ();
2607 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
2610 nested.something = true;
2613 Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
2617 public void TestSerializeGenListClassListNested ()
2619 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2620 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2621 Serialize (genlist);
2622 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2624 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2625 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2626 GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2628 inval1.something = "ONE";
2629 inlist1.somelist.Add (inval1);
2630 GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2632 inval2.something = "TWO";
2633 inlist1.somelist.Add (inval2);
2634 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2635 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2636 GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2638 inval3.something = "THIRTY";
2639 inlist2.somelist.Add (inval3);
2640 genlist.somelist.Add (inlist1);
2641 genlist.somelist.Add (inlist2);
2643 Serialize (genlist);
2644 Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
2647 public enum Myenum { one, two, three, four, five, six };
2649 public void TestSerializeGenArrayClassEnum ()
2651 GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2653 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2655 genarr.arr[0] = Myenum.one;
2656 genarr.arr[1] = Myenum.three;
2657 genarr.arr[2] = Myenum.five;
2660 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2664 public void TestSerializeGenArrayStruct ()
2666 GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2668 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
2670 GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2671 genstruct.something = 111;
2672 genarr.arr[0] = genstruct;
2673 genstruct.something = 222;
2674 genarr.arr[1] = genstruct;
2675 genstruct.something = 333;
2676 genarr.arr[2] = genstruct;
2679 Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
2683 public void TestSerializeGenArrayList ()
2685 GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2687 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
2689 GenListClass<string> genlist1 = new GenListClass<string> ();
2690 genlist1.somelist.Add ("list1-val1");
2691 genlist1.somelist.Add ("list1-val2");
2692 genarr.arr[0] = genlist1;
2693 GenListClass<string> genlist2 = new GenListClass<string> ();
2694 genlist2.somelist.Add ("list2-val1");
2695 genlist2.somelist.Add ("list2-val2");
2696 genlist2.somelist.Add ("list2-val3");
2697 genlist2.somelist.Add ("list2-val4");
2698 genarr.arr[1] = genlist2;
2699 GenListClass<string> genlist3 = new GenListClass<string> ();
2700 genlist3.somelist.Add ("list3val");
2701 genarr.arr[2] = genlist3;
2704 Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
2708 public void TestSerializeGenComplexStruct ()
2710 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2711 Serialize (complex);
2712 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
2714 complex.something = 123;
2715 complex.simpleclass.something = 456;
2716 complex.simplestruct.something = 789;
2717 GenListClass<int> genlist = new GenListClass<int> ();
2718 genlist.somelist.Add (100);
2719 genlist.somelist.Add (200);
2720 complex.listclass = genlist;
2721 GenArrayClass<int> genarr = new GenArrayClass<int> ();
2725 complex.arrayclass = genarr;
2726 complex.twoclass.something1 = 10;
2727 complex.twoclass.something2 = "Ten";
2728 complex.derivedclass.another1 = 1;
2729 complex.derivedclass.another2 = "one";
2730 complex.derivedclass.something1 = "two";
2731 complex.derivedclass.something2 = 2;
2732 complex.derived2.another1 = 3;
2733 complex.derived2.another2 = "three";
2734 complex.derived2.something1 = 4;
2735 complex.derived2.something2 = "four";
2736 complex.nestedouter.outer = 5;
2737 complex.nestedinner.inner = "six";
2738 complex.nestedinner.something = 6;
2740 Serialize (complex);
2741 Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
2744 [Test] // bug #80759
2745 public void HasNullableField ()
2747 Bug80759 foo = new Bug80759 ();
2749 foo.NullableInt = 10;
2751 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2753 MemoryStream stream = new MemoryStream ();
2755 serializer.Serialize (stream, foo);
2756 stream.Position = 0;
2757 foo = (Bug80759) serializer.Deserialize (stream);
2760 [Test] // bug #80759, with fieldSpecified.
2761 [ExpectedException (typeof (InvalidOperationException))]
2762 [Category ("NotWorking")]
2763 public void HasFieldSpecifiedButIrrelevant ()
2765 Bug80759_2 foo = new Bug80759_2 ();
2767 foo.NullableInt = 10;
2769 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2771 MemoryStream stream = new MemoryStream ();
2773 serializer.Serialize (stream, foo);
2774 stream.Position = 0;
2775 foo = (Bug80759_2) serializer.Deserialize (stream);
2779 public void HasNullableField2 ()
2781 Bug80759 foo = new Bug80759 ();
2783 foo.NullableInt = 10;
2785 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2787 MemoryStream stream = new MemoryStream ();
2789 serializer.Serialize (stream, foo);
2790 stream.Position = 0;
2791 foo = (Bug80759) serializer.Deserialize (stream);
2793 Assert.AreEqual ("BAR", foo.Test, "#1");
2794 Assert.AreEqual (10, foo.NullableInt, "#2");
2796 foo.NullableInt = null;
2797 stream = new MemoryStream ();
2798 serializer.Serialize (stream, foo);
2799 stream.Position = 0;
2800 foo = (Bug80759) serializer.Deserialize (stream);
2802 Assert.AreEqual ("BAR", foo.Test, "#3");
2803 Assert.IsNull (foo.NullableInt, "#4");
2807 public void SupportPrivateCtorOnly ()
2810 new XmlSerializer (typeof (PrivateCtorOnly));
2811 StringWriter sw = new StringWriter ();
2812 xs.Serialize (sw, PrivateCtorOnly.Instance);
2813 xs.Deserialize (new StringReader (sw.ToString ()));
2817 public void XmlSchemaProviderQNameBecomesRootName ()
2819 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2820 Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2821 Assert.AreEqual (Infoset ("<foo />"), WriterText);
2822 xs.Deserialize (new StringReader ("<foo/>"));
2826 public void XmlSchemaProviderQNameBecomesRootName2 ()
2828 string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2829 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2830 Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2831 Assert.AreEqual (Infoset (xml), WriterText);
2832 xs.Deserialize (new StringReader (xml));
2836 public void XmlAnyElementForObjects () // bug #553032
2838 new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2842 [ExpectedException (typeof (InvalidOperationException))]
2843 public void XmlAnyElementForObjects2 () // bug #553032-2
2845 new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2849 public void XmlRootOverridesSchemaProviderQName ()
2851 var obj = new XmlRootOverridesSchemaProviderQNameType ();
2853 XmlSerializer xs = new XmlSerializer (obj.GetType ());
2855 var sw = new StringWriter ();
2856 using (XmlWriter xw = XmlWriter.Create (sw))
2857 xs.Serialize (xw, obj);
2858 Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2862 #endregion //GenericsSeralizationTests
2864 public class XmlArrayOnInt
2870 public class XmlArrayUnqualifiedWithNamespace
2872 [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2873 public ArrayList Sane = new ArrayList ();
2876 public class XmlArrayItemUnqualifiedWithNamespace
2878 [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2879 public ArrayList Sane = new ArrayList ();
2882 [XmlRoot (Namespace = "urn:foo")]
2883 public class XmlArrayOnArrayList
2885 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2886 public ArrayList Sane = new ArrayList ();
2889 [XmlRoot (Namespace = "urn:foo")]
2890 public class XmlArrayOnArray
2892 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2893 public string[] Sane = new string[] { "foo", "bar" };
2895 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2896 public ArrayItemInXmlArray[] Mids =
2897 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
2900 [XmlType (Namespace = "urn:gyabo")]
2901 public class ArrayItemInXmlArray
2903 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2904 public string[] Whee = new string[] { "foo", "bar" };
2907 [XmlRoot ("Base64Binary")]
2908 public class Base64Binary
2910 [XmlAttribute (DataType = "base64Binary")]
2911 public byte [] Data = new byte [] {1, 2, 3};
2914 [XmlRoot ("HexBinary")]
2915 public class HexBinary
2917 [XmlAttribute (DataType = "hexBinary")]
2918 public byte[] Data = new byte[] { 1, 2, 3 };
2921 [XmlRoot ("PrivateCtorOnly")]
2922 public class PrivateCtorOnly
2924 public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
2925 private PrivateCtorOnly ()
2930 public class CDataTextNodesType
2932 public CDataTextNodesInternal foo;
2935 public class CDataTextNodesInternal
2938 public string Value;
2941 public class InvalidTypeContainer
2943 [XmlElement (DataType = "invalid")]
2944 public string InvalidTypeItem = "aaa";
2947 public class TimeSpanContainer1
2949 [XmlElement (DataType = "duration")]
2950 public string StringDuration = "aaa";
2953 public class TimeSpanContainer2
2955 [XmlElement (DataType = "duration")]
2956 public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
2960 public class Bug80759
2963 public int? NullableInt;
2966 public class Bug80759_2
2969 public int? NullableInt;
2972 public bool NullableIntSpecified {
2973 get { return NullableInt.HasValue; }
2977 [XmlSchemaProvider ("GetXsdType")]
2978 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
2980 public XmlSchema GetSchema ()
2985 public void ReadXml (XmlReader reader)
2990 public void WriteXml (XmlWriter writer)
2994 public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
2996 if (xss.Count == 0) {
2997 XmlSchema xs = new XmlSchema ();
2998 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3003 return new XmlQualifiedName ("foo");
3007 public class XmlSchemaProviderQNameBecomesRootNameType2
3009 [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3010 public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3013 public class XmlAnyElementForObjectsType
3016 public object [] arr = new object [] {3,4,5};
3020 [XmlSchemaProvider ("GetSchema")]
3021 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3023 public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3025 var xs = new XmlSchema ();
3026 var xse = new XmlSchemaComplexType () { Name = "bar" };
3029 return new XmlQualifiedName ("bar");
3032 XmlSchema IXmlSerializable.GetSchema ()
3037 void IXmlSerializable.ReadXml (XmlReader reader)
3040 void IXmlSerializable.WriteXml (XmlWriter writer)
3047 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3054 public static string Infoset (string sx)
3056 XmlDocument doc = new XmlDocument ();
3058 StringBuilder sb = new StringBuilder ();
3059 GetInfoset (doc.DocumentElement, sb);
3060 return sb.ToString ();
3063 public static string Infoset (XmlNode nod)
3065 StringBuilder sb = new StringBuilder ();
3066 GetInfoset (nod, sb);
3067 return sb.ToString ();
3070 static void GetInfoset (XmlNode nod, StringBuilder sb)
3072 switch (nod.NodeType) {
3073 case XmlNodeType.Attribute:
3074 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3075 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3078 case XmlNodeType.Element:
3079 XmlElement elem = (XmlElement) nod;
3080 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3082 ArrayList ats = new ArrayList ();
3083 foreach (XmlAttribute at in elem.Attributes)
3084 ats.Add (at.LocalName + " " + at.NamespaceURI);
3088 foreach (string name in ats) {
3089 string[] nn = name.Split (' ');
3090 GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3094 foreach (XmlNode cn in elem.ChildNodes)
3095 GetInfoset (cn, sb);
3100 sb.Append (nod.OuterXml);
3105 static XmlTypeMapping CreateSoapMapping (Type type)
3107 SoapReflectionImporter importer = new SoapReflectionImporter ();
3108 return importer.ImportTypeMapping (type);
3111 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3113 SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3114 return importer.ImportTypeMapping (type);
3117 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3119 SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3120 return importer.ImportTypeMapping (type);