2 // System.Xml.XmlSerializerTests
5 // Erik LeBel <eriklebel@yahoo.ca>
6 // Hagit Yidov <hagity@mainsoft.com>
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Where possible, these tests avoid testing the order of
14 // an object's members serialization. Mono and .NET do not
15 // reflect members in the same order.
17 // Only serializations tests so far, no deserialization.
20 // test XmlArrayAttribute
21 // test XmlArrayItemAttribute
22 // test serialization of decimal type
23 // test serialization of Guid type
24 // test XmlNode serialization with and without modifying attributes.
25 // test deserialization
26 // FIXMEs found in this file
29 using System.Collections;
30 using System.Globalization;
35 using System.Xml.Schema;
36 using System.Xml.Serialization;
38 using System.Collections.Generic;
41 using NUnit.Framework;
43 using MonoTests.System.Xml.TestClasses;
45 namespace MonoTests.System.XmlSerialization
48 public class XmlSerializerTests
50 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
51 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
52 const string ANamespace = "some:urn";
53 const string AnotherNamespace = "another:urn";
59 private void SetUpWriter ()
61 sw = new StringWriter ();
62 xtw = new XmlTextWriter (sw);
64 xtw.Formatting = Formatting.None;
67 private string WriterText
71 string val = sw.GetStringBuilder ().ToString ();
72 int offset = val.IndexOf ('>') + 1;
73 val = val.Substring (offset);
78 private void Serialize (object o)
81 xs = new XmlSerializer (o.GetType ());
82 xs.Serialize (xtw, o);
85 private void Serialize (object o, Type type)
88 xs = new XmlSerializer (type);
89 xs.Serialize (xtw, o);
92 private void Serialize (object o, XmlSerializerNamespaces ns)
95 xs = new XmlSerializer (o.GetType ());
96 xs.Serialize (xtw, o, ns);
99 private void Serialize (object o, XmlAttributeOverrides ao)
102 xs = new XmlSerializer (o.GetType (), ao);
103 xs.Serialize (xtw, o);
106 private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
109 xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
110 (XmlRootAttribute) null, defaultNamespace);
111 xs.Serialize (xtw, o);
114 private void Serialize (object o, XmlRootAttribute root)
117 xs = new XmlSerializer (o.GetType (), root);
118 xs.Serialize (xtw, o);
121 private void Serialize (object o, XmlTypeMapping typeMapping)
124 xs = new XmlSerializer (typeMapping);
125 xs.Serialize (xtw, o);
128 private void SerializeEncoded (object o)
130 SerializeEncoded (o, o.GetType ());
133 private void SerializeEncoded (object o, SoapAttributeOverrides ao)
135 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
137 xs = new XmlSerializer (mapping);
138 xs.Serialize (xtw, o);
141 private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
143 XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
145 xs = new XmlSerializer (mapping);
146 xs.Serialize (xtw, o);
149 private void SerializeEncoded (object o, Type type)
151 XmlTypeMapping mapping = CreateSoapMapping (type);
153 xs = new XmlSerializer (mapping);
154 xs.Serialize (xtw, o);
157 private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
159 XmlTypeMapping mapping = CreateSoapMapping (type);
160 xs = new XmlSerializer (mapping);
161 xs.Serialize (xtw, o);
165 #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
167 public void TestConstructor()
169 XmlSerializer ser = new XmlSerializer (null, "");
174 // test basic types ////////////////////////////////////////////////////////
176 public void TestSerializeInt ()
179 Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
183 public void TestSerializeBool ()
186 Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
189 Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
193 public void TestSerializeString ()
196 Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
200 public void TestSerializeEmptyString ()
202 Serialize (String.Empty);
203 Assert.AreEqual (Infoset ("<string />"), WriterText);
207 public void TestSerializeNullObject ()
209 Serialize (null, typeof (object));
210 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
211 "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
212 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
216 [Ignore ("The generated XML is not exact but it is equivalent")]
217 public void TestSerializeNullString ()
219 Serialize (null, typeof (string));
220 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
221 "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
222 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
226 public void TestSerializeIntArray ()
228 Serialize (new int[] { 1, 2, 3, 4 });
229 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
230 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
231 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
235 public void TestSerializeEmptyArray ()
237 Serialize (new int[] { });
238 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
239 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
240 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
244 public void TestSerializeChar ()
247 Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
250 Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
253 Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
255 Serialize ('\uFF01');
256 Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
260 public void TestSerializeFloat ()
263 Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
266 Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
268 // FIXME test INF and other boundary conditions that may exist with floats
272 public void TestSerializeEnumeration_FromValue ()
274 Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
276 "<?xml version='1.0' encoding='utf-16'?>" +
277 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
282 [Category ("NotWorking")]
283 public void TestSerializeEnumeration_FromValue_Encoded ()
285 SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
286 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
287 "<?xml version='1.0' encoding='utf-16'?>" +
288 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
289 XmlSchema.InstanceNamespace), sw.ToString ());
293 public void TestSerializeEnumeration ()
295 Serialize (SimpleEnumeration.FIRST);
296 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
298 Serialize (SimpleEnumeration.SECOND);
299 Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
303 public void TestSerializeEnumeration_Encoded ()
305 SerializeEncoded (SimpleEnumeration.FIRST);
306 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
307 "<?xml version='1.0' encoding='utf-16'?>" +
308 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
309 XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
311 SerializeEncoded (SimpleEnumeration.SECOND);
312 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
313 "<?xml version='1.0' encoding='utf-16'?>" +
314 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
315 XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
319 public void TestSerializeEnumDefaultValue ()
321 Serialize (new EnumDefaultValue ());
322 Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
324 Serialize (new SimpleEnumeration ());
325 Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
327 Serialize (3, typeof (EnumDefaultValue));
328 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
330 Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
331 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
333 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
334 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
336 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
337 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
339 Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
340 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
342 Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
343 Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
345 Serialize (3, typeof (FlagEnum));
346 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
348 Serialize (5, typeof (FlagEnum));
349 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
351 Serialize (FlagEnum.e4, typeof (FlagEnum));
352 Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
354 Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
355 Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
357 Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
358 Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
360 Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
361 Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
363 Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
364 Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
366 Serialize (3, typeof (EnumDefaultValueNF));
367 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
369 Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
370 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
372 Serialize (2, typeof (ZeroFlagEnum));
373 Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t<w>o</ZeroFlagEnum>"), WriterText, "#18");
375 Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
376 Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
380 [Category ("NotWorking")]
381 public void TestSerializeEnumDefaultValue_Encoded ()
383 SerializeEncoded (new EnumDefaultValue ());
384 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
385 "<?xml version='1.0' encoding='utf-16'?>" +
386 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
387 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
389 SerializeEncoded (new SimpleEnumeration ());
390 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
391 "<?xml version='1.0' encoding='utf-16'?>" +
392 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
393 XmlSchema.InstanceNamespace), sw.ToString (), "#2");
395 SerializeEncoded (3, typeof (EnumDefaultValue));
396 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
397 "<?xml version='1.0' encoding='utf-16'?>" +
398 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
399 XmlSchema.InstanceNamespace), sw.ToString (), "#3");
401 SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
402 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
403 "<?xml version='1.0' encoding='utf-16'?>" +
404 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
405 XmlSchema.InstanceNamespace), sw.ToString (), "#4");
407 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
408 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
409 "<?xml version='1.0' encoding='utf-16'?>" +
410 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
411 XmlSchema.InstanceNamespace), sw.ToString (), "#5");
413 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
414 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
415 "<?xml version='1.0' encoding='utf-16'?>" +
416 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
417 XmlSchema.InstanceNamespace), sw.ToString (), "#6");
419 SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
420 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
421 "<?xml version='1.0' encoding='utf-16'?>" +
422 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
423 XmlSchema.InstanceNamespace), sw.ToString (), "#7");
425 SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
426 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
427 "<?xml version='1.0' encoding='utf-16'?>" +
428 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
429 XmlSchema.InstanceNamespace), sw.ToString (), "#8");
431 SerializeEncoded (3, typeof (FlagEnum));
432 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
433 "<?xml version='1.0' encoding='utf-16'?>" +
434 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
435 XmlSchema.InstanceNamespace), sw.ToString (), "#9");
437 SerializeEncoded (5, typeof (FlagEnum));
438 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
439 "<?xml version='1.0' encoding='utf-16'?>" +
440 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
441 XmlSchema.InstanceNamespace), sw.ToString (), "#10");
443 SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
444 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
445 "<?xml version='1.0' encoding='utf-16'?>" +
446 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
447 XmlSchema.InstanceNamespace), sw.ToString (), "#11");
449 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
450 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
451 "<?xml version='1.0' encoding='utf-16'?>" +
452 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
453 XmlSchema.InstanceNamespace), sw.ToString (), "#12");
455 SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
456 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
457 "<?xml version='1.0' encoding='utf-16'?>" +
458 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
459 XmlSchema.InstanceNamespace), sw.ToString (), "#13");
461 SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
462 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
463 "<?xml version='1.0' encoding='utf-16'?>" +
464 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
465 XmlSchema.InstanceNamespace), sw.ToString (), "#14");
467 SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
468 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
469 "<?xml version='1.0' encoding='utf-16'?>" +
470 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
471 XmlSchema.InstanceNamespace), sw.ToString (), "#15");
473 SerializeEncoded (3, typeof (EnumDefaultValueNF));
474 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
475 "<?xml version='1.0' encoding='utf-16'?>" +
476 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
477 XmlSchema.InstanceNamespace), sw.ToString (), "#16");
479 SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
480 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
481 "<?xml version='1.0' encoding='utf-16'?>" +
482 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
483 XmlSchema.InstanceNamespace), sw.ToString (), "#17");
485 SerializeEncoded (2, typeof (ZeroFlagEnum));
486 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
487 "<?xml version='1.0' encoding='utf-16'?>" +
488 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
489 XmlSchema.InstanceNamespace), sw.ToString (), "#18");
491 SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
492 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
493 "<?xml version='1.0' encoding='utf-16'?>" +
494 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
495 XmlSchema.InstanceNamespace), sw.ToString (), "#19");
499 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
502 Serialize ("b", typeof (EnumDefaultValue));
504 } catch (InvalidOperationException ex) {
505 Assert.IsNotNull (ex.InnerException, "#A2");
506 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
510 Serialize ("e1", typeof (EnumDefaultValue));
512 } catch (InvalidOperationException ex) {
513 Assert.IsNotNull (ex.InnerException, "#B2");
514 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
518 Serialize ("e1,e2", typeof (EnumDefaultValue));
520 } catch (InvalidOperationException ex) {
521 Assert.IsNotNull (ex.InnerException, "#C2");
522 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
526 Serialize (string.Empty, typeof (EnumDefaultValue));
528 } catch (InvalidOperationException ex) {
529 Assert.IsNotNull (ex.InnerException, "#D2");
530 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
534 Serialize ("1", typeof (EnumDefaultValue));
536 } catch (InvalidOperationException ex) {
537 Assert.IsNotNull (ex.InnerException, "#E2");
538 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
542 Serialize ("0", typeof (EnumDefaultValue));
544 } catch (InvalidOperationException ex) {
545 Assert.IsNotNull (ex.InnerException, "#F2");
546 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
550 Serialize (new SimpleClass (), typeof (EnumDefaultValue));
552 } catch (InvalidOperationException ex) {
553 Assert.IsNotNull (ex.InnerException, "#G2");
554 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
559 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
563 Serialize (5, typeof (EnumDefaultValue));
565 } catch (InvalidOperationException ex) {
566 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
567 Assert.IsNotNull (ex.InnerException, "#3");
568 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
569 Assert.IsNotNull (ex.InnerException.Message, "#5");
570 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
571 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
574 Serialize (5, typeof (EnumDefaultValue));
575 Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
580 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
584 Serialize (new EnumDefaultValueNF ());
586 } catch (InvalidOperationException ex) {
587 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
588 Assert.IsNotNull (ex.InnerException, "#3");
589 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
590 Assert.IsNotNull (ex.InnerException.Message, "#5");
591 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
592 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
595 Serialize (new EnumDefaultValueNF ());
596 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
601 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
605 Serialize (15, typeof (EnumDefaultValueNF));
607 } catch (InvalidOperationException ex) {
608 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
609 Assert.IsNotNull (ex.InnerException, "#3");
610 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
611 Assert.IsNotNull (ex.InnerException.Message, "#5");
612 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
613 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
616 Serialize (15, typeof (EnumDefaultValueNF));
617 Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
622 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
625 Serialize ("b", typeof (EnumDefaultValueNF));
627 } catch (InvalidOperationException ex) {
628 Assert.IsNotNull (ex.InnerException, "#A2");
629 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
633 Serialize ("e2", typeof (EnumDefaultValueNF));
635 } catch (InvalidOperationException ex) {
636 Assert.IsNotNull (ex.InnerException, "#B2");
637 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
641 Serialize (string.Empty, typeof (EnumDefaultValueNF));
643 } catch (InvalidOperationException ex) {
644 Assert.IsNotNull (ex.InnerException, "#C2");
645 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
649 Serialize ("1", typeof (EnumDefaultValueNF));
651 } catch (InvalidOperationException ex) {
652 Assert.IsNotNull (ex.InnerException, "#D2");
653 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
657 Serialize ("0", typeof (EnumDefaultValueNF));
659 } catch (InvalidOperationException ex) {
660 Assert.IsNotNull (ex.InnerException, "#E2");
661 Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
666 public void TestSerializeField ()
668 Field f = new Field ();
669 Serialize (f, typeof (Field));
670 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
671 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
672 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
673 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
675 f.Flags1 = FlagEnum.e1;
676 f.Flags2 = FlagEnum.e1;
677 f.Flags3 = FlagEnum.e2;
678 f.Modifiers = MapModifiers.Protected;
679 f.Modifiers2 = MapModifiers.Public;
680 f.Modifiers3 = MapModifiers.Public;
681 f.Modifiers4 = MapModifiers.Protected;
682 f.Modifiers5 = MapModifiers.Public;
683 Serialize (f, typeof (Field));
684 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
685 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
686 " modifiers='protected' modifiers2='public' />",
687 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
689 f.Flags1 = (FlagEnum) 1;
690 f.Flags1 = FlagEnum.e2;
691 f.Flags2 = FlagEnum.e2;
692 f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
693 f.Modifiers = MapModifiers.Public;
694 f.Modifiers2 = MapModifiers.Protected;
695 f.Modifiers3 = MapModifiers.Protected;
696 f.Modifiers4 = MapModifiers.Public;
697 f.Modifiers5 = MapModifiers.Protected;
698 Serialize (f, typeof (Field));
699 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
700 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
701 " flag4='' modifiers='public' modifiers2='protected'" +
702 " modifiers3='protected' modifiers4='public'" +
703 " modifiers5='protected' />",
704 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
706 f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
707 f.Flags2 = FlagEnum.e2;
708 f.Flags3 = FlagEnum.e4;
709 f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
710 f.Modifiers3 = MapModifiers.Public;
711 f.Modifiers4 = MapModifiers.Protected;
712 f.Modifiers5 = MapModifiers.Public;
713 f.Names = new string[] { "a", "b" };
714 Serialize (f, typeof (Field));
715 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
716 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
717 " flag3='four' flag4='one two four' modifiers='public'" +
718 " modifiers2='protected' names='a b' />",
719 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
721 f.Flags2 = (FlagEnum) 444;
722 f.Flags3 = (FlagEnum) 555;
723 f.Modifiers = (MapModifiers) 666;
724 f.Modifiers2 = (MapModifiers) 777;
725 f.Modifiers3 = (MapModifiers) 0;
726 f.Modifiers4 = (MapModifiers) 888;
727 f.Modifiers5 = (MapModifiers) 999;
730 Serialize (f, typeof (Field));
732 } catch (InvalidOperationException ex) {
733 // There was an error generating the XML document
734 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
735 Assert.IsNotNull (ex.Message, "#E3");
736 Assert.IsNotNull (ex.InnerException, "#E4");
738 // Instance validation error: '444' is not a valid value for
739 // MonoTests.System.Xml.TestClasses.FlagEnum
740 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
741 Assert.IsNotNull (ex.InnerException.Message, "#E6");
742 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
743 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
744 Assert.IsNull (ex.InnerException.InnerException, "#E9");
747 Serialize (f, typeof (Field));
748 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
749 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
750 " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
751 " modifiers4='888' modifiers5='999' names='a b' />",
752 XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
757 [Category ("NotDotNet")] // MS bug
758 public void TestSerializeField_Encoded ()
760 Field_Encoded f = new Field_Encoded ();
761 SerializeEncoded (f, typeof (Field_Encoded));
762 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
763 "<?xml version='1.0' encoding='utf-16'?>" +
765 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
766 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
767 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
769 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
770 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
771 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
773 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
774 sw.GetStringBuilder ().ToString (), "#A");
776 f.Flags1 = FlagEnum_Encoded.e1;
777 f.Flags2 = FlagEnum_Encoded.e1;
778 f.Flags3 = FlagEnum_Encoded.e2;
779 f.Modifiers = MapModifiers.Protected;
780 f.Modifiers2 = MapModifiers.Public;
781 f.Modifiers3 = MapModifiers.Public;
782 f.Modifiers4 = MapModifiers.Protected;
783 f.Modifiers5 = MapModifiers.Public;
784 SerializeEncoded (f, typeof (Field_Encoded));
785 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
786 "<?xml version='1.0' encoding='utf-16'?>" +
788 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
789 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
790 " xmlns:q1='some:urn' />",
792 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
793 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
794 " xmlns:q1='some:urn' />",
796 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
797 sw.GetStringBuilder ().ToString (), "#B");
799 f.Flags1 = FlagEnum_Encoded.e2;
800 f.Flags2 = FlagEnum_Encoded.e2;
801 f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
802 f.Modifiers = MapModifiers.Public;
803 f.Modifiers2 = MapModifiers.Protected;
804 f.Modifiers3 = MapModifiers.Protected;
805 f.Modifiers4 = MapModifiers.Public;
806 f.Modifiers5 = MapModifiers.Protected;
807 SerializeEncoded (f, typeof (Field_Encoded));
808 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
809 "<?xml version='1.0' encoding='utf-16'?>" +
811 "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
812 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
813 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
814 " xmlns:q1='some:urn' />",
816 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
817 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
818 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
819 " xmlns:q1='some:urn' />",
821 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
822 sw.GetStringBuilder ().ToString (), "#C");
824 f.Flags1 = (FlagEnum_Encoded) 1;
825 f.Flags2 = (FlagEnum_Encoded) 444;
826 f.Flags3 = (FlagEnum_Encoded) 555;
827 f.Modifiers = (MapModifiers) 666;
828 f.Modifiers2 = (MapModifiers) 777;
829 f.Modifiers3 = (MapModifiers) 0;
830 f.Modifiers4 = (MapModifiers) 888;
831 f.Modifiers5 = (MapModifiers) 999;
835 SerializeEncoded (f, typeof (Field_Encoded));
838 } catch (InvalidOperationException ex) {
839 // There was an error generating the XML document
840 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
841 Assert.IsNotNull (ex.Message, "#D3");
842 Assert.IsNotNull (ex.InnerException, "#D4");
844 // Instance validation error: '444' is not a valid value for
845 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
846 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
847 Assert.IsNotNull (ex.InnerException.Message, "#D6");
848 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
849 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
850 Assert.IsNull (ex.InnerException.InnerException, "#D9");
853 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
854 "<?xml version='1.0' encoding='utf-16'?>" +
855 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
856 " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
857 " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
858 XmlSchema.Namespace, XmlSchema.InstanceNamespace),
859 sw.GetStringBuilder ().ToString (), "#D");
865 [Ignore ("JVM returns fields in different order")]
867 public void TestSerializeGroup ()
869 Group myGroup = new Group ();
870 myGroup.GroupName = ".NET";
872 Byte[] hexByte = new Byte[] { 0x64, 0x32 };
873 myGroup.GroupNumber = hexByte;
875 DateTime myDate = new DateTime (2002, 5, 2);
876 myGroup.Today = myDate;
877 myGroup.PostitiveInt = "10000";
878 myGroup.IgnoreThis = true;
879 Car thisCar = (Car) myGroup.myCar ("1234566");
880 myGroup.MyVehicle = thisCar;
883 xtw.WriteStartDocument (true);
884 xtw.WriteStartElement ("Wrapper");
885 SerializeEncoded (xtw, myGroup, typeof (Group));
886 xtw.WriteEndElement ();
889 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
891 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
892 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
893 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
894 "<MyVehicle href='#id2' />" +
896 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
897 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
898 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
901 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
904 myGroup.GroupName = null;
905 myGroup.Grouptype = GroupType.B;
906 myGroup.MyVehicle.licenseNumber = null;
907 myGroup.MyVehicle.weight = "450";
910 xtw.WriteStartDocument (true);
911 xtw.WriteStartElement ("Wrapper");
912 SerializeEncoded (xtw, myGroup, typeof (Group));
913 xtw.WriteEndElement ();
916 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
918 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
919 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
920 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
921 "<MyVehicle href='#id2' />" +
923 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
924 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
925 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
928 XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
933 public void TestSerializeZeroFlagEnum_InvalidValue ()
937 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
939 } catch (InvalidOperationException ex) {
940 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
941 Assert.IsNotNull (ex.InnerException, "#3");
942 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
943 Assert.IsNotNull (ex.InnerException.Message, "#5");
944 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
945 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
948 Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
949 Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
954 public void TestSerializeQualifiedName ()
956 Serialize (new XmlQualifiedName ("me", "home.urn"));
957 Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
961 public void TestSerializeBytes ()
963 Serialize ((byte) 0xAB);
964 Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
966 Serialize ((byte) 15);
967 Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
971 public void TestSerializeByteArrays ()
973 Serialize (new byte[] { });
974 Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
976 Serialize (new byte[] { 0xAB, 0xCD });
977 Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
981 public void TestSerializeDateTime ()
983 DateTime d = new DateTime ();
986 TimeZone tz = TimeZone.CurrentTimeZone;
987 TimeSpan off = tz.GetUtcOffset (d);
988 string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
990 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
992 Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
1003 public void TestSerialize()
1006 Assert.AreEqual (WriterText, "");
1010 // test basic class serialization /////////////////////////////////////
1012 public void TestSerializeSimpleClass ()
1014 SimpleClass simple = new SimpleClass ();
1016 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1018 simple.something = "hello";
1021 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);
1025 public void TestSerializeStringCollection ()
1027 StringCollection strings = new StringCollection ();
1028 Serialize (strings);
1029 Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1031 strings.Add ("hello");
1032 strings.Add ("goodbye");
1033 Serialize (strings);
1034 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);
1038 public void TestSerializeOptionalValueTypeContainer ()
1040 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1042 OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
1044 Serialize (optionalValue);
1045 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1046 "<?xml version='1.0' encoding='utf-16'?>" +
1048 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
1050 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
1052 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
1053 sw.ToString (), "#1");
1055 attr = new XmlAttributes ();
1057 // remove the DefaultValue attribute on the Flags member
1058 overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
1059 // remove the DefaultValue attribute on the Attributes member
1060 overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
1062 Serialize (optionalValue, overrides);
1063 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1064 "<?xml version='1.0' encoding='utf-16'?>" +
1066 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1068 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1070 "<Attributes xmlns='{3}'>one four</Attributes>" +
1071 "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1072 AnotherNamespace, ANamespace), sw.ToString (), "#2");
1074 optionalValue.FlagsSpecified = true;
1075 Serialize (optionalValue, overrides);
1076 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1077 "<?xml version='1.0' encoding='utf-16'?>" +
1079 "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
1081 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1083 "<Attributes xmlns='{3}'>one four</Attributes>" +
1084 "<Flags xmlns='{3}'>one</Flags>" +
1086 XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
1087 ANamespace), sw.ToString (), "#3");
1091 public void TestSerializePlainContainer ()
1093 StringCollectionContainer container = new StringCollectionContainer ();
1094 Serialize (container);
1095 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
1097 container.Messages.Add ("hello");
1098 container.Messages.Add ("goodbye");
1099 Serialize (container);
1100 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);
1104 public void TestSerializeArrayContainer ()
1106 ArrayContainer container = new ArrayContainer ();
1107 Serialize (container);
1108 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1110 container.items = new object[] { 10, 20 };
1111 Serialize (container);
1112 Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
1114 container.items = new object[] { 10, "hello" };
1115 Serialize (container);
1116 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);
1120 public void TestSerializeClassArrayContainer ()
1122 ClassArrayContainer container = new ClassArrayContainer ();
1123 Serialize (container);
1124 Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1126 SimpleClass simple1 = new SimpleClass ();
1127 simple1.something = "hello";
1128 SimpleClass simple2 = new SimpleClass ();
1129 simple2.something = "hello";
1130 container.items = new SimpleClass[2];
1131 container.items[0] = simple1;
1132 container.items[1] = simple2;
1133 Serialize (container);
1134 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);
1137 // test basic attributes ///////////////////////////////////////////////
1139 public void TestSerializeSimpleClassWithXmlAttributes ()
1141 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1143 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1145 simple.something = "hello";
1147 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
1150 // test overrides ///////////////////////////////////////////////////////
1152 public void TestSerializeSimpleClassWithOverrides ()
1154 // Also tests XmlIgnore
1155 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1157 XmlAttributes attr = new XmlAttributes ();
1158 attr.XmlIgnore = true;
1159 overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
1161 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
1162 simple.something = "hello";
1163 Serialize (simple, overrides);
1164 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1168 public void TestSerializeSchema ()
1170 XmlSchema schema = new XmlSchema ();
1171 schema.Items.Add (new XmlSchemaAttribute ());
1172 schema.Items.Add (new XmlSchemaAttributeGroup ());
1173 schema.Items.Add (new XmlSchemaComplexType ());
1174 schema.Items.Add (new XmlSchemaNotation ());
1175 schema.Items.Add (new XmlSchemaSimpleType ());
1176 schema.Items.Add (new XmlSchemaGroup ());
1177 schema.Items.Add (new XmlSchemaElement ());
1179 StringWriter sw = new StringWriter ();
1180 XmlTextWriter xtw = new XmlTextWriter (sw);
1181 xtw.QuoteChar = '\'';
1182 xtw.Formatting = Formatting.Indented;
1183 XmlSerializer xs = new XmlSerializer (schema.GetType ());
1184 xs.Serialize (xtw, schema);
1186 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
1187 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1188 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1189 " <xsd:attribute />{0}" +
1190 " <xsd:attributeGroup />{0}" +
1191 " <xsd:complexType />{0}" +
1192 " <xsd:notation />{0}" +
1193 " <xsd:simpleType />{0}" +
1194 " <xsd:group />{0}" +
1195 " <xsd:element />{0}" +
1196 "</xsd:schema>", Environment.NewLine), sw.ToString ());
1199 // test xmlText //////////////////////////////////////////////////////////
1201 public void TestSerializeXmlTextAttribute ()
1203 SimpleClass simple = new SimpleClass ();
1204 simple.something = "hello";
1206 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1207 XmlAttributes attr = new XmlAttributes ();
1208 overrides.Add (typeof (SimpleClass), "something", attr);
1210 attr.XmlText = new XmlTextAttribute ();
1211 Serialize (simple, overrides);
1212 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
1214 attr.XmlText = new XmlTextAttribute (typeof (string));
1215 Serialize (simple, overrides);
1216 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");
1219 attr.XmlText = new XmlTextAttribute (typeof (byte[]));
1220 Serialize (simple, overrides);
1221 Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1222 } catch (InvalidOperationException ex) {
1223 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1224 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1225 Assert.IsNotNull (ex.Message, "#A3");
1226 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
1227 Assert.IsNotNull (ex.InnerException, "#A5");
1229 // there was an error reflecting field 'something'.
1230 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
1231 Assert.IsNotNull (ex.InnerException.Message, "#A7");
1232 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
1233 Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
1235 // the type for XmlText may not be specified for primitive types.
1236 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
1237 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
1238 Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
1242 attr.XmlText = new XmlTextAttribute ();
1243 attr.XmlText.DataType = "sometype";
1244 Serialize (simple, overrides);
1245 Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1246 } catch (InvalidOperationException ex) {
1247 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1248 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1249 Assert.IsNotNull (ex.Message, "#B3");
1250 Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
1251 Assert.IsNotNull (ex.InnerException, "#B5");
1253 // There was an error reflecting field 'something'.
1254 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
1255 Assert.IsNotNull (ex.InnerException.Message, "#B7");
1256 Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
1257 Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
1261 // There was an error reflecting type 'System.String'.
1262 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1263 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1264 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1265 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1267 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
1268 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1269 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1270 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1271 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1272 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1277 // test xmlRoot //////////////////////////////////////////////////////////
1279 public void TestSerializeXmlRootAttribute ()
1281 // constructor override & element name
1282 XmlRootAttribute root = new XmlRootAttribute ();
1283 root.ElementName = "renamed";
1285 SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
1286 Serialize (simpleWithAttributes, root);
1287 Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1289 SimpleClass simple = null;
1290 root.IsNullable = false;
1292 Serialize (simple, root);
1293 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1294 } catch (NullReferenceException) {
1297 root.IsNullable = true;
1299 Serialize (simple, root);
1300 Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1301 } catch (NullReferenceException) {
1304 simple = new SimpleClass ();
1305 root.ElementName = null;
1306 root.Namespace = "some.urn";
1307 Serialize (simple, root);
1308 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);
1312 public void TestSerializeXmlRootAttributeOnMember ()
1315 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1316 XmlAttributes childAttr = new XmlAttributes ();
1317 childAttr.XmlRoot = new XmlRootAttribute ("simple");
1318 overrides.Add (typeof (SimpleClass), childAttr);
1320 XmlAttributes attr = new XmlAttributes ();
1321 attr.XmlRoot = new XmlRootAttribute ("simple");
1322 overrides.Add (typeof (ClassArrayContainer), attr);
1324 ClassArrayContainer container = new ClassArrayContainer ();
1325 container.items = new SimpleClass[1];
1326 container.items[0] = new SimpleClass ();
1327 Serialize (container, overrides);
1328 Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
1330 // FIXME test data type
1333 // test XmlAttribute /////////////////////////////////////////////////////
1335 public void TestSerializeXmlAttributeAttribute ()
1338 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1339 XmlAttributes attr = new XmlAttributes ();
1340 attr.XmlAttribute = new XmlAttributeAttribute ();
1341 overrides.Add (typeof (SimpleClass), "something", attr);
1343 SimpleClass simple = new SimpleClass (); ;
1344 Serialize (simple, overrides);
1345 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1348 simple.something = "hello";
1349 Serialize (simple, overrides);
1350 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");
1353 attr.XmlAttribute.AttributeName = "somethingelse";
1354 Serialize (simple, overrides);
1355 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");
1358 // FIXME this should work, shouldnt it?
1359 // attr.XmlAttribute.Type = typeof(string);
1360 // Serialize(simple, overrides);
1361 // Assert(WriterText.EndsWith(" something='hello' />"));
1364 attr.XmlAttribute.Namespace = "some:urn";
1365 Serialize (simple, overrides);
1366 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");
1369 // FIXME XmlSchemaForm Form
1371 // FIXME write XmlQualifiedName as attribute
1374 // test XmlElement ///////////////////////////////////////////////////////
1376 public void TestSerializeXmlElementAttribute ()
1378 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1379 XmlAttributes attr = new XmlAttributes ();
1380 XmlElementAttribute element = new XmlElementAttribute ();
1381 attr.XmlElements.Add (element);
1382 overrides.Add (typeof (SimpleClass), "something", attr);
1385 SimpleClass simple = new SimpleClass (); ;
1386 Serialize (simple, overrides);
1387 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1390 simple.something = "hello";
1391 Serialize (simple, overrides);
1392 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");
1395 element.ElementName = "saying";
1396 Serialize (simple, overrides);
1397 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");
1400 element.IsNullable = false;
1401 simple.something = null;
1402 Serialize (simple, overrides);
1403 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
1405 element.IsNullable = true;
1406 simple.something = null;
1407 Serialize (simple, overrides);
1408 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");
1411 element.ElementName = null;
1412 element.IsNullable = false;
1413 element.Namespace = "some:urn";
1414 simple.something = "hello";
1415 Serialize (simple, overrides);
1416 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");
1423 // test XmlElementAttribute with arrays and collections //////////////////
1425 public void TestSerializeCollectionWithXmlElementAttribute ()
1428 // if no type is specified or the specified type
1429 // matches the contents of the collection,
1430 // serialize each element in an element named after the member.
1431 // if the type does not match, or matches the collection itself,
1432 // create a base wrapping element for the member, and then
1433 // wrap each collection item in its own wrapping element based on type.
1435 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1436 XmlAttributes attr = new XmlAttributes ();
1437 XmlElementAttribute element = new XmlElementAttribute ();
1438 attr.XmlElements.Add (element);
1439 overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
1441 // empty collection & no type info in XmlElementAttribute
1442 StringCollectionContainer container = new StringCollectionContainer ();
1443 Serialize (container, overrides);
1444 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
1446 // non-empty collection & no type info in XmlElementAttribute
1447 container.Messages.Add ("hello");
1448 Serialize (container, overrides);
1449 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
1451 // non-empty collection & only type info in XmlElementAttribute
1452 element.Type = typeof (StringCollection);
1453 Serialize (container, overrides);
1454 Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
1456 // non-empty collection & only type info in XmlElementAttribute
1457 element.Type = typeof (string);
1458 Serialize (container, overrides);
1459 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");
1462 container.Messages.Add ("goodbye");
1463 element.Type = null;
1464 Serialize (container, overrides);
1465 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");
1468 // test DefaultValue /////////////////////////////////////////////////////
1470 public void TestSerializeDefaultValueAttribute ()
1472 XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
1474 XmlAttributes attr = new XmlAttributes ();
1475 string defaultValueInstance = "nothing";
1476 attr.XmlDefaultValue = defaultValueInstance;
1477 overrides.Add (typeof (SimpleClass), "something", attr);
1480 SimpleClass simple = new SimpleClass ();
1481 Serialize (simple, overrides);
1482 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
1484 // same value as default
1485 simple.something = defaultValueInstance;
1486 Serialize (simple, overrides);
1487 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
1490 simple.something = "hello";
1491 Serialize (simple, overrides);
1492 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");
1494 overrides = new XmlAttributeOverrides ();
1495 attr = new XmlAttributes ();
1496 attr.XmlAttribute = new XmlAttributeAttribute ();
1497 attr.XmlDefaultValue = defaultValueInstance;
1498 overrides.Add (typeof (SimpleClass), "something", attr);
1501 simple = new SimpleClass ();
1502 Serialize (simple, overrides);
1503 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
1505 // same value as default
1506 simple.something = defaultValueInstance;
1507 Serialize (simple, overrides);
1508 Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
1511 simple.something = "hello";
1512 Serialize (simple, overrides);
1513 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");
1515 overrides = new XmlAttributeOverrides ();
1516 attr = new XmlAttributes ();
1517 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1518 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1521 TestDefault testDefault = new TestDefault ();
1522 Serialize (testDefault);
1523 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");
1525 // use the default with overrides
1526 Serialize (testDefault, overrides);
1527 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");
1529 overrides = new XmlAttributeOverrides ();
1530 attr = new XmlAttributes ();
1531 attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
1532 attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
1533 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1535 // use the default with overrides
1536 Serialize (testDefault, overrides);
1537 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");
1539 // use the default with overrides and default namspace
1540 Serialize (testDefault, overrides, AnotherNamespace);
1541 Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
1543 // non-default values
1544 testDefault.strDefault = "Some Text";
1545 testDefault.boolT = false;
1546 testDefault.boolF = true;
1547 testDefault.decimalval = 20m;
1548 testDefault.flag = FlagEnum.e2;
1549 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1550 Serialize (testDefault);
1551 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1552 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1553 " <strDefault>Some Text</strDefault>" +
1554 " <boolT>false</boolT>" +
1555 " <boolF>true</boolF>" +
1556 " <decimalval>20</decimalval>" +
1557 " <flag>two</flag>" +
1558 " <flagencoded>e1 e2</flagencoded>" +
1559 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1562 Serialize (testDefault, overrides);
1563 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1564 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1565 " <strDefault>Some Text</strDefault>" +
1566 " <boolT>false</boolT>" +
1567 " <boolF>true</boolF>" +
1568 " <decimalval>20</decimalval>" +
1569 " <flag>two</flag>" +
1570 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1573 Serialize (testDefault, overrides, AnotherNamespace);
1574 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1575 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1576 " <strDefault>Some Text</strDefault>" +
1577 " <boolT>false</boolT>" +
1578 " <boolF>true</boolF>" +
1579 " <decimalval>20</decimalval>" +
1580 " <flag>two</flag>" +
1581 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1584 attr = new XmlAttributes ();
1585 XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
1586 xmlType.Namespace = "yetanother:urn";
1587 attr.XmlType = xmlType;
1588 overrides.Add (typeof (FlagEnum_Encoded), attr);
1590 Serialize (testDefault, overrides, AnotherNamespace);
1591 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1592 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1593 " <strDefault>Some Text</strDefault>" +
1594 " <boolT>false</boolT>" +
1595 " <boolF>true</boolF>" +
1596 " <decimalval>20</decimalval>" +
1597 " <flag>two</flag>" +
1598 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1601 attr = new XmlAttributes ();
1602 attr.XmlType = new XmlTypeAttribute ("testDefault");
1603 overrides.Add (typeof (TestDefault), attr);
1605 Serialize (testDefault, overrides, AnotherNamespace);
1606 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1607 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1608 " <strDefault>Some Text</strDefault>" +
1609 " <boolT>false</boolT>" +
1610 " <boolF>true</boolF>" +
1611 " <decimalval>20</decimalval>" +
1612 " <flag>two</flag>" +
1613 "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1614 AnotherNamespace)), WriterText, "#C9");
1618 [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
1619 public void TestSerializeDefaultValueAttribute_Encoded ()
1621 SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
1622 SoapAttributes attr = new SoapAttributes ();
1623 attr.SoapAttribute = new SoapAttributeAttribute ();
1624 string defaultValueInstance = "nothing";
1625 attr.SoapDefaultValue = defaultValueInstance;
1626 overrides.Add (typeof (SimpleClass), "something", attr);
1629 SimpleClass simple = new SimpleClass ();
1630 SerializeEncoded (simple, overrides);
1631 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");
1633 // same value as default
1634 simple.something = defaultValueInstance;
1635 SerializeEncoded (simple, overrides);
1636 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");
1639 simple.something = "hello";
1640 SerializeEncoded (simple, overrides);
1641 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");
1643 attr.SoapAttribute = null;
1644 attr.SoapElement = new SoapElementAttribute ();
1647 simple = new SimpleClass ();
1648 SerializeEncoded (simple, overrides);
1649 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");
1651 // same value as default
1652 simple.something = defaultValueInstance;
1653 SerializeEncoded (simple, overrides);
1654 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");
1657 simple.something = "hello";
1658 SerializeEncoded (simple, overrides);
1659 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");
1661 overrides = new SoapAttributeOverrides ();
1662 attr = new SoapAttributes ();
1663 attr.SoapElement = new SoapElementAttribute ("flagenc");
1664 overrides.Add (typeof (TestDefault), "flagencoded", attr);
1666 // use the default (from MS KB325691)
1667 TestDefault testDefault = new TestDefault ();
1668 SerializeEncoded (testDefault);
1669 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1670 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1671 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1672 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1673 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1674 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1675 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1676 " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1677 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1680 SerializeEncoded (testDefault, overrides);
1681 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1682 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1683 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1684 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1685 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1686 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1687 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1688 " <flagenc xsi:type='flagenum'>one four</flagenc>" +
1689 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1692 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1693 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1694 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1695 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1696 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1697 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1698 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1699 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1700 " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1701 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1702 AnotherNamespace)), WriterText, "#C3");
1704 // non-default values
1705 testDefault.strDefault = "Some Text";
1706 testDefault.boolT = false;
1707 testDefault.boolF = true;
1708 testDefault.decimalval = 20m;
1709 testDefault.flag = FlagEnum.e2;
1710 testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
1711 SerializeEncoded (testDefault);
1712 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1713 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1714 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1715 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1716 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1717 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1718 " <flag xsi:type='FlagEnum'>e2</flag>" +
1719 " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1720 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1723 SerializeEncoded (testDefault, overrides);
1724 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1725 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1726 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1727 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1728 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1729 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1730 " <flag xsi:type='FlagEnum'>e2</flag>" +
1731 " <flagenc xsi:type='flagenum'>one two</flagenc>" +
1732 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
1735 attr = new SoapAttributes ();
1736 attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
1737 overrides.Add (typeof (FlagEnum_Encoded), attr);
1739 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1740 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1741 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1742 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1743 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1744 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1745 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1746 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1747 " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1748 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1749 AnotherNamespace)), WriterText, "#C6");
1751 attr = new SoapAttributes ();
1752 attr.SoapType = new SoapTypeAttribute ("testDefault");
1753 overrides.Add (typeof (TestDefault), attr);
1755 SerializeEncoded (testDefault, overrides, AnotherNamespace);
1756 Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
1757 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1758 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1759 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1760 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1761 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1762 " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1763 " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1764 "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
1765 AnotherNamespace)), WriterText, "#C7");
1768 // test XmlEnum //////////////////////////////////////////////////////////
1770 public void TestSerializeXmlEnumAttribute ()
1772 Serialize (XmlSchemaForm.Qualified);
1773 Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
1775 Serialize (XmlSchemaForm.Unqualified);
1776 Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
1780 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1782 // technically XmlSchemaForm.None has an XmlIgnore attribute,
1783 // but it is not being serialized as a member.
1787 Serialize (XmlSchemaForm.None);
1789 } catch (InvalidOperationException ex) {
1790 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1791 Assert.IsNotNull (ex.InnerException, "#3");
1792 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
1793 Assert.IsNotNull (ex.InnerException.Message, "#5");
1794 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
1795 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
1798 Serialize (XmlSchemaForm.None);
1799 Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
1804 public void TestSerializeXmlNodeArray ()
1806 XmlDocument doc = new XmlDocument ();
1807 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1808 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1812 public void TestSerializeXmlNodeArray2 ()
1814 XmlDocument doc = new XmlDocument ();
1815 Serialize (new XmlNode[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1816 Assert.AreEqual (Infoset (String.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
1820 [ExpectedException (typeof (InvalidOperationException))]
1821 [Category ("NotWorking")]
1822 public void TestSerializeXmlNodeArrayIncludesAttribute ()
1824 XmlDocument doc = new XmlDocument ();
1825 Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
1829 public void TestSerializeXmlElementArray ()
1831 XmlDocument doc = new XmlDocument ();
1832 Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
1833 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1838 [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
1839 public void TestSerializeGenericListOfNode ()
1841 XmlDocument doc = new XmlDocument ();
1842 Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1843 Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
1847 [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
1848 public void TestSerializeGenericListOfElement ()
1850 XmlDocument doc = new XmlDocument ();
1851 Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
1852 Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
1856 public void TestSerializeXmlDocument ()
1858 XmlDocument doc = new XmlDocument ();
1859 doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
1860 Serialize (doc, typeof (XmlDocument));
1861 Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
1862 sw.GetStringBuilder ().ToString ());
1866 public void TestSerializeXmlElement ()
1868 XmlDocument doc = new XmlDocument ();
1869 Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
1870 Assert.AreEqual (Infoset ("<elem/>"), WriterText);
1874 public void TestSerializeXmlElementSubclass ()
1876 XmlDocument doc = new XmlDocument ();
1877 Serialize (new MyElem (doc), typeof (XmlElement));
1878 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
1880 Serialize (new MyElem (doc), typeof (MyElem));
1881 Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
1885 public void TestSerializeXmlCDataSection ()
1887 XmlDocument doc = new XmlDocument ();
1888 CDataContainer c = new CDataContainer ();
1889 c.cdata = doc.CreateCDataSection ("data section contents");
1891 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);
1895 public void TestSerializeXmlNode ()
1897 XmlDocument doc = new XmlDocument ();
1898 NodeContainer c = new NodeContainer ();
1899 c.node = doc.CreateTextNode ("text");
1901 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);
1905 public void TestSerializeChoice ()
1907 Choices ch = new Choices ();
1908 ch.MyChoice = "choice text";
1909 ch.ItemType = ItemChoiceType.ChoiceZero;
1911 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");
1912 ch.ItemType = ItemChoiceType.StrangeOne;
1914 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");
1915 ch.ItemType = ItemChoiceType.ChoiceTwo;
1917 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");
1921 public void TestSerializeNamesWithSpaces ()
1923 TestSpace ts = new TestSpace ();
1927 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);
1931 public void TestSerializeReadOnlyProps ()
1933 ReadOnlyProperties ts = new ReadOnlyProperties ();
1935 Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
1939 public void TestSerializeReadOnlyListProp ()
1941 ReadOnlyListProperty ts = new ReadOnlyListProperty ();
1943 Assert.AreEqual (Infoset ("<ReadOnlyListProperty xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><StrList><string>listString1</string><string>listString2</string></StrList></ReadOnlyListProperty>"), WriterText);
1948 public void TestSerializeIList ()
1950 clsPerson k = new clsPerson ();
1951 k.EmailAccounts = new ArrayList ();
1952 k.EmailAccounts.Add ("a");
1953 k.EmailAccounts.Add ("b");
1955 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);
1959 public void TestSerializeArrayEnc ()
1961 SoapReflectionImporter imp = new SoapReflectionImporter ();
1962 XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1963 XmlSerializer ser = new XmlSerializer (map);
1964 StringWriter sw = new StringWriter ();
1965 XmlTextWriter tw = new XmlTextWriter (sw);
1966 tw.WriteStartElement ("aa");
1967 ser.Serialize (tw, new ArrayClass ());
1968 tw.WriteEndElement ();
1971 [Test] // bug #76049
1972 public void TestIncludeType ()
1974 XmlReflectionImporter imp = new XmlReflectionImporter ();
1975 XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1976 imp.IncludeType (typeof (TestSpace));
1977 XmlSerializer ser = new XmlSerializer (map);
1978 ser.Serialize (new StringWriter (), new TestSpace ());
1982 public void TestSerializeChoiceArray ()
1984 CompositeValueType v = new CompositeValueType ();
1987 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);
1991 public void TestArrayAttributeWithDataType ()
1993 Serialize (new ArrayAttributeWithType ());
1994 string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1995 res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1996 Assert.AreEqual (Infoset (res), WriterText);
2000 public void TestSubclassElementType ()
2002 SubclassTestContainer c = new SubclassTestContainer ();
2003 c.data = new SubclassTestSub ();
2006 string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
2007 res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
2008 Assert.AreEqual (Infoset (res), WriterText);
2012 [ExpectedException (typeof (InvalidOperationException))]
2013 public void TestArrayAttributeWithWrongDataType ()
2015 Serialize (new ArrayAttributeWithWrongType ());
2019 [Category ("NotWorking")]
2020 public void TestSerializePrimitiveTypesContainer ()
2022 Serialize (new PrimitiveTypesContainer ());
2023 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2024 "<?xml version='1.0' encoding='utf-16'?>" +
2026 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
2028 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
2030 "<Number>2004</Number>" +
2031 "<Name>some name</Name>" +
2032 "<Index>56</Index>" +
2033 "<Password>8w8=</Password>" +
2034 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2035 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2036 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2038 SerializeEncoded (new PrimitiveTypesContainer ());
2039 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2040 "<?xml version='1.0' encoding='utf-16'?>" +
2042 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2044 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2046 "<Number xsi:type='xsd:int'>2004</Number>" +
2047 "<Name xsi:type='xsd:string'>some name</Name>" +
2048 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2049 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2050 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2051 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2052 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2053 sw.ToString (), "#2");
2057 public void TestSchemaForm ()
2059 TestSchemaForm1 t1 = new TestSchemaForm1 ();
2060 t1.p1 = new PrintTypeResponse ();
2062 t1.p2 = new PrintTypeResponse ();
2065 TestSchemaForm2 t2 = new TestSchemaForm2 ();
2066 t2.p1 = new PrintTypeResponse ();
2068 t2.p2 = new PrintTypeResponse ();
2073 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2074 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
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 += " <p2 xmlns=\"urn:oo\">";
2086 res += " <result xmlns=\"\">";
2087 res += " <data>data1</data>";
2088 res += " </result>";
2089 res += " <intern xmlns=\"urn:responseTypes\">";
2090 res += " <result xmlns=\"\">";
2091 res += " <data>data2</data>";
2092 res += " </result>";
2093 res += " </intern>";
2095 res += "</TestSchemaForm1>";
2096 Assert.AreEqual (Infoset (res), WriterText);
2100 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2101 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2102 res += " <p1 xmlns=\"urn:testForm\">";
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 += " <p2 xmlns=\"urn:oo\">";
2113 res += " <result xmlns=\"\">";
2114 res += " <data>data1</data>";
2115 res += " </result>";
2116 res += " <intern xmlns=\"urn:responseTypes\">";
2117 res += " <result xmlns=\"\">";
2118 res += " <data>data2</data>";
2119 res += " </result>";
2120 res += " </intern>";
2122 res += "</TestSchemaForm2>";
2123 Assert.AreEqual (Infoset (res), WriterText);
2125 XmlReflectionImporter imp = new XmlReflectionImporter ();
2126 XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2127 Serialize (t1, map);
2129 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2130 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
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 += " <p2 xmlns=\"urn:oo\">";
2142 res += " <result xmlns=\"\">";
2143 res += " <data>data1</data>";
2144 res += " </result>";
2145 res += " <intern xmlns=\"urn:responseTypes\">";
2146 res += " <result xmlns=\"\">";
2147 res += " <data>data2</data>";
2148 res += " </result>";
2149 res += " </intern>";
2151 res += "</TestSchemaForm1>";
2152 Assert.AreEqual (Infoset (res), WriterText);
2154 imp = new XmlReflectionImporter ();
2155 map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2156 Serialize (t2, map);
2158 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2159 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2160 res += " <p1 xmlns=\"urn:testForm\">";
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 += " <p2 xmlns=\"urn:oo\">";
2171 res += " <result xmlns=\"\">";
2172 res += " <data>data1</data>";
2173 res += " </result>";
2174 res += " <intern xmlns=\"urn:responseTypes\">";
2175 res += " <result xmlns=\"\">";
2176 res += " <data>data2</data>";
2177 res += " </result>";
2178 res += " </intern>";
2180 res += "</TestSchemaForm2>";
2181 Assert.AreEqual (Infoset (res), WriterText);
2184 [Test] // bug #78536
2185 public void CDataTextNodes ()
2187 XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2188 ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2189 string xml = @"<CDataTextNodesType>
2191 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2192 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2193 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2195 </CDataTextNodesType>";
2196 ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2199 #if !TARGET_JVM && !MOBILE
2201 public void GenerateSerializerGenerics ()
2203 XmlReflectionImporter imp = new XmlReflectionImporter ();
2204 Type type = typeof (List<int>);
2205 XmlSerializer.GenerateSerializer (
2207 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2212 public void Nullable ()
2214 XmlSerializer ser = new XmlSerializer (typeof (int?));
2215 int? nullableType = 5;
2216 sw = new StringWriter ();
2217 xtw = new XmlTextWriter (sw);
2218 ser.Serialize (xtw, nullableType);
2220 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2221 Assert.AreEqual (Infoset (expected), WriterText);
2222 int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2223 Assert.AreEqual (5, i);
2227 public void NullableEnums ()
2229 WithNulls w = new WithNulls ();
2230 XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2231 StringWriter tw = new StringWriter ();
2232 ser.Serialize (tw, w);
2234 string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2235 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2236 "<nint xsi:nil='true' />" +
2237 "<nenum xsi:nil='true' />" +
2238 "<ndate xsi:nil='true' />" +
2241 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2243 StringReader sr = new StringReader (tw.ToString ());
2244 w = (WithNulls) ser.Deserialize (sr);
2246 Assert.IsFalse (w.nint.HasValue);
2247 Assert.IsFalse (w.nenum.HasValue);
2248 Assert.IsFalse (w.ndate.HasValue);
2250 DateTime t = new DateTime (2008,4,1);
2253 w.nenum = TestEnumWithNulls.bb;
2255 tw = new StringWriter ();
2256 ser.Serialize (tw, w);
2258 expected = "<?xml version='1.0' encoding='utf-16'?>" +
2259 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2261 "<nenum>bb</nenum>" +
2262 "<ndate>2008-04-01T00:00:00</ndate>" +
2265 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2267 sr = new StringReader (tw.ToString ());
2268 w = (WithNulls) ser.Deserialize (sr);
2270 Assert.IsTrue (w.nint.HasValue);
2271 Assert.IsTrue (w.nenum.HasValue);
2272 Assert.IsTrue (w.ndate.HasValue);
2273 Assert.AreEqual (4, w.nint.Value);
2274 Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2275 Assert.AreEqual (t, w.ndate.Value);
2279 public void SerializeBase64Binary()
2281 XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2282 sw = new StringWriter ();
2283 XmlTextWriter xtw = new XmlTextWriter (sw);
2284 ser.Serialize (xtw, new Base64Binary ());
2286 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"" />";
2287 Assert.AreEqual (Infoset (expected), WriterText);
2288 Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2289 Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2292 [Test] // bug #79989, #79990
2293 public void SerializeHexBinary ()
2295 XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2296 sw = new StringWriter ();
2297 XmlTextWriter xtw = new XmlTextWriter (sw);
2298 ser.Serialize (xtw, new HexBinary ());
2300 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"" />";
2301 Assert.AreEqual (Infoset (expected), WriterText);
2302 HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2303 Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2307 [ExpectedException (typeof (InvalidOperationException))]
2308 public void XmlArrayAttributeOnInt ()
2310 new XmlSerializer (typeof (XmlArrayOnInt));
2314 [ExpectedException (typeof (InvalidOperationException))]
2315 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2317 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2321 [ExpectedException (typeof (InvalidOperationException))]
2322 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2324 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2327 [Test] // bug #78042
2328 public void XmlArrayAttributeOnArray ()
2330 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2331 sw = new StringWriter ();
2332 XmlTextWriter xtw = new XmlTextWriter (sw);
2333 ser.Serialize (xtw, new XmlArrayOnArray ());
2335 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>";
2336 Assert.AreEqual (Infoset (expected), WriterText);
2340 public void XmlArrayAttributeOnCollection ()
2342 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2343 XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2344 inst.Sane.Add ("abc");
2346 sw = new StringWriter ();
2347 XmlTextWriter xtw = new XmlTextWriter (sw);
2348 ser.Serialize (xtw, inst);
2350 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>";
2351 Assert.AreEqual (Infoset (expected), WriterText);
2354 [Test] // bug #338705
2355 public void SerializeTimeSpan ()
2357 // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2358 XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2359 ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2363 public void SerializeDurationToString ()
2365 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2366 ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2370 [ExpectedException (typeof (InvalidOperationException))]
2371 public void SerializeDurationToTimeSpan ()
2373 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2374 ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2378 [ExpectedException (typeof (InvalidOperationException))]
2379 public void SerializeInvalidDataType ()
2381 XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2382 ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2387 [ExpectedException (typeof (ApplicationException))]
2389 public void SerializeErrorneousIXmlSerializable ()
2391 Serialize (new ErrorneousGetSchema ());
2392 Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2396 public void DateTimeRoundtrip ()
2399 XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2400 StringWriter sw = new StringWriter ();
2401 ser.Serialize (sw, DateTime.UtcNow);
2402 DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2403 Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2408 public void SupportIXmlSerializableImplicitlyConvertible ()
2410 XmlAttributes attrs = new XmlAttributes ();
2411 XmlElementAttribute attr = new XmlElementAttribute ();
2412 attr.ElementName = "XmlSerializable";
2413 attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2414 attrs.XmlElements.Add (attr);
2415 XmlAttributeOverrides attrOverrides = new
2416 XmlAttributeOverrides ();
2417 attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2419 XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2420 new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2423 [Test] // bug #566370
2424 public void SerializeEnumWithCSharpKeyword ()
2426 var ser = new XmlSerializer (typeof (DoxCompoundKind));
2427 for (int i = 0; i < 100; i++) // test serialization code generator
2428 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2431 public enum DoxCompoundKind
2438 [XmlEnum("interface")]
2444 [XmlEnum("namespace")]
2452 #region GenericsSeralizationTests
2456 public void TestSerializeGenSimpleClassString ()
2458 GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2460 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2462 simple.something = "hello";
2465 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);
2469 public void TestSerializeGenSimpleClassBool ()
2471 GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2473 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);
2475 simple.something = true;
2478 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);
2482 public void TestSerializeGenSimpleStructInt ()
2484 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2486 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);
2488 simple.something = 123;
2491 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);
2495 public void TestSerializeGenListClassString ()
2497 GenListClass<string> genlist = new GenListClass<string> ();
2498 Serialize (genlist);
2499 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);
2501 genlist.somelist.Add ("Value1");
2502 genlist.somelist.Add ("Value2");
2504 Serialize (genlist);
2505 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);
2509 public void TestSerializeGenListClassFloat ()
2511 GenListClass<float> genlist = new GenListClass<float> ();
2512 Serialize (genlist);
2513 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);
2515 genlist.somelist.Add (1);
2516 genlist.somelist.Add (2.2F);
2518 Serialize (genlist);
2519 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);
2523 public void TestSerializeGenListClassList ()
2525 GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2526 Serialize (genlist);
2527 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);
2529 GenListClass<int> inlist1 = new GenListClass<int> ();
2530 inlist1.somelist.Add (1);
2531 inlist1.somelist.Add (2);
2532 GenListClass<int> inlist2 = new GenListClass<int> ();
2533 inlist2.somelist.Add (10);
2534 inlist2.somelist.Add (20);
2535 genlist.somelist.Add (inlist1);
2536 genlist.somelist.Add (inlist2);
2538 Serialize (genlist);
2539 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);
2543 public void TestSerializeGenListClassArray ()
2545 GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2546 Serialize (genlist);
2547 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);
2549 GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2550 genarr1.arr[0] = 'a';
2551 genarr1.arr[1] = 'b';
2552 genlist.somelist.Add (genarr1);
2553 GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2554 genarr2.arr[0] = 'd';
2555 genarr2.arr[1] = 'e';
2556 genarr2.arr[2] = 'f';
2557 genlist.somelist.Add (genarr2);
2559 Serialize (genlist);
2560 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);
2564 public void TestSerializeGenTwoClassCharDouble ()
2566 GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2568 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);
2570 gentwo.something1 = 'a';
2571 gentwo.something2 = 2.2;
2574 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);
2578 public void TestSerializeGenDerivedClassDecimalShort ()
2580 GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
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'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
2584 derived.something1 = "Value1";
2585 derived.something2 = 1;
2586 derived.another1 = 1.1M;
2587 derived.another2 = -22;
2589 Serialize (derived);
2590 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);
2594 public void TestSerializeGenDerivedSecondClassByteUlong ()
2596 GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
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>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
2600 derived2.something1 = 1;
2601 derived2.something2 = 222;
2602 derived2.another1 = 111;
2603 derived2.another2 = 222222;
2605 Serialize (derived2);
2606 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);
2610 public void TestSerializeGenNestedClass ()
2612 GenNestedClass<string, int>.InnerClass<bool> nested =
2613 new GenNestedClass<string, int>.InnerClass<bool> ();
2615 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);
2618 nested.something = true;
2621 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);
2625 public void TestSerializeGenListClassListNested ()
2627 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2628 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2629 Serialize (genlist);
2630 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);
2632 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2633 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2634 GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2636 inval1.something = "ONE";
2637 inlist1.somelist.Add (inval1);
2638 GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2640 inval2.something = "TWO";
2641 inlist1.somelist.Add (inval2);
2642 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2643 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2644 GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2646 inval3.something = "THIRTY";
2647 inlist2.somelist.Add (inval3);
2648 genlist.somelist.Add (inlist1);
2649 genlist.somelist.Add (inlist2);
2651 Serialize (genlist);
2652 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);
2655 public enum Myenum { one, two, three, four, five, six };
2657 public void TestSerializeGenArrayClassEnum ()
2659 GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2661 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);
2663 genarr.arr[0] = Myenum.one;
2664 genarr.arr[1] = Myenum.three;
2665 genarr.arr[2] = Myenum.five;
2668 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);
2672 public void TestSerializeGenArrayStruct ()
2674 GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2676 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);
2678 GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2679 genstruct.something = 111;
2680 genarr.arr[0] = genstruct;
2681 genstruct.something = 222;
2682 genarr.arr[1] = genstruct;
2683 genstruct.something = 333;
2684 genarr.arr[2] = genstruct;
2687 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);
2691 public void TestSerializeGenArrayList ()
2693 GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2695 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);
2697 GenListClass<string> genlist1 = new GenListClass<string> ();
2698 genlist1.somelist.Add ("list1-val1");
2699 genlist1.somelist.Add ("list1-val2");
2700 genarr.arr[0] = genlist1;
2701 GenListClass<string> genlist2 = new GenListClass<string> ();
2702 genlist2.somelist.Add ("list2-val1");
2703 genlist2.somelist.Add ("list2-val2");
2704 genlist2.somelist.Add ("list2-val3");
2705 genlist2.somelist.Add ("list2-val4");
2706 genarr.arr[1] = genlist2;
2707 GenListClass<string> genlist3 = new GenListClass<string> ();
2708 genlist3.somelist.Add ("list3val");
2709 genarr.arr[2] = genlist3;
2712 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);
2716 public void TestSerializeGenComplexStruct ()
2718 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2719 Serialize (complex);
2720 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);
2722 complex.something = 123;
2723 complex.simpleclass.something = 456;
2724 complex.simplestruct.something = 789;
2725 GenListClass<int> genlist = new GenListClass<int> ();
2726 genlist.somelist.Add (100);
2727 genlist.somelist.Add (200);
2728 complex.listclass = genlist;
2729 GenArrayClass<int> genarr = new GenArrayClass<int> ();
2733 complex.arrayclass = genarr;
2734 complex.twoclass.something1 = 10;
2735 complex.twoclass.something2 = "Ten";
2736 complex.derivedclass.another1 = 1;
2737 complex.derivedclass.another2 = "one";
2738 complex.derivedclass.something1 = "two";
2739 complex.derivedclass.something2 = 2;
2740 complex.derived2.another1 = 3;
2741 complex.derived2.another2 = "three";
2742 complex.derived2.something1 = 4;
2743 complex.derived2.something2 = "four";
2744 complex.nestedouter.outer = 5;
2745 complex.nestedinner.inner = "six";
2746 complex.nestedinner.something = 6;
2748 Serialize (complex);
2749 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);
2752 [Test] // bug #80759
2753 public void HasNullableField ()
2755 Bug80759 foo = new Bug80759 ();
2757 foo.NullableInt = 10;
2759 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2761 MemoryStream stream = new MemoryStream ();
2763 serializer.Serialize (stream, foo);
2764 stream.Position = 0;
2765 foo = (Bug80759) serializer.Deserialize (stream);
2768 [Test] // bug #80759, with fieldSpecified.
2769 [ExpectedException (typeof (InvalidOperationException))]
2770 [Category ("NotWorking")]
2771 public void HasFieldSpecifiedButIrrelevant ()
2773 Bug80759_2 foo = new Bug80759_2 ();
2775 foo.NullableInt = 10;
2777 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2779 MemoryStream stream = new MemoryStream ();
2781 serializer.Serialize (stream, foo);
2782 stream.Position = 0;
2783 foo = (Bug80759_2) serializer.Deserialize (stream);
2787 public void HasNullableField2 ()
2789 Bug80759 foo = new Bug80759 ();
2791 foo.NullableInt = 10;
2793 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2795 MemoryStream stream = new MemoryStream ();
2797 serializer.Serialize (stream, foo);
2798 stream.Position = 0;
2799 foo = (Bug80759) serializer.Deserialize (stream);
2801 Assert.AreEqual ("BAR", foo.Test, "#1");
2802 Assert.AreEqual (10, foo.NullableInt, "#2");
2804 foo.NullableInt = null;
2805 stream = new MemoryStream ();
2806 serializer.Serialize (stream, foo);
2807 stream.Position = 0;
2808 foo = (Bug80759) serializer.Deserialize (stream);
2810 Assert.AreEqual ("BAR", foo.Test, "#3");
2811 Assert.IsNull (foo.NullableInt, "#4");
2815 public void SupportPrivateCtorOnly ()
2818 new XmlSerializer (typeof (PrivateCtorOnly));
2819 StringWriter sw = new StringWriter ();
2820 xs.Serialize (sw, PrivateCtorOnly.Instance);
2821 xs.Deserialize (new StringReader (sw.ToString ()));
2825 public void XmlSchemaProviderQNameBecomesRootName ()
2827 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2828 Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2829 Assert.AreEqual (Infoset ("<foo />"), WriterText);
2830 xs.Deserialize (new StringReader ("<foo/>"));
2834 public void XmlSchemaProviderQNameBecomesRootName2 ()
2836 string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2837 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2838 Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2839 Assert.AreEqual (Infoset (xml), WriterText);
2840 xs.Deserialize (new StringReader (xml));
2844 public void XmlAnyElementForObjects () // bug #553032
2846 new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2850 [ExpectedException (typeof (InvalidOperationException))]
2851 public void XmlAnyElementForObjects2 () // bug #553032-2
2853 new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2857 public class Bug2893 {
2860 Contents = new XmlDataDocument();
2863 [XmlAnyElement("Contents")]
2864 public XmlNode Contents;
2867 // Bug Xamarin #2893
2869 public void XmlAnyElementForXmlNode ()
2871 var obj = new Bug2893 ();
2872 XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
2873 XmlWriterSettings settings = new XmlWriterSettings();
2875 var xsn = new XmlSerializerNamespaces();
2876 xsn.Add(string.Empty, string.Empty);
2878 byte[] buffer = new byte[2048];
2879 var ms = new MemoryStream(buffer);
2880 using (var xw = XmlWriter.Create(ms, settings))
2882 mySerializer.Serialize(xw, obj, xsn);
2886 mySerializer.Serialize(ms, obj);
2890 public void XmlRootOverridesSchemaProviderQName ()
2892 var obj = new XmlRootOverridesSchemaProviderQNameType ();
2894 XmlSerializer xs = new XmlSerializer (obj.GetType ());
2896 var sw = new StringWriter ();
2897 using (XmlWriter xw = XmlWriter.Create (sw))
2898 xs.Serialize (xw, obj);
2899 Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2902 public class AnotherArrayListType
2905 public string one = "aaa";
2907 public string another = "bbb";
2910 public class DerivedArrayListType : AnotherArrayListType
2915 public class ClassWithArrayList
2917 [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2918 [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2919 [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2920 [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2921 public ArrayList list;
2924 public class ClassWithArray
2926 [XmlElement (Type = typeof(int), ElementName = "int_elem")]
2927 [XmlElement (Type = typeof(string), ElementName = "string_elem")]
2928 [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
2929 [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
2930 public object[] list;
2935 public void MultipleXmlElementAttributesOnArrayList()
2937 var test = new ClassWithArrayList();
2939 test.list = new ArrayList();
2941 test.list.Add("apepe");
2942 test.list.Add(new AnotherArrayListType());
2943 test.list.Add(new DerivedArrayListType());
2946 var expected_text = "<:ClassWithArrayList http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2948 Assert.AreEqual(WriterText, expected_text, WriterText);
2952 public void MultipleXmlElementAttributesOnArray()
2954 var test = new ClassWithArray();
2956 test.list = new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() };
2959 var expected_text = "<:ClassWithArray http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2961 Assert.AreEqual(WriterText, expected_text, WriterText);
2966 #endregion //GenericsSeralizationTests
2968 public class XmlArrayOnInt
2974 public class XmlArrayUnqualifiedWithNamespace
2976 [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2977 public ArrayList Sane = new ArrayList ();
2980 public class XmlArrayItemUnqualifiedWithNamespace
2982 [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2983 public ArrayList Sane = new ArrayList ();
2986 [XmlRoot (Namespace = "urn:foo")]
2987 public class XmlArrayOnArrayList
2989 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2990 public ArrayList Sane = new ArrayList ();
2993 [XmlRoot (Namespace = "urn:foo")]
2994 public class XmlArrayOnArray
2996 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2997 public string[] Sane = new string[] { "foo", "bar" };
2999 [XmlArray (Form = XmlSchemaForm.Unqualified)]
3000 public ArrayItemInXmlArray[] Mids =
3001 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
3004 [XmlType (Namespace = "urn:gyabo")]
3005 public class ArrayItemInXmlArray
3007 [XmlArray (Form = XmlSchemaForm.Unqualified)]
3008 public string[] Whee = new string[] { "foo", "bar" };
3011 [XmlRoot ("Base64Binary")]
3012 public class Base64Binary
3014 [XmlAttribute (DataType = "base64Binary")]
3015 public byte [] Data = new byte [] {1, 2, 3};
3018 [XmlRoot ("HexBinary")]
3019 public class HexBinary
3021 [XmlAttribute (DataType = "hexBinary")]
3022 public byte[] Data = new byte[] { 1, 2, 3 };
3025 [XmlRoot ("PrivateCtorOnly")]
3026 public class PrivateCtorOnly
3028 public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
3029 private PrivateCtorOnly ()
3034 public class CDataTextNodesType
3036 public CDataTextNodesInternal foo;
3039 public class CDataTextNodesInternal
3042 public string Value;
3045 public class InvalidTypeContainer
3047 [XmlElement (DataType = "invalid")]
3048 public string InvalidTypeItem = "aaa";
3051 public class TimeSpanContainer1
3053 [XmlElement (DataType = "duration")]
3054 public string StringDuration = "aaa";
3057 public class TimeSpanContainer2
3059 [XmlElement (DataType = "duration")]
3060 public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
3064 public class Bug80759
3067 public int? NullableInt;
3070 public class Bug80759_2
3073 public int? NullableInt;
3076 public bool NullableIntSpecified {
3077 get { return NullableInt.HasValue; }
3081 [XmlSchemaProvider ("GetXsdType")]
3082 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
3084 public XmlSchema GetSchema ()
3089 public void ReadXml (XmlReader reader)
3094 public void WriteXml (XmlWriter writer)
3098 public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
3100 if (xss.Count == 0) {
3101 XmlSchema xs = new XmlSchema ();
3102 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3107 return new XmlQualifiedName ("foo");
3111 public class XmlSchemaProviderQNameBecomesRootNameType2
3113 [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3114 public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3117 public class XmlAnyElementForObjectsType
3120 public object [] arr = new object [] {3,4,5};
3124 [XmlSchemaProvider ("GetSchema")]
3125 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3127 public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3129 var xs = new XmlSchema ();
3130 var xse = new XmlSchemaComplexType () { Name = "bar" };
3133 return new XmlQualifiedName ("bar");
3136 XmlSchema IXmlSerializable.GetSchema ()
3141 void IXmlSerializable.ReadXml (XmlReader reader)
3144 void IXmlSerializable.WriteXml (XmlWriter writer)
3151 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3158 public static string Infoset (string sx)
3160 XmlDocument doc = new XmlDocument ();
3162 StringBuilder sb = new StringBuilder ();
3163 GetInfoset (doc.DocumentElement, sb);
3164 return sb.ToString ();
3167 public static string Infoset (XmlNode nod)
3169 StringBuilder sb = new StringBuilder ();
3170 GetInfoset (nod, sb);
3171 return sb.ToString ();
3174 static void GetInfoset (XmlNode nod, StringBuilder sb)
3176 switch (nod.NodeType) {
3177 case XmlNodeType.Attribute:
3178 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3179 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3182 case XmlNodeType.Element:
3183 XmlElement elem = (XmlElement) nod;
3184 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3186 ArrayList ats = new ArrayList ();
3187 foreach (XmlAttribute at in elem.Attributes)
3188 ats.Add (at.LocalName + " " + at.NamespaceURI);
3192 foreach (string name in ats) {
3193 string[] nn = name.Split (' ');
3194 GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3198 foreach (XmlNode cn in elem.ChildNodes)
3199 GetInfoset (cn, sb);
3204 sb.Append (nod.OuterXml);
3209 static XmlTypeMapping CreateSoapMapping (Type type)
3211 SoapReflectionImporter importer = new SoapReflectionImporter ();
3212 return importer.ImportTypeMapping (type);
3215 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3217 SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3218 return importer.ImportTypeMapping (type);
3221 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3223 SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3224 return importer.ImportTypeMapping (type);