2 // XmlObjectSerializerTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Ankit Jain <JAnkit@novell.com>
8 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // This test code contains tests for both DataContractSerializer and
33 // NetDataContractSerializer. The code could be mostly common.
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
42 using System.Runtime.Serialization;
45 using NUnit.Framework;
46 using System.Xml.Serialization;
47 using System.Xml.Schema;
49 namespace MonoTests.System.Runtime.Serialization
52 public class DataContractSerializerTest
54 static readonly XmlWriterSettings settings;
56 static DataContractSerializerTest ()
58 settings = new XmlWriterSettings ();
59 settings.OmitXmlDeclaration = true;
66 public string Member1;
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void ConstructorTypeNull ()
73 new DataContractSerializer (null);
77 public void ConstructorKnownTypesNull ()
79 // null knownTypes is allowed.
80 new DataContractSerializer (typeof (Sample1), null);
81 new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null);
82 new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null);
86 [ExpectedException (typeof (ArgumentNullException))]
87 public void ConstructorNameNull ()
89 new DataContractSerializer (typeof (Sample1), null, String.Empty);
93 [ExpectedException (typeof (ArgumentNullException))]
94 public void ConstructorNamespaceNull ()
96 new DataContractSerializer (typeof (Sample1), "foo", null);
100 [ExpectedException (typeof (ArgumentOutOfRangeException))]
101 public void ConstructorNegativeMaxObjects ()
103 new DataContractSerializer (typeof (Sample1), null,
104 -1, false, false, null);
108 public void ConstructorMisc ()
110 new DataContractSerializer (typeof (GlobalSample1));
114 public void WriteObjectContent ()
116 StringWriter sw = new StringWriter ();
117 using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
118 DataContractSerializer ser =
119 new DataContractSerializer (typeof (string));
120 xw.WriteStartElement ("my-element");
121 ser.WriteObjectContent (xw, "TEST STRING");
122 xw.WriteEndElement ();
124 Assert.AreEqual ("<my-element>TEST STRING</my-element>",
129 public void WriteObjectToStream ()
131 DataContractSerializer ser =
132 new DataContractSerializer (typeof (int));
133 MemoryStream sw = new MemoryStream ();
134 ser.WriteObject (sw, 1);
135 string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
136 byte[] buf = sw.ToArray ();
137 Assert.AreEqual (expected, Encoding.UTF8.GetString (buf, 0, buf.Length));
141 public void ReadObjectFromStream ()
143 DataContractSerializer ser =
144 new DataContractSerializer (typeof (int));
145 string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
146 byte[] buf = Encoding.UTF8.GetBytes (expected);
147 MemoryStream sw = new MemoryStream (buf);
148 object res = ser.ReadObject (sw);
149 Assert.AreEqual (1, res);
155 public void SerializeInt ()
157 DataContractSerializer ser =
158 new DataContractSerializer (typeof (int));
159 SerializeInt (ser, "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>");
164 [Category ("NotWorking")]
165 public void NetSerializeInt ()
167 NetDataContractSerializer ser =
168 new NetDataContractSerializer ();
169 // z:Assembly="0" ???
170 SerializeInt (ser, String.Format ("<int z:Type=\"System.Int32\" z:Assembly=\"0\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>", typeof (int).Assembly.FullName));
173 void SerializeInt (XmlObjectSerializer ser, string expected)
175 StringWriter sw = new StringWriter ();
176 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
177 ser.WriteObject (w, 1);
179 Assert.AreEqual (expected, sw.ToString ());
182 // pass typeof(DCEmpty), serialize int
185 public void SerializeIntForDCEmpty ()
187 DataContractSerializer ser =
188 new DataContractSerializer (typeof (DCEmpty));
190 SerializeIntForDCEmpty (ser, "<DCEmpty xmlns:d1p1=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d1p1:int\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\">1</DCEmpty>");
193 void SerializeIntForDCEmpty (XmlObjectSerializer ser, string expected)
195 StringWriter sw = new StringWriter ();
196 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
197 ser.WriteObject (w, 1);
199 XmlComparer.AssertAreEqual (expected, sw.ToString ());
205 public void SerializeEmptyClass ()
207 DataContractSerializer ser =
208 new DataContractSerializer (typeof (DCEmpty));
209 SerializeEmptyClass (ser, "<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
213 [Category ("NotWorking")]
214 public void NetSerializeEmptyClass ()
216 NetDataContractSerializer ser =
217 new NetDataContractSerializer ();
218 SerializeEmptyClass (ser, String.Format ("<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCEmpty\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
221 void SerializeEmptyClass (XmlObjectSerializer ser, string expected)
223 StringWriter sw = new StringWriter ();
224 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
225 ser.WriteObject (w, new DCEmpty ());
227 Assert.AreEqual (expected, sw.ToString ());
230 // string (primitive)
233 public void SerializePrimitiveString ()
235 XmlObjectSerializer ser =
236 new DataContractSerializer (typeof (string));
237 SerializePrimitiveString (ser, "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
241 [Category ("NotWorking")]
242 public void NetSerializePrimitiveString ()
244 XmlObjectSerializer ser = new NetDataContractSerializer ();
245 SerializePrimitiveString (ser, "<string z:Type=\"System.String\" z:Assembly=\"0\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
248 void SerializePrimitiveString (XmlObjectSerializer ser, string expected)
250 StringWriter sw = new StringWriter ();
251 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
252 ser.WriteObject (w, "TEST");
254 Assert.AreEqual (expected, sw.ToString ());
257 // QName (primitive but ...)
260 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
261 public void SerializePrimitiveQName ()
263 XmlObjectSerializer ser =
264 new DataContractSerializer (typeof (XmlQualifiedName));
265 SerializePrimitiveQName (ser, "<z:QName xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
269 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
270 public void NetSerializePrimitiveQName ()
272 XmlObjectSerializer ser = new NetDataContractSerializer ();
273 SerializePrimitiveQName (ser, "<z:QName z:Type=\"System.Xml.XmlQualifiedName\" z:Assembly=\"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
276 void SerializePrimitiveQName (XmlObjectSerializer ser, string expected)
278 StringWriter sw = new StringWriter ();
279 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
280 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
282 Assert.AreEqual (expected, sw.ToString ());
288 public void SerializeSimpleClass1 ()
290 DataContractSerializer ser =
291 new DataContractSerializer (typeof (DCSimple1));
292 SerializeSimpleClass1 (ser, "<DCSimple1 xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Foo>TEST</Foo></DCSimple1>");
296 [ExpectedException (typeof (SerializationException))]
297 [Category ("NotWorking")] // behavior changed in 3.5/SP1
298 public void SerializeSimpleXml ()
300 DataContractSerializer ser =
301 new DataContractSerializer (typeof (SimpleXml));
302 SerializeSimpleClass1 (ser, @"<simple i:type=""d1p1:DCSimple1"" xmlns:d1p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><d1p1:Foo>TEST</d1p1:Foo></simple>");
306 [Category ("NotWorking")]
307 public void NetSerializeSimpleClass1 ()
309 NetDataContractSerializer ser =
310 new NetDataContractSerializer ();
311 SerializeSimpleClass1 (ser, String.Format ("<DCSimple1 xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCSimple1\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Foo z:Id=\"2\">TEST</Foo></DCSimple1>", this.GetType ().Assembly.FullName));
314 void SerializeSimpleClass1 (XmlObjectSerializer ser, string expected)
316 StringWriter sw = new StringWriter ();
317 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
318 ser.WriteObject (w, new DCSimple1 ());
320 Console.WriteLine(sw.ToString());
321 Assert.AreEqual (expected, sw.ToString ());
324 // NonDC (behavior changed in 3.5/SP1; not it's not rejected)
327 public void SerializeNonDC ()
329 DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
330 var sw = new StringWriter ();
331 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
332 ser.WriteObject (w, new NonDC ());
334 Assert.AreEqual ("<NonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Whee>whee!</Whee></NonDC>".Replace ('\'', '"'), sw.ToString ());
340 public void SerializeDCHasNonDC ()
342 DataContractSerializer ser = new DataContractSerializer (typeof (DCHasNonDC));
343 var sw = new StringWriter ();
344 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
345 ser.WriteObject (w, new DCHasNonDC ());
347 Assert.AreEqual ("<DCHasNonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Hoge><Whee>whee!</Whee></Hoge></DCHasNonDC>".Replace ('\'', '"'), sw.ToString ());
353 // DCHasSerializable itself is DataContract and has a field
354 // whose type is not contract but serializable.
355 public void SerializeSimpleSerializable1 ()
357 DataContractSerializer ser = new DataContractSerializer (typeof (DCHasSerializable));
358 StringWriter sw = new StringWriter ();
359 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
360 ser.WriteObject (w, new DCHasSerializable ());
362 Assert.AreEqual ("<DCHasSerializable xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Ser><Doh>doh!</Doh></Ser></DCHasSerializable>", sw.ToString ());
366 public void SerializeDCWithName ()
368 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
369 StringWriter sw = new StringWriter ();
370 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
371 ser.WriteObject (w, new DCWithName ());
373 Assert.AreEqual ("<Foo xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><FooMember>value</FooMember></Foo>", sw.ToString ());
377 public void SerializeDCWithEmptyName1 ()
379 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyName));
380 StringWriter sw = new StringWriter ();
381 DCWithEmptyName dc = new DCWithEmptyName ();
382 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
384 ser.WriteObject (w, dc);
385 } catch (InvalidDataContractException) {
389 Assert.Fail ("Expected InvalidDataContractException");
393 [Category ("NotWorking")]
394 public void SerializeDCWithEmptyName2 ()
396 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
397 StringWriter sw = new StringWriter ();
399 /* DataContractAttribute.Name == "", not valid */
400 DCWithEmptyName dc = new DCWithEmptyName ();
401 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
403 ser.WriteObject (w, dc);
404 } catch (InvalidDataContractException) {
408 Assert.Fail ("Expected InvalidDataContractException");
412 [Category ("NotWorking")]
413 public void SerializeDCWithNullName ()
415 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithNullName));
416 StringWriter sw = new StringWriter ();
417 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
419 /* DataContractAttribute.Name == "", not valid */
420 ser.WriteObject (w, new DCWithNullName ());
421 } catch (InvalidDataContractException) {
425 Assert.Fail ("Expected InvalidDataContractException");
429 public void SerializeDCWithEmptyNamespace1 ()
431 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyNamespace));
432 StringWriter sw = new StringWriter ();
433 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
434 ser.WriteObject (w, new DCWithEmptyNamespace ());
441 public void SerializeWrappedClass ()
443 DataContractSerializer ser =
444 new DataContractSerializer (typeof (Wrapper.DCWrapped));
445 SerializeWrappedClass (ser, "<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
449 [Category ("NotWorking")]
450 public void NetSerializeWrappedClass ()
452 NetDataContractSerializer ser =
453 new NetDataContractSerializer ();
454 SerializeWrappedClass (ser, String.Format ("<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.Wrapper+DCWrapped\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
457 void SerializeWrappedClass (XmlObjectSerializer ser, string expected)
459 StringWriter sw = new StringWriter ();
460 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
461 ser.WriteObject (w, new Wrapper.DCWrapped ());
463 Assert.AreEqual (expected, sw.ToString ());
468 // CollectionContainer : Items must have a setter.
469 [ExpectedException (typeof (InvalidDataContractException))]
470 [Category ("NotWorking")]
472 public void SerializeReadOnlyCollectionMember ()
474 DataContractSerializer ser =
475 new DataContractSerializer (typeof (CollectionContainer));
477 StringWriter sw = new StringWriter ();
478 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
479 ser.WriteObject (w, null);
481 Assert.AreEqual ("<CollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
483 sw = new StringWriter ();
484 var c = new CollectionContainer ();
487 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
488 ser.WriteObject (w, c);
490 Assert.AreEqual ("<CollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items xmlns:d2p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><d2p1:string>foo</d2p1:string><d2p1:string>bar</d2p1:string></Items></CollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
493 // DataCollectionContainer : Items must have a setter.
495 //[ExpectedException (typeof (InvalidDataContractException))]
496 public void SerializeReadOnlyDataCollectionMember ()
498 DataContractSerializer ser =
499 new DataContractSerializer (typeof (DataCollectionContainer));
500 StringWriter sw = new StringWriter ();
501 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
502 ser.WriteObject (w, null);
504 Assert.AreEqual ("<DataCollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
506 sw = new StringWriter ();
507 var c = new DataCollectionContainer ();
510 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
511 ser.WriteObject (w, c);
513 // LAMESPEC: this is bogus behavior. .NET serializes
514 // System.String as "string" without overriding its
515 // element namespace, but then it must be regarded as
516 // in parent's namespace. What if there already is an
517 // element definition for "string" with the same
519 Assert.AreEqual ("<DataCollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items><string>foo</string><string>bar</string></Items></DataCollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
523 public void SerializeGuid ()
525 DataContractSerializer ser = new DataContractSerializer (typeof (Guid));
526 StringWriter sw = new StringWriter ();
527 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
528 ser.WriteObject (w, Guid.Empty);
531 "<guid xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">00000000-0000-0000-0000-000000000000</guid>",
536 public void SerializeEnum ()
538 DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
539 StringWriter sw = new StringWriter ();
540 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
541 ser.WriteObject (w, new Colors ());
545 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>",
550 public void SerializeEnum2 ()
552 DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
553 StringWriter sw = new StringWriter ();
554 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
555 ser.WriteObject (w, 0);
558 XmlComparer.AssertAreEqual (
559 @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">0</Colors>",
564 public void SerializeEnumWithDC ()
566 DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
567 StringWriter sw = new StringWriter ();
568 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
569 ser.WriteObject (w, new ColorsWithDC ());
573 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>",
578 public void SerializeEnumWithNoDC ()
580 DataContractSerializer ser = new DataContractSerializer (typeof (ColorsEnumMemberNoDC));
581 StringWriter sw = new StringWriter ();
582 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
583 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
587 @"<ColorsEnumMemberNoDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</ColorsEnumMemberNoDC>",
592 public void SerializeEnumWithDC2 ()
594 DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
595 StringWriter sw = new StringWriter ();
596 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
597 ser.WriteObject (w, 3);
600 XmlComparer.AssertAreEqual (
601 @"<_ColorsWithDC xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">3</_ColorsWithDC>",
606 [ExpectedException (typeof (SerializationException))]
607 public void SerializeEnumWithDCInvalid ()
609 DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
610 StringWriter sw = new StringWriter ();
611 ColorsWithDC cdc = ColorsWithDC.Blue;
612 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
613 ser.WriteObject (w, cdc);
618 public void SerializeDCWithEnum ()
620 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum));
621 StringWriter sw = new StringWriter ();
622 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
623 ser.WriteObject (w, new DCWithEnum ());
627 @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>",
632 public void SerializeDCWithTwoEnums ()
634 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithTwoEnums));
635 StringWriter sw = new StringWriter ();
636 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
637 DCWithTwoEnums e = new DCWithTwoEnums ();
638 e.colors = Colors.Blue;
639 e.colors2 = Colors.Green;
640 ser.WriteObject (w, e);
644 @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors>Blue</colors><colors2>Green</colors2></DCWithTwoEnums>",
649 public void SerializeNestingDC2 ()
651 DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC2));
652 StringWriter sw = new StringWriter ();
653 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
654 NestingDC2 e = new NestingDC2 ();
655 e.Field = new NestedDC2 ("Something");
656 ser.WriteObject (w, e);
660 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>",
665 public void SerializeNestingDC ()
667 DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC));
668 StringWriter sw = new StringWriter ();
669 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
670 NestingDC e = new NestingDC ();
671 e.Field1 = new NestedDC ("test1");
672 e.Field2 = new NestedDC ("test2");
673 ser.WriteObject (w, e);
677 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>",
679 sw = new StringWriter ();
680 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
681 NestingDC e = new NestingDC ();
682 ser.WriteObject (w, e);
686 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>",
691 public void SerializeDerivedDC ()
693 DataContractSerializer ser = new DataContractSerializer (typeof (DerivedDC));
694 StringWriter sw = new StringWriter ();
695 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
696 DerivedDC e = new DerivedDC ();
697 ser.WriteObject (w, e);
701 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">0</baseVal><derivedVal>0</derivedVal></DerivedDC>",
706 public void SerializerDCArray ()
708 DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum []));
709 StringWriter sw = new StringWriter ();
710 DCWithEnum [] arr = new DCWithEnum [2];
711 arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
712 arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
713 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
714 ser.WriteObject (w, arr);
717 XmlComparer.AssertAreEqual (
718 @"<ArrayOfDCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><DCWithEnum><_colors>Red</_colors></DCWithEnum><DCWithEnum><_colors>Green</_colors></DCWithEnum></ArrayOfDCWithEnum>",
723 public void SerializerDCArray2 ()
725 List<Type> known = new List<Type> ();
726 known.Add (typeof (DCWithEnum));
727 known.Add (typeof (DCSimple1));
728 DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
729 StringWriter sw = new StringWriter ();
730 object [] arr = new object [2];
731 arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
732 arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
734 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
735 ser.WriteObject (w, arr);
738 XmlComparer.AssertAreEqual (
739 @"<ArrayOfanyType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCWithEnum""><d2p1:_colors>Red</d2p1:_colors></anyType><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCSimple1""><d2p1:Foo>hello</d2p1:Foo></anyType></ArrayOfanyType>",
744 public void SerializerDCArray3 ()
746 DataContractSerializer ser = new DataContractSerializer (typeof (int []));
747 StringWriter sw = new StringWriter ();
748 int [] arr = new int [2];
749 arr [0] = 1; arr [1] = 2;
751 using (XmlWriter w = XmlWriter.Create (sw, settings)) {
752 ser.WriteObject (w, arr);
755 XmlComparer.AssertAreEqual (
756 @"<ArrayOfint xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><int>1</int><int>2</int></ArrayOfint>",
761 public void SerializeNonDCArray ()
763 DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
764 StringWriter sw = new StringWriter ();
765 using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
766 ser.WriteObject (xw, new SerializeNonDCArrayType ());
768 Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses xmlns:d2p1=""http://schemas.datacontract.org/2004/07/System.Net"" /></SerializeNonDCArrayType>",
773 public void SerializeNonDCArrayItems ()
775 DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
776 StringWriter sw = new StringWriter ();
777 using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
778 SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
779 obj.IPAddresses = new IPAddress [] {new IPAddress (new byte [] {1, 2, 3, 4})};
780 ser.WriteObject (xw, obj);
783 XmlDocument doc = new XmlDocument ();
784 doc.LoadXml (sw.ToString ());
785 XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
786 nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
787 nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
788 nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
790 Assert.AreEqual (1, doc.SelectNodes ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress", nsmgr).Count, "#1");
791 Assert.AreEqual ("67305985", doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress/n:m_Address", nsmgr).InnerText, "#2");
792 XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/n:IPAddress/n:m_Numbers", nsmgr) as XmlElement;
793 Assert.IsNotNull (el, "#3");
794 Assert.AreEqual (8, el.SelectNodes ("a:unsignedShort", nsmgr).Count, "#4");
798 public void DeserializeEnum ()
800 Colors c = Deserialize<Colors> (
801 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>");
803 Assert.AreEqual (Colors.Red, c, "#de2");
807 public void DeserializeEnum2 ()
809 Colors c = Deserialize<Colors> (
810 @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">1</Colors>",
813 Assert.AreEqual (Colors.Green, c, "#de4");
817 [ExpectedException (typeof (SerializationException))]
818 public void DeserializeEnumInvalid1 ()
820 Deserialize<Colors> (
821 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></Colors>");
825 [ExpectedException (typeof (SerializationException))]
826 public void DeserializeEnumInvalid2 ()
828 Deserialize<Colors> (
829 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
833 [ExpectedException (typeof (SerializationException))]
834 public void DeserializeEnumInvalid3 ()
836 //"red" instead of "Red"
837 Deserialize<Colors> (
838 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">red</Colors>");
842 public void DeserializeEnumFlags ()
844 Deserialize<Colors2> (
845 @"<Colors2 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
849 public void DeserializeEnumWithDC ()
851 ColorsWithDC cdc = Deserialize<ColorsWithDC> (
852 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>");
854 Assert.AreEqual (ColorsWithDC.Red, cdc, "#de6");
858 [ExpectedException (typeof (SerializationException))]
859 public void DeserializeEnumWithDCInvalid ()
861 Deserialize<ColorsWithDC> (
862 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">NonExistant</_ColorsWithDC>");
866 public void DeserializeDCWithEnum ()
868 DCWithEnum dc = Deserialize<DCWithEnum> (
869 @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>");
871 Assert.AreEqual (Colors.Red, dc.colors, "#de8");
875 public void DeserializeNestingDC ()
877 NestingDC dc = Deserialize<NestingDC> (
878 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>");
880 Assert.IsNotNull (dc.Field1, "#N1: Field1 should not be null.");
881 Assert.IsNotNull (dc.Field2, "#N2: Field2 should not be null.");
882 Assert.AreEqual ("test1", dc.Field1.Name, "#1");
883 Assert.AreEqual ("test2", dc.Field2.Name, "#2");
887 public void DeserializeNestingDC2 ()
889 NestingDC2 dc = Deserialize<NestingDC2> (
890 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>");
892 Assert.IsNotNull (dc.Field, "#N1: Field should not be null.");
893 Assert.AreEqual ("Something", dc.Field.Name, "#N2");
897 public void DeserializeDerivedDC ()
899 DerivedDC dc = Deserialize<DerivedDC> (
900 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">1</baseVal><derivedVal>2</derivedVal></DerivedDC>");
902 Assert.AreEqual (1, dc.baseVal, "#N1");
903 Assert.AreEqual (2, dc.derivedVal, "#N2");
907 public void DeserializeTwice ()
910 @"<any><_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC> <_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC></any>";
911 DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
912 XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
913 xr.ReadStartElement ();
914 object o = ser.ReadObject (xr);
915 Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
916 ColorsWithDC cdc = (ColorsWithDC) o;
917 Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
919 o = ser.ReadObject (xr);
920 Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
921 cdc = (ColorsWithDC) o;
922 Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
923 Assert.AreEqual (XmlNodeType.EndElement, xr.NodeType, "#de6");
924 Assert.AreEqual ("any", xr.LocalName, "#de6");
925 xr.ReadEndElement ();
930 public void DeserializeEmptyNestingDC ()
932 NestingDC dc = Deserialize<NestingDC> (
933 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></NestingDC>");
935 Assert.IsNotNull (dc, "#A0: The object should not be null.");
936 Assert.IsNull (dc.Field1, "#A1: Field1 should be null.");
937 Assert.IsNull (dc.Field2, "#A2: Field2 should be null.");
939 dc = Deserialize<NestingDC> (
940 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
942 Assert.IsNotNull (dc, "#B0: The object should not be null.");
943 Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
944 Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
946 dc = Deserialize<NestingDC> (
947 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>");
949 Assert.IsNotNull (dc, "#B0: The object should not be null.");
950 Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
951 Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
955 [ExpectedException (typeof (SerializationException))]
956 public void DeserializeEmptyDCWithTwoEnums ()
958 Deserialize<DCWithTwoEnums> (
959 @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors i:nil=""true""/><colors2 i:nil=""true""/></DCWithTwoEnums>");
963 [Category ("NotWorking")]
964 public void DeserializeDCWithNullableEnum ()
966 DCWithNullableEnum dc = Deserialize<DCWithNullableEnum> (
967 @"<DCWithNullableEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors i:nil=""true""/></DCWithNullableEnum>");
969 Assert.IsNull (dc.colors, "#B1: Field should be null.");
973 public void DeserializeDCWithTwoEnums ()
975 DCWithTwoEnums dc = Deserialize<DCWithTwoEnums> (
976 @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors>Blue</colors><colors2>Green</colors2></DCWithTwoEnums>");
978 Assert.AreEqual (Colors.Blue, dc.colors, "#0");
979 Assert.AreEqual (Colors.Green, dc.colors2, "#1");
983 public void DeserializerDCArray ()
985 DCWithEnum [] dcArray = Deserialize<DCWithEnum []> (
986 @"<ArrayOfDCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><DCWithEnum><_colors>Red</_colors></DCWithEnum><DCWithEnum><_colors>Green</_colors></DCWithEnum></ArrayOfDCWithEnum>");
988 Assert.AreEqual (2, dcArray.Length, "#N1");
989 Assert.AreEqual (Colors.Red, dcArray [0].colors, "#N2");
990 Assert.AreEqual (Colors.Green, dcArray [1].colors, "#N3");
994 public void DeserializerDCArray2 ()
997 @"<ArrayOfanyType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCWithEnum""><d2p1:_colors>Red</d2p1:_colors></anyType><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCSimple1""><d2p1:Foo>hello</d2p1:Foo></anyType></ArrayOfanyType>";
999 List<Type> known = new List<Type> ();
1000 known.Add (typeof (DCWithEnum));
1001 known.Add (typeof (DCSimple1));
1002 DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
1003 XmlReader xr = XmlReader.Create (new StringReader (xml));
1005 object [] dc = (object []) ser.ReadObject (xr);
1006 Assert.AreEqual (2, dc.Length, "#N1");
1007 Assert.AreEqual (typeof (DCWithEnum), dc [0].GetType (), "#N2");
1008 DCWithEnum dc0 = (DCWithEnum) dc [0];
1009 Assert.AreEqual (Colors.Red, dc0.colors, "#N3");
1010 Assert.AreEqual (typeof (DCSimple1), dc [1].GetType (), "#N4");
1011 DCSimple1 dc1 = (DCSimple1) dc [1];
1012 Assert.AreEqual ("hello", dc1.Foo, "#N4");
1016 public void DeserializerDCArray3 ()
1018 int [] intArray = Deserialize<int []> (
1019 @"<ArrayOfint xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><int>1</int><int>2</int></ArrayOfint>");
1021 Assert.AreEqual (2, intArray.Length, "#N0");
1022 Assert.AreEqual (1, intArray [0], "#N1");
1023 Assert.AreEqual (2, intArray [1], "#N2");
1027 public void ReadObjectNoVerifyObjectName ()
1029 string xml = @"<any><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization1"">bar1</Member1><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization2"">bar2</Member1><Member1 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">bar</Member1></any>";
1030 VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
1031 .ReadObject (XmlReader.Create (new StringReader (xml)), false);
1032 Assert.AreEqual ("bar", res.GetMember());
1036 public void ReadObjectVerifyObjectName ()
1038 string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Member1>bar</Member1></VerifyObjectNameTestData>";
1039 VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
1040 .ReadObject (XmlReader.Create (new StringReader (xml)));
1041 Assert.AreEqual ("bar", res.GetMember());
1045 [ExpectedException (typeof (SerializationException))]
1046 public void ReadObjectWrongNamespace ()
1048 string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization2""><Member1>bar</Member1></VerifyObjectNameTestData>";
1049 new DataContractSerializer (typeof (VerifyObjectNameTestData))
1050 .ReadObject (XmlReader.Create (new StringReader (xml)));
1054 public void ReferenceSerialization ()
1056 var dc = new DataContractSerializer (typeof (ReferenceWrapper));
1057 var t = new ReferenceType ();
1058 StringWriter sw = new StringWriter ();
1059 using (var xw = XmlWriter.Create (sw)) {
1060 xw.WriteStartElement ("z", "root", "http://schemas.microsoft.com/2003/10/Serialization/");
1061 dc.WriteObject (xw, new ReferenceWrapper () {T = t, T2 = t});
1062 xw.WriteEndElement ();
1064 string xml = @"<?xml version='1.0' encoding='utf-16'?><z:root xmlns:z='http://schemas.microsoft.com/2003/10/Serialization/'><ReferenceWrapper xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><T z:Id='i1'><F>x</F></T><T2 z:Ref='i1' /></ReferenceWrapper></z:root>";
1065 Assert.AreEqual (xml.Replace ('\'', '"'), sw.ToString (), "#1");
1068 using (XmlReader r = XmlReader.Create (new StringReader (xml)))
1070 r.ReadStartElement ();
1071 w = (ReferenceWrapper) dc.ReadObject (r);
1072 r.ReadEndElement ();
1074 Assert.AreEqual (w.T, w.T2, "#2");
1078 public void GenericSerialization ()
1080 var sw = new StringWriter ();
1081 var ser = new DataContractSerializer (typeof (Foo<string,int,int>));
1082 using (var xw = XmlWriter.Create (sw))
1083 ser.WriteObject (xw, new Foo<string,int,int> () {Field = "f"
1085 var s = sw.ToString ();
1087 var ret = (Foo<string,int,int>) ser.ReadObject (XmlReader.Create (new StringReader (s)));
1088 Assert.AreEqual ("f", ret.Field);
1092 public void GenericCollectionSerialization ()
1094 var l = new MyList ();
1097 var ds = new DataContractSerializer (typeof (MyList));
1098 var sw = new StringWriter ();
1099 using (var xw = XmlWriter.Create (sw))
1100 ds.WriteObject (xw, l);
1101 l = (MyList) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
1102 Assert.AreEqual (2, l.Count);
1106 public void GenericListOfKeyValuePairSerialization ()
1108 string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValuePairOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections.Generic'><KeyValuePairOfstringstring><key>foo</key><value>bar</value></KeyValuePairOfstringstring></ArrayOfKeyValuePairOfstringstring>".Replace ('\'', '"');
1110 var ds = new DataContractSerializer (typeof (List<KeyValuePair<string,string>>));
1111 var d = new List<KeyValuePair<string,string>> ();
1112 d.Add (new KeyValuePair<string,string> ("foo", "bar"));
1113 var sw = new StringWriter ();
1114 using (var xw = XmlWriter.Create (sw))
1115 ds.WriteObject (xw, d);
1116 Assert.AreEqual (xml, sw.ToString (), "#1");
1117 d = (List<KeyValuePair<string,string>>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1118 Assert.AreEqual (1, d.Count, "#2");
1119 Assert.AreEqual ("bar", d [0].Value, "#3");
1123 public void GenericListOfDictionaryEntrySerialization ()
1125 string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfDictionaryEntry xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System.Collections'><DictionaryEntry><_key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</_key><_value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</_value></DictionaryEntry></ArrayOfDictionaryEntry>".Replace ('\'', '"');
1127 var ds = new DataContractSerializer (typeof (List<DictionaryEntry>));
1128 var d = new List<DictionaryEntry> ();
1129 d.Add (new DictionaryEntry ("foo", "bar"));
1130 var sw = new StringWriter ();
1131 using (var xw = XmlWriter.Create (sw))
1132 ds.WriteObject (xw, d);
1133 Assert.AreEqual (xml, sw.ToString (), "#1");
1134 Assert.IsTrue (sw.ToString ().IndexOf ("i:type") >= 0);
1135 d = (List<DictionaryEntry>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1136 Assert.AreEqual (1, d.Count, "#2");
1137 Assert.AreEqual ("bar", d [0].Value, "#3");
1141 public void GenericDictionarySerialization ()
1143 string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfstringstring xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfstringstring><Key>foo</Key><Value>bar</Value></KeyValueOfstringstring></ArrayOfKeyValueOfstringstring>".Replace ('\'', '"');
1145 var ds = new DataContractSerializer (typeof (Dictionary<string,string>));
1146 var d = new Dictionary<string,string> ();
1148 var sw = new StringWriter ();
1149 using (var xw = XmlWriter.Create (sw))
1150 ds.WriteObject (xw, d);
1151 Assert.AreEqual (xml, sw.ToString (), "#1");
1152 d = (Dictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1153 Assert.AreEqual (1, d.Count, "#2");
1154 Assert.AreEqual ("bar", d ["foo"], "#3");
1158 public void HashtableSerialization ()
1160 string xml = @"<?xml version='1.0' encoding='utf-16'?><ArrayOfKeyValueOfanyTypeanyType xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><KeyValueOfanyTypeanyType><Key xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>foo</Key><Value xmlns:d3p1='http://www.w3.org/2001/XMLSchema' i:type='d3p1:string'>bar</Value></KeyValueOfanyTypeanyType></ArrayOfKeyValueOfanyTypeanyType>".Replace ('\'', '"');
1162 var ds = new DataContractSerializer (typeof (Hashtable));
1163 var d = new Hashtable ();
1165 var sw = new StringWriter ();
1166 using (var xw = XmlWriter.Create (sw))
1167 ds.WriteObject (xw, d);
1168 Assert.AreEqual (xml, sw.ToString (), "#1");
1169 d = (Hashtable) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1170 Assert.AreEqual (1, d.Count, "#2");
1171 Assert.AreEqual ("bar", d ["foo"], "#3");
1175 public void CollectionContarctDictionarySerialization ()
1177 string xml = @"<?xml version='1.0' encoding='utf-16'?><NAME xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:foo'><ITEM><KEY>foo</KEY><VALUE>bar</VALUE></ITEM></NAME>".Replace ('\'', '"');
1179 var ds = new DataContractSerializer (typeof (MyDictionary<string,string>));
1180 var d = new MyDictionary<string,string> ();
1182 var sw = new StringWriter ();
1183 using (var xw = XmlWriter.Create (sw))
1184 ds.WriteObject (xw, d);
1185 Assert.AreEqual (xml, sw.ToString (), "#1");
1186 d = (MyDictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1187 Assert.AreEqual (1, d.Count, "#2");
1188 Assert.AreEqual ("bar", d ["foo"], "#3");
1192 public void SerializeInterfaceCollection ()
1194 var ser = new DataContractSerializer (typeof (InterfaceCollectionType));
1195 var sw = new StringWriter ();
1196 var obj = new InterfaceCollectionType ();
1197 using (var xw = XmlWriter.Create (sw))
1198 ser.WriteObject (xw, obj);
1199 using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
1200 obj = (InterfaceCollectionType) ser.ReadObject (xr);
1201 Assert.IsNull (obj.Array, "#1");
1204 sw = new StringWriter ();
1205 obj.Array = new List<int> ();
1207 using (var xw = XmlWriter.Create (sw))
1208 ser.WriteObject (xw, obj);
1209 using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
1210 obj = (InterfaceCollectionType) ser.ReadObject (xr);
1211 Assert.AreEqual (5, obj.Array [0], "#2");
1216 public void EmptyChildren ()
1219 <DummyPlaylist xmlns='http://example.com/schemas/asx'>
1222 <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-ins
1223 tance' i:type='PartDummyEntryInfo'/>
1224 <Href>http://vmsservices.example.com:8080/VideoService.s
1225 vc?crid=45541/part=1/guid=ae968b5d-e4a5-41fe-9b23-ed631b27cd21/</Href>
1230 var reader = XmlReader.Create (new StringReader (xml));
1231 DummyPlaylist playlist = (DummyPlaylist) new DataContractSerializer (typeof (DummyPlaylist)).ReadObject (reader);
1232 Assert.AreEqual (1, playlist.entries.Count, "#1");
1233 Assert.IsTrue (playlist.entries [0] is DummyEntry, "#2");
1234 Assert.IsNotNull (playlist.entries [0].Href, "#3");
1238 public void BaseKnownTypeAttributes ()
1242 <DummyPlaylist xmlns='http://example.com/schemas/asx'>
1245 <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
1250 using (XmlReader reader = XmlReader.Create (new StringReader (xml))) {
1251 DummyPlaylist playlist = new DataContractSerializer(typeof(DummyPlaylist)).ReadObject(reader) as DummyPlaylist;
1252 Assert.IsNotNull (playlist);
1257 public void Bug524083 ()
1260 <AsxEntryInfo xmlns='http://example.com/schemas/asx'>
1264 using (XmlReader reader = XmlReader.Create (new StringReader (xml)))
1265 new DataContractSerializer(typeof (AsxEntryInfo)).ReadObject (reader);
1268 private T Deserialize<T> (string xml)
1270 return Deserialize<T> (xml, typeof (T));
1273 private T Deserialize<T> (string xml, Type runtimeType)
1275 DataContractSerializer ser = new DataContractSerializer (typeof (T));
1276 XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
1277 object o = ser.ReadObject (xr);
1278 Assert.AreEqual (runtimeType, o.GetType (), "#DS0");
1282 public Dictionary<string, object> GenericDictionary (Dictionary<string, object> settings)
1284 using (MemoryStream ms = new MemoryStream ()) {
1285 DataContractSerializer save = new DataContractSerializer (settings.GetType ());
1286 save.WriteObject (ms, settings);
1290 DataContractSerializer load = new DataContractSerializer (typeof (Dictionary<string, object>));
1291 return (Dictionary<string, object>) load.ReadObject (ms);
1296 public void GenericDictionaryEmpty ()
1298 Dictionary<string, object> in_settings = new Dictionary<string, object> ();
1299 Dictionary<string, object> out_settings = GenericDictionary (in_settings);
1300 out_settings.Clear ();
1304 public void GenericDictionaryOneElement ()
1306 Dictionary<string, object> in_settings = new Dictionary<string, object> ();
1307 in_settings.Add ("one", "ONE");
1308 Dictionary<string, object> out_settings = GenericDictionary (in_settings);
1309 Assert.AreEqual ("ONE", out_settings ["one"], "out");
1310 out_settings.Clear ();
1314 public enum Colors {
1319 public enum Colors2 {
1323 [DataContract (Name = "_ColorsWithDC")]
1324 public enum ColorsWithDC {
1326 [EnumMember (Value = "_Red")]
1334 public enum ColorsEnumMemberNoDC {
1335 [EnumMember (Value = "_Red")]
1343 public class DCWithEnum {
1344 [DataMember (Name = "_colors")]
1345 public Colors colors;
1349 public class DCWithTwoEnums {
1351 public Colors colors;
1353 public Colors colors2;
1357 public class DCWithNullableEnum {
1359 public Colors? colors;
1363 [DataContract (Namespace = "Base")]
1364 public class BaseDC {
1369 [DataContract (Namespace = "Derived")]
1370 public class DerivedDC : BaseDC {
1372 public int derivedVal;
1376 public class NestedDC {
1377 public NestedDC (string name) { this.Name = name; }
1384 public class NestingDC {
1386 public NestedDC Field1;
1388 public NestedDC Field2;
1391 [DataContract (Namespace = "test1")]
1392 public class NestedDC2 {
1393 public NestedDC2 (string name) { this.Name = name; }
1399 [DataContract (Namespace = "test2")]
1400 public class NestingDC2 {
1402 public NestedDC2 Field;
1406 public class DCEmpty
1408 // serializer doesn't touch it.
1409 public string Foo = "TEST";
1413 public class DCSimple1
1416 public string Foo = "TEST";
1420 public class DCHasNonDC
1423 public NonDC Hoge= new NonDC ();
1428 public string Whee = "whee!";
1432 public class DCHasSerializable
1435 public SimpleSer1 Ser = new SimpleSer1 ();
1438 [DataContract (Name = "Foo")]
1439 public class DCWithName
1441 [DataMember (Name = "FooMember")]
1442 public string DMWithName = "value";
1445 [DataContract (Name = "")]
1446 public class DCWithEmptyName
1450 [DataContract (Name = null)]
1451 public class DCWithNullName
1455 [DataContract (Namespace = "")]
1456 public class DCWithEmptyNamespace
1461 public class SimpleSer1
1463 public string Doh = "doh!";
1465 public string Bah = "bah!";
1468 public class Wrapper
1471 public class DCWrapped
1477 public class CollectionContainer
1479 Collection<string> items = new Collection<string> ();
1482 public Collection<string> Items {
1483 get { return items; }
1487 [CollectionDataContract]
1488 public class DataCollection<T> : Collection<T>
1493 public class DataCollectionContainer
1495 DataCollection<string> items = new DataCollection<string> ();
1498 public DataCollection<string> Items {
1499 get { return items; }
1504 class SerializeNonDCArrayType
1507 public IPAddress [] IPAddresses = new IPAddress [0];
1511 public class VerifyObjectNameTestData
1514 string Member1 = "foo";
1516 public string GetMember() { return Member1; }
1519 [XmlRoot(ElementName = "simple", Namespace = "")]
1520 public class SimpleXml : IXmlSerializable
1522 void IXmlSerializable.ReadXml (XmlReader reader)
1526 void IXmlSerializable.WriteXml (XmlWriter writer)
1530 XmlSchema IXmlSerializable.GetSchema ()
1538 public class ReferenceWrapper
1540 [DataMember (Order = 1)]
1541 public ReferenceType T;
1543 [DataMember (Order = 2)]
1544 public ReferenceType T2;
1547 [DataContract (IsReference = true)]
1548 public class ReferenceType
1551 public string F = "x";
1554 public class MyList : IList<string>
1556 List<string> l = new List<string> ();
1557 public void Clear () { l.Clear (); }
1558 public void Add(string s) { l.Add (s);}
1559 public void Insert(int idx, string s) { l.Insert(idx,s);}
1560 public bool Contains(string s) { return l.Contains(s); }
1561 public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
1562 IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
1563 public bool Remove(string s) { return l.Remove(s); }
1564 public void RemoveAt(int i) { l.RemoveAt (i);}
1565 public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
1566 public int IndexOf (string s) { return l.IndexOf (s); }
1568 public int Count { get { return l.Count; } }
1569 public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
1570 public string this [int index] { get { return l [index]; } set { l [index] = value; } }
1574 internal class InterfaceCollectionType
1577 public IList<int> Array { get; set; }
1593 [CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
1594 public class MyDictionary<K,V> : Dictionary<K,V>
1599 [DataContract(Namespace="http://example.com/schemas/asx")]
1600 public class DummyEntry
1603 public DummyEntryInfo EntryInfo { get; set; }
1605 public string Href { get; set; }
1608 [DataContract(Namespace="http://example.com/schemas/asx"),
1609 KnownType(typeof(PartDummyEntryInfo))]
1610 public abstract class DummyEntryInfo
1614 [DataContract(Namespace="http://example.com/schemas/asx")]
1615 public class DummyPlaylist
1617 public IList<DummyEntry> entries = new List<DummyEntry> ();
1620 public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
1623 [DataContract(Namespace="http://example.com/schemas/asx")]
1624 public class PartDummyEntryInfo : DummyEntryInfo
1626 public PartDummyEntryInfo() {}
1631 [DataContract(Namespace="http://example.com/schemas/asx")]
1632 public class AsxEntryInfo
1635 public string AdvertPrompt { get; set; }