2 // System.Xml.XmlSerializerTests
5 // Erik LeBel <eriklebel@yahoo.ca>
6 // Hagit Yidov <hagity@mainsoft.com>
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Where possible, these tests avoid testing the order of
14 // an object's members serialization. Mono and .NET do not
15 // reflect members in the same order.
17 // Only serializations tests so far, no deserialization.
20 // test XmlArrayAttribute
21 // test XmlArrayItemAttribute
22 // test serialization of decimal type
23 // test serialization of Guid type
24 // test XmlNode serialization with and without modifying attributes.
25 // test deserialization
26 // FIXMEs found in this file
29 using System
.Collections
;
30 using System
.Globalization
;
35 using System
.Xml
.Schema
;
36 using System
.Xml
.Serialization
;
37 using System
.Reflection
;
38 using System
.Collections
.Generic
;
40 using NUnit
.Framework
;
42 using MonoTests
.System
.Xml
.TestClasses
;
44 namespace MonoTests
.System
.XmlSerialization
47 public class XmlSerializerTests
49 const string SoapEncodingNamespace
= "http://schemas.xmlsoap.org/soap/encoding/";
50 const string WsdlTypesNamespace
= "http://microsoft.com/wsdl/types/";
51 const string ANamespace
= "some:urn";
52 const string AnotherNamespace
= "another:urn";
58 private void SetUpWriter ()
60 sw
= new StringWriter ();
61 xtw
= new XmlTextWriter (sw
);
63 xtw
.Formatting
= Formatting
.None
;
66 private string WriterText
70 string val
= sw
.GetStringBuilder ().ToString ();
71 int offset
= val
.IndexOf ('>') + 1;
72 val
= val
.Substring (offset
);
77 private void Serialize (object o
)
80 xs
= new XmlSerializer (o
.GetType ());
81 xs
.Serialize (xtw
, o
);
84 private void Serialize (object o
, Type type
)
87 xs
= new XmlSerializer (type
);
88 xs
.Serialize (xtw
, o
);
91 private void Serialize (object o
, XmlSerializerNamespaces ns
)
94 xs
= new XmlSerializer (o
.GetType ());
95 xs
.Serialize (xtw
, o
, ns
);
98 private void Serialize (object o
, XmlAttributeOverrides ao
)
101 xs
= new XmlSerializer (o
.GetType (), ao
);
102 xs
.Serialize (xtw
, o
);
105 private void Serialize (object o
, XmlAttributeOverrides ao
, string defaultNamespace
)
108 xs
= new XmlSerializer (o
.GetType (), ao
, Type
.EmptyTypes
,
109 (XmlRootAttribute
) null, defaultNamespace
);
110 xs
.Serialize (xtw
, o
);
113 private void Serialize (object o
, XmlRootAttribute root
)
116 xs
= new XmlSerializer (o
.GetType (), root
);
117 xs
.Serialize (xtw
, o
);
120 private void Serialize (object o
, XmlTypeMapping typeMapping
)
123 xs
= new XmlSerializer (typeMapping
);
124 xs
.Serialize (xtw
, o
);
127 private void SerializeEncoded (object o
)
129 SerializeEncoded (o
, o
.GetType ());
132 private void SerializeEncoded (object o
, SoapAttributeOverrides ao
)
134 XmlTypeMapping mapping
= CreateSoapMapping (o
.GetType (), ao
);
136 xs
= new XmlSerializer (mapping
);
137 xs
.Serialize (xtw
, o
);
140 private void SerializeEncoded (object o
, SoapAttributeOverrides ao
, string defaultNamespace
)
142 XmlTypeMapping mapping
= CreateSoapMapping (o
.GetType (), ao
, defaultNamespace
);
144 xs
= new XmlSerializer (mapping
);
145 xs
.Serialize (xtw
, o
);
148 private void SerializeEncoded (object o
, Type type
)
150 XmlTypeMapping mapping
= CreateSoapMapping (type
);
152 xs
= new XmlSerializer (mapping
);
153 xs
.Serialize (xtw
, o
);
156 private void SerializeEncoded (XmlTextWriter xtw
, object o
, Type type
)
158 XmlTypeMapping mapping
= CreateSoapMapping (type
);
159 xs
= new XmlSerializer (mapping
);
160 xs
.Serialize (xtw
, o
);
165 [ExpectedException (typeof (ArgumentNullException
))]
166 public void TestConstructor()
168 XmlSerializer ser
= new XmlSerializer (null, "");
171 // test basic types ////////////////////////////////////////////////////////
173 public void TestSerializeInt ()
176 Assert
.AreEqual (Infoset ("<int>10</int>"), WriterText
);
180 public void TestSerializeBool ()
183 Assert
.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText
);
186 Assert
.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText
);
190 public void TestSerializeString ()
193 Assert
.AreEqual (Infoset ("<string>hello</string>"), WriterText
);
197 public void TestSerializeEmptyString ()
199 Serialize (String
.Empty
);
200 Assert
.AreEqual (Infoset ("<string />"), WriterText
);
204 public void TestSerializeNullObject ()
206 Serialize (null, typeof (object));
207 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
208 "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
209 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
);
213 [Ignore ("The generated XML is not exact but it is equivalent")]
214 public void TestSerializeNullString ()
216 Serialize (null, typeof (string));
217 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
218 "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
219 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
);
223 public void TestSerializeIntArray ()
225 Serialize (new int[] { 1, 2, 3, 4 }
);
226 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
227 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
228 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
);
232 public void TestSerializeEmptyArray ()
234 Serialize (new int[] { }
);
235 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
236 "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
237 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
);
241 public void TestSerializeChar ()
244 Assert
.AreEqual (Infoset ("<char>65</char>"), WriterText
);
247 Assert
.AreEqual (Infoset ("<char>0</char>"), WriterText
);
250 Assert
.AreEqual (Infoset ("<char>10</char>"), WriterText
);
252 Serialize ('\uFF01');
253 Assert
.AreEqual (Infoset ("<char>65281</char>"), WriterText
);
257 public void TestSerializeFloat ()
260 Assert
.AreEqual (Infoset ("<double>10.78</double>"), WriterText
);
263 Assert
.AreEqual (Infoset ("<double>-100000000</double>"), WriterText
);
265 // FIXME test INF and other boundary conditions that may exist with floats
269 public void TestSerializeEnumeration_FromValue ()
271 Serialize ((int) SimpleEnumeration
.SECOND
, typeof (SimpleEnumeration
));
273 "<?xml version='1.0' encoding='utf-16'?>" +
274 "<SimpleEnumeration>SECOND</SimpleEnumeration>",
279 [Category ("MobileNotWorking")]
280 public void TestSerializeEnumeration_FromValue_Encoded ()
282 SerializeEncoded ((int) SimpleEnumeration
.SECOND
, typeof (SimpleEnumeration
));
283 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
284 "<?xml version='1.0' encoding='utf-16'?>" +
285 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
286 XmlSchema
.InstanceNamespace
), sw
.ToString ());
290 public void TestSerializeEnumeration ()
292 Serialize (SimpleEnumeration
.FIRST
);
293 Assert
.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText
, "#1");
295 Serialize (SimpleEnumeration
.SECOND
);
296 Assert
.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText
, "#2");
300 public void TestSerializeEnumeration_Encoded ()
302 SerializeEncoded (SimpleEnumeration
.FIRST
);
303 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
304 "<?xml version='1.0' encoding='utf-16'?>" +
305 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
306 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#B1");
308 SerializeEncoded (SimpleEnumeration
.SECOND
);
309 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
310 "<?xml version='1.0' encoding='utf-16'?>" +
311 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
312 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#B2");
316 public void TestSerializeEnumDefaultValue ()
318 Serialize (new EnumDefaultValue ());
319 Assert
.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText
, "#1");
321 Serialize (new SimpleEnumeration ());
322 Assert
.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText
, "#2");
324 Serialize (3, typeof (EnumDefaultValue
));
325 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#3");
327 Serialize (EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
328 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#4");
330 Serialize (EnumDefaultValue
.e1
| EnumDefaultValue
.e2
, typeof (EnumDefaultValue
));
331 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#5");
333 Serialize (EnumDefaultValue
.e1
| EnumDefaultValue
.e2
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
334 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#6");
336 Serialize (EnumDefaultValue
.e1
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
337 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#7");
339 Serialize (EnumDefaultValue
.e2
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
340 Assert
.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText
, "#8");
342 Serialize (3, typeof (FlagEnum
));
343 Assert
.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText
, "#9");
345 Serialize (5, typeof (FlagEnum
));
346 Assert
.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText
, "#10");
348 Serialize (FlagEnum
.e4
, typeof (FlagEnum
));
349 Assert
.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText
, "#11");
351 Serialize (FlagEnum
.e1
| FlagEnum
.e2
, typeof (FlagEnum
));
352 Assert
.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText
, "#12");
354 Serialize (FlagEnum
.e1
| FlagEnum
.e2
| FlagEnum
.e4
, typeof (FlagEnum
));
355 Assert
.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText
, "#13");
357 Serialize (FlagEnum
.e1
| FlagEnum
.e4
, typeof (FlagEnum
));
358 Assert
.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText
, "#14");
360 Serialize (FlagEnum
.e2
| FlagEnum
.e4
, typeof (FlagEnum
));
361 Assert
.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText
, "#15");
363 Serialize (3, typeof (EnumDefaultValueNF
));
364 Assert
.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText
, "#16");
366 Serialize (EnumDefaultValueNF
.e2
, typeof (EnumDefaultValueNF
));
367 Assert
.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText
, "#17");
369 Serialize (2, typeof (ZeroFlagEnum
));
370 Assert
.AreEqual (Infoset ("<ZeroFlagEnum>tns:t<w>o</ZeroFlagEnum>"), WriterText
, "#18");
372 Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
373 Assert
.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText
, "#19");
377 [Category ("MobileNotWorking")]
378 public void TestSerializeEnumDefaultValue_Encoded ()
380 SerializeEncoded (new EnumDefaultValue ());
381 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
382 "<?xml version='1.0' encoding='utf-16'?>" +
383 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
384 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#1");
386 SerializeEncoded (new SimpleEnumeration ());
387 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
388 "<?xml version='1.0' encoding='utf-16'?>" +
389 "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
390 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#2");
392 SerializeEncoded (3, typeof (EnumDefaultValue
));
393 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
394 "<?xml version='1.0' encoding='utf-16'?>" +
395 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
396 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#3");
398 SerializeEncoded (EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
399 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
400 "<?xml version='1.0' encoding='utf-16'?>" +
401 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
402 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#4");
404 SerializeEncoded (EnumDefaultValue
.e1
| EnumDefaultValue
.e2
, typeof (EnumDefaultValue
));
405 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
406 "<?xml version='1.0' encoding='utf-16'?>" +
407 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
408 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#5");
410 SerializeEncoded (EnumDefaultValue
.e1
| EnumDefaultValue
.e2
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
411 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
412 "<?xml version='1.0' encoding='utf-16'?>" +
413 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
414 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#6");
416 SerializeEncoded (EnumDefaultValue
.e1
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
417 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
418 "<?xml version='1.0' encoding='utf-16'?>" +
419 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
420 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#7");
422 SerializeEncoded (EnumDefaultValue
.e2
| EnumDefaultValue
.e3
, typeof (EnumDefaultValue
));
423 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
424 "<?xml version='1.0' encoding='utf-16'?>" +
425 "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
426 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#8");
428 SerializeEncoded (3, typeof (FlagEnum
));
429 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
430 "<?xml version='1.0' encoding='utf-16'?>" +
431 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
432 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#9");
434 SerializeEncoded (5, typeof (FlagEnum
));
435 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
436 "<?xml version='1.0' encoding='utf-16'?>" +
437 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
438 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#10");
440 SerializeEncoded (FlagEnum
.e4
, typeof (FlagEnum
));
441 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
442 "<?xml version='1.0' encoding='utf-16'?>" +
443 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
444 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#11");
446 SerializeEncoded (FlagEnum
.e1
| FlagEnum
.e2
, typeof (FlagEnum
));
447 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
448 "<?xml version='1.0' encoding='utf-16'?>" +
449 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
450 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#12");
452 SerializeEncoded (FlagEnum
.e1
| FlagEnum
.e2
| FlagEnum
.e4
, typeof (FlagEnum
));
453 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
454 "<?xml version='1.0' encoding='utf-16'?>" +
455 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
456 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#13");
458 SerializeEncoded (FlagEnum
.e1
| FlagEnum
.e4
, typeof (FlagEnum
));
459 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
460 "<?xml version='1.0' encoding='utf-16'?>" +
461 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
462 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#14");
464 SerializeEncoded (FlagEnum
.e2
| FlagEnum
.e4
, typeof (FlagEnum
));
465 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
466 "<?xml version='1.0' encoding='utf-16'?>" +
467 "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
468 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#15");
470 SerializeEncoded (3, typeof (EnumDefaultValueNF
));
471 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
472 "<?xml version='1.0' encoding='utf-16'?>" +
473 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
474 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#16");
476 SerializeEncoded (EnumDefaultValueNF
.e2
, typeof (EnumDefaultValueNF
));
477 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
478 "<?xml version='1.0' encoding='utf-16'?>" +
479 "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
480 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#17");
482 SerializeEncoded (2, typeof (ZeroFlagEnum
));
483 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
484 "<?xml version='1.0' encoding='utf-16'?>" +
485 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
486 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#18");
488 SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
489 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
490 "<?xml version='1.0' encoding='utf-16'?>" +
491 "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
492 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#19");
496 public void TestSerializeEnumDefaultValue_InvalidValue1 ()
499 Serialize ("b", typeof (EnumDefaultValue
));
501 } catch (InvalidOperationException ex
) {
502 Assert
.IsNotNull (ex
.InnerException
, "#A2");
503 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#A3");
507 Serialize ("e1", typeof (EnumDefaultValue
));
509 } catch (InvalidOperationException ex
) {
510 Assert
.IsNotNull (ex
.InnerException
, "#B2");
511 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#B3");
515 Serialize ("e1,e2", typeof (EnumDefaultValue
));
517 } catch (InvalidOperationException ex
) {
518 Assert
.IsNotNull (ex
.InnerException
, "#C2");
519 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#C3");
523 Serialize (string.Empty
, typeof (EnumDefaultValue
));
525 } catch (InvalidOperationException ex
) {
526 Assert
.IsNotNull (ex
.InnerException
, "#D2");
527 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#D3");
531 Serialize ("1", typeof (EnumDefaultValue
));
533 } catch (InvalidOperationException ex
) {
534 Assert
.IsNotNull (ex
.InnerException
, "#E2");
535 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#E3");
539 Serialize ("0", typeof (EnumDefaultValue
));
541 } catch (InvalidOperationException ex
) {
542 Assert
.IsNotNull (ex
.InnerException
, "#F2");
543 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#F3");
547 Serialize (new SimpleClass (), typeof (EnumDefaultValue
));
549 } catch (InvalidOperationException ex
) {
550 Assert
.IsNotNull (ex
.InnerException
, "#G2");
551 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#G3");
556 public void TestSerializeEnumDefaultValue_InvalidValue2 ()
559 Serialize (5, typeof (EnumDefaultValue
));
561 } catch (InvalidOperationException ex
) {
562 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
563 Assert
.IsNotNull (ex
.InnerException
, "#3");
564 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#4");
565 Assert
.IsNotNull (ex
.InnerException
.Message
, "#5");
566 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'5'") != -1, "#6");
567 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (EnumDefaultValue
).FullName
) != -1, "#7");
572 public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
575 Serialize (new EnumDefaultValueNF ());
577 } catch (InvalidOperationException ex
) {
578 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
579 Assert
.IsNotNull (ex
.InnerException
, "#3");
580 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#4");
581 Assert
.IsNotNull (ex
.InnerException
.Message
, "#5");
582 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'0'") != -1, "#6");
583 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (EnumDefaultValueNF
).FullName
) != -1, "#7");
588 public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
591 Serialize (15, typeof (EnumDefaultValueNF
));
593 } catch (InvalidOperationException ex
) {
594 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
595 Assert
.IsNotNull (ex
.InnerException
, "#3");
596 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#4");
597 Assert
.IsNotNull (ex
.InnerException
.Message
, "#5");
598 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'15'") != -1, "#6");
599 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (EnumDefaultValueNF
).FullName
) != -1, "#7");
604 public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
607 Serialize ("b", typeof (EnumDefaultValueNF
));
609 } catch (InvalidOperationException ex
) {
610 Assert
.IsNotNull (ex
.InnerException
, "#A2");
611 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#A3");
615 Serialize ("e2", typeof (EnumDefaultValueNF
));
617 } catch (InvalidOperationException ex
) {
618 Assert
.IsNotNull (ex
.InnerException
, "#B2");
619 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#B3");
623 Serialize (string.Empty
, typeof (EnumDefaultValueNF
));
625 } catch (InvalidOperationException ex
) {
626 Assert
.IsNotNull (ex
.InnerException
, "#C2");
627 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#C3");
631 Serialize ("1", typeof (EnumDefaultValueNF
));
633 } catch (InvalidOperationException ex
) {
634 Assert
.IsNotNull (ex
.InnerException
, "#D2");
635 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#D3");
639 Serialize ("0", typeof (EnumDefaultValueNF
));
641 } catch (InvalidOperationException ex
) {
642 Assert
.IsNotNull (ex
.InnerException
, "#E2");
643 Assert
.AreEqual (typeof (InvalidCastException
), ex
.InnerException
.GetType (), "#E3");
648 public void TestSerializeField ()
650 Field f
= new Field ();
651 Serialize (f
, typeof (Field
));
652 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
653 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
654 " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
655 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
, "#A");
657 f
.Flags1
= FlagEnum
.e1
;
658 f
.Flags2
= FlagEnum
.e1
;
659 f
.Flags3
= FlagEnum
.e2
;
660 f
.Modifiers
= MapModifiers
.Protected
;
661 f
.Modifiers2
= MapModifiers
.Public
;
662 f
.Modifiers3
= MapModifiers
.Public
;
663 f
.Modifiers4
= MapModifiers
.Protected
;
664 f
.Modifiers5
= MapModifiers
.Public
;
665 Serialize (f
, typeof (Field
));
666 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
667 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
668 " modifiers='protected' modifiers2='public' />",
669 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
, "#B");
671 f
.Flags1
= (FlagEnum
) 1;
672 f
.Flags1
= FlagEnum
.e2
;
673 f
.Flags2
= FlagEnum
.e2
;
674 f
.Flags3
= FlagEnum
.e1
| FlagEnum
.e2
;
675 f
.Modifiers
= MapModifiers
.Public
;
676 f
.Modifiers2
= MapModifiers
.Protected
;
677 f
.Modifiers3
= MapModifiers
.Protected
;
678 f
.Modifiers4
= MapModifiers
.Public
;
679 f
.Modifiers5
= MapModifiers
.Protected
;
680 Serialize (f
, typeof (Field
));
681 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
682 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
683 " flag4='' modifiers='public' modifiers2='protected'" +
684 " modifiers3='protected' modifiers4='public'" +
685 " modifiers5='protected' />",
686 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
, "#C");
688 f
.Flags1
= FlagEnum
.e1
| FlagEnum
.e2
;
689 f
.Flags2
= FlagEnum
.e2
;
690 f
.Flags3
= FlagEnum
.e4
;
691 f
.Flags4
= FlagEnum
.e1
| FlagEnum
.e2
| FlagEnum
.e4
;
692 f
.Modifiers3
= MapModifiers
.Public
;
693 f
.Modifiers4
= MapModifiers
.Protected
;
694 f
.Modifiers5
= MapModifiers
.Public
;
695 f
.Names
= new string[] { "a", "b" }
;
696 Serialize (f
, typeof (Field
));
697 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
698 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
699 " flag3='four' flag4='one two four' modifiers='public'" +
700 " modifiers2='protected' names='a b' />",
701 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
, "#D");
703 f
.Flags2
= (FlagEnum
) 444;
704 f
.Flags3
= (FlagEnum
) 555;
705 f
.Modifiers
= (MapModifiers
) 666;
706 f
.Modifiers2
= (MapModifiers
) 777;
707 f
.Modifiers3
= (MapModifiers
) 0;
708 f
.Modifiers4
= (MapModifiers
) 888;
709 f
.Modifiers5
= (MapModifiers
) 999;
711 Serialize (f
, typeof (Field
));
713 } catch (InvalidOperationException ex
) {
714 // There was an error generating the XML document
715 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#E2");
716 Assert
.IsNotNull (ex
.Message
, "#E3");
717 Assert
.IsNotNull (ex
.InnerException
, "#E4");
719 // Instance validation error: '444' is not a valid value for
720 // MonoTests.System.Xml.TestClasses.FlagEnum
721 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#E5");
722 Assert
.IsNotNull (ex
.InnerException
.Message
, "#E6");
723 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'444'") != -1, "#E7");
724 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (FlagEnum
).FullName
) != -1, "#E8");
725 Assert
.IsNull (ex
.InnerException
.InnerException
, "#E9");
730 [Category ("NotWorking")] // MS bug
731 public void TestSerializeField_Encoded ()
733 Field_Encoded f
= new Field_Encoded ();
734 SerializeEncoded (f
, typeof (Field_Encoded
));
735 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
736 "<?xml version='1.0' encoding='utf-16'?>" +
737 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
738 " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
739 " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
740 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
),
741 sw
.GetStringBuilder ().ToString (), "#A");
743 f
.Flags1
= FlagEnum_Encoded
.e1
;
744 f
.Flags2
= FlagEnum_Encoded
.e1
;
745 f
.Flags3
= FlagEnum_Encoded
.e2
;
746 f
.Modifiers
= MapModifiers
.Protected
;
747 f
.Modifiers2
= MapModifiers
.Public
;
748 f
.Modifiers3
= MapModifiers
.Public
;
749 f
.Modifiers4
= MapModifiers
.Protected
;
750 f
.Modifiers5
= MapModifiers
.Public
;
751 SerializeEncoded (f
, typeof (Field_Encoded
));
752 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
753 "<?xml version='1.0' encoding='utf-16'?>" +
754 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
755 " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
756 " xmlns:q1='some:urn' />",
757 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
),
758 sw
.GetStringBuilder ().ToString (), "#B");
760 f
.Flags1
= FlagEnum_Encoded
.e2
;
761 f
.Flags2
= FlagEnum_Encoded
.e2
;
762 f
.Flags3
= FlagEnum_Encoded
.e1
| FlagEnum_Encoded
.e2
;
763 f
.Modifiers
= MapModifiers
.Public
;
764 f
.Modifiers2
= MapModifiers
.Protected
;
765 f
.Modifiers3
= MapModifiers
.Protected
;
766 f
.Modifiers4
= MapModifiers
.Public
;
767 f
.Modifiers5
= MapModifiers
.Protected
;
768 SerializeEncoded (f
, typeof (Field_Encoded
));
769 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
770 "<?xml version='1.0' encoding='utf-16'?>" +
771 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
772 " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
773 " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
774 " xmlns:q1='some:urn' />",
775 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
),
776 sw
.GetStringBuilder ().ToString (), "#C");
778 f
.Flags1
= (FlagEnum_Encoded
) 1;
779 f
.Flags2
= (FlagEnum_Encoded
) 444;
780 f
.Flags3
= (FlagEnum_Encoded
) 555;
781 f
.Modifiers
= (MapModifiers
) 666;
782 f
.Modifiers2
= (MapModifiers
) 777;
783 f
.Modifiers3
= (MapModifiers
) 0;
784 f
.Modifiers4
= (MapModifiers
) 888;
785 f
.Modifiers5
= (MapModifiers
) 999;
787 SerializeEncoded (f
, typeof (Field_Encoded
));
789 } catch (InvalidOperationException ex
) {
790 // There was an error generating the XML document
791 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#D2");
792 Assert
.IsNotNull (ex
.Message
, "#D3");
793 Assert
.IsNotNull (ex
.InnerException
, "#D4");
795 // Instance validation error: '444' is not a valid value for
796 // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
797 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#D5");
798 Assert
.IsNotNull (ex
.InnerException
.Message
, "#D6");
799 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'444'") != -1, "#D7");
800 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (FlagEnum_Encoded
).FullName
) != -1, "#D8");
801 Assert
.IsNull (ex
.InnerException
.InnerException
, "#D9");
806 public void TestSerializeGroup ()
808 Group myGroup
= new Group ();
809 myGroup
.GroupName
= ".NET";
811 Byte
[] hexByte
= new Byte
[] { 0x64, 0x32 }
;
812 myGroup
.GroupNumber
= hexByte
;
814 DateTime myDate
= new DateTime (2002, 5, 2);
815 myGroup
.Today
= myDate
;
816 myGroup
.PostitiveInt
= "10000";
817 myGroup
.IgnoreThis
= true;
818 Car thisCar
= (Car
) myGroup
.myCar ("1234566");
819 myGroup
.MyVehicle
= thisCar
;
822 xtw
.WriteStartDocument (true);
823 xtw
.WriteStartElement ("Wrapper");
824 SerializeEncoded (xtw
, myGroup
, typeof (Group
));
825 xtw
.WriteEndElement ();
828 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
830 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
831 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
832 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
833 "<MyVehicle href='#id2' />" +
835 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
836 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
837 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
840 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
843 myGroup
.GroupName
= null;
844 myGroup
.Grouptype
= GroupType
.B
;
845 myGroup
.MyVehicle
.licenseNumber
= null;
846 myGroup
.MyVehicle
.weight
= "450";
849 xtw
.WriteStartDocument (true);
850 xtw
.WriteStartElement ("Wrapper");
851 SerializeEncoded (xtw
, myGroup
, typeof (Group
));
852 xtw
.WriteEndElement ();
855 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
857 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
858 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
859 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
860 "<MyVehicle href='#id2' />" +
862 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
863 "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
864 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
867 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
872 public void TestSerializeZeroFlagEnum_InvalidValue ()
875 Serialize (4, typeof (ZeroFlagEnum
)); // corresponding enum field is marked XmlIgnore
877 } catch (InvalidOperationException ex
) {
878 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
879 Assert
.IsNotNull (ex
.InnerException
, "#3");
880 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#4");
881 Assert
.IsNotNull (ex
.InnerException
.Message
, "#5");
882 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'4'") != -1, "#6");
883 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (ZeroFlagEnum
).FullName
) != -1, "#7");
888 public void TestSerializeQualifiedName ()
890 Serialize (new XmlQualifiedName ("me", "home.urn"));
891 Assert
.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText
);
895 public void TestSerializeBytes ()
897 Serialize ((byte) 0xAB);
898 Assert
.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText
);
900 Serialize ((byte) 15);
901 Assert
.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText
);
905 public void TestSerializeByteArrays ()
907 Serialize (new byte[] { }
);
908 Assert
.AreEqual (Infoset ("<base64Binary />"), WriterText
);
910 Serialize (new byte[] { 0xAB, 0xCD }
);
911 Assert
.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText
);
915 public void TestSerializeDateTime ()
917 DateTime d
= new DateTime ();
920 TimeZone tz
= TimeZone
.CurrentTimeZone
;
921 TimeSpan off
= tz
.GetUtcOffset (d
);
922 string sp
= string.Format ("{0}{1:00}:{2:00}", off
.Ticks
>= 0 ? "+" : "", off
.Hours
, off
.Minutes
);
923 Assert
.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText
);
933 public void TestSerialize()
936 Assert.AreEqual (WriterText, "");
940 // test basic class serialization /////////////////////////////////////
942 public void TestSerializeSimpleClass ()
944 SimpleClass simple
= new SimpleClass ();
946 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
948 simple
.something
= "hello";
951 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
);
955 public void TestSerializeStringCollection ()
957 StringCollection strings
= new StringCollection ();
959 Assert
.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
961 strings
.Add ("hello");
962 strings
.Add ("goodbye");
964 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
);
968 public void TestSerializeOptionalValueTypeContainer ()
970 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
972 OptionalValueTypeContainer optionalValue
= new OptionalValueTypeContainer ();
974 Serialize (optionalValue
);
975 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
976 "<?xml version='1.0' encoding='utf-16'?>" +
977 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
978 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
, AnotherNamespace
),
979 sw
.ToString (), "#1");
981 attr
= new XmlAttributes ();
983 // remove the DefaultValue attribute on the Flags member
984 overrides
.Add (typeof (OptionalValueTypeContainer
), "Flags", attr
);
985 // remove the DefaultValue attribute on the Attributes member
986 overrides
.Add (typeof (OptionalValueTypeContainer
), "Attributes", attr
);
988 Serialize (optionalValue
, overrides
);
989 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
990 "<?xml version='1.0' encoding='utf-16'?>" +
991 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
992 "<Attributes xmlns='{3}'>one four</Attributes>" +
993 "</optionalValue>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
,
994 AnotherNamespace
, ANamespace
), sw
.ToString (), "#2");
996 optionalValue
.FlagsSpecified
= true;
997 Serialize (optionalValue
, overrides
);
998 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
999 "<?xml version='1.0' encoding='utf-16'?>" +
1000 "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1001 "<Attributes xmlns='{3}'>one four</Attributes>" +
1002 "<Flags xmlns='{3}'>one</Flags>" +
1004 XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
, AnotherNamespace
,
1005 ANamespace
), sw
.ToString (), "#3");
1009 public void TestRoundTripSerializeOptionalValueTypeContainer ()
1011 var source
= new OptionalValueTypeContainer ();
1012 source
.IsEmpty
= true;
1013 source
.IsEmptySpecified
= true;
1014 var ser
= new XmlSerializer (typeof (OptionalValueTypeContainer
));
1016 using (var t
= new StringWriter ()) {
1017 ser
.Serialize (t
, source
);
1020 using (var s
= new StringReader (xml
)) {
1021 var obj
= (OptionalValueTypeContainer
) ser
.Deserialize(s
);
1022 Assert
.AreEqual (source
.IsEmpty
, obj
.IsEmpty
, "#1");
1023 Assert
.AreEqual (source
.IsEmptySpecified
, obj
.IsEmptySpecified
, "#2");
1028 public void TestSerializePlainContainer ()
1030 StringCollectionContainer container
= new StringCollectionContainer ();
1031 Serialize (container
);
1032 Assert
.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText
);
1034 container
.Messages
.Add ("hello");
1035 container
.Messages
.Add ("goodbye");
1036 Serialize (container
);
1037 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
);
1041 public void TestSerializeArrayContainer ()
1043 ArrayContainer container
= new ArrayContainer ();
1044 Serialize (container
);
1045 Assert
.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1047 container
.items
= new object[] { 10, 20 }
;
1048 Serialize (container
);
1049 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
);
1051 container
.items
= new object[] { 10, "hello" }
;
1052 Serialize (container
);
1053 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
);
1057 public void TestSerializeClassArrayContainer ()
1059 ClassArrayContainer container
= new ClassArrayContainer ();
1060 Serialize (container
);
1061 Assert
.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1063 SimpleClass simple1
= new SimpleClass ();
1064 simple1
.something
= "hello";
1065 SimpleClass simple2
= new SimpleClass ();
1066 simple2
.something
= "hello";
1067 container
.items
= new SimpleClass
[2];
1068 container
.items
[0] = simple1
;
1069 container
.items
[1] = simple2
;
1070 Serialize (container
);
1071 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
);
1074 // test basic attributes ///////////////////////////////////////////////
1076 public void TestSerializeSimpleClassWithXmlAttributes ()
1078 SimpleClassWithXmlAttributes simple
= new SimpleClassWithXmlAttributes ();
1080 Assert
.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1082 simple
.something
= "hello";
1084 Assert
.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText
);
1087 // test overrides ///////////////////////////////////////////////////////
1089 public void TestSerializeSimpleClassWithOverrides ()
1091 // Also tests XmlIgnore
1092 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1094 XmlAttributes attr
= new XmlAttributes ();
1095 attr
.XmlIgnore
= true;
1096 overrides
.Add (typeof (SimpleClassWithXmlAttributes
), "something", attr
);
1098 SimpleClassWithXmlAttributes simple
= new SimpleClassWithXmlAttributes ();
1099 simple
.something
= "hello";
1100 Serialize (simple
, overrides
);
1101 Assert
.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1105 public void TestSerializeSchema ()
1107 XmlSchema schema
= new XmlSchema ();
1108 schema
.Items
.Add (new XmlSchemaAttribute ());
1109 schema
.Items
.Add (new XmlSchemaAttributeGroup ());
1110 schema
.Items
.Add (new XmlSchemaComplexType ());
1111 schema
.Items
.Add (new XmlSchemaNotation ());
1112 schema
.Items
.Add (new XmlSchemaSimpleType ());
1113 schema
.Items
.Add (new XmlSchemaGroup ());
1114 schema
.Items
.Add (new XmlSchemaElement ());
1116 StringWriter sw
= new StringWriter ();
1117 XmlTextWriter xtw
= new XmlTextWriter (sw
);
1118 xtw
.QuoteChar
= '\'';
1119 xtw
.Formatting
= Formatting
.Indented
;
1120 XmlSerializer xs
= new XmlSerializer (schema
.GetType ());
1121 xs
.Serialize (xtw
, schema
);
1123 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
1124 "<?xml version='1.0' encoding='utf-16'?>{0}" +
1125 "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
1126 " <xsd:attribute />{0}" +
1127 " <xsd:attributeGroup />{0}" +
1128 " <xsd:complexType />{0}" +
1129 " <xsd:notation />{0}" +
1130 " <xsd:simpleType />{0}" +
1131 " <xsd:group />{0}" +
1132 " <xsd:element />{0}" +
1133 "</xsd:schema>", Environment
.NewLine
), sw
.ToString ());
1136 // test xmlText //////////////////////////////////////////////////////////
1138 public void TestSerializeXmlTextAttribute ()
1140 SimpleClass simple
= new SimpleClass ();
1141 simple
.something
= "hello";
1143 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1144 XmlAttributes attr
= new XmlAttributes ();
1145 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1147 attr
.XmlText
= new XmlTextAttribute ();
1148 Serialize (simple
, overrides
);
1149 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");
1151 attr
.XmlText
= new XmlTextAttribute (typeof (string));
1152 Serialize (simple
, overrides
);
1153 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");
1156 attr
.XmlText
= new XmlTextAttribute (typeof (byte[]));
1157 Serialize (simple
, overrides
);
1158 Assert
.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
1159 } catch (InvalidOperationException ex
) {
1160 // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
1161 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#A2");
1162 Assert
.IsNotNull (ex
.Message
, "#A3");
1163 Assert
.IsTrue (ex
.Message
.IndexOf (typeof (SimpleClass
).FullName
) != -1, "#A4");
1164 Assert
.IsNotNull (ex
.InnerException
, "#A5");
1166 // there was an error reflecting field 'something'.
1167 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#A6");
1168 Assert
.IsNotNull (ex
.InnerException
.Message
, "#A7");
1169 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("something") != -1, "#A8");
1170 Assert
.IsNotNull (ex
.InnerException
.InnerException
, "#A9");
1172 // the type for XmlText may not be specified for primitive types.
1173 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.InnerException
.GetType (), "#A10");
1174 Assert
.IsNotNull (ex
.InnerException
.InnerException
.Message
, "#A11");
1175 Assert
.IsNull (ex
.InnerException
.InnerException
.InnerException
, "#A12");
1179 attr
.XmlText
= new XmlTextAttribute ();
1180 attr
.XmlText
.DataType
= "sometype";
1181 Serialize (simple
, overrides
);
1182 Assert
.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
1183 } catch (InvalidOperationException ex
) {
1184 // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
1185 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#B2");
1186 Assert
.IsNotNull (ex
.Message
, "#B3");
1187 Assert
.IsTrue (ex
.Message
.IndexOf (typeof (SimpleClass
).FullName
) != -1, "#B4");
1188 Assert
.IsNotNull (ex
.InnerException
, "#B5");
1190 // There was an error reflecting field 'something'.
1191 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#B6");
1192 Assert
.IsNotNull (ex
.InnerException
.Message
, "#B7");
1193 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("something") != -1, "#B8");
1194 Assert
.IsNotNull (ex
.InnerException
.InnerException
, "#B9");
1198 // There was an error reflecting type 'System.String'.
1199 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
1200 Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
1201 Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
1202 Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
1204 // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
1205 // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
1206 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
1207 Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
1208 Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
1209 Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
1214 // test xmlRoot //////////////////////////////////////////////////////////
1216 public void TestSerializeXmlRootAttribute ()
1218 // constructor override & element name
1219 XmlRootAttribute root
= new XmlRootAttribute ();
1220 root
.ElementName
= "renamed";
1222 SimpleClassWithXmlAttributes simpleWithAttributes
= new SimpleClassWithXmlAttributes ();
1223 Serialize (simpleWithAttributes
, root
);
1224 Assert
.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1226 SimpleClass simple
= null;
1227 root
.IsNullable
= false;
1229 Serialize (simple
, root
);
1230 Assert
.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
1231 } catch (NullReferenceException
) {
1234 root
.IsNullable
= true;
1236 Serialize (simple
, root
);
1237 Assert
.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
1238 } catch (NullReferenceException
) {
1241 simple
= new SimpleClass ();
1242 root
.ElementName
= null;
1243 root
.Namespace
= "some.urn";
1244 Serialize (simple
, root
);
1245 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
);
1249 public void TestSerializeXmlRootAttributeOnMember ()
1252 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1253 XmlAttributes childAttr
= new XmlAttributes ();
1254 childAttr
.XmlRoot
= new XmlRootAttribute ("simple");
1255 overrides
.Add (typeof (SimpleClass
), childAttr
);
1257 XmlAttributes attr
= new XmlAttributes ();
1258 attr
.XmlRoot
= new XmlRootAttribute ("simple");
1259 overrides
.Add (typeof (ClassArrayContainer
), attr
);
1261 ClassArrayContainer container
= new ClassArrayContainer ();
1262 container
.items
= new SimpleClass
[1];
1263 container
.items
[0] = new SimpleClass ();
1264 Serialize (container
, overrides
);
1265 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
);
1267 // FIXME test data type
1270 // test XmlAttribute /////////////////////////////////////////////////////
1272 public void TestSerializeXmlAttributeAttribute ()
1275 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1276 XmlAttributes attr
= new XmlAttributes ();
1277 attr
.XmlAttribute
= new XmlAttributeAttribute ();
1278 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1280 SimpleClass simple
= new SimpleClass (); ;
1281 Serialize (simple
, overrides
);
1282 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#1");
1285 simple
.something
= "hello";
1286 Serialize (simple
, overrides
);
1287 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");
1290 attr
.XmlAttribute
.AttributeName
= "somethingelse";
1291 Serialize (simple
, overrides
);
1292 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");
1295 // FIXME this should work, shouldnt it?
1296 // attr.XmlAttribute.Type = typeof(string);
1297 // Serialize(simple, overrides);
1298 // Assert(WriterText.EndsWith(" something='hello' />"));
1301 attr
.XmlAttribute
.Namespace
= "some:urn";
1302 Serialize (simple
, overrides
);
1303 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");
1306 // FIXME XmlSchemaForm Form
1308 // FIXME write XmlQualifiedName as attribute
1311 // test XmlElement ///////////////////////////////////////////////////////
1313 public void TestSerializeXmlElementAttribute ()
1315 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1316 XmlAttributes attr
= new XmlAttributes ();
1317 XmlElementAttribute element
= new XmlElementAttribute ();
1318 attr
.XmlElements
.Add (element
);
1319 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1322 SimpleClass simple
= new SimpleClass (); ;
1323 Serialize (simple
, overrides
);
1324 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#1");
1327 simple
.something
= "hello";
1328 Serialize (simple
, overrides
);
1329 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");
1332 element
.ElementName
= "saying";
1333 Serialize (simple
, overrides
);
1334 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");
1337 element
.IsNullable
= false;
1338 simple
.something
= null;
1339 Serialize (simple
, overrides
);
1340 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#4");
1342 element
.IsNullable
= true;
1343 simple
.something
= null;
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'><saying xsi:nil='true' /></SimpleClass>"), WriterText
, "#5");
1348 element
.ElementName
= null;
1349 element
.IsNullable
= false;
1350 element
.Namespace
= "some:urn";
1351 simple
.something
= "hello";
1352 Serialize (simple
, overrides
);
1353 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText
, "#6");
1360 // test XmlElementAttribute with arrays and collections //////////////////
1362 public void TestSerializeCollectionWithXmlElementAttribute ()
1365 // if no type is specified or the specified type
1366 // matches the contents of the collection,
1367 // serialize each element in an element named after the member.
1368 // if the type does not match, or matches the collection itself,
1369 // create a base wrapping element for the member, and then
1370 // wrap each collection item in its own wrapping element based on type.
1372 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1373 XmlAttributes attr
= new XmlAttributes ();
1374 XmlElementAttribute element
= new XmlElementAttribute ();
1375 attr
.XmlElements
.Add (element
);
1376 overrides
.Add (typeof (StringCollectionContainer
), "Messages", attr
);
1378 // empty collection & no type info in XmlElementAttribute
1379 StringCollectionContainer container
= new StringCollectionContainer ();
1380 Serialize (container
, overrides
);
1381 Assert
.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#1");
1383 // non-empty collection & no type info in XmlElementAttribute
1384 container
.Messages
.Add ("hello");
1385 Serialize (container
, overrides
);
1386 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");
1388 // non-empty collection & only type info in XmlElementAttribute
1389 element
.Type
= typeof (StringCollection
);
1390 Serialize (container
, overrides
);
1391 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");
1393 // non-empty collection & only type info in XmlElementAttribute
1394 element
.Type
= typeof (string);
1395 Serialize (container
, overrides
);
1396 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");
1399 container
.Messages
.Add ("goodbye");
1400 element
.Type
= null;
1401 Serialize (container
, overrides
);
1402 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");
1405 // test DefaultValue /////////////////////////////////////////////////////
1407 public void TestSerializeDefaultValueAttribute ()
1409 XmlAttributeOverrides overrides
= new XmlAttributeOverrides ();
1411 XmlAttributes attr
= new XmlAttributes ();
1412 string defaultValueInstance
= "nothing";
1413 attr
.XmlDefaultValue
= defaultValueInstance
;
1414 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1417 SimpleClass simple
= new SimpleClass ();
1418 Serialize (simple
, overrides
);
1419 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#A1");
1421 // same value as default
1422 simple
.something
= defaultValueInstance
;
1423 Serialize (simple
, overrides
);
1424 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#A2");
1427 simple
.something
= "hello";
1428 Serialize (simple
, overrides
);
1429 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");
1431 overrides
= new XmlAttributeOverrides ();
1432 attr
= new XmlAttributes ();
1433 attr
.XmlAttribute
= new XmlAttributeAttribute ();
1434 attr
.XmlDefaultValue
= defaultValueInstance
;
1435 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1438 simple
= new SimpleClass ();
1439 Serialize (simple
, overrides
);
1440 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#B1");
1442 // same value as default
1443 simple
.something
= defaultValueInstance
;
1444 Serialize (simple
, overrides
);
1445 Assert
.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
, "#B2");
1448 simple
.something
= "hello";
1449 Serialize (simple
, overrides
);
1450 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");
1452 overrides
= new XmlAttributeOverrides ();
1453 attr
= new XmlAttributes ();
1454 attr
.XmlAttribute
= new XmlAttributeAttribute ("flagenc");
1455 overrides
.Add (typeof (TestDefault
), "flagencoded", attr
);
1458 TestDefault testDefault
= new TestDefault ();
1459 Serialize (testDefault
);
1460 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");
1462 // use the default with overrides
1463 Serialize (testDefault
, overrides
);
1464 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");
1466 overrides
= new XmlAttributeOverrides ();
1467 attr
= new XmlAttributes ();
1468 attr
.XmlAttribute
= new XmlAttributeAttribute ("flagenc");
1469 attr
.XmlDefaultValue
= (FlagEnum_Encoded
.e1
| FlagEnum_Encoded
.e4
); // add default again
1470 overrides
.Add (typeof (TestDefault
), "flagencoded", attr
);
1472 // use the default with overrides
1473 Serialize (testDefault
, overrides
);
1474 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");
1476 // use the default with overrides and default namspace
1477 Serialize (testDefault
, overrides
, AnotherNamespace
);
1478 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");
1480 // non-default values
1481 testDefault
.strDefault
= "Some Text";
1482 testDefault
.boolT
= false;
1483 testDefault
.boolF
= true;
1484 testDefault
.decimalval
= 20m
;
1485 testDefault
.flag
= FlagEnum
.e2
;
1486 testDefault
.flagencoded
= FlagEnum_Encoded
.e2
| FlagEnum_Encoded
.e1
;
1487 Serialize (testDefault
);
1488 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1489 "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1490 " <strDefault>Some Text</strDefault>" +
1491 " <boolT>false</boolT>" +
1492 " <boolF>true</boolF>" +
1493 " <decimalval>20</decimalval>" +
1494 " <flag>two</flag>" +
1495 " <flagencoded>e1 e2</flagencoded>" +
1496 "</testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1499 Serialize (testDefault
, overrides
);
1500 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1501 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1502 " <strDefault>Some Text</strDefault>" +
1503 " <boolT>false</boolT>" +
1504 " <boolF>true</boolF>" +
1505 " <decimalval>20</decimalval>" +
1506 " <flag>two</flag>" +
1507 "</testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1510 Serialize (testDefault
, overrides
, AnotherNamespace
);
1511 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1512 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1513 " <strDefault>Some Text</strDefault>" +
1514 " <boolT>false</boolT>" +
1515 " <boolF>true</boolF>" +
1516 " <decimalval>20</decimalval>" +
1517 " <flag>two</flag>" +
1518 "</testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1521 attr
= new XmlAttributes ();
1522 XmlTypeAttribute xmlType
= new XmlTypeAttribute ("flagenum");
1523 xmlType
.Namespace
= "yetanother:urn";
1524 attr
.XmlType
= xmlType
;
1525 overrides
.Add (typeof (FlagEnum_Encoded
), attr
);
1527 Serialize (testDefault
, overrides
, AnotherNamespace
);
1528 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1529 "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1530 " <strDefault>Some Text</strDefault>" +
1531 " <boolT>false</boolT>" +
1532 " <boolF>true</boolF>" +
1533 " <decimalval>20</decimalval>" +
1534 " <flag>two</flag>" +
1535 "</testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1538 attr
= new XmlAttributes ();
1539 attr
.XmlType
= new XmlTypeAttribute ("testDefault");
1540 overrides
.Add (typeof (TestDefault
), attr
);
1542 Serialize (testDefault
, overrides
, AnotherNamespace
);
1543 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1544 "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1545 " <strDefault>Some Text</strDefault>" +
1546 " <boolT>false</boolT>" +
1547 " <boolF>true</boolF>" +
1548 " <decimalval>20</decimalval>" +
1549 " <flag>two</flag>" +
1550 "</testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
,
1551 AnotherNamespace
)), WriterText
, "#C9");
1555 public void TestSerializeDefaultValueAttribute_Encoded ()
1557 SoapAttributeOverrides overrides
= new SoapAttributeOverrides ();
1558 SoapAttributes attr
= new SoapAttributes ();
1559 attr
.SoapAttribute
= new SoapAttributeAttribute ();
1560 string defaultValueInstance
= "nothing";
1561 attr
.SoapDefaultValue
= defaultValueInstance
;
1562 overrides
.Add (typeof (SimpleClass
), "something", attr
);
1565 SimpleClass simple
= new SimpleClass ();
1566 SerializeEncoded (simple
, overrides
);
1567 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");
1569 // same value as default
1570 simple
.something
= defaultValueInstance
;
1571 SerializeEncoded (simple
, overrides
);
1572 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");
1575 simple
.something
= "hello";
1576 SerializeEncoded (simple
, overrides
);
1577 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");
1579 attr
.SoapAttribute
= null;
1580 attr
.SoapElement
= new SoapElementAttribute ();
1583 simple
= new SimpleClass ();
1584 SerializeEncoded (simple
, overrides
);
1585 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");
1587 // same value as default
1588 simple
.something
= defaultValueInstance
;
1589 SerializeEncoded (simple
, overrides
);
1590 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");
1593 simple
.something
= "hello";
1594 SerializeEncoded (simple
, overrides
);
1595 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");
1597 overrides
= new SoapAttributeOverrides ();
1598 attr
= new SoapAttributes ();
1599 attr
.SoapElement
= new SoapElementAttribute ("flagenc");
1600 overrides
.Add (typeof (TestDefault
), "flagencoded", attr
);
1602 // use the default (from MS KB325691)
1603 TestDefault testDefault
= new TestDefault ();
1604 SerializeEncoded (testDefault
);
1605 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1606 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1607 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1608 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1609 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1610 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1611 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1612 " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
1613 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1616 SerializeEncoded (testDefault
, overrides
);
1617 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1618 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1619 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1620 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1621 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1622 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1623 " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
1624 " <flagenc xsi:type='flagenum'>one four</flagenc>" +
1625 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1628 SerializeEncoded (testDefault
, overrides
, AnotherNamespace
);
1629 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1630 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1631 " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
1632 " <boolT xsi:type='xsd:boolean'>true</boolT>" +
1633 " <boolF xsi:type='xsd:boolean'>false</boolF>" +
1634 " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
1635 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
1636 " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
1637 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
,
1638 AnotherNamespace
)), WriterText
, "#C3");
1640 // non-default values
1641 testDefault
.strDefault
= "Some Text";
1642 testDefault
.boolT
= false;
1643 testDefault
.boolF
= true;
1644 testDefault
.decimalval
= 20m
;
1645 testDefault
.flag
= FlagEnum
.e2
;
1646 testDefault
.flagencoded
= FlagEnum_Encoded
.e2
| FlagEnum_Encoded
.e1
;
1647 SerializeEncoded (testDefault
);
1648 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1649 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1650 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1651 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1652 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1653 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1654 " <flag xsi:type='FlagEnum'>e2</flag>" +
1655 " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
1656 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1659 SerializeEncoded (testDefault
, overrides
);
1660 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1661 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1662 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1663 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1664 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1665 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1666 " <flag xsi:type='FlagEnum'>e2</flag>" +
1667 " <flagenc xsi:type='flagenum'>one two</flagenc>" +
1668 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)),
1671 attr
= new SoapAttributes ();
1672 attr
.SoapType
= new SoapTypeAttribute ("flagenum", "yetanother:urn");
1673 overrides
.Add (typeof (FlagEnum_Encoded
), attr
);
1675 SerializeEncoded (testDefault
, overrides
, AnotherNamespace
);
1676 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1677 "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1678 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1679 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1680 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1681 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1682 " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
1683 " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
1684 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
,
1685 AnotherNamespace
)), WriterText
, "#C6");
1687 attr
= new SoapAttributes ();
1688 attr
.SoapType
= new SoapTypeAttribute ("testDefault");
1689 overrides
.Add (typeof (TestDefault
), attr
);
1691 SerializeEncoded (testDefault
, overrides
, AnotherNamespace
);
1692 Assert
.AreEqual (Infoset (string.Format (CultureInfo
.InvariantCulture
,
1693 "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
1694 " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
1695 " <boolT xsi:type='xsd:boolean'>false</boolT>" +
1696 " <boolF xsi:type='xsd:boolean'>true</boolF>" +
1697 " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
1698 " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
1699 " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
1700 "</q1:testDefault>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
,
1701 AnotherNamespace
)), WriterText
, "#C7");
1704 // test XmlEnum //////////////////////////////////////////////////////////
1706 public void TestSerializeXmlEnumAttribute ()
1708 Serialize (XmlSchemaForm
.Qualified
);
1709 Assert
.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText
, "#1");
1711 Serialize (XmlSchemaForm
.Unqualified
);
1712 Assert
.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText
, "#2");
1716 public void TestSerializeXmlEnumAttribute_IgnoredValue ()
1718 // technically XmlSchemaForm.None has an XmlIgnore attribute,
1719 // but it is not being serialized as a member.
1722 Serialize (XmlSchemaForm
.None
);
1724 } catch (InvalidOperationException ex
) {
1725 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
1726 Assert
.IsNotNull (ex
.InnerException
, "#3");
1727 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.InnerException
.GetType (), "#4");
1728 Assert
.IsNotNull (ex
.InnerException
.Message
, "#5");
1729 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf ("'0'") != -1, "#6");
1730 Assert
.IsTrue (ex
.InnerException
.Message
.IndexOf (typeof (XmlSchemaForm
).FullName
) != -1, "#7");
1735 public void TestSerializeXmlNodeArray ()
1737 XmlDocument doc
= new XmlDocument ();
1738 Serialize (new XmlNode
[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
, typeof (object));
1739 Assert
.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText
);
1743 public void TestSerializeXmlNodeArray2 ()
1745 XmlDocument doc
= new XmlDocument ();
1746 Serialize (new XmlNode
[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
, typeof (XmlNode
[]));
1747 Assert
.AreEqual (Infoset (String
.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema
.Namespace
, XmlSchema
.InstanceNamespace
)), WriterText
);
1751 [ExpectedException (typeof (InvalidOperationException
))]
1752 [Category ("MobileNotWorking")]
1753 public void TestSerializeXmlNodeArrayIncludesAttribute ()
1755 XmlDocument doc
= new XmlDocument ();
1756 Serialize (new XmlNode
[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
, typeof (XmlNode
[]));
1760 public void TestSerializeXmlElementArray ()
1762 XmlDocument doc
= new XmlDocument ();
1763 Serialize (new XmlElement
[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
, typeof (object));
1764 Assert
.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText
);
1768 [ExpectedException (typeof (InvalidOperationException
))] // List<XmlNode> is not supported
1769 public void TestSerializeGenericListOfNode ()
1771 XmlDocument doc
= new XmlDocument ();
1772 Serialize (new List
<XmlNode
> (new XmlNode
[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
), typeof (object));
1773 Assert
.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText
);
1777 [ExpectedException (typeof (InvalidOperationException
))] // List<XmlElement> is not supported
1778 public void TestSerializeGenericListOfElement ()
1780 XmlDocument doc
= new XmlDocument ();
1781 Serialize (new List
<XmlElement
> (new XmlElement
[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }
), typeof (object));
1782 Assert
.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText
);
1785 public void TestSerializeXmlDocument ()
1787 XmlDocument doc
= new XmlDocument ();
1788 doc
.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
1789 Serialize (doc
, typeof (XmlDocument
));
1790 Assert
.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
1791 sw
.GetStringBuilder ().ToString ());
1795 public void TestSerializeXmlElement ()
1797 XmlDocument doc
= new XmlDocument ();
1798 Serialize (doc
.CreateElement ("elem"), typeof (XmlElement
));
1799 Assert
.AreEqual (Infoset ("<elem/>"), WriterText
);
1803 public void TestSerializeXmlElementSubclass ()
1805 XmlDocument doc
= new XmlDocument ();
1806 Serialize (new MyElem (doc
), typeof (XmlElement
));
1807 Assert
.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText
, "#1");
1809 Serialize (new MyElem (doc
), typeof (MyElem
));
1810 Assert
.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText
, "#2");
1814 public void TestSerializeXmlCDataSection ()
1816 XmlDocument doc
= new XmlDocument ();
1817 CDataContainer c
= new CDataContainer ();
1818 c
.cdata
= doc
.CreateCDataSection ("data section contents");
1820 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
);
1824 public void TestSerializeXmlNode ()
1826 XmlDocument doc
= new XmlDocument ();
1827 NodeContainer c
= new NodeContainer ();
1828 c
.node
= doc
.CreateTextNode ("text");
1830 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
);
1834 public void TestSerializeChoice ()
1836 Choices ch
= new Choices ();
1837 ch
.MyChoice
= "choice text";
1838 ch
.ItemType
= ItemChoiceType
.ChoiceZero
;
1840 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");
1841 ch
.ItemType
= ItemChoiceType
.StrangeOne
;
1843 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");
1844 ch
.ItemType
= ItemChoiceType
.ChoiceTwo
;
1846 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");
1850 public void TestSerializeNamesWithSpaces ()
1852 TestSpace ts
= new TestSpace ();
1856 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
);
1860 public void TestSerializeReadOnlyProps ()
1862 ReadOnlyProperties ts
= new ReadOnlyProperties ();
1864 Assert
.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
1868 public void TestSerializeReadOnlyListProp ()
1870 ReadOnlyListProperty ts
= new ReadOnlyListProperty ();
1872 Assert
.AreEqual (Infoset ("<ReadOnlyListProperty xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><StrList><string>listString1</string><string>listString2</string></StrList></ReadOnlyListProperty>"), WriterText
);
1877 public void TestSerializeIList ()
1879 clsPerson k
= new clsPerson ();
1880 k
.EmailAccounts
= new ArrayList ();
1881 k
.EmailAccounts
.Add ("a");
1882 k
.EmailAccounts
.Add ("b");
1884 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
);
1888 public void TestSerializeArrayEnc ()
1890 SoapReflectionImporter imp
= new SoapReflectionImporter ();
1891 XmlTypeMapping map
= imp
.ImportTypeMapping (typeof (ArrayClass
));
1892 XmlSerializer ser
= new XmlSerializer (map
);
1893 StringWriter sw
= new StringWriter ();
1894 XmlTextWriter tw
= new XmlTextWriter (sw
);
1895 tw
.WriteStartElement ("aa");
1896 ser
.Serialize (tw
, new ArrayClass ());
1897 tw
.WriteEndElement ();
1900 [Test
] // bug #76049
1901 public void TestIncludeType ()
1903 XmlReflectionImporter imp
= new XmlReflectionImporter ();
1904 XmlTypeMapping map
= imp
.ImportTypeMapping (typeof (object));
1905 imp
.IncludeType (typeof (TestSpace
));
1906 XmlSerializer ser
= new XmlSerializer (map
);
1907 ser
.Serialize (new StringWriter (), new TestSpace ());
1911 public void TestSerializeChoiceArray ()
1913 CompositeValueType v
= new CompositeValueType ();
1916 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
);
1920 public void TestArrayAttributeWithDataType ()
1922 Serialize (new ArrayAttributeWithType ());
1923 string res
= "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
1924 res
+= "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
1925 Assert
.AreEqual (Infoset (res
), WriterText
);
1929 public void TestSubclassElementType ()
1931 SubclassTestContainer c
= new SubclassTestContainer ();
1932 c
.data
= new SubclassTestSub ();
1935 string res
= "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1936 res
+= "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
1937 Assert
.AreEqual (Infoset (res
), WriterText
);
1940 [Test
] // Covers #36829
1941 public void TestSubclassElementList ()
1943 var o
= new SubclassTestList () { Items = new List<object> () { new SubclassTestSub () }
};
1946 string res
= "<SubclassTestList xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
1947 res
+= "<b xsi:type=\"SubclassTestSub\"/></SubclassTestList>";
1948 Assert
.AreEqual (Infoset (res
), WriterText
);
1952 [ExpectedException (typeof (InvalidOperationException
))]
1953 public void TestArrayAttributeWithWrongDataType ()
1955 Serialize (new ArrayAttributeWithWrongType ());
1959 [Category ("NotWorking")]
1960 public void TestSerializePrimitiveTypesContainer ()
1962 Serialize (new PrimitiveTypesContainer ());
1963 // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Not a secret.")]
1964 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
1965 "<?xml version='1.0' encoding='utf-16'?>" +
1966 "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
1967 "<Number>2004</Number>" +
1968 "<Name>some name</Name>" +
1969 "<Index>56</Index>" +
1970 "<Password>8w8=</Password>" +
1971 "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
1972 "</PrimitiveTypesContainer>", XmlSchema
.Namespace
,
1973 XmlSchema
.InstanceNamespace
), sw
.ToString (), "#1");
1975 SerializeEncoded (new PrimitiveTypesContainer ());
1976 Assert
.AreEqual (string.Format (CultureInfo
.InvariantCulture
,
1977 "<?xml version='1.0' encoding='utf-16'?>" +
1978 "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
1979 "<Number xsi:type='xsd:int'>2004</Number>" +
1980 "<Name xsi:type='xsd:string'>some name</Name>" +
1981 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1982 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1983 "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
1984 "</q1:PrimitiveTypesContainer>", XmlSchema
.Namespace
,
1985 XmlSchema
.InstanceNamespace
, AnotherNamespace
, WsdlTypesNamespace
),
1986 sw
.ToString (), "#2");
1990 public void TestSchemaForm ()
1992 TestSchemaForm1 t1
= new TestSchemaForm1 ();
1993 t1
.p1
= new PrintTypeResponse ();
1995 t1
.p2
= new PrintTypeResponse ();
1998 TestSchemaForm2 t2
= new TestSchemaForm2 ();
1999 t2
.p1
= new PrintTypeResponse ();
2001 t2
.p2
= new PrintTypeResponse ();
2006 res
+= "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2007 res
+= "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2010 res
+= " <data>data1</data>";
2011 res
+= " </result>";
2012 res
+= " <intern xmlns=\"urn:responseTypes\">";
2013 res
+= " <result xmlns=\"\">";
2014 res
+= " <data>data2</data>";
2015 res
+= " </result>";
2016 res
+= " </intern>";
2018 res
+= " <p2 xmlns=\"urn:oo\">";
2019 res
+= " <result xmlns=\"\">";
2020 res
+= " <data>data1</data>";
2021 res
+= " </result>";
2022 res
+= " <intern xmlns=\"urn:responseTypes\">";
2023 res
+= " <result xmlns=\"\">";
2024 res
+= " <data>data2</data>";
2025 res
+= " </result>";
2026 res
+= " </intern>";
2028 res
+= "</TestSchemaForm1>";
2029 Assert
.AreEqual (Infoset (res
), WriterText
);
2033 res
+= "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2034 res
+= "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
2035 res
+= " <p1 xmlns=\"urn:testForm\">";
2036 res
+= " <result xmlns=\"\">";
2037 res
+= " <data>data1</data>";
2038 res
+= " </result>";
2039 res
+= " <intern xmlns=\"urn:responseTypes\">";
2040 res
+= " <result xmlns=\"\">";
2041 res
+= " <data>data2</data>";
2042 res
+= " </result>";
2043 res
+= " </intern>";
2045 res
+= " <p2 xmlns=\"urn:oo\">";
2046 res
+= " <result xmlns=\"\">";
2047 res
+= " <data>data1</data>";
2048 res
+= " </result>";
2049 res
+= " <intern xmlns=\"urn:responseTypes\">";
2050 res
+= " <result xmlns=\"\">";
2051 res
+= " <data>data2</data>";
2052 res
+= " </result>";
2053 res
+= " </intern>";
2055 res
+= "</TestSchemaForm2>";
2056 Assert
.AreEqual (Infoset (res
), WriterText
);
2058 XmlReflectionImporter imp
= new XmlReflectionImporter ();
2059 XmlTypeMapping map
= imp
.ImportTypeMapping (typeof (TestSchemaForm1
), "urn:extra");
2060 Serialize (t1
, map
);
2062 res
+= "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
2063 res
+= "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
2065 res
+= " <result xmlns=\"\">";
2066 res
+= " <data>data1</data>";
2067 res
+= " </result>";
2068 res
+= " <intern xmlns=\"urn:responseTypes\">";
2069 res
+= " <result xmlns=\"\">";
2070 res
+= " <data>data2</data>";
2071 res
+= " </result>";
2072 res
+= " </intern>";
2074 res
+= " <p2 xmlns=\"urn:oo\">";
2075 res
+= " <result xmlns=\"\">";
2076 res
+= " <data>data1</data>";
2077 res
+= " </result>";
2078 res
+= " <intern xmlns=\"urn:responseTypes\">";
2079 res
+= " <result xmlns=\"\">";
2080 res
+= " <data>data2</data>";
2081 res
+= " </result>";
2082 res
+= " </intern>";
2084 res
+= "</TestSchemaForm1>";
2085 Assert
.AreEqual (Infoset (res
), WriterText
);
2087 imp
= new XmlReflectionImporter ();
2088 map
= imp
.ImportTypeMapping (typeof (TestSchemaForm2
), "urn:extra");
2089 Serialize (t2
, map
);
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\" xmlns=\"urn:extra\">";
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
);
2117 [Test
] // bug #78536
2118 public void CDataTextNodes ()
2120 XmlSerializer ser
= new XmlSerializer (typeof (CDataTextNodesType
));
2121 ser
.UnknownNode
+= new XmlNodeEventHandler (CDataTextNodes_BadNode
);
2122 string xml
= @"<CDataTextNodesType>
2124 (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
2125 \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
2126 (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
2128 </CDataTextNodesType>";
2129 ser
.Deserialize (new XmlTextReader (xml
, XmlNodeType
.Document
, null));
2134 public void GenerateSerializerGenerics ()
2136 XmlReflectionImporter imp
= new XmlReflectionImporter ();
2137 Type type
= typeof (List
<int>);
2138 XmlSerializer
.GenerateSerializer (
2140 new XmlTypeMapping
[] {imp.ImportTypeMapping (type)}
);
2145 public void Nullable ()
2147 XmlSerializer ser
= new XmlSerializer (typeof (int?));
2148 int? nullableType
= 5;
2149 sw
= new StringWriter ();
2150 xtw
= new XmlTextWriter (sw
);
2151 ser
.Serialize (xtw
, nullableType
);
2153 string expected
= "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
2154 Assert
.AreEqual (Infoset (expected
), WriterText
);
2155 int? i
= (int?) ser
.Deserialize (new StringReader (sw
.ToString ()));
2156 Assert
.AreEqual (5, i
);
2160 public void NullableEnums ()
2162 WithNulls w
= new WithNulls ();
2163 XmlSerializer ser
= new XmlSerializer (typeof(WithNulls
));
2164 StringWriter tw
= new StringWriter ();
2165 ser
.Serialize (tw
, w
);
2167 string expected
= "<?xml version='1.0' encoding='utf-16'?>" +
2168 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2169 "<nint xsi:nil='true' />" +
2170 "<nenum xsi:nil='true' />" +
2171 "<ndate xsi:nil='true' />" +
2174 Assert
.AreEqual (Infoset (expected
), Infoset (tw
.ToString ()));
2176 StringReader sr
= new StringReader (tw
.ToString ());
2177 w
= (WithNulls
) ser
.Deserialize (sr
);
2179 Assert
.IsFalse (w
.nint
.HasValue
);
2180 Assert
.IsFalse (w
.nenum
.HasValue
);
2181 Assert
.IsFalse (w
.ndate
.HasValue
);
2183 DateTime t
= new DateTime (2008,4,1);
2186 w
.nenum
= TestEnumWithNulls
.bb
;
2188 tw
= new StringWriter ();
2189 ser
.Serialize (tw
, w
);
2191 expected
= "<?xml version='1.0' encoding='utf-16'?>" +
2192 "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
2194 "<nenum>bb</nenum>" +
2195 "<ndate>2008-04-01T00:00:00</ndate>" +
2198 Assert
.AreEqual (Infoset (expected
), Infoset (tw
.ToString ()));
2200 sr
= new StringReader (tw
.ToString ());
2201 w
= (WithNulls
) ser
.Deserialize (sr
);
2203 Assert
.IsTrue (w
.nint
.HasValue
);
2204 Assert
.IsTrue (w
.nenum
.HasValue
);
2205 Assert
.IsTrue (w
.ndate
.HasValue
);
2206 Assert
.AreEqual (4, w
.nint
.Value
);
2207 Assert
.AreEqual (TestEnumWithNulls
.bb
, w
.nenum
.Value
);
2208 Assert
.AreEqual (t
, w
.ndate
.Value
);
2212 public void SerializeBase64Binary()
2214 XmlSerializer ser
= new XmlSerializer (typeof (Base64Binary
));
2215 sw
= new StringWriter ();
2216 XmlTextWriter xtw
= new XmlTextWriter (sw
);
2217 ser
.Serialize (xtw
, new Base64Binary ());
2219 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"" />";
2220 Assert
.AreEqual (Infoset (expected
), WriterText
);
2221 Base64Binary h
= (Base64Binary
) ser
.Deserialize (new StringReader (sw
.ToString ()));
2222 Assert
.AreEqual (new byte [] {1, 2, 3}
, h
.Data
);
2225 [Test
] // bug #79989, #79990
2226 public void SerializeHexBinary ()
2228 XmlSerializer ser
= new XmlSerializer (typeof (HexBinary
));
2229 sw
= new StringWriter ();
2230 XmlTextWriter xtw
= new XmlTextWriter (sw
);
2231 ser
.Serialize (xtw
, new HexBinary ());
2233 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"" />";
2234 Assert
.AreEqual (Infoset (expected
), WriterText
);
2235 HexBinary h
= (HexBinary
) ser
.Deserialize (new StringReader (sw
.ToString ()));
2236 Assert
.AreEqual (new byte[] { 1, 2, 3 }
, h
.Data
);
2240 [ExpectedException (typeof (InvalidOperationException
))]
2241 public void XmlArrayAttributeOnInt ()
2243 new XmlSerializer (typeof (XmlArrayOnInt
));
2247 [Category ("MobileNotWorking")]
2248 public void XmlArrayAttributeUnqualifiedWithNamespace ()
2250 new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace
));
2254 [Category ("MobileNotWorking")]
2255 public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
2257 new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace
));
2260 [Test
] // bug #78042
2261 public void XmlArrayAttributeOnArray ()
2263 XmlSerializer ser
= new XmlSerializer (typeof (XmlArrayOnArray
));
2264 sw
= new StringWriter ();
2265 XmlTextWriter xtw
= new XmlTextWriter (sw
);
2266 ser
.Serialize (xtw
, new XmlArrayOnArray ());
2268 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>";
2269 Assert
.AreEqual (Infoset (expected
), WriterText
);
2273 public void XmlArrayAttributeOnCollection ()
2275 XmlSerializer ser
= new XmlSerializer (typeof (XmlArrayOnArrayList
));
2276 XmlArrayOnArrayList inst
= new XmlArrayOnArrayList ();
2277 inst
.Sane
.Add ("abc");
2279 sw
= new StringWriter ();
2280 XmlTextWriter xtw
= new XmlTextWriter (sw
);
2281 ser
.Serialize (xtw
, inst
);
2283 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>";
2284 Assert
.AreEqual (Infoset (expected
), WriterText
);
2287 [Test
] // bug #338705
2288 public void SerializeTimeSpan ()
2290 // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
2291 XmlSerializer ser
= new XmlSerializer (typeof (TimeSpan
));
2292 ser
.Serialize (TextWriter
.Null
, TimeSpan
.Zero
);
2296 public void SerializeDurationToString ()
2298 XmlSerializer ser
= new XmlSerializer (typeof (TimeSpanContainer1
));
2299 ser
.Serialize (TextWriter
.Null
, new TimeSpanContainer1 ());
2303 [ExpectedException (typeof (InvalidOperationException
))]
2304 public void SerializeDurationToTimeSpan ()
2306 XmlSerializer ser
= new XmlSerializer (typeof (TimeSpanContainer2
));
2307 ser
.Serialize (TextWriter
.Null
, new TimeSpanContainer2 ());
2311 [ExpectedException (typeof (InvalidOperationException
))]
2312 public void SerializeInvalidDataType ()
2314 XmlSerializer ser
= new XmlSerializer (typeof (InvalidTypeContainer
));
2315 ser
.Serialize (TextWriter
.Null
, new InvalidTypeContainer ());
2319 public void SerializeErrorneousIXmlSerializable ()
2321 Serialize (new ErrorneousGetSchema ());
2322 Assert
.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw
.ToString ()));
2326 public void DateTimeRoundtrip ()
2329 XmlSerializer ser
= new XmlSerializer (typeof (DateTime
));
2330 StringWriter sw
= new StringWriter ();
2331 ser
.Serialize (sw
, DateTime
.UtcNow
);
2332 DateTime d
= (DateTime
) ser
.Deserialize (new StringReader (sw
.ToString ()));
2333 Assert
.AreEqual (DateTimeKind
.Utc
, d
.Kind
);
2337 public void SupportIXmlSerializableImplicitlyConvertible ()
2339 XmlAttributes attrs
= new XmlAttributes ();
2340 XmlElementAttribute attr
= new XmlElementAttribute ();
2341 attr
.ElementName
= "XmlSerializable";
2342 attr
.Type
= typeof (XmlSerializableImplicitConvertible
.XmlSerializable
);
2343 attrs
.XmlElements
.Add (attr
);
2344 XmlAttributeOverrides attrOverrides
= new
2345 XmlAttributeOverrides ();
2346 attrOverrides
.Add (typeof (XmlSerializableImplicitConvertible
), "B", attrs
);
2348 XmlSerializableImplicitConvertible x
= new XmlSerializableImplicitConvertible ();
2349 new XmlSerializer (typeof (XmlSerializableImplicitConvertible
), attrOverrides
).Serialize (TextWriter
.Null
, x
);
2352 [Test
] // bug #566370
2353 public void SerializeEnumWithCSharpKeyword ()
2355 var ser
= new XmlSerializer (typeof (DoxCompoundKind
));
2356 for (int i
= 0; i
< 100; i
++) // test serialization code generator
2357 ser
.Serialize (Console
.Out
, DoxCompoundKind
.@class);
2360 public enum DoxCompoundKind
2367 [XmlEnum("interface")]
2373 [XmlEnum("namespace")]
2381 #region GenericsSeralizationTests
2384 public void TestSerializeGenSimpleClassString ()
2386 GenSimpleClass
<string> simple
= new GenSimpleClass
<string> ();
2388 Assert
.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText
);
2390 simple
.something
= "hello";
2393 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
);
2397 public void TestSerializeGenSimpleClassBool ()
2399 GenSimpleClass
<bool> simple
= new GenSimpleClass
<bool> ();
2401 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
);
2403 simple
.something
= true;
2406 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
);
2410 public void TestSerializeGenSimpleStructInt ()
2412 GenSimpleStruct
<int> simple
= new GenSimpleStruct
<int> (0);
2414 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
);
2416 simple
.something
= 123;
2419 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
);
2423 public void TestSerializeGenListClassString ()
2425 GenListClass
<string> genlist
= new GenListClass
<string> ();
2426 Serialize (genlist
);
2427 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
);
2429 genlist
.somelist
.Add ("Value1");
2430 genlist
.somelist
.Add ("Value2");
2432 Serialize (genlist
);
2433 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
);
2437 public void TestSerializeGenListClassFloat ()
2439 GenListClass
<float> genlist
= new GenListClass
<float> ();
2440 Serialize (genlist
);
2441 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
);
2443 genlist
.somelist
.Add (1);
2444 genlist
.somelist
.Add (2.2F
);
2446 Serialize (genlist
);
2447 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
);
2451 public void TestSerializeGenListClassList ()
2453 GenListClass
<GenListClass
<int>> genlist
= new GenListClass
<GenListClass
<int>> ();
2454 Serialize (genlist
);
2455 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
);
2457 GenListClass
<int> inlist1
= new GenListClass
<int> ();
2458 inlist1
.somelist
.Add (1);
2459 inlist1
.somelist
.Add (2);
2460 GenListClass
<int> inlist2
= new GenListClass
<int> ();
2461 inlist2
.somelist
.Add (10);
2462 inlist2
.somelist
.Add (20);
2463 genlist
.somelist
.Add (inlist1
);
2464 genlist
.somelist
.Add (inlist2
);
2466 Serialize (genlist
);
2467 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
);
2471 public void TestSerializeGenListClassArray ()
2473 GenListClass
<GenArrayClass
<char>> genlist
= new GenListClass
<GenArrayClass
<char>> ();
2474 Serialize (genlist
);
2475 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
);
2477 GenArrayClass
<char> genarr1
= new GenArrayClass
<char> ();
2478 genarr1
.arr
[0] = 'a';
2479 genarr1
.arr
[1] = 'b';
2480 genlist
.somelist
.Add (genarr1
);
2481 GenArrayClass
<char> genarr2
= new GenArrayClass
<char> ();
2482 genarr2
.arr
[0] = 'd';
2483 genarr2
.arr
[1] = 'e';
2484 genarr2
.arr
[2] = 'f';
2485 genlist
.somelist
.Add (genarr2
);
2487 Serialize (genlist
);
2488 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
);
2492 public void TestSerializeGenTwoClassCharDouble ()
2494 GenTwoClass
<char, double> gentwo
= new GenTwoClass
<char, double> ();
2496 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
);
2498 gentwo
.something1
= 'a';
2499 gentwo
.something2
= 2.2;
2502 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
);
2506 public void TestSerializeGenDerivedClassDecimalShort ()
2508 GenDerivedClass
<decimal, short> derived
= new GenDerivedClass
<decimal, short> ();
2509 Serialize (derived
);
2510 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
);
2512 derived
.something1
= "Value1";
2513 derived
.something2
= 1;
2514 derived
.another1
= 1.1M
;
2515 derived
.another2
= -22;
2517 Serialize (derived
);
2518 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
);
2522 public void TestSerializeGenDerivedSecondClassByteUlong ()
2524 GenDerived2Class
<byte, ulong> derived2
= new GenDerived2Class
<byte, ulong> ();
2525 Serialize (derived2
);
2526 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
);
2528 derived2
.something1
= 1;
2529 derived2
.something2
= 222;
2530 derived2
.another1
= 111;
2531 derived2
.another2
= 222222;
2533 Serialize (derived2
);
2534 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
);
2538 public void TestSerializeGenNestedClass ()
2540 GenNestedClass
<string, int>.InnerClass
<bool> nested
=
2541 new GenNestedClass
<string, int>.InnerClass
<bool> ();
2543 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
);
2546 nested
.something
= true;
2549 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
);
2553 public void TestSerializeGenListClassListNested ()
2555 GenListClass
<GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>>> genlist
=
2556 new GenListClass
<GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>>> ();
2557 Serialize (genlist
);
2558 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
);
2560 GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>> inlist1
=
2561 new GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>> ();
2562 GenNestedClass
<int, int>.InnerClass
<string> inval1
= new GenNestedClass
<int, int>.InnerClass
<string> ();
2564 inval1
.something
= "ONE";
2565 inlist1
.somelist
.Add (inval1
);
2566 GenNestedClass
<int, int>.InnerClass
<string> inval2
= new GenNestedClass
<int, int>.InnerClass
<string> ();
2568 inval2
.something
= "TWO";
2569 inlist1
.somelist
.Add (inval2
);
2570 GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>> inlist2
=
2571 new GenListClass
<GenNestedClass
<int, int>.InnerClass
<string>> ();
2572 GenNestedClass
<int, int>.InnerClass
<string> inval3
= new GenNestedClass
<int, int>.InnerClass
<string> ();
2574 inval3
.something
= "THIRTY";
2575 inlist2
.somelist
.Add (inval3
);
2576 genlist
.somelist
.Add (inlist1
);
2577 genlist
.somelist
.Add (inlist2
);
2579 Serialize (genlist
);
2580 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
);
2583 public enum Myenum { one, two, three, four, five, six }
;
2585 public void TestSerializeGenArrayClassEnum ()
2587 GenArrayClass
<Myenum
> genarr
= new GenArrayClass
<Myenum
> ();
2589 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
);
2591 genarr
.arr
[0] = Myenum
.one
;
2592 genarr
.arr
[1] = Myenum
.three
;
2593 genarr
.arr
[2] = Myenum
.five
;
2596 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
);
2600 public void TestSerializeGenArrayStruct ()
2602 GenArrayClass
<GenSimpleStruct
<uint>> genarr
= new GenArrayClass
<GenSimpleStruct
<uint>> ();
2604 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
);
2606 GenSimpleStruct
<uint> genstruct
= new GenSimpleStruct
<uint> ();
2607 genstruct
.something
= 111;
2608 genarr
.arr
[0] = genstruct
;
2609 genstruct
.something
= 222;
2610 genarr
.arr
[1] = genstruct
;
2611 genstruct
.something
= 333;
2612 genarr
.arr
[2] = genstruct
;
2615 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
);
2619 public void TestSerializeGenArrayList ()
2621 GenArrayClass
<GenListClass
<string>> genarr
= new GenArrayClass
<GenListClass
<string>> ();
2623 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
);
2625 GenListClass
<string> genlist1
= new GenListClass
<string> ();
2626 genlist1
.somelist
.Add ("list1-val1");
2627 genlist1
.somelist
.Add ("list1-val2");
2628 genarr
.arr
[0] = genlist1
;
2629 GenListClass
<string> genlist2
= new GenListClass
<string> ();
2630 genlist2
.somelist
.Add ("list2-val1");
2631 genlist2
.somelist
.Add ("list2-val2");
2632 genlist2
.somelist
.Add ("list2-val3");
2633 genlist2
.somelist
.Add ("list2-val4");
2634 genarr
.arr
[1] = genlist2
;
2635 GenListClass
<string> genlist3
= new GenListClass
<string> ();
2636 genlist3
.somelist
.Add ("list3val");
2637 genarr
.arr
[2] = genlist3
;
2640 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
);
2644 public void TestSerializeGenComplexStruct ()
2646 GenComplexStruct
<int, string> complex
= new GenComplexStruct
<int, string> (0);
2647 Serialize (complex
);
2648 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
);
2650 complex
.something
= 123;
2651 complex
.simpleclass
.something
= 456;
2652 complex
.simplestruct
.something
= 789;
2653 GenListClass
<int> genlist
= new GenListClass
<int> ();
2654 genlist
.somelist
.Add (100);
2655 genlist
.somelist
.Add (200);
2656 complex
.listclass
= genlist
;
2657 GenArrayClass
<int> genarr
= new GenArrayClass
<int> ();
2661 complex
.arrayclass
= genarr
;
2662 complex
.twoclass
.something1
= 10;
2663 complex
.twoclass
.something2
= "Ten";
2664 complex
.derivedclass
.another1
= 1;
2665 complex
.derivedclass
.another2
= "one";
2666 complex
.derivedclass
.something1
= "two";
2667 complex
.derivedclass
.something2
= 2;
2668 complex
.derived2
.another1
= 3;
2669 complex
.derived2
.another2
= "three";
2670 complex
.derived2
.something1
= 4;
2671 complex
.derived2
.something2
= "four";
2672 complex
.nestedouter
.outer
= 5;
2673 complex
.nestedinner
.inner
= "six";
2674 complex
.nestedinner
.something
= 6;
2676 Serialize (complex
);
2677 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
);
2681 public void TestSerializeStreamPreserveUTFChars () {
2682 string foo
= "BÄR";
2683 XmlSerializer serializer
= new XmlSerializer (typeof (string));
2685 MemoryStream stream
= new MemoryStream ();
2687 serializer
.Serialize (stream
, foo
);
2688 stream
.Position
= 0;
2689 foo
= (string) serializer
.Deserialize (stream
);
2690 Assert
.AreEqual("BÄR", foo
);
2693 [Test
] // bug #80759
2694 public void HasNullableField ()
2696 Bug80759 foo
= new Bug80759 ();
2698 foo
.NullableInt
= 10;
2700 XmlSerializer serializer
= new XmlSerializer (typeof (Bug80759
));
2702 MemoryStream stream
= new MemoryStream ();
2704 serializer
.Serialize (stream
, foo
);
2705 stream
.Position
= 0;
2706 foo
= (Bug80759
) serializer
.Deserialize (stream
);
2709 [Test
] // bug #80759, with fieldSpecified.
2710 [ExpectedException (typeof (InvalidOperationException
))]
2711 [Category ("NotWorking")]
2712 public void HasFieldSpecifiedButIrrelevant ()
2714 Bug80759_2 foo
= new Bug80759_2 ();
2716 foo
.NullableInt
= 10;
2718 XmlSerializer serializer
= new XmlSerializer (typeof (Bug80759_2
));
2720 MemoryStream stream
= new MemoryStream ();
2722 serializer
.Serialize (stream
, foo
);
2723 stream
.Position
= 0;
2724 foo
= (Bug80759_2
) serializer
.Deserialize (stream
);
2728 public void HasNullableField2 ()
2730 Bug80759 foo
= new Bug80759 ();
2732 foo
.NullableInt
= 10;
2734 XmlSerializer serializer
= new XmlSerializer (typeof (Bug80759
));
2736 MemoryStream stream
= new MemoryStream ();
2738 serializer
.Serialize (stream
, foo
);
2739 stream
.Position
= 0;
2740 foo
= (Bug80759
) serializer
.Deserialize (stream
);
2742 Assert
.AreEqual ("BAR", foo
.Test
, "#1");
2743 Assert
.AreEqual (10, foo
.NullableInt
, "#2");
2745 foo
.NullableInt
= null;
2746 stream
= new MemoryStream ();
2747 serializer
.Serialize (stream
, foo
);
2748 stream
.Position
= 0;
2749 foo
= (Bug80759
) serializer
.Deserialize (stream
);
2751 Assert
.AreEqual ("BAR", foo
.Test
, "#3");
2752 Assert
.IsNull (foo
.NullableInt
, "#4");
2756 public void SupportPrivateCtorOnly ()
2759 new XmlSerializer (typeof (PrivateCtorOnly
));
2760 StringWriter sw
= new StringWriter ();
2761 xs
.Serialize (sw
, PrivateCtorOnly
.Instance
);
2762 xs
.Deserialize (new StringReader (sw
.ToString ()));
2766 public void XmlSchemaProviderQNameBecomesRootName ()
2768 xs
= new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType
));
2769 Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
2770 Assert
.AreEqual (Infoset ("<foo />"), WriterText
);
2771 xs
.Deserialize (new StringReader ("<foo/>"));
2775 public void XmlSchemaProviderQNameBecomesRootName2 ()
2777 string xml
= "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
2778 xs
= new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2
));
2779 Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
2780 Assert
.AreEqual (Infoset (xml
), WriterText
);
2781 xs
.Deserialize (new StringReader (xml
));
2785 public void XmlAnyElementForObjects () // bug #553032
2787 new XmlSerializer (typeof (XmlAnyElementForObjectsType
));
2791 [ExpectedException (typeof (InvalidOperationException
))]
2792 public void XmlAnyElementForObjects2 () // bug #553032-2
2794 new XmlSerializer (typeof (XmlAnyElementForObjectsType
)).Serialize (TextWriter
.Null
, new XmlAnyElementForObjectsType ());
2798 public class Bug2893
{
2801 Contents
= new XmlDataDocument();
2804 [XmlAnyElement("Contents")]
2805 public XmlNode Contents
;
2808 // Bug Xamarin #2893
2810 public void XmlAnyElementForXmlNode ()
2812 var obj
= new Bug2893 ();
2813 XmlSerializer mySerializer
= new XmlSerializer(typeof(Bug2893
));
2814 XmlWriterSettings settings
= new XmlWriterSettings();
2816 var xsn
= new XmlSerializerNamespaces();
2817 xsn
.Add(string.Empty
, string.Empty
);
2819 byte[] buffer
= new byte[2048];
2820 var ms
= new MemoryStream(buffer
);
2821 using (var xw
= XmlWriter
.Create(ms
, settings
))
2823 mySerializer
.Serialize(xw
, obj
, xsn
);
2827 mySerializer
.Serialize(ms
, obj
);
2831 public void XmlRootOverridesSchemaProviderQName ()
2833 var obj
= new XmlRootOverridesSchemaProviderQNameType ();
2835 XmlSerializer xs
= new XmlSerializer (obj
.GetType ());
2837 var sw
= new StringWriter ();
2838 using (XmlWriter xw
= XmlWriter
.Create (sw
))
2839 xs
.Serialize (xw
, obj
);
2840 Assert
.IsTrue (sw
.ToString ().IndexOf ("foo") > 0, "#1");
2843 public class AnotherArrayListType
2846 public string one
= "aaa";
2848 public string another
= "bbb";
2851 public class DerivedArrayListType
: AnotherArrayListType
2856 public class ClassWithArrayList
2858 [XmlElement (Type
= typeof(int), ElementName
= "int_elem")]
2859 [XmlElement (Type
= typeof(string), ElementName
= "string_elem")]
2860 [XmlElement (Type
= typeof(AnotherArrayListType
), ElementName
= "another_elem")]
2861 [XmlElement (Type
= typeof(DerivedArrayListType
), ElementName
= "derived_elem")]
2862 public ArrayList list
;
2865 public class ClassWithArray
2867 [XmlElement (Type
= typeof(int), ElementName
= "int_elem")]
2868 [XmlElement (Type
= typeof(string), ElementName
= "string_elem")]
2869 [XmlElement (Type
= typeof(AnotherArrayListType
), ElementName
= "another_elem")]
2870 [XmlElement (Type
= typeof(DerivedArrayListType
), ElementName
= "derived_elem")]
2871 public object[] list
;
2876 public void MultipleXmlElementAttributesOnArrayList()
2878 var test
= new ClassWithArrayList();
2880 test
.list
= new ArrayList();
2882 test
.list
.Add("apepe");
2883 test
.list
.Add(new AnotherArrayListType());
2884 test
.list
.Add(new DerivedArrayListType());
2887 var expected_text
= "<:ClassWithArrayList http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2889 Assert
.AreEqual(WriterText
, expected_text
, WriterText
);
2893 public void MultipleXmlElementAttributesOnArray()
2895 var test
= new ClassWithArray();
2897 test
.list
= new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() }
;
2900 var expected_text
= "<:ClassWithArray http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:int_elem>3</><:string_elem>apepe</><:another_elem :another='bbb' :one='aaa'></><:derived_elem :another='bbb' :one='aaa'></></>";
2902 Assert
.AreEqual(WriterText
, expected_text
, WriterText
);
2906 #endregion //GenericsSeralizationTests
2907 #region XmlInclude on abstract class tests (Bug #18558)
2909 public void TestSerializeIntermediateType ()
2911 string expectedXml
= "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlIntermediateType intermediate=\"false\"/></ContainerTypeForTest>";
2912 var obj
= new ContainerTypeForTest();
2913 obj
.MemberToUseInclude
= new IntermediateTypeForTest ();
2915 Assert
.AreEqual (Infoset (expectedXml
), WriterText
, "Serialized Output : " + WriterText
);
2919 public void TestSerializeSecondType ()
2921 string expectedXml
= "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlSecondType intermediate=\"false\"/></ContainerTypeForTest>";
2922 var obj
= new ContainerTypeForTest();
2923 obj
.MemberToUseInclude
= new SecondDerivedTypeForTest ();
2925 Assert
.AreEqual (Infoset (expectedXml
), WriterText
, "Serialized Output : " + WriterText
);
2928 public class XmlArrayOnInt
2934 public class XmlArrayUnqualifiedWithNamespace
2936 [XmlArray (Namespace
= "", Form
= XmlSchemaForm
.Unqualified
)]
2937 public ArrayList Sane
= new ArrayList ();
2940 public class XmlArrayItemUnqualifiedWithNamespace
2942 [XmlArrayItem ("foo", Namespace
= "", Form
= XmlSchemaForm
.Unqualified
)]
2943 public ArrayList Sane
= new ArrayList ();
2946 [XmlRoot (Namespace
= "urn:foo")]
2947 public class XmlArrayOnArrayList
2949 [XmlArray (Form
= XmlSchemaForm
.Unqualified
)]
2950 public ArrayList Sane
= new ArrayList ();
2953 [XmlRoot (Namespace
= "urn:foo")]
2954 public class XmlArrayOnArray
2956 [XmlArray (Form
= XmlSchemaForm
.Unqualified
)]
2957 public string[] Sane
= new string[] { "foo", "bar" }
;
2959 [XmlArray (Form
= XmlSchemaForm
.Unqualified
)]
2960 public ArrayItemInXmlArray
[] Mids
=
2961 new ArrayItemInXmlArray
[] { new ArrayItemInXmlArray () }
;
2964 [XmlType (Namespace
= "urn:gyabo")]
2965 public class ArrayItemInXmlArray
2967 [XmlArray (Form
= XmlSchemaForm
.Unqualified
)]
2968 public string[] Whee
= new string[] { "foo", "bar" }
;
2971 [XmlRoot ("Base64Binary")]
2972 public class Base64Binary
2974 [XmlAttribute (DataType
= "base64Binary")]
2975 public byte [] Data
= new byte [] {1, 2, 3}
;
2978 [XmlRoot ("HexBinary")]
2979 public class HexBinary
2981 [XmlAttribute (DataType
= "hexBinary")]
2982 public byte[] Data
= new byte[] { 1, 2, 3 }
;
2985 [XmlRoot ("PrivateCtorOnly")]
2986 public class PrivateCtorOnly
2988 public static PrivateCtorOnly Instance
= new PrivateCtorOnly ();
2989 private PrivateCtorOnly ()
2994 public class CDataTextNodesType
2996 public CDataTextNodesInternal foo
;
2999 public class CDataTextNodesInternal
3002 public string Value
;
3005 public class InvalidTypeContainer
3007 [XmlElement (DataType
= "invalid")]
3008 public string InvalidTypeItem
= "aaa";
3011 public class TimeSpanContainer1
3013 [XmlElement (DataType
= "duration")]
3014 public string StringDuration
= "aaa";
3017 public class TimeSpanContainer2
3019 [XmlElement (DataType
= "duration")]
3020 public TimeSpan StringDuration
= TimeSpan
.FromSeconds (1);
3023 public class Bug80759
3026 public int? NullableInt
;
3029 public class Bug80759_2
3032 public int? NullableInt
;
3035 public bool NullableIntSpecified
{
3036 get { return NullableInt.HasValue; }
3040 [XmlSchemaProvider ("GetXsdType")]
3041 public class XmlSchemaProviderQNameBecomesRootNameType
: IXmlSerializable
3043 public XmlSchema
GetSchema ()
3048 public void ReadXml (XmlReader reader
)
3053 public void WriteXml (XmlWriter writer
)
3057 public static XmlQualifiedName
GetXsdType (XmlSchemaSet xss
)
3059 if (xss
.Count
== 0) {
3060 XmlSchema xs
= new XmlSchema ();
3061 XmlSchemaComplexType ct
= new XmlSchemaComplexType ();
3066 return new XmlQualifiedName ("foo");
3070 public class XmlSchemaProviderQNameBecomesRootNameType2
3072 [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType
))]
3073 public object [] Foo
= new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()}
;
3076 public class XmlAnyElementForObjectsType
3079 public object [] arr
= new object [] {3,4,5}
;
3083 [XmlSchemaProvider ("GetSchema")]
3084 public class XmlRootOverridesSchemaProviderQNameType
: IXmlSerializable
3086 public static XmlQualifiedName
GetSchema (XmlSchemaSet xss
)
3088 var xs
= new XmlSchema ();
3089 var xse
= new XmlSchemaComplexType () { Name = "bar" }
;
3092 return new XmlQualifiedName ("bar");
3095 XmlSchema IXmlSerializable
.GetSchema ()
3100 void IXmlSerializable
.ReadXml (XmlReader reader
)
3103 void IXmlSerializable
.WriteXml (XmlWriter writer
)
3109 void CDataTextNodes_BadNode (object s
, XmlNodeEventArgs e
)
3116 public static string Infoset (string sx
)
3118 XmlDocument doc
= new XmlDocument ();
3120 StringBuilder sb
= new StringBuilder ();
3121 GetInfoset (doc
.DocumentElement
, sb
);
3122 return sb
.ToString ();
3125 public static string Infoset (XmlNode nod
)
3127 StringBuilder sb
= new StringBuilder ();
3128 GetInfoset (nod
, sb
);
3129 return sb
.ToString ();
3132 static void GetInfoset (XmlNode nod
, StringBuilder sb
)
3134 switch (nod
.NodeType
) {
3135 case XmlNodeType
.Attribute
:
3136 if (nod
.LocalName
== "xmlns" && nod
.NamespaceURI
== "http://www.w3.org/2000/xmlns/") return;
3137 sb
.Append (" " + nod
.NamespaceURI
+ ":" + nod
.LocalName
+ "='" + nod
.Value
+ "'");
3140 case XmlNodeType
.Element
:
3141 XmlElement elem
= (XmlElement
) nod
;
3142 sb
.Append ("<" + elem
.NamespaceURI
+ ":" + elem
.LocalName
);
3144 ArrayList ats
= new ArrayList ();
3145 foreach (XmlAttribute at
in elem
.Attributes
)
3146 ats
.Add (at
.LocalName
+ " " + at
.NamespaceURI
);
3150 foreach (string name
in ats
) {
3151 string[] nn
= name
.Split (' ');
3152 GetInfoset (elem
.Attributes
[nn
[0], nn
[1]], sb
);
3156 foreach (XmlNode cn
in elem
.ChildNodes
)
3157 GetInfoset (cn
, sb
);
3162 sb
.Append (nod
.OuterXml
);
3167 static XmlTypeMapping
CreateSoapMapping (Type type
)
3169 SoapReflectionImporter importer
= new SoapReflectionImporter ();
3170 return importer
.ImportTypeMapping (type
);
3173 static XmlTypeMapping
CreateSoapMapping (Type type
, SoapAttributeOverrides ao
)
3175 SoapReflectionImporter importer
= new SoapReflectionImporter (ao
);
3176 return importer
.ImportTypeMapping (type
);
3179 static XmlTypeMapping
CreateSoapMapping (Type type
, SoapAttributeOverrides ao
, string defaultNamespace
)
3181 SoapReflectionImporter importer
= new SoapReflectionImporter (ao
, defaultNamespace
);
3182 return importer
.ImportTypeMapping (type
);
3185 [XmlSchemaProvider (null, IsAny
= true)]
3186 public class AnySchemaProviderClass
: IXmlSerializable
{
3190 void IXmlSerializable
.WriteXml (XmlWriter writer
)
3192 writer
.WriteElementString ("text", Text
);
3195 void IXmlSerializable
.ReadXml (XmlReader reader
)
3197 Text
= reader
.ReadElementString ("text");
3200 XmlSchema IXmlSerializable
.GetSchema ()
3207 public void SerializeAnySchemaProvider ()
3209 string expected
= "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3210 Environment
.NewLine
+ "<text>test</text>";
3212 var ser
= new XmlSerializer (typeof (AnySchemaProviderClass
));
3214 var obj
= new AnySchemaProviderClass
{
3218 using (var t
= new StringWriter ()) {
3219 ser
.Serialize (t
, obj
);
3220 Assert
.AreEqual (expected
, t
.ToString ());
3225 public void DeserializeAnySchemaProvider ()
3227 string expected
= "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
3228 Environment
.NewLine
+ "<text>test</text>";
3230 var ser
= new XmlSerializer (typeof (AnySchemaProviderClass
));
3232 using (var t
= new StringReader (expected
)) {
3233 var obj
= (AnySchemaProviderClass
) ser
.Deserialize (t
);
3234 Assert
.AreEqual ("test", obj
.Text
);
3238 public class SubNoParameterlessConstructor
: NoParameterlessConstructor
3240 public SubNoParameterlessConstructor ()
3246 public class NoParameterlessConstructor
3248 [XmlElement ("Text")]
3251 public NoParameterlessConstructor (string parameter
)
3257 public void BaseClassWithoutParameterlessConstructor ()
3259 var ser
= new XmlSerializer (typeof (SubNoParameterlessConstructor
));
3261 var obj
= new SubNoParameterlessConstructor
{
3265 using (var w
= new StringWriter ()) {
3266 ser
.Serialize (w
, obj
);
3267 using (var r
= new StringReader ( w
.ToString ())) {
3268 var desObj
= (SubNoParameterlessConstructor
) ser
.Deserialize (r
);
3269 Assert
.AreEqual (obj
.Text
, desObj
.Text
);
3274 public class ClassWithXmlAnyElement
3276 [XmlAnyElement ("Contents")]
3277 public XmlNode Contents
;
3281 public void TestClassWithXmlAnyElement ()
3283 var d
= new XmlDocument ();
3284 var e
= d
.CreateElement ("Contents");
3285 e
.AppendChild (d
.CreateElement ("SomeElement"));
3287 var c
= new ClassWithXmlAnyElement
{
3291 var ser
= new XmlSerializer (typeof (ClassWithXmlAnyElement
));
3292 using (var sw
= new StringWriter ())
3293 ser
.Serialize (sw
, c
);
3297 public void ClassWithImplicitlyConvertibleElement ()
3299 var ser
= new XmlSerializer (typeof (ObjectWithElementRequiringImplicitCast
));
3301 var obj
= new ObjectWithElementRequiringImplicitCast ("test");
3303 using (var w
= new StringWriter ()) {
3304 ser
.Serialize (w
, obj
);
3305 using (var r
= new StringReader ( w
.ToString ())) {
3306 var desObj
= (ObjectWithElementRequiringImplicitCast
) ser
.Deserialize (r
);
3307 Assert
.AreEqual (obj
.Object
.Text
, desObj
.Object
.Text
);
3312 public class ClassWithOptionalMethods
3314 private readonly bool shouldSerializeX
;
3315 private readonly bool xSpecified
;
3318 public int X { get; set; }
3320 public bool ShouldSerializeX () { return shouldSerializeX; }
3322 public bool XSpecified
3324 get { return xSpecified; }
3327 public ClassWithOptionalMethods ()
3331 public ClassWithOptionalMethods (int x
, bool shouldSerializeX
, bool xSpecified
)
3334 this.shouldSerializeX
= shouldSerializeX
;
3335 this.xSpecified
= xSpecified
;
3340 public void OptionalMethods ()
3342 var ser
= new XmlSerializer (typeof (ClassWithOptionalMethods
));
3344 var expectedValueWithoutX
= Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment
.NewLine
+
3345 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />");
3347 var expectedValueWithX
= Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment
.NewLine
+
3348 "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3350 using (var t
= new StringWriter ()) {
3351 var obj
= new ClassWithOptionalMethods (11, false, false);
3352 ser
.Serialize (t
, obj
);
3353 Assert
.AreEqual (expectedValueWithoutX
, Infoset (t
.ToString ()));
3356 using (var t
= new StringWriter ()) {
3357 var obj
= new ClassWithOptionalMethods (11, true, false);
3358 ser
.Serialize (t
, obj
);
3359 Assert
.AreEqual (expectedValueWithoutX
, Infoset (t
.ToString ()));
3362 using (var t
= new StringWriter ()) {
3363 var obj
= new ClassWithOptionalMethods (11, false, true);
3364 ser
.Serialize (t
, obj
);
3365 Assert
.AreEqual (expectedValueWithoutX
, Infoset (t
.ToString ()));
3368 using (var t
= new StringWriter ()) {
3369 var obj
= new ClassWithOptionalMethods (11, true, true);
3370 ser
.Serialize (t
, obj
);
3371 Assert
.AreEqual (expectedValueWithX
, Infoset (t
.ToString ()));
3375 public class ClassWithShouldSerializeGeneric
3378 public int X { get; set; }
3380 public bool ShouldSerializeX
<T
> () { return false; }
3384 [Category("NotWorking")]
3385 public void ShouldSerializeGeneric ()
3387 var ser
= new XmlSerializer (typeof (ClassWithShouldSerializeGeneric
));
3389 var expectedValueWithX
= Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment
.NewLine
+
3390 "<ClassWithShouldSerializeGeneric xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
3392 using (var t
= new StringWriter ()) {
3393 var obj
= new ClassWithShouldSerializeGeneric { X = 11 }
;
3394 ser
.Serialize (t
, obj
);
3395 Assert
.AreEqual (expectedValueWithX
, Infoset (t
.ToString ()));
3400 public void NullableArrayItems ()
3402 var ser
= new XmlSerializer (typeof (ObjectWithNullableArrayItems
));
3404 var obj
= new ObjectWithNullableArrayItems ();
3405 obj
.Elements
= new List
<SimpleClass
> ();
3406 obj
.Elements
.Add (new SimpleClass { something = "Hello" }
);
3407 obj
.Elements
.Add (null);
3408 obj
.Elements
.Add (new SimpleClass { something = "World" }
);
3410 using (var w
= new StringWriter ()) {
3411 ser
.Serialize (w
, obj
);
3412 using (var r
= new StringReader ( w
.ToString ())) {
3413 var desObj
= (ObjectWithNullableArrayItems
) ser
.Deserialize (r
);
3414 Assert
.IsNull (desObj
.Elements
[1]);
3420 public void NonNullableArrayItems ()
3422 var ser
= new XmlSerializer (typeof (ObjectWithNonNullableArrayItems
));
3424 var obj
= new ObjectWithNonNullableArrayItems ();
3425 obj
.Elements
= new List
<SimpleClass
> ();
3426 obj
.Elements
.Add (new SimpleClass { something = "Hello" }
);
3427 obj
.Elements
.Add (null);
3428 obj
.Elements
.Add (new SimpleClass { something = "World" }
);
3430 using (var w
= new StringWriter ()) {
3431 ser
.Serialize (w
, obj
);
3432 using (var r
= new StringReader ( w
.ToString ())) {
3433 var desObj
= (ObjectWithNonNullableArrayItems
) ser
.Deserialize (r
);
3434 Assert
.IsNotNull (desObj
.Elements
[1]);
3440 public void NotSpecifiedNullableArrayItems ()
3442 var ser
= new XmlSerializer (typeof (ObjectWithNotSpecifiedNullableArrayItems
));
3444 var obj
= new ObjectWithNotSpecifiedNullableArrayItems ();
3445 obj
.Elements
= new List
<SimpleClass
> ();
3446 obj
.Elements
.Add (new SimpleClass { something = "Hello" }
);
3447 obj
.Elements
.Add (null);
3448 obj
.Elements
.Add (new SimpleClass { something = "World" }
);
3450 using (var w
= new StringWriter ()) {
3451 ser
.Serialize (w
, obj
);
3452 using (var r
= new StringReader ( w
.ToString ())) {
3453 var desObj
= (ObjectWithNotSpecifiedNullableArrayItems
) ser
.Deserialize (r
);
3454 Assert
.IsNull (desObj
.Elements
[1]);
3459 private static void TestClassWithDefaultTextNotNullAux (string value, string expected
)
3461 var obj
= new ClassWithDefaultTextNotNull (value);
3462 var ser
= new XmlSerializer (typeof (ClassWithDefaultTextNotNull
));
3464 using (var mstream
= new MemoryStream ())
3465 using (var writer
= new XmlTextWriter (mstream
, Encoding
.ASCII
)) {
3466 ser
.Serialize (writer
, obj
);
3468 mstream
.Seek (0, SeekOrigin
.Begin
);
3469 using (var reader
= new XmlTextReader (mstream
)) {
3470 var result
= (ClassWithDefaultTextNotNull
) ser
.Deserialize (reader
);
3471 Assert
.AreEqual (expected
, result
.Value
);
3477 public void TestClassWithDefaultTextNotNull ()
3479 TestClassWithDefaultTextNotNullAux ("my_text", "my_text");
3480 TestClassWithDefaultTextNotNullAux ("", ClassWithDefaultTextNotNull
.DefaultValue
);
3481 TestClassWithDefaultTextNotNullAux (null, ClassWithDefaultTextNotNull
.DefaultValue
);
3485 // Test generated serialization code.
3486 public class XmlSerializerGeneratorTests
: XmlSerializerTests
{
3488 private FieldInfo backgroundGeneration
;
3489 private FieldInfo generationThreshold
;
3490 private FieldInfo generatorFallback
;
3492 private bool backgroundGenerationOld
;
3493 private int generationThresholdOld
;
3494 private bool generatorFallbackOld
;
3497 public void SetUp ()
3499 // Make sure XmlSerializer static constructor is called
3500 XmlSerializer
.FromTypes (new Type
[] {});
3502 const BindingFlags binding
= BindingFlags
.Static
| BindingFlags
.NonPublic
;
3503 backgroundGeneration
= typeof (XmlSerializer
).GetField ("backgroundGeneration", binding
);
3504 generationThreshold
= typeof (XmlSerializer
).GetField ("generationThreshold", binding
);
3505 generatorFallback
= typeof (XmlSerializer
).GetField ("generatorFallback", binding
);
3507 if (backgroundGeneration
== null)
3508 Assert
.Ignore ("Unable to access field backgroundGeneration");
3509 if (generationThreshold
== null)
3510 Assert
.Ignore ("Unable to access field generationThreshold");
3511 if (generatorFallback
== null)
3512 Assert
.Ignore ("Unable to access field generatorFallback");
3514 backgroundGenerationOld
= (bool) backgroundGeneration
.GetValue (null);
3515 generationThresholdOld
= (int) generationThreshold
.GetValue (null);
3516 generatorFallbackOld
= (bool) generatorFallback
.GetValue (null);
3518 backgroundGeneration
.SetValue (null, false);
3519 generationThreshold
.SetValue (null, 0);
3520 generatorFallback
.SetValue (null, false);
3524 public void TearDown ()
3526 if (backgroundGeneration
== null || generationThreshold
== null || generatorFallback
== null)
3529 backgroundGeneration
.SetValue (null, backgroundGenerationOld
);
3530 generationThreshold
.SetValue (null, generationThresholdOld
);
3531 generatorFallback
.SetValue (null, generatorFallbackOld
);
3535 #region XmlInclude on abstract class test classes
3538 public class ContainerTypeForTest
3540 [XmlElement ("XmlSecondType", typeof (SecondDerivedTypeForTest
))]
3541 [XmlElement ("XmlIntermediateType", typeof (IntermediateTypeForTest
))]
3542 [XmlElement ("XmlFirstType", typeof (FirstDerivedTypeForTest
))]
3543 public AbstractTypeForTest MemberToUseInclude { get; set; }
3546 [XmlInclude (typeof (SecondDerivedTypeForTest
))]
3547 [XmlInclude (typeof (IntermediateTypeForTest
))]
3548 [XmlInclude (typeof (FirstDerivedTypeForTest
))]
3549 public abstract class AbstractTypeForTest
3553 public class IntermediateTypeForTest
: AbstractTypeForTest
3555 [XmlAttribute (AttributeName
= "intermediate")]
3556 public bool IntermediateMember { get; set; }
3559 public class FirstDerivedTypeForTest
: AbstractTypeForTest
3561 public string FirstMember { get; set; }
3564 public class SecondDerivedTypeForTest
: IntermediateTypeForTest
3566 public string SecondMember { get; set; }