2 // System.Xml.XmlSerializerTests
5 // Erik LeBel <eriklebel@yahoo.ca>
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.
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
27 using System
.Collections
;
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
41 public class XmlSerializerTests
: Assertion
47 private void SetUpWriter()
49 sw
= new StringWriter ();
50 xtw
= new XmlTextWriter (sw
);
52 xtw
.Formatting
= Formatting
.None
;
55 private string WriterText
59 string val
= sw
.GetStringBuilder().ToString();
60 int offset
= val
.IndexOf('>') + 1;
61 val
= val
.Substring(offset
);
66 private void Serialize(object o
)
69 xs
= new XmlSerializer(o
.GetType());
73 private void Serialize(object o
, Type type
)
76 xs
= new XmlSerializer(type
);
80 private void Serialize(object o
, XmlSerializerNamespaces ns
)
83 xs
= new XmlSerializer(o
.GetType());
84 xs
.Serialize(xtw
, o
, ns
);
87 private void Serialize(object o
, XmlAttributeOverrides ao
)
90 xs
= new XmlSerializer(o
.GetType(), ao
);
94 private void Serialize(object o
, XmlRootAttribute root
)
97 xs
= new XmlSerializer(o
.GetType(), root
);
102 #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
104 public void TestConstructor()
106 XmlSerializer ser
= new XmlSerializer (null, "");
111 // test basic types ////////////////////////////////////////////////////////
113 public void TestSerializeInt()
116 AssertEquals(Infoset("<int>10</int>"), WriterText
);
120 public void TestSerializeBool()
123 AssertEquals(Infoset("<boolean>true</boolean>"), WriterText
);
126 AssertEquals(Infoset("<boolean>false</boolean>"), WriterText
);
130 public void TestSerializeString()
133 AssertEquals(Infoset("<string>hello</string>"), WriterText
);
137 public void TestSerializeEmptyString()
139 Serialize(String
.Empty
);
140 AssertEquals(Infoset("<string />"), WriterText
);
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
);
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
);
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
);
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
);
174 public void TestSerializeChar()
177 AssertEquals(Infoset("<char>65</char>"), WriterText
);
180 AssertEquals(Infoset("<char>0</char>"), WriterText
);
183 AssertEquals(Infoset("<char>10</char>"), WriterText
);
186 AssertEquals(Infoset("<char>65281</char>"), WriterText
);
190 public void TestSerializeFloat()
193 AssertEquals(Infoset("<double>10.78</double>"), WriterText
);
196 AssertEquals(Infoset("<double>-100000000</double>"), WriterText
);
198 // FIXME test INF and other boundary conditions that may exist with floats
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
);
213 public void TestSerializeQualifiedName()
215 Serialize(new XmlQualifiedName("me", "home.urn"));
216 AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText
);
220 public void TestSerializeBytes()
222 Serialize((byte)0xAB);
223 AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText
);
226 AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText
);
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
);
240 public void TestSerializeDateTime()
242 DateTime d
= new DateTime();
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
;
251 AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp
+ "</dateTime>"), WriterText
);
261 public void TestSerialize()
264 AssertEquals(WriterText, "");
268 // test basic class serialization /////////////////////////////////////
270 public void TestSerializeSimpleClass()
272 SimpleClass simple
= new SimpleClass();
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";
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
);
283 public void TestSerializeStringCollection()
285 StringCollection strings
= new StringCollection();
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");
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
);
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
);
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
);
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 ///////////////////////////////////////////////
345 public void TestSerializeSimpleClassWithXmlAttributes()
347 SimpleClassWithXmlAttributes simple
= new SimpleClassWithXmlAttributes();
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";
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 ///////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////
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");
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");
414 // test xmlRoot //////////////////////////////////////////////////////////
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");
437 root
.IsNullable
= true;
440 Serialize(simple
, root
);
441 Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
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
);
455 public void TestSerializeXmlRootAttributeOnMember()
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 /////////////////////////////////////////////////////
478 public void TestSerializeXmlAttributeAttribute()
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
);
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
);
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
);
501 // FIXME this should work, shouldnt it?
502 // attr.XmlAttribute.Type = typeof(string);
503 // Serialize(simple, overrides);
504 // Assert(WriterText.EndsWith(" something='hello' />"));
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
);
512 // FIXME XmlSchemaForm Form
514 // FIXME write XmlQualifiedName as attribute
517 // test XmlElement ///////////////////////////////////////////////////////
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
);
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
);
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
);
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
);
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
);
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
);
568 // test XmlElementAttribute with arrays and collections //////////////////
570 public void TestSerializeCollectionWithXmlElementAttribute()
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
);
607 container
.Messages
.Add("goodbye");
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 /////////////////////////////////////////////////////
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
);
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
);
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 //////////////////////////////////////////////////////////
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 ();
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
+ "'");
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
);
691 foreach (string name
in ats
)
693 string[] nn
= name
.Split (' ');
694 GetInfoset (elem
.Attributes
[nn
[0],nn
[1]], sb
);
698 foreach (XmlNode cn
in elem
.ChildNodes
)
704 sb
.Append (nod
.OuterXml
);