(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTests.cs
blob20360ee8756713481de2377917f2eea1fe7b8212
1 //
2 // System.Xml.XmlSerializerTests
3 //
4 // Author:
5 // Erik LeBel <eriklebel@yahoo.ca>
6 //
7 // (C) 2003 Erik LeBel
8 //
9 //
10 // NOTES:
11 // Where possible, these tests avoid testing the order of
12 // an object's members serialization. Mono and .NET do not
13 // reflect members in the same order.
15 // Only serializations tests so far, no deserialization.
17 // FIXME
18 // test XmlArrayAttribute
19 // test XmlArrayItemAttribute
20 // test serialization of decimal type
21 // test serialization of Guid type
22 // test XmlNode serialization with and without modifying attributes.
23 // test deserialization
24 // FIXMEs found in this file
26 using System;
27 using System.Collections;
28 using System.IO;
29 using System.Text;
30 using System.Xml;
31 using System.Xml.Schema;
32 using System.Xml.Serialization;
34 using NUnit.Framework;
36 using MonoTests.System.Xml.TestClasses;
38 namespace MonoTests.System.XmlSerialization
40 [TestFixture]
41 public class XmlSerializerTests : Assertion
43 StringWriter sw;
44 XmlTextWriter xtw;
45 XmlSerializer xs;
47 private void SetUpWriter()
49 sw = new StringWriter ();
50 xtw = new XmlTextWriter (sw);
51 xtw.QuoteChar = '\'';
52 xtw.Formatting = Formatting.None;
55 private string WriterText
57 get
59 string val = sw.GetStringBuilder().ToString();
60 int offset = val.IndexOf('>') + 1;
61 val = val.Substring(offset);
62 return Infoset(val);
66 private void Serialize(object o)
68 SetUpWriter();
69 xs = new XmlSerializer(o.GetType());
70 xs.Serialize(xtw, o);
73 private void Serialize(object o, Type type)
75 SetUpWriter();
76 xs = new XmlSerializer(type);
77 xs.Serialize(xtw, o);
80 private void Serialize(object o, XmlSerializerNamespaces ns)
82 SetUpWriter();
83 xs = new XmlSerializer(o.GetType());
84 xs.Serialize(xtw, o, ns);
87 private void Serialize(object o, XmlAttributeOverrides ao)
89 SetUpWriter();
90 xs = new XmlSerializer(o.GetType(), ao);
91 xs.Serialize(xtw, o);
94 private void Serialize(object o, XmlRootAttribute root)
96 SetUpWriter();
97 xs = new XmlSerializer(o.GetType(), root);
98 xs.Serialize(xtw, o);
101 // test constructors
102 #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
103 [Test]
104 public void TestConstructor()
106 XmlSerializer ser = new XmlSerializer (null, "");
108 #else
109 #endif
111 // test basic types ////////////////////////////////////////////////////////
112 [Test]
113 public void TestSerializeInt()
115 Serialize(10);
116 AssertEquals(Infoset("<int>10</int>"), WriterText);
119 [Test]
120 public void TestSerializeBool()
122 Serialize(true);
123 AssertEquals(Infoset("<boolean>true</boolean>"), WriterText);
125 Serialize(false);
126 AssertEquals(Infoset("<boolean>false</boolean>"), WriterText);
129 [Test]
130 public void TestSerializeString()
132 Serialize("hello");
133 AssertEquals(Infoset("<string>hello</string>"), WriterText);
136 [Test]
137 public void TestSerializeEmptyString()
139 Serialize(String.Empty);
140 AssertEquals(Infoset("<string />"), WriterText);
143 [Test]
144 public void TestSerializeNullObject()
146 Serialize(null, typeof(object));
147 AssertEquals(Infoset("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
150 [Test]
151 [Ignore ("The generated XML is not exact but it is equivalent")]
152 public void TestSerializeNullString()
154 Serialize(null, typeof(string));
155 Console.WriteLine (WriterText);
156 AssertEquals (Infoset("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
159 [Test]
160 public void TestSerializeIntArray()
162 Serialize(new int[] {1, 2, 3, 4});
163 AssertEquals (Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);
166 [Test]
167 public void TestSerializeEmptyArray()
169 Serialize(new int[] {});
170 AssertEquals(Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
173 [Test]
174 public void TestSerializeChar()
176 Serialize('A');
177 AssertEquals(Infoset("<char>65</char>"), WriterText);
179 Serialize('\0');
180 AssertEquals(Infoset("<char>0</char>"), WriterText);
182 Serialize('\n');
183 AssertEquals(Infoset("<char>10</char>"), WriterText);
185 Serialize('\uFF01');
186 AssertEquals(Infoset("<char>65281</char>"), WriterText);
189 [Test]
190 public void TestSerializeFloat()
192 Serialize(10.78);
193 AssertEquals(Infoset("<double>10.78</double>"), WriterText);
195 Serialize(-1e8);
196 AssertEquals(Infoset("<double>-100000000</double>"), WriterText);
198 // FIXME test INF and other boundary conditions that may exist with floats
202 [Test]
203 public void TestSerializeEnumeration()
205 Serialize(SimpleEnumeration.FIRST);
206 AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
208 Serialize(SimpleEnumeration.SECOND);
209 AssertEquals(Infoset("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);
212 [Test]
213 public void TestSerializeQualifiedName()
215 Serialize(new XmlQualifiedName("me", "home.urn"));
216 AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
219 [Test]
220 public void TestSerializeBytes()
222 Serialize((byte)0xAB);
223 AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);
225 Serialize((byte)15);
226 AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);
229 [Test]
230 public void TestSerializeByteArrays()
232 Serialize(new byte[] {});
233 AssertEquals(Infoset("<base64Binary />"), WriterText);
235 Serialize(new byte[] {0xAB, 0xCD});
236 AssertEquals(Infoset("<base64Binary>q80=</base64Binary>"), WriterText);
239 [Test]
240 public void TestSerializeDateTime()
242 DateTime d = new DateTime();
243 Serialize(d);
245 TimeZone tz = TimeZone.CurrentTimeZone;
246 TimeSpan off = tz.GetUtcOffset (d);
247 string sp = string.Format ("{0:00}:{1:00}", off.TotalHours, off.TotalMinutes%60);
248 if (off.Ticks > 0) sp = "+" + sp;
249 else sp = "-" + sp;
251 AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
255 FIXME
256 - decimal
257 - Guid
258 - XmlNode objects
260 [Test]
261 public void TestSerialize()
263 Serialize();
264 AssertEquals(WriterText, "");
268 // test basic class serialization /////////////////////////////////////
269 [Test]
270 public void TestSerializeSimpleClass()
272 SimpleClass simple = new SimpleClass();
273 Serialize(simple);
274 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
276 simple.something = "hello";
278 Serialize(simple);
279 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
282 [Test]
283 public void TestSerializeStringCollection()
285 StringCollection strings = new StringCollection();
286 Serialize(strings);
287 AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
289 strings.Add("hello");
290 strings.Add("goodbye");
291 Serialize(strings);
292 AssertEquals(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);
296 [Test]
297 public void TestSerializePlainContainer()
299 StringCollectionContainer container = new StringCollectionContainer();
300 Serialize(container);
301 AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
303 container.Messages.Add("hello");
304 container.Messages.Add("goodbye");
305 Serialize(container);
306 AssertEquals(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);
309 [Test]
310 public void TestSerializeArrayContainer()
312 ArrayContainer container = new ArrayContainer();
313 Serialize(container);
314 AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
316 container.items = new object[] {10, 20};
317 Serialize(container);
318 AssertEquals(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);
320 container.items = new object[] {10, "hello"};
321 Serialize(container);
322 AssertEquals(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);
325 [Test]
326 public void TestSerializeClassArrayContainer()
328 ClassArrayContainer container = new ClassArrayContainer();
329 Serialize(container);
330 AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
332 SimpleClass simple1 = new SimpleClass();
333 simple1.something = "hello";
334 SimpleClass simple2 = new SimpleClass();
335 simple2.something = "hello";
336 container.items = new SimpleClass[2];
337 container.items[0] = simple1;
338 container.items[1] = simple2;
339 Serialize(container);
340 AssertEquals(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);
343 // test basic attributes ///////////////////////////////////////////////
344 [Test]
345 public void TestSerializeSimpleClassWithXmlAttributes()
347 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
348 Serialize(simple);
349 AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
351 simple.something = "hello";
352 Serialize(simple);
353 AssertEquals (Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
356 // test overrides ///////////////////////////////////////////////////////
357 [Test]
358 public void TestSerializeSimpleClassWithOverrides()
360 // Also tests XmlIgnore
361 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
363 XmlAttributes attr = new XmlAttributes();
364 attr.XmlIgnore = true;
365 overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
367 SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
368 simple.something = "hello";
369 Serialize(simple, overrides);
370 AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
373 // test xmlText //////////////////////////////////////////////////////////
374 [Test]
375 public void TestSerializeXmlTextAttribute()
377 SimpleClass simple = new SimpleClass();
378 simple.something = "hello";
380 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
381 XmlAttributes attr = new XmlAttributes();
382 overrides.Add(typeof(SimpleClass), "something", attr);
384 attr.XmlText = new XmlTextAttribute();
385 Serialize(simple, overrides);
386 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
388 attr.XmlText = new XmlTextAttribute(typeof(string));
389 Serialize(simple, overrides);
390 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
394 attr.XmlText = new XmlTextAttribute(typeof(byte[]));
395 Serialize(simple, overrides);
396 Fail("XmlText.Type does not match the type it serializes: this should have failed");
398 catch (Exception)
404 attr.XmlText = new XmlTextAttribute();
405 attr.XmlText.DataType = "sometype";
406 Serialize(simple, overrides);
407 Fail("XmlText.DataType does not match the type it serializes: this should have failed");
409 catch (Exception)
414 // test xmlRoot //////////////////////////////////////////////////////////
415 [Test]
416 public void TestSerializeXmlRootAttribute()
418 // constructor override & element name
419 XmlRootAttribute root = new XmlRootAttribute();
420 root.ElementName = "renamed";
422 SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
423 Serialize(simpleWithAttributes, root);
424 AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
426 SimpleClass simple = null;
427 root.IsNullable = false;
430 Serialize(simple, root);
431 Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
433 catch (Exception)
437 root.IsNullable = true;
440 Serialize(simple, root);
441 Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
443 catch (Exception)
447 simple = new SimpleClass();
448 root.ElementName = null;
449 root.Namespace = "some.urn";
450 Serialize(simple, root);
451 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
454 [Test]
455 public void TestSerializeXmlRootAttributeOnMember()
457 // nested root
458 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
459 XmlAttributes childAttr = new XmlAttributes();
460 childAttr.XmlRoot = new XmlRootAttribute("simple");
461 overrides.Add(typeof(SimpleClass), childAttr);
463 XmlAttributes attr = new XmlAttributes();
464 attr.XmlRoot = new XmlRootAttribute("simple");
465 overrides.Add(typeof(ClassArrayContainer), attr);
467 ClassArrayContainer container = new ClassArrayContainer();
468 container.items = new SimpleClass[1];
469 container.items[0] = new SimpleClass();
470 Serialize(container, overrides);
471 AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"),WriterText);
473 // FIXME test data type
476 // test XmlAttribute /////////////////////////////////////////////////////
477 [Test]
478 public void TestSerializeXmlAttributeAttribute()
480 // null
481 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
482 XmlAttributes attr = new XmlAttributes();
483 attr.XmlAttribute = new XmlAttributeAttribute();
484 overrides.Add(typeof(SimpleClass), "something", attr);
486 SimpleClass simple = new SimpleClass();;
487 Serialize(simple, overrides);
488 AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
490 // regular
491 simple.something = "hello";
492 Serialize(simple, overrides);
493 AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);
495 // AttributeName
496 attr.XmlAttribute.AttributeName = "somethingelse";
497 Serialize(simple, overrides);
498 AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);
500 // Type
501 // FIXME this should work, shouldnt it?
502 // attr.XmlAttribute.Type = typeof(string);
503 // Serialize(simple, overrides);
504 // Assert(WriterText.EndsWith(" something='hello' />"));
506 // Namespace
507 attr.XmlAttribute.Namespace = "some:urn";
508 Serialize(simple, overrides);
509 AssertEquals ("#4", 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);
511 // FIXME DataType
512 // FIXME XmlSchemaForm Form
514 // FIXME write XmlQualifiedName as attribute
517 // test XmlElement ///////////////////////////////////////////////////////
518 [Test]
519 public void TestSerializeXmlElementAttribute()
523 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
524 XmlAttributes attr = new XmlAttributes();
525 XmlElementAttribute element = new XmlElementAttribute();
526 attr.XmlElements.Add(element);
527 overrides.Add(typeof(SimpleClass), "something", attr);
529 // null
530 SimpleClass simple = new SimpleClass();;
531 Serialize(simple, overrides);
532 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
534 // not null
535 simple.something = "hello";
536 Serialize(simple, overrides);
537 AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
539 //ElementName
540 element.ElementName = "saying";
541 Serialize(simple, overrides);
542 AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);
544 //IsNullable
545 element.IsNullable = false;
546 simple.something = null;
547 Serialize(simple, overrides);
548 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
550 element.IsNullable = true;
551 simple.something = null;
552 Serialize(simple, overrides);
553 AssertEquals (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);
555 //Namespace
556 element.ElementName = null;
557 element.IsNullable = false;
558 element.Namespace = "some:urn";
559 simple.something = "hello";
560 Serialize(simple, overrides);
561 AssertEquals (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);
563 //FIXME DataType
564 //FIXME Form
565 //FIXME Type
568 // test XmlElementAttribute with arrays and collections //////////////////
569 [Test]
570 public void TestSerializeCollectionWithXmlElementAttribute()
572 // the rule is:
573 // if no type is specified or the specified type
574 // matches the contents of the collection,
575 // serialize each element in an element named after the member.
576 // if the type does not match, or matches the collection itself,
577 // create a base wrapping element for the member, and then
578 // wrap each collection item in its own wrapping element based on type.
580 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
581 XmlAttributes attr = new XmlAttributes();
582 XmlElementAttribute element = new XmlElementAttribute();
583 attr.XmlElements.Add(element);
584 overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
586 // empty collection & no type info in XmlElementAttribute
587 StringCollectionContainer container = new StringCollectionContainer();
588 Serialize(container, overrides);
589 AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
591 // non-empty collection & no type info in XmlElementAttribute
592 container.Messages.Add("hello");
593 Serialize(container, overrides);
594 AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
596 // non-empty collection & only type info in XmlElementAttribute
597 element.Type = typeof(StringCollection);
598 Serialize(container, overrides);
599 AssertEquals (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);
601 // non-empty collection & only type info in XmlElementAttribute
602 element.Type = typeof(string);
603 Serialize(container, overrides);
604 AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
606 // two elements
607 container.Messages.Add("goodbye");
608 element.Type = null;
609 Serialize(container, overrides);
610 AssertEquals(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);
613 // test DefaultValue /////////////////////////////////////////////////////
614 [Test]
615 public void TestSerializeDefaultValueAttribute()
617 XmlAttributeOverrides overrides = new XmlAttributeOverrides();
619 XmlAttributes attr = new XmlAttributes();
620 string defaultValueInstance = "nothing";
621 attr.XmlDefaultValue = defaultValueInstance;
622 overrides.Add(typeof(SimpleClass), "something", attr);
624 // use the default
625 SimpleClass simple = new SimpleClass();
626 Serialize(simple, overrides);
627 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
629 // same value as default
630 simple.something = defaultValueInstance;
631 Serialize(simple, overrides);
632 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
634 // some other value
635 simple.something = "hello";
636 Serialize(simple, overrides);
637 AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
640 // test XmlEnum //////////////////////////////////////////////////////////
641 [Test]
642 public void TestSerializeXmlEnumAttribute()
644 // technically this has an XmlIgnore attribute,
645 // but it is not being serialized as a member.
646 Serialize(XmlSchemaForm.None);
647 AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
649 Serialize(XmlSchemaForm.Qualified);
650 AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
652 Serialize(XmlSchemaForm.Unqualified);
653 AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
656 public static string Infoset (string sx)
658 XmlDocument doc = new XmlDocument ();
659 doc.LoadXml (sx);
660 StringBuilder sb = new StringBuilder ();
661 GetInfoset (doc.DocumentElement, sb);
662 return sb.ToString ();
665 public static string Infoset (XmlNode nod)
667 StringBuilder sb = new StringBuilder ();
668 GetInfoset (nod, sb);
669 return sb.ToString ();
672 static void GetInfoset (XmlNode nod, StringBuilder sb)
674 switch (nod.NodeType)
676 case XmlNodeType.Attribute:
677 if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
678 sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
679 break;
681 case XmlNodeType.Element:
682 XmlElement elem = (XmlElement) nod;
683 sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
685 ArrayList ats = new ArrayList ();
686 foreach (XmlAttribute at in elem.Attributes)
687 ats.Add (at.LocalName + " " + at.NamespaceURI);
689 ats.Sort ();
691 foreach (string name in ats)
693 string[] nn = name.Split (' ');
694 GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
697 sb.Append (">");
698 foreach (XmlNode cn in elem.ChildNodes)
699 GetInfoset (cn, sb);
700 sb.Append ("</>");
701 break;
703 default:
704 sb.Append (nod.OuterXml);
705 break;