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 TestSerializeIList ()
1941 clsPerson k = new clsPerson ();
1942 k.EmailAccounts = new ArrayList ();
1943 k.EmailAccounts.Add ("a");
1944 k.EmailAccounts.Add ("b");
1946 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);
1950 public void TestSerializeArrayEnc ()
1952 SoapReflectionImporter imp = new SoapReflectionImporter ();
1953 XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
1954 XmlSerializer ser = new XmlSerializer (map);
1955 StringWriter sw = new StringWriter ();
1956 XmlTextWriter tw = new XmlTextWriter (sw);
1957 tw.WriteStartElement ("aa");
1958 ser.Serialize (tw, new ArrayClass ());
1959 tw.WriteEndElement ();
1962 [Test] // bug #76049
1963 public void TestIncludeType ()
1965 XmlReflectionImporter imp = new XmlReflectionImporter ();
1966 XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
1967 imp.IncludeType (typeof (TestSpace));
1968 XmlSerializer ser = new XmlSerializer (map);
1969 ser.Serialize (new StringWriter (), new TestSpace ());
1973 public void TestSerializeChoiceArray ()
1975 CompositeValueType v = new CompositeValueType ();
1978 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);
1982 public void TestArrayAttributeWithDataType ()
1984 Serialize (new ArrayAttributeWithType ());
1985 string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1986 res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1987 Assert.AreEqual (Infoset (res), WriterText);
1991 public void TestSubclassElementType ()
1993 SubclassTestContainer c = new SubclassTestContainer ();
1994 c.data = new SubclassTestSub ();
1997 string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1998 res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1999 Assert.AreEqual (Infoset (res), WriterText);
2003 [ExpectedException (typeof (InvalidOperationException))]
2004 public void TestArrayAttributeWithWrongDataType ()
2006 Serialize (new ArrayAttributeWithWrongType ());
2010 [Category ("NotWorking")]
2011 public void TestSerializePrimitiveTypesContainer ()
2013 Serialize (new PrimitiveTypesContainer ());
2014 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2015 "<?xml version='1.0' encoding='utf-16'?>" +
2017 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
2019 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
2021 "<Number>2004</Number>" +
2022 "<Name>some name</Name>" +
2023 "<Index>56</Index>" +
2024 "<Password>8w8=</Password>" +
2025 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
2026 "</PrimitiveTypesContainer>", XmlSchema.Namespace,
2027 XmlSchema.InstanceNamespace), sw.ToString (), "#1");
2029 SerializeEncoded (new PrimitiveTypesContainer ());
2030 Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
2031 "<?xml version='1.0' encoding='utf-16'?>" +
2033 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
2035 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
2037 "<Number xsi:type='xsd:int'>2004</Number>" +
2038 "<Name xsi:type='xsd:string'>some name</Name>" +
2039 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
2040 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
2041 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
2042 "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
2043 XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
2044 sw.ToString (), "#2");
2048 public void TestSchemaForm ()
2050 TestSchemaForm1 t1 = new TestSchemaForm1 ();
2051 t1.p1 = new PrintTypeResponse ();
2053 t1.p2 = new PrintTypeResponse ();
2056 TestSchemaForm2 t2 = new TestSchemaForm2 ();
2057 t2.p1 = new PrintTypeResponse ();
2059 t2.p2 = new PrintTypeResponse ();
2064 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2065 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2068 res += " <data>data1</data>";
2069 res += " </result>";
2070 res += " <intern xmlns=\"urn:responseTypes\">";
2071 res += " <result xmlns=\"\">";
2072 res += " <data>data2</data>";
2073 res += " </result>";
2074 res += " </intern>";
2076 res += " <p2 xmlns=\"urn:oo\">";
2077 res += " <result xmlns=\"\">";
2078 res += " <data>data1</data>";
2079 res += " </result>";
2080 res += " <intern xmlns=\"urn:responseTypes\">";
2081 res += " <result xmlns=\"\">";
2082 res += " <data>data2</data>";
2083 res += " </result>";
2084 res += " </intern>";
2086 res += "</TestSchemaForm1>";
2087 Assert.AreEqual (Infoset (res), WriterText);
2091 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2092 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2093 res += " <p1 xmlns=\"urn:testForm\">";
2094 res += " <result xmlns=\"\">";
2095 res += " <data>data1</data>";
2096 res += " </result>";
2097 res += " <intern xmlns=\"urn:responseTypes\">";
2098 res += " <result xmlns=\"\">";
2099 res += " <data>data2</data>";
2100 res += " </result>";
2101 res += " </intern>";
2103 res += " <p2 xmlns=\"urn:oo\">";
2104 res += " <result xmlns=\"\">";
2105 res += " <data>data1</data>";
2106 res += " </result>";
2107 res += " <intern xmlns=\"urn:responseTypes\">";
2108 res += " <result xmlns=\"\">";
2109 res += " <data>data2</data>";
2110 res += " </result>";
2111 res += " </intern>";
2113 res += "</TestSchemaForm2>";
2114 Assert.AreEqual (Infoset (res), WriterText);
2116 XmlReflectionImporter imp = new XmlReflectionImporter ();
2117 XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
2118 Serialize (t1, map);
2120 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2121 res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2123 res += " <result xmlns=\"\">";
2124 res += " <data>data1</data>";
2125 res += " </result>";
2126 res += " <intern xmlns=\"urn:responseTypes\">";
2127 res += " <result xmlns=\"\">";
2128 res += " <data>data2</data>";
2129 res += " </result>";
2130 res += " </intern>";
2132 res += " <p2 xmlns=\"urn:oo\">";
2133 res += " <result xmlns=\"\">";
2134 res += " <data>data1</data>";
2135 res += " </result>";
2136 res += " <intern xmlns=\"urn:responseTypes\">";
2137 res += " <result xmlns=\"\">";
2138 res += " <data>data2</data>";
2139 res += " </result>";
2140 res += " </intern>";
2142 res += "</TestSchemaForm1>";
2143 Assert.AreEqual (Infoset (res), WriterText);
2145 imp = new XmlReflectionImporter ();
2146 map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
2147 Serialize (t2, map);
2149 res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2150 res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2151 res += " <p1 xmlns=\"urn:testForm\">";
2152 res += " <result xmlns=\"\">";
2153 res += " <data>data1</data>";
2154 res += " </result>";
2155 res += " <intern xmlns=\"urn:responseTypes\">";
2156 res += " <result xmlns=\"\">";
2157 res += " <data>data2</data>";
2158 res += " </result>";
2159 res += " </intern>";
2161 res += " <p2 xmlns=\"urn:oo\">";
2162 res += " <result xmlns=\"\">";
2163 res += " <data>data1</data>";
2164 res += " </result>";
2165 res += " <intern xmlns=\"urn:responseTypes\">";
2166 res += " <result xmlns=\"\">";
2167 res += " <data>data2</data>";
2168 res += " </result>";
2169 res += " </intern>";
2171 res += "</TestSchemaForm2>";
2172 Assert.AreEqual (Infoset (res), WriterText);
2175 [Test] // bug #78536
2176 public void CDataTextNodes ()
2178 XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
2179 ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
2180 string xml = @"<CDataTextNodesType>
2182 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2183 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2184 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2186 </CDataTextNodesType>";
2187 ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
2190 #if !TARGET_JVM && !MOBILE
2192 public void GenerateSerializerGenerics ()
2194 XmlReflectionImporter imp = new XmlReflectionImporter ();
2195 Type type = typeof (List<int>);
2196 XmlSerializer.GenerateSerializer (
2198 new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
2203 public void Nullable ()
2205 XmlSerializer ser = new XmlSerializer (typeof (int?));
2206 int? nullableType = 5;
2207 sw = new StringWriter ();
2208 xtw = new XmlTextWriter (sw);
2209 ser.Serialize (xtw, nullableType);
2211 string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2212 Assert.AreEqual (Infoset (expected), WriterText);
2213 int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
2214 Assert.AreEqual (5, i);
2218 public void NullableEnums ()
2220 WithNulls w = new WithNulls ();
2221 XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
2222 StringWriter tw = new StringWriter ();
2223 ser.Serialize (tw, w);
2225 string expected = "<?xml version='1.0' encoding='utf-16'?>" +
2226 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2227 "<nint xsi:nil='true' />" +
2228 "<nenum xsi:nil='true' />" +
2229 "<ndate xsi:nil='true' />" +
2232 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2234 StringReader sr = new StringReader (tw.ToString ());
2235 w = (WithNulls) ser.Deserialize (sr);
2237 Assert.IsFalse (w.nint.HasValue);
2238 Assert.IsFalse (w.nenum.HasValue);
2239 Assert.IsFalse (w.ndate.HasValue);
2241 DateTime t = new DateTime (2008,4,1);
2244 w.nenum = TestEnumWithNulls.bb;
2246 tw = new StringWriter ();
2247 ser.Serialize (tw, w);
2249 expected = "<?xml version='1.0' encoding='utf-16'?>" +
2250 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2252 "<nenum>bb</nenum>" +
2253 "<ndate>2008-04-01T00:00:00</ndate>" +
2256 Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
2258 sr = new StringReader (tw.ToString ());
2259 w = (WithNulls) ser.Deserialize (sr);
2261 Assert.IsTrue (w.nint.HasValue);
2262 Assert.IsTrue (w.nenum.HasValue);
2263 Assert.IsTrue (w.ndate.HasValue);
2264 Assert.AreEqual (4, w.nint.Value);
2265 Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
2266 Assert.AreEqual (t, w.ndate.Value);
2270 public void SerializeBase64Binary()
2272 XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
2273 sw = new StringWriter ();
2274 XmlTextWriter xtw = new XmlTextWriter (sw);
2275 ser.Serialize (xtw, new Base64Binary ());
2277 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"" />";
2278 Assert.AreEqual (Infoset (expected), WriterText);
2279 Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
2280 Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
2283 [Test] // bug #79989, #79990
2284 public void SerializeHexBinary ()
2286 XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
2287 sw = new StringWriter ();
2288 XmlTextWriter xtw = new XmlTextWriter (sw);
2289 ser.Serialize (xtw, new HexBinary ());
2291 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"" />";
2292 Assert.AreEqual (Infoset (expected), WriterText);
2293 HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
2294 Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
2298 [ExpectedException (typeof (InvalidOperationException))]
2299 public void XmlArrayAttributeOnInt ()
2301 new XmlSerializer (typeof (XmlArrayOnInt));
2305 [ExpectedException (typeof (InvalidOperationException))]
2306 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2308 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
2312 [ExpectedException (typeof (InvalidOperationException))]
2313 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2315 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
2318 [Test] // bug #78042
2319 public void XmlArrayAttributeOnArray ()
2321 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
2322 sw = new StringWriter ();
2323 XmlTextWriter xtw = new XmlTextWriter (sw);
2324 ser.Serialize (xtw, new XmlArrayOnArray ());
2326 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>";
2327 Assert.AreEqual (Infoset (expected), WriterText);
2331 public void XmlArrayAttributeOnCollection ()
2333 XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
2334 XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
2335 inst.Sane.Add ("abc");
2337 sw = new StringWriter ();
2338 XmlTextWriter xtw = new XmlTextWriter (sw);
2339 ser.Serialize (xtw, inst);
2341 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>";
2342 Assert.AreEqual (Infoset (expected), WriterText);
2345 [Test] // bug #338705
2346 public void SerializeTimeSpan ()
2348 // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2349 XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
2350 ser.Serialize (TextWriter.Null, TimeSpan.Zero);
2354 public void SerializeDurationToString ()
2356 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
2357 ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
2361 [ExpectedException (typeof (InvalidOperationException))]
2362 public void SerializeDurationToTimeSpan ()
2364 XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
2365 ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
2369 [ExpectedException (typeof (InvalidOperationException))]
2370 public void SerializeInvalidDataType ()
2372 XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
2373 ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
2378 [ExpectedException (typeof (ApplicationException))]
2380 public void SerializeErrorneousIXmlSerializable ()
2382 Serialize (new ErrorneousGetSchema ());
2383 Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
2387 public void DateTimeRoundtrip ()
2390 XmlSerializer ser = new XmlSerializer (typeof (DateTime));
2391 StringWriter sw = new StringWriter ();
2392 ser.Serialize (sw, DateTime.UtcNow);
2393 DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
2394 Assert.AreEqual (DateTimeKind.Utc, d.Kind);
2399 public void SupportIXmlSerializableImplicitlyConvertible ()
2401 XmlAttributes attrs = new XmlAttributes ();
2402 XmlElementAttribute attr = new XmlElementAttribute ();
2403 attr.ElementName = "XmlSerializable";
2404 attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
2405 attrs.XmlElements.Add (attr);
2406 XmlAttributeOverrides attrOverrides = new
2407 XmlAttributeOverrides ();
2408 attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
2410 XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
2411 new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
2414 [Test] // bug #566370
2415 public void SerializeEnumWithCSharpKeyword ()
2417 var ser = new XmlSerializer (typeof (DoxCompoundKind));
2418 for (int i = 0; i < 100; i++) // test serialization code generator
2419 ser.Serialize (Console.Out, DoxCompoundKind.@class);
2422 public enum DoxCompoundKind
2429 [XmlEnum("interface")]
2435 [XmlEnum("namespace")]
2443 #region GenericsSeralizationTests
2447 public void TestSerializeGenSimpleClassString ()
2449 GenSimpleClass<string> simple = new GenSimpleClass<string> ();
2451 Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
2453 simple.something = "hello";
2456 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);
2460 public void TestSerializeGenSimpleClassBool ()
2462 GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
2464 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);
2466 simple.something = true;
2469 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);
2473 public void TestSerializeGenSimpleStructInt ()
2475 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
2477 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);
2479 simple.something = 123;
2482 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);
2486 public void TestSerializeGenListClassString ()
2488 GenListClass<string> genlist = new GenListClass<string> ();
2489 Serialize (genlist);
2490 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);
2492 genlist.somelist.Add ("Value1");
2493 genlist.somelist.Add ("Value2");
2495 Serialize (genlist);
2496 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);
2500 public void TestSerializeGenListClassFloat ()
2502 GenListClass<float> genlist = new GenListClass<float> ();
2503 Serialize (genlist);
2504 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);
2506 genlist.somelist.Add (1);
2507 genlist.somelist.Add (2.2F);
2509 Serialize (genlist);
2510 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);
2514 public void TestSerializeGenListClassList ()
2516 GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
2517 Serialize (genlist);
2518 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);
2520 GenListClass<int> inlist1 = new GenListClass<int> ();
2521 inlist1.somelist.Add (1);
2522 inlist1.somelist.Add (2);
2523 GenListClass<int> inlist2 = new GenListClass<int> ();
2524 inlist2.somelist.Add (10);
2525 inlist2.somelist.Add (20);
2526 genlist.somelist.Add (inlist1);
2527 genlist.somelist.Add (inlist2);
2529 Serialize (genlist);
2530 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);
2534 public void TestSerializeGenListClassArray ()
2536 GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
2537 Serialize (genlist);
2538 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);
2540 GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
2541 genarr1.arr[0] = 'a';
2542 genarr1.arr[1] = 'b';
2543 genlist.somelist.Add (genarr1);
2544 GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
2545 genarr2.arr[0] = 'd';
2546 genarr2.arr[1] = 'e';
2547 genarr2.arr[2] = 'f';
2548 genlist.somelist.Add (genarr2);
2550 Serialize (genlist);
2551 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);
2555 public void TestSerializeGenTwoClassCharDouble ()
2557 GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
2559 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);
2561 gentwo.something1 = 'a';
2562 gentwo.something2 = 2.2;
2565 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);
2569 public void TestSerializeGenDerivedClassDecimalShort ()
2571 GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
2572 Serialize (derived);
2573 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);
2575 derived.something1 = "Value1";
2576 derived.something2 = 1;
2577 derived.another1 = 1.1M;
2578 derived.another2 = -22;
2580 Serialize (derived);
2581 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);
2585 public void TestSerializeGenDerivedSecondClassByteUlong ()
2587 GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
2588 Serialize (derived2);
2589 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);
2591 derived2.something1 = 1;
2592 derived2.something2 = 222;
2593 derived2.another1 = 111;
2594 derived2.another2 = 222222;
2596 Serialize (derived2);
2597 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);
2601 public void TestSerializeGenNestedClass ()
2603 GenNestedClass<string, int>.InnerClass<bool> nested =
2604 new GenNestedClass<string, int>.InnerClass<bool> ();
2606 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);
2609 nested.something = true;
2612 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);
2616 public void TestSerializeGenListClassListNested ()
2618 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
2619 new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
2620 Serialize (genlist);
2621 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);
2623 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
2624 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2625 GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
2627 inval1.something = "ONE";
2628 inlist1.somelist.Add (inval1);
2629 GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
2631 inval2.something = "TWO";
2632 inlist1.somelist.Add (inval2);
2633 GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
2634 new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
2635 GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
2637 inval3.something = "THIRTY";
2638 inlist2.somelist.Add (inval3);
2639 genlist.somelist.Add (inlist1);
2640 genlist.somelist.Add (inlist2);
2642 Serialize (genlist);
2643 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);
2646 public enum Myenum { one, two, three, four, five, six };
2648 public void TestSerializeGenArrayClassEnum ()
2650 GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
2652 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);
2654 genarr.arr[0] = Myenum.one;
2655 genarr.arr[1] = Myenum.three;
2656 genarr.arr[2] = Myenum.five;
2659 Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
2663 public void TestSerializeGenArrayStruct ()
2665 GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
2667 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);
2669 GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
2670 genstruct.something = 111;
2671 genarr.arr[0] = genstruct;
2672 genstruct.something = 222;
2673 genarr.arr[1] = genstruct;
2674 genstruct.something = 333;
2675 genarr.arr[2] = genstruct;
2678 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);
2682 public void TestSerializeGenArrayList ()
2684 GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
2686 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);
2688 GenListClass<string> genlist1 = new GenListClass<string> ();
2689 genlist1.somelist.Add ("list1-val1");
2690 genlist1.somelist.Add ("list1-val2");
2691 genarr.arr[0] = genlist1;
2692 GenListClass<string> genlist2 = new GenListClass<string> ();
2693 genlist2.somelist.Add ("list2-val1");
2694 genlist2.somelist.Add ("list2-val2");
2695 genlist2.somelist.Add ("list2-val3");
2696 genlist2.somelist.Add ("list2-val4");
2697 genarr.arr[1] = genlist2;
2698 GenListClass<string> genlist3 = new GenListClass<string> ();
2699 genlist3.somelist.Add ("list3val");
2700 genarr.arr[2] = genlist3;
2703 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);
2707 public void TestSerializeGenComplexStruct ()
2709 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
2710 Serialize (complex);
2711 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);
2713 complex.something = 123;
2714 complex.simpleclass.something = 456;
2715 complex.simplestruct.something = 789;
2716 GenListClass<int> genlist = new GenListClass<int> ();
2717 genlist.somelist.Add (100);
2718 genlist.somelist.Add (200);
2719 complex.listclass = genlist;
2720 GenArrayClass<int> genarr = new GenArrayClass<int> ();
2724 complex.arrayclass = genarr;
2725 complex.twoclass.something1 = 10;
2726 complex.twoclass.something2 = "Ten";
2727 complex.derivedclass.another1 = 1;
2728 complex.derivedclass.another2 = "one";
2729 complex.derivedclass.something1 = "two";
2730 complex.derivedclass.something2 = 2;
2731 complex.derived2.another1 = 3;
2732 complex.derived2.another2 = "three";
2733 complex.derived2.something1 = 4;
2734 complex.derived2.something2 = "four";
2735 complex.nestedouter.outer = 5;
2736 complex.nestedinner.inner = "six";
2737 complex.nestedinner.something = 6;
2739 Serialize (complex);
2740 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);
2743 [Test] // bug #80759
2744 public void HasNullableField ()
2746 Bug80759 foo = new Bug80759 ();
2748 foo.NullableInt = 10;
2750 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2752 MemoryStream stream = new MemoryStream ();
2754 serializer.Serialize (stream, foo);
2755 stream.Position = 0;
2756 foo = (Bug80759) serializer.Deserialize (stream);
2759 [Test] // bug #80759, with fieldSpecified.
2760 [ExpectedException (typeof (InvalidOperationException))]
2761 [Category ("NotWorking")]
2762 public void HasFieldSpecifiedButIrrelevant ()
2764 Bug80759_2 foo = new Bug80759_2 ();
2766 foo.NullableInt = 10;
2768 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
2770 MemoryStream stream = new MemoryStream ();
2772 serializer.Serialize (stream, foo);
2773 stream.Position = 0;
2774 foo = (Bug80759_2) serializer.Deserialize (stream);
2778 public void HasNullableField2 ()
2780 Bug80759 foo = new Bug80759 ();
2782 foo.NullableInt = 10;
2784 XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
2786 MemoryStream stream = new MemoryStream ();
2788 serializer.Serialize (stream, foo);
2789 stream.Position = 0;
2790 foo = (Bug80759) serializer.Deserialize (stream);
2792 Assert.AreEqual ("BAR", foo.Test, "#1");
2793 Assert.AreEqual (10, foo.NullableInt, "#2");
2795 foo.NullableInt = null;
2796 stream = new MemoryStream ();
2797 serializer.Serialize (stream, foo);
2798 stream.Position = 0;
2799 foo = (Bug80759) serializer.Deserialize (stream);
2801 Assert.AreEqual ("BAR", foo.Test, "#3");
2802 Assert.IsNull (foo.NullableInt, "#4");
2806 public void SupportPrivateCtorOnly ()
2809 new XmlSerializer (typeof (PrivateCtorOnly));
2810 StringWriter sw = new StringWriter ();
2811 xs.Serialize (sw, PrivateCtorOnly.Instance);
2812 xs.Deserialize (new StringReader (sw.ToString ()));
2816 public void XmlSchemaProviderQNameBecomesRootName ()
2818 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
2819 Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2820 Assert.AreEqual (Infoset ("<foo />"), WriterText);
2821 xs.Deserialize (new StringReader ("<foo/>"));
2825 public void XmlSchemaProviderQNameBecomesRootName2 ()
2827 string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2828 xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
2829 Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2830 Assert.AreEqual (Infoset (xml), WriterText);
2831 xs.Deserialize (new StringReader (xml));
2835 public void XmlAnyElementForObjects () // bug #553032
2837 new XmlSerializer (typeof (XmlAnyElementForObjectsType));
2841 [ExpectedException (typeof (InvalidOperationException))]
2842 public void XmlAnyElementForObjects2 () // bug #553032-2
2844 new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
2848 public class Bug2893 {
2851 Contents = new XmlDataDocument();
2854 [XmlAnyElement("Contents")]
2855 public XmlNode Contents;
2858 // Bug Xamarin #2893
2860 public void XmlAnyElementForXmlNode ()
2862 var obj = new Bug2893 ();
2863 XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
2864 XmlWriterSettings settings = new XmlWriterSettings();
2866 var xsn = new XmlSerializerNamespaces();
2867 xsn.Add(string.Empty, string.Empty);
2869 byte[] buffer = new byte[2048];
2870 var ms = new MemoryStream(buffer);
2871 using (var xw = XmlWriter.Create(ms, settings))
2873 mySerializer.Serialize(xw, obj, xsn);
2877 mySerializer.Serialize(ms, obj);
2881 public void XmlRootOverridesSchemaProviderQName ()
2883 var obj = new XmlRootOverridesSchemaProviderQNameType ();
2885 XmlSerializer xs = new XmlSerializer (obj.GetType ());
2887 var sw = new StringWriter ();
2888 using (XmlWriter xw = XmlWriter.Create (sw))
2889 xs.Serialize (xw, obj);
2890 Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
2894 #endregion //GenericsSeralizationTests
2896 public class XmlArrayOnInt
2902 public class XmlArrayUnqualifiedWithNamespace
2904 [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
2905 public ArrayList Sane = new ArrayList ();
2908 public class XmlArrayItemUnqualifiedWithNamespace
2910 [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
2911 public ArrayList Sane = new ArrayList ();
2914 [XmlRoot (Namespace = "urn:foo")]
2915 public class XmlArrayOnArrayList
2917 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2918 public ArrayList Sane = new ArrayList ();
2921 [XmlRoot (Namespace = "urn:foo")]
2922 public class XmlArrayOnArray
2924 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2925 public string[] Sane = new string[] { "foo", "bar" };
2927 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2928 public ArrayItemInXmlArray[] Mids =
2929 new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
2932 [XmlType (Namespace = "urn:gyabo")]
2933 public class ArrayItemInXmlArray
2935 [XmlArray (Form = XmlSchemaForm.Unqualified)]
2936 public string[] Whee = new string[] { "foo", "bar" };
2939 [XmlRoot ("Base64Binary")]
2940 public class Base64Binary
2942 [XmlAttribute (DataType = "base64Binary")]
2943 public byte [] Data = new byte [] {1, 2, 3};
2946 [XmlRoot ("HexBinary")]
2947 public class HexBinary
2949 [XmlAttribute (DataType = "hexBinary")]
2950 public byte[] Data = new byte[] { 1, 2, 3 };
2953 [XmlRoot ("PrivateCtorOnly")]
2954 public class PrivateCtorOnly
2956 public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
2957 private PrivateCtorOnly ()
2962 public class CDataTextNodesType
2964 public CDataTextNodesInternal foo;
2967 public class CDataTextNodesInternal
2970 public string Value;
2973 public class InvalidTypeContainer
2975 [XmlElement (DataType = "invalid")]
2976 public string InvalidTypeItem = "aaa";
2979 public class TimeSpanContainer1
2981 [XmlElement (DataType = "duration")]
2982 public string StringDuration = "aaa";
2985 public class TimeSpanContainer2
2987 [XmlElement (DataType = "duration")]
2988 public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
2992 public class Bug80759
2995 public int? NullableInt;
2998 public class Bug80759_2
3001 public int? NullableInt;
3004 public bool NullableIntSpecified {
3005 get { return NullableInt.HasValue; }
3009 [XmlSchemaProvider ("GetXsdType")]
3010 public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
3012 public XmlSchema GetSchema ()
3017 public void ReadXml (XmlReader reader)
3022 public void WriteXml (XmlWriter writer)
3026 public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
3028 if (xss.Count == 0) {
3029 XmlSchema xs = new XmlSchema ();
3030 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
3035 return new XmlQualifiedName ("foo");
3039 public class XmlSchemaProviderQNameBecomesRootNameType2
3041 [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
3042 public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
3045 public class XmlAnyElementForObjectsType
3048 public object [] arr = new object [] {3,4,5};
3052 [XmlSchemaProvider ("GetSchema")]
3053 public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
3055 public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
3057 var xs = new XmlSchema ();
3058 var xse = new XmlSchemaComplexType () { Name = "bar" };
3061 return new XmlQualifiedName ("bar");
3064 XmlSchema IXmlSerializable.GetSchema ()
3069 void IXmlSerializable.ReadXml (XmlReader reader)
3072 void IXmlSerializable.WriteXml (XmlWriter writer)
3079 void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
3086 public static string Infoset (string sx)
3088 XmlDocument doc = new XmlDocument ();
3090 StringBuilder sb = new StringBuilder ();
3091 GetInfoset (doc.DocumentElement, sb);
3092 return sb.ToString ();
3095 public static string Infoset (XmlNode nod)
3097 StringBuilder sb = new StringBuilder ();
3098 GetInfoset (nod, sb);
3099 return sb.ToString ();
3102 static void GetInfoset (XmlNode nod, StringBuilder sb)
3104 switch (nod.NodeType) {
3105 case XmlNodeType.Attribute:
3106 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
3107 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
3110 case XmlNodeType.Element:
3111 XmlElement elem = (XmlElement) nod;
3112 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
3114 ArrayList ats = new ArrayList ();
3115 foreach (XmlAttribute at in elem.Attributes)
3116 ats.Add (at.LocalName + " " + at.NamespaceURI);
3120 foreach (string name in ats) {
3121 string[] nn = name.Split (' ');
3122 GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
3126 foreach (XmlNode cn in elem.ChildNodes)
3127 GetInfoset (cn, sb);
3132 sb.Append (nod.OuterXml);
3137 static XmlTypeMapping CreateSoapMapping (Type type)
3139 SoapReflectionImporter importer = new SoapReflectionImporter ();
3140 return importer.ImportTypeMapping (type);
3143 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
3145 SoapReflectionImporter importer = new SoapReflectionImporter (ao);
3146 return importer.ImportTypeMapping (type);
3149 static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
3151 SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
3152 return importer.ImportTypeMapping (type);