2010-06-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / XmlObjectSerializerTest.cs
1 //
2 // XmlObjectSerializerTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Ankit Jain <JAnkit@novell.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 //
32 // This test code contains tests for both DataContractSerializer and
33 // NetDataContractSerializer. The code could be mostly common.
34 //
35
36 using System;
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
40 using System.Data;
41 using System.IO;
42 using System.Net;
43 using System.Runtime.Serialization;
44 using System.Text;
45 using System.Xml;
46 using System.Xml.Schema;
47 using System.Xml.Serialization;
48 using NUnit.Framework;
49
50 [assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
51
52 namespace MonoTests.System.Runtime.Serialization
53 {
54         [TestFixture]
55         public class DataContractSerializerTest
56         {
57                 static readonly XmlWriterSettings settings;
58
59                 static DataContractSerializerTest ()
60                 {
61                         settings = new XmlWriterSettings ();
62                         settings.OmitXmlDeclaration = true;
63                 }
64
65                 [DataContract]
66                 class Sample1
67                 {
68                         [DataMember]
69                         public string Member1;
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentNullException))]
74                 public void ConstructorTypeNull ()
75                 {
76                         new DataContractSerializer (null);
77                 }
78
79                 [Test]
80                 public void ConstructorKnownTypesNull ()
81                 {
82                         // null knownTypes is allowed.
83                         new DataContractSerializer (typeof (Sample1), null);
84                         new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null);
85                         new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentNullException))]
90                 public void ConstructorNameNull ()
91                 {
92                         new DataContractSerializer (typeof (Sample1), null, String.Empty);
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ArgumentNullException))]
97                 public void ConstructorNamespaceNull ()
98                 {
99                         new DataContractSerializer (typeof (Sample1), "foo", null);
100                 }
101
102                 [Test]
103                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
104                 public void ConstructorNegativeMaxObjects ()
105                 {
106                         new DataContractSerializer (typeof (Sample1), null,
107                                 -1, false, false, null);
108                 }
109
110                 [Test]
111                 public void ConstructorMisc ()
112                 {
113                         new DataContractSerializer (typeof (GlobalSample1));
114                 }
115
116                 [Test]
117                 public void WriteObjectContent ()
118                 {
119                         StringWriter sw = new StringWriter ();
120                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
121                                 DataContractSerializer ser =
122                                         new DataContractSerializer (typeof (string));
123                                 xw.WriteStartElement ("my-element");
124                                 ser.WriteObjectContent (xw, "TEST STRING");
125                                 xw.WriteEndElement ();
126                         }
127                         Assert.AreEqual ("<my-element>TEST STRING</my-element>",
128                                 sw.ToString ());
129                 }
130
131                 [Test]
132                 public void WriteObjectToStream ()
133                 {
134                         DataContractSerializer ser =
135                                 new DataContractSerializer (typeof (int));
136                         MemoryStream sw = new MemoryStream ();
137                         ser.WriteObject (sw, 1);
138                         string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
139                         byte[] buf = sw.ToArray ();
140                         Assert.AreEqual (expected, Encoding.UTF8.GetString (buf, 0, buf.Length));
141                 }
142
143                 [Test]
144                 public void ReadObjectFromStream ()
145                 {
146                         DataContractSerializer ser =
147                                 new DataContractSerializer (typeof (int));
148                         string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
149                         byte[] buf = Encoding.UTF8.GetBytes (expected);
150                         MemoryStream sw = new MemoryStream (buf);
151                         object res = ser.ReadObject (sw);
152                         Assert.AreEqual (1, res);
153                 }
154
155                 // int
156
157                 [Test]
158                 public void SerializeInt ()
159                 {
160                         DataContractSerializer ser =
161                                 new DataContractSerializer (typeof (int));
162                         SerializeInt (ser, "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>");
163                 }
164
165
166                 [Test]
167                 [Category ("NotWorking")]
168                 public void NetSerializeInt ()
169                 {
170                         NetDataContractSerializer ser =
171                                 new NetDataContractSerializer ();
172                         // z:Assembly="0" ???
173                         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));
174                 }
175
176                 void SerializeInt (XmlObjectSerializer ser, string expected)
177                 {
178                         StringWriter sw = new StringWriter ();
179                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
180                                 ser.WriteObject (w, 1);
181                         }
182                         Assert.AreEqual (expected, sw.ToString ());
183                 }
184
185                 // pass typeof(DCEmpty), serialize int
186
187                 [Test]
188                 public void SerializeIntForDCEmpty ()
189                 {
190                         DataContractSerializer ser =
191                                 new DataContractSerializer (typeof (DCEmpty));
192                         // tricky!
193                         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>");
194                 }
195
196                 void SerializeIntForDCEmpty (XmlObjectSerializer ser, string expected)
197                 {
198                         StringWriter sw = new StringWriter ();
199                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
200                                 ser.WriteObject (w, 1);
201                         }
202                         XmlComparer.AssertAreEqual (expected, sw.ToString ());
203                 }
204
205                 // DCEmpty
206
207                 [Test]
208                 public void SerializeEmptyClass ()
209                 {
210                         DataContractSerializer ser =
211                                 new DataContractSerializer (typeof (DCEmpty));
212                         SerializeEmptyClass (ser, "<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
213                 }
214
215                 [Test]
216                 [Category ("NotWorking")]
217                 public void NetSerializeEmptyClass ()
218                 {
219                         NetDataContractSerializer ser =
220                                 new NetDataContractSerializer ();
221                         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));
222                 }
223
224                 void SerializeEmptyClass (XmlObjectSerializer ser, string expected)
225                 {
226                         StringWriter sw = new StringWriter ();
227                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
228                                 ser.WriteObject (w, new DCEmpty ());
229                         }
230                         Assert.AreEqual (expected, sw.ToString ());
231                 }
232
233                 // DCEmpty
234
235                 [Test]
236                 public void SerializeEmptyNoNSClass ()
237                 {
238                         var ser = new DataContractSerializer (typeof (DCEmptyNoNS));
239                         SerializeEmptyNoNSClass (ser, "<DCEmptyNoNS xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />");
240                 }
241
242                 void SerializeEmptyNoNSClass (XmlObjectSerializer ser, string expected)
243                 {
244                         var sw = new StringWriter ();
245                         using (var w = XmlWriter.Create (sw, settings)) {
246                                 ser.WriteObject (w, new DCEmptyNoNS ());
247                         }
248                         Assert.AreEqual (expected, sw.ToString ());
249                 }
250                 // string (primitive)
251
252                 [Test]
253                 public void SerializePrimitiveString ()
254                 {
255                         XmlObjectSerializer ser =
256                                 new DataContractSerializer (typeof (string));
257                         SerializePrimitiveString (ser, "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
258                 }
259
260                 [Test]
261                 [Category ("NotWorking")]
262                 public void NetSerializePrimitiveString ()
263                 {
264                         XmlObjectSerializer ser = new NetDataContractSerializer ();
265                         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>");
266                 }
267
268                 void SerializePrimitiveString (XmlObjectSerializer ser, string expected)
269                 {
270                         StringWriter sw = new StringWriter ();
271                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
272                                 ser.WriteObject (w, "TEST");
273                         }
274                         Assert.AreEqual (expected, sw.ToString ());
275                 }
276
277                 // QName (primitive but ...)
278
279                 [Test]
280                 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
281                 public void SerializePrimitiveQName ()
282                 {
283                         XmlObjectSerializer ser =
284                                 new DataContractSerializer (typeof (XmlQualifiedName));
285                         SerializePrimitiveQName (ser, "<z:QName xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
286                 }
287
288                 [Test]
289                 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
290                 public void NetSerializePrimitiveQName ()
291                 {
292                         XmlObjectSerializer ser = new NetDataContractSerializer ();
293                         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>");
294                 }
295
296                 void SerializePrimitiveQName (XmlObjectSerializer ser, string expected)
297                 {
298                         StringWriter sw = new StringWriter ();
299                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
300                                 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
301                         }
302                         Assert.AreEqual (expected, sw.ToString ());
303                 }
304
305                 // DCSimple1
306
307                 [Test]
308                 public void SerializeSimpleClass1 ()
309                 {
310                         DataContractSerializer ser =
311                                 new DataContractSerializer (typeof (DCSimple1));
312                         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>");
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (SerializationException))]
317                 [Category ("NotWorking")] // behavior changed in 3.5/SP1
318                 public void SerializeSimpleXml ()
319                 {
320                         DataContractSerializer ser =
321                                 new DataContractSerializer (typeof (SimpleXml));
322                         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>");
323                 }
324
325                 [Test]
326                 [Category ("NotWorking")]
327                 public void NetSerializeSimpleClass1 ()
328                 {
329                         NetDataContractSerializer ser =
330                                 new NetDataContractSerializer ();
331                         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));
332                 }
333
334                 void SerializeSimpleClass1 (XmlObjectSerializer ser, string expected)
335                 {
336                         StringWriter sw = new StringWriter ();
337                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
338                                 ser.WriteObject (w, new DCSimple1 ());
339                         }
340                         Console.WriteLine(sw.ToString());
341                         Assert.AreEqual (expected, sw.ToString ());
342                 }
343
344                 // NonDC (behavior changed in 3.5/SP1; not it's not rejected)
345
346                 [Test]
347                 public void SerializeNonDC ()
348                 {
349                         DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
350                         var sw = new StringWriter ();
351                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
352                                 ser.WriteObject (w, new NonDC ());
353                         }
354                         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 ());
355                 }
356
357                 // DCHasNonDC
358
359                 [Test]
360                 public void SerializeDCHasNonDC ()
361                 {
362                         DataContractSerializer ser = new DataContractSerializer (typeof (DCHasNonDC));
363                         var sw = new StringWriter ();
364                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
365                                 ser.WriteObject (w, new DCHasNonDC ());
366                         }
367                         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 ());
368                 }
369
370                 // DCHasSerializable
371
372                 [Test]
373                 // DCHasSerializable itself is DataContract and has a field
374                 // whose type is not contract but serializable.
375                 public void SerializeSimpleSerializable1 ()
376                 {
377                         DataContractSerializer ser = new DataContractSerializer (typeof (DCHasSerializable));
378                         StringWriter sw = new StringWriter ();
379                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
380                                 ser.WriteObject (w, new DCHasSerializable ());
381                         }
382                         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 ());
383                 }
384
385                 [Test]
386                 public void SerializeDCWithName ()
387                 {
388                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
389                         StringWriter sw = new StringWriter ();
390                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
391                                 ser.WriteObject (w, new DCWithName ());
392                         }
393                         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 ());
394                 }
395
396                 [Test]
397                 public void SerializeDCWithEmptyName1 ()
398                 {
399                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyName));
400                         StringWriter sw = new StringWriter ();
401                         DCWithEmptyName dc = new DCWithEmptyName ();
402                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
403                                 try {
404                                         ser.WriteObject (w, dc);
405                                 } catch (InvalidDataContractException) {
406                                         return;
407                                 }
408                         }
409                         Assert.Fail ("Expected InvalidDataContractException");
410                 }
411
412                 [Test]
413                 [Category ("NotWorking")]
414                 public void SerializeDCWithEmptyName2 ()
415                 {
416                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
417                         StringWriter sw = new StringWriter ();
418
419                         /* DataContractAttribute.Name == "", not valid */
420                         DCWithEmptyName dc = new DCWithEmptyName ();
421                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
422                                 try {
423                                         ser.WriteObject (w, dc);
424                                 } catch (InvalidDataContractException) {
425                                         return;
426                                 }
427                         }
428                         Assert.Fail ("Expected InvalidDataContractException");
429                 }
430
431                 [Test]
432                 [Category ("NotWorking")]
433                 public void SerializeDCWithNullName ()
434                 {
435                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithNullName));
436                         StringWriter sw = new StringWriter ();
437                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
438                                 try {
439                                         /* DataContractAttribute.Name == "", not valid */
440                                         ser.WriteObject (w, new DCWithNullName ());
441                                 } catch (InvalidDataContractException) {
442                                         return;
443                                 }
444                         }
445                         Assert.Fail ("Expected InvalidDataContractException");
446                 }
447
448                 [Test]
449                 public void SerializeDCWithEmptyNamespace1 ()
450                 {
451                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyNamespace));
452                         StringWriter sw = new StringWriter ();
453                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
454                                 ser.WriteObject (w, new DCWithEmptyNamespace ());
455                         }
456                 }
457
458                 // Wrapper.DCWrapped
459
460                 [Test]
461                 public void SerializeWrappedClass ()
462                 {
463                         DataContractSerializer ser =
464                                 new DataContractSerializer (typeof (Wrapper.DCWrapped));
465                         SerializeWrappedClass (ser, "<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
466                 }
467
468                 [Test]
469                 [Category ("NotWorking")]
470                 public void NetSerializeWrappedClass ()
471                 {
472                         NetDataContractSerializer ser =
473                                 new NetDataContractSerializer ();
474                         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));
475                 }
476
477                 void SerializeWrappedClass (XmlObjectSerializer ser, string expected)
478                 {
479                         StringWriter sw = new StringWriter ();
480                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
481                                 ser.WriteObject (w, new Wrapper.DCWrapped ());
482                         }
483                         Assert.AreEqual (expected, sw.ToString ());
484                 }
485
486                 [Test]
487                 /* old code
488                 // CollectionContainer : Items must have a setter.
489                 [ExpectedException (typeof (InvalidDataContractException))]
490                 [Category ("NotWorking")]
491                 */
492                 public void SerializeReadOnlyCollectionMember ()
493                 {
494                         DataContractSerializer ser =
495                                 new DataContractSerializer (typeof (CollectionContainer));
496
497                         StringWriter sw = new StringWriter ();
498                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
499                                 ser.WriteObject (w, null);
500                         }
501                         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");
502
503                         sw = new StringWriter ();
504                         var c = new CollectionContainer ();
505                         c.Items.Add ("foo");
506                         c.Items.Add ("bar");
507                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
508                                 ser.WriteObject (w, c);
509                         }
510                         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");
511                 }
512
513                 // DataCollectionContainer : Items must have a setter.
514                 [Test]
515                 //[ExpectedException (typeof (InvalidDataContractException))]
516                 public void SerializeReadOnlyDataCollectionMember ()
517                 {
518                         DataContractSerializer ser =
519                                 new DataContractSerializer (typeof (DataCollectionContainer));
520                         StringWriter sw = new StringWriter ();
521                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
522                                 ser.WriteObject (w, null);
523                         }
524                         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");
525
526                         sw = new StringWriter ();
527                         var c = new DataCollectionContainer ();
528                         c.Items.Add ("foo");
529                         c.Items.Add ("bar");
530                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
531                                 ser.WriteObject (w, c);
532                         }
533                         // LAMESPEC: this is bogus behavior. .NET serializes 
534                         // System.String as "string" without overriding its 
535                         // element namespace, but then it must be regarded as
536                         // in parent's namespace. What if there already is an
537                         // element definition for "string" with the same
538                         // namespace?
539                         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");
540                 }
541
542                 [Test]
543                 public void SerializeGuid ()
544                 {
545                         DataContractSerializer ser = new DataContractSerializer (typeof (Guid));
546                         StringWriter sw = new StringWriter ();
547                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
548                                 ser.WriteObject (w, Guid.Empty);
549                         }
550                         Assert.AreEqual (
551                                 "<guid xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">00000000-0000-0000-0000-000000000000</guid>",
552                                 sw.ToString ());
553                 }
554
555                 [Test]
556                 public void SerializeEnum ()
557                 {
558                         DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
559                         StringWriter sw = new StringWriter ();
560                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
561                                 ser.WriteObject (w, new Colors ());
562                         }
563
564                         Assert.AreEqual (
565                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>",
566                                 sw.ToString ());
567                 }
568
569                 [Test]
570                 public void SerializeEnum2 ()
571                 {
572                         DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
573                         StringWriter sw = new StringWriter ();
574                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
575                                 ser.WriteObject (w, 0);
576                         }
577
578                         XmlComparer.AssertAreEqual (
579                                 @"<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>",
580                                 sw.ToString ());
581                 }
582
583                 [Test]
584                 public void SerializeEnumWithDC ()
585                 {
586                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
587                         StringWriter sw = new StringWriter ();
588                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
589                                 ser.WriteObject (w, new ColorsWithDC ());
590                         }
591
592                         Assert.AreEqual (
593                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>",
594                                 sw.ToString ());
595                 }
596
597                 [Test]
598                 public void SerializeEnumWithNoDC ()
599                 {
600                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsEnumMemberNoDC));
601                         StringWriter sw = new StringWriter ();
602                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
603                                 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
604                         }
605
606                         Assert.AreEqual (
607                                 @"<ColorsEnumMemberNoDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</ColorsEnumMemberNoDC>",
608                                 sw.ToString ());
609                 }
610
611                 [Test]
612                 public void SerializeEnumWithDC2 ()
613                 {
614                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
615                         StringWriter sw = new StringWriter ();
616                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
617                                 ser.WriteObject (w, 3);
618                         }
619
620                         XmlComparer.AssertAreEqual (
621                                 @"<_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>",
622                                 sw.ToString ());
623                 }
624
625                 [Test]
626                 [ExpectedException (typeof (SerializationException))]
627                 public void SerializeEnumWithDCInvalid ()
628                 {
629                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
630                         StringWriter sw = new StringWriter ();
631                         ColorsWithDC cdc = ColorsWithDC.Blue;
632                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
633                                 ser.WriteObject (w, cdc);
634                         }
635                 }
636
637                 [Test]
638                 public void SerializeDCWithEnum ()
639                 {
640                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum));
641                         StringWriter sw = new StringWriter ();
642                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
643                                 ser.WriteObject (w, new DCWithEnum ());
644                         }
645  
646                         Assert.AreEqual (
647                                 @"<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>",
648                                 sw.ToString ());
649                 }
650
651                 [Test]
652                 public void SerializeDCWithTwoEnums ()
653                 {
654                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithTwoEnums));
655                         StringWriter sw = new StringWriter ();
656                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
657                                 DCWithTwoEnums e = new DCWithTwoEnums ();
658                                 e.colors = Colors.Blue;
659                                 e.colors2 = Colors.Green;
660                                 ser.WriteObject (w, e);
661                         }
662  
663                         Assert.AreEqual (
664                                 @"<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>",
665                                 sw.ToString ());
666                 }
667
668                 [Test]
669                 public void SerializeNestingDC2 ()
670                 {
671                         DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC2));
672                         StringWriter sw = new StringWriter ();
673                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
674                                 NestingDC2 e = new NestingDC2 ();
675                                 e.Field = new NestedDC2 ("Something");
676                                 ser.WriteObject (w, e);
677                         }
678  
679                         Assert.AreEqual (
680                                 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>",
681                                 sw.ToString ());
682                 }
683
684                 [Test]
685                 public void SerializeNestingDC ()
686                 {
687                         DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC));
688                         StringWriter sw = new StringWriter ();
689                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
690                                 NestingDC e = new NestingDC ();
691                                 e.Field1 = new NestedDC ("test1");
692                                 e.Field2 = new NestedDC ("test2");
693                                 ser.WriteObject (w, e);
694                         }
695  
696                         Assert.AreEqual (
697                                 @"<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>",
698                                 sw.ToString ());
699                         sw = new StringWriter ();
700                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
701                                 NestingDC e = new NestingDC ();
702                                 ser.WriteObject (w, e);
703                         }
704  
705                         Assert.AreEqual (
706                                 @"<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>",
707                                 sw.ToString ());
708                 }
709
710                 [Test]
711                 public void SerializeDerivedDC ()
712                 {
713                         DataContractSerializer ser = new DataContractSerializer (typeof (DerivedDC));
714                         StringWriter sw = new StringWriter ();
715                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
716                                 DerivedDC e = new DerivedDC ();
717                                 ser.WriteObject (w, e);
718                         }
719  
720                         Assert.AreEqual (
721                                 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">0</baseVal><derivedVal>0</derivedVal></DerivedDC>",
722                                 sw.ToString ());
723                 }
724
725                 [Test]
726                 public void SerializerDCArray ()
727                 {
728                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum []));
729                         StringWriter sw = new StringWriter ();
730                         DCWithEnum [] arr = new DCWithEnum [2];
731                         arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
732                         arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
733                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
734                                 ser.WriteObject (w, arr);
735                         }
736
737                         XmlComparer.AssertAreEqual (
738                                 @"<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>",
739                                 sw.ToString ());
740                 }
741
742                 [Test]
743                 public void SerializerDCArray2 ()
744                 {
745                         List<Type> known = new List<Type> ();
746                         known.Add (typeof (DCWithEnum));
747                         known.Add (typeof (DCSimple1));
748                         DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
749                         StringWriter sw = new StringWriter ();
750                         object [] arr = new object [2];
751                         arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
752                         arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
753
754                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
755                                 ser.WriteObject (w, arr);
756                         }
757
758                         XmlComparer.AssertAreEqual (
759                                 @"<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>",
760                                 sw.ToString ());
761                 }
762
763                 [Test]
764                 public void SerializerDCArray3 ()
765                 {
766                         DataContractSerializer ser = new DataContractSerializer (typeof (int []));
767                         StringWriter sw = new StringWriter ();
768                         int [] arr = new int [2];
769                         arr [0] = 1; arr [1] = 2;
770
771                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
772                                 ser.WriteObject (w, arr);
773                         }
774
775                         XmlComparer.AssertAreEqual (
776                                 @"<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>",
777                                 sw.ToString ());
778                 }
779
780                 [Test]
781                 public void SerializeNonDCArray ()
782                 {
783                         DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
784                         StringWriter sw = new StringWriter ();
785                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
786                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
787                         }
788                         Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses /></SerializeNonDCArrayType>",
789                                 sw.ToString ());
790                 }
791
792                 [Test]
793                 public void SerializeNonDCArrayItems ()
794                 {
795                         DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
796                         StringWriter sw = new StringWriter ();
797                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
798                                 SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
799                                 obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new int [] {1, 2, 3, 4} } };
800                                 ser.WriteObject (xw, obj);
801                         }
802
803                         XmlDocument doc = new XmlDocument ();
804                         doc.LoadXml (sw.ToString ());
805                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
806                         nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
807                         nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
808                         nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
809
810                         Assert.AreEqual (1, doc.SelectNodes ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem", nsmgr).Count, "#1");
811                         XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem/s:Data", nsmgr) as XmlElement;
812                         Assert.IsNotNull (el, "#3");
813                         Assert.AreEqual (4, el.SelectNodes ("a:int", nsmgr).Count, "#4");
814                 }
815
816                 [Test]
817                 public void DeserializeEnum ()
818                 {
819                         Colors c = Deserialize<Colors> (
820                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>");
821
822                         Assert.AreEqual (Colors.Red, c, "#de2");
823                 }
824
825                 [Test]
826                 public void DeserializeEnum2 ()
827                 {
828                         Colors c = Deserialize<Colors> (
829                                 @"<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>",
830                                 typeof (int));
831
832                         Assert.AreEqual (Colors.Green, c, "#de4");
833                 }
834                 
835                 [Test]
836                 [ExpectedException (typeof (SerializationException))]
837                 public void DeserializeEnumInvalid1 ()
838                 {
839                         Deserialize<Colors> (
840                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></Colors>");
841                 }
842
843                 [Test]
844                 [ExpectedException (typeof (SerializationException))]
845                 public void DeserializeEnumInvalid2 ()
846                 {
847                         Deserialize<Colors> (
848                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
849                 }
850
851                 [Test]
852                 [ExpectedException (typeof (SerializationException))]
853                 public void DeserializeEnumInvalid3 ()
854                 {
855                         //"red" instead of "Red"
856                         Deserialize<Colors> (
857                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">red</Colors>");
858                 }
859
860                 [Test]
861                 public void DeserializeEnumFlags ()
862                 {
863                         Deserialize<Colors2> (
864                                 @"<Colors2 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
865                 }
866
867                 [Test]
868                 public void DeserializeEnumWithDC ()
869                 {
870                         ColorsWithDC cdc = Deserialize<ColorsWithDC> (
871                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>");
872                         
873                         Assert.AreEqual (ColorsWithDC.Red, cdc, "#de6");
874                 }
875
876                 [Test]
877                 [ExpectedException (typeof (SerializationException))]
878                 public void DeserializeEnumWithDCInvalid ()
879                 {
880                         Deserialize<ColorsWithDC> (
881                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">NonExistant</_ColorsWithDC>");
882                 }
883
884                 [Test]
885                 public void DeserializeDCWithEnum ()
886                 {
887                         DCWithEnum dc = Deserialize<DCWithEnum> (
888                                 @"<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>");
889
890                         Assert.AreEqual (Colors.Red, dc.colors, "#de8");
891                 }
892
893                 [Test]
894                 public void DeserializeNestingDC ()
895                 {
896                         NestingDC dc = Deserialize<NestingDC> (
897                                 @"<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>");
898
899                         Assert.IsNotNull (dc.Field1, "#N1: Field1 should not be null.");
900                         Assert.IsNotNull (dc.Field2, "#N2: Field2 should not be null.");
901                         Assert.AreEqual ("test1", dc.Field1.Name, "#1");
902                         Assert.AreEqual ("test2", dc.Field2.Name, "#2");
903                 }
904
905                 [Test]
906                 public void DeserializeNestingDC2 ()
907                 {
908                         NestingDC2 dc = Deserialize<NestingDC2> (
909                                 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>");
910
911                         Assert.IsNotNull (dc.Field, "#N1: Field should not be null.");
912                         Assert.AreEqual ("Something", dc.Field.Name, "#N2");
913                 }
914
915                 [Test]
916                 public void DeserializeDerivedDC ()
917                 {
918                         DerivedDC dc = Deserialize<DerivedDC> (
919                                 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">1</baseVal><derivedVal>2</derivedVal></DerivedDC>");
920
921                         Assert.AreEqual (1, dc.baseVal, "#N1");
922                         Assert.AreEqual (2, dc.derivedVal, "#N2");
923                 }
924
925                 [Test]
926                 public void DeserializeTwice ()
927                 {
928                         string xml = 
929                                 @"<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>";
930                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
931                         XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
932                         xr.ReadStartElement ();
933                         object o = ser.ReadObject (xr);
934                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
935                         ColorsWithDC cdc = (ColorsWithDC) o;
936                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
937
938                         o = ser.ReadObject (xr);
939                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
940                         cdc = (ColorsWithDC) o;
941                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
942                         Assert.AreEqual (XmlNodeType.EndElement, xr.NodeType, "#de6");
943                         Assert.AreEqual ("any", xr.LocalName, "#de6");
944                         xr.ReadEndElement ();
945                 }
946
947
948                 [Test]
949                 public void DeserializeEmptyNestingDC ()
950                 {
951                         NestingDC dc = Deserialize<NestingDC> (
952                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></NestingDC>");
953
954                         Assert.IsNotNull (dc, "#A0: The object should not be null.");
955                         Assert.IsNull (dc.Field1, "#A1: Field1 should be null.");
956                         Assert.IsNull (dc.Field2, "#A2: Field2 should be null.");
957
958                         dc = Deserialize<NestingDC> (
959                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
960
961                         Assert.IsNotNull (dc, "#B0: The object should not be null.");
962                         Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
963                         Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
964
965                         dc = Deserialize<NestingDC> (
966                                 @"<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>");
967
968                         Assert.IsNotNull (dc, "#B0: The object should not be null.");
969                         Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
970                         Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
971                 }
972
973                 [Test]
974                 [ExpectedException (typeof (SerializationException))]
975                 public void DeserializeEmptyDCWithTwoEnums ()
976                 {
977                         Deserialize<DCWithTwoEnums> (
978                                 @"<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>");
979                 }
980
981                 [Test]
982                 [Category ("NotWorking")]
983                 public void DeserializeDCWithNullableEnum ()
984                 {
985                         DCWithNullableEnum dc = Deserialize<DCWithNullableEnum> (
986                                 @"<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>");
987
988                         Assert.IsNull (dc.colors, "#B1: Field should be null.");
989                 }
990
991                 [Test]
992                 public void DeserializeDCWithTwoEnums ()
993                 {
994                         DCWithTwoEnums dc = Deserialize<DCWithTwoEnums> (
995                                 @"<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>");
996
997                         Assert.AreEqual (Colors.Blue, dc.colors, "#0");
998                         Assert.AreEqual (Colors.Green, dc.colors2, "#1");
999                 }
1000
1001                 [Test]
1002                 public void DeserializerDCArray ()
1003                 {
1004                         DCWithEnum [] dcArray = Deserialize<DCWithEnum []> (
1005                                 @"<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>");
1006
1007                         Assert.AreEqual (2, dcArray.Length, "#N1");
1008                         Assert.AreEqual (Colors.Red, dcArray [0].colors, "#N2");
1009                         Assert.AreEqual (Colors.Green, dcArray [1].colors, "#N3");
1010                 }
1011
1012                 [Test]
1013                 public void DeserializerDCArray2 ()
1014                 {
1015                         string xml = 
1016                                 @"<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>";
1017
1018                         List<Type> known = new List<Type> ();
1019                         known.Add (typeof (DCWithEnum));
1020                         known.Add (typeof (DCSimple1));
1021                         DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
1022                         XmlReader xr = XmlReader.Create (new StringReader (xml));
1023
1024                         object [] dc = (object []) ser.ReadObject (xr);
1025                         Assert.AreEqual (2, dc.Length, "#N1");
1026                         Assert.AreEqual (typeof (DCWithEnum), dc [0].GetType (), "#N2");
1027                         DCWithEnum dc0 = (DCWithEnum) dc [0];
1028                         Assert.AreEqual (Colors.Red, dc0.colors, "#N3");
1029                         Assert.AreEqual (typeof (DCSimple1), dc [1].GetType (), "#N4");
1030                         DCSimple1 dc1 = (DCSimple1) dc [1];
1031                         Assert.AreEqual ("hello", dc1.Foo, "#N4");
1032                 }
1033
1034                 [Test]
1035                 public void DeserializerDCArray3 ()
1036                 {
1037                         int [] intArray = Deserialize<int []> (
1038                                 @"<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>");
1039
1040                         Assert.AreEqual (2, intArray.Length, "#N0");
1041                         Assert.AreEqual (1, intArray [0], "#N1");
1042                         Assert.AreEqual (2, intArray [1], "#N2");
1043                 }
1044
1045                 [Test]
1046                 public void ReadObjectNoVerifyObjectName ()
1047                 {
1048                         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>";
1049                         VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
1050                                 .ReadObject (XmlReader.Create (new StringReader (xml)), false);
1051                         Assert.AreEqual ("bar", res.GetMember());
1052                 }
1053
1054                 [Test]
1055                 public void ReadObjectVerifyObjectName ()
1056                 {
1057                         string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Member1>bar</Member1></VerifyObjectNameTestData>";
1058                         VerifyObjectNameTestData res = (VerifyObjectNameTestData)new DataContractSerializer (typeof (VerifyObjectNameTestData))
1059                                 .ReadObject (XmlReader.Create (new StringReader (xml)));
1060                         Assert.AreEqual ("bar", res.GetMember());
1061                 }
1062
1063                 [Test]
1064                 [ExpectedException (typeof (SerializationException))]
1065                 public void ReadObjectWrongNamespace ()
1066                 {
1067                         string xml = @"<VerifyObjectNameTestData xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization2""><Member1>bar</Member1></VerifyObjectNameTestData>";
1068                         new DataContractSerializer (typeof (VerifyObjectNameTestData))
1069                                 .ReadObject (XmlReader.Create (new StringReader (xml)));
1070                 }
1071
1072                 [Test]
1073                 public void ReferenceSerialization ()
1074                 {
1075                         var dc = new DataContractSerializer (typeof (ReferenceWrapper));
1076                         var t = new ReferenceType ();
1077                         StringWriter sw = new StringWriter ();
1078                         using (var xw = XmlWriter.Create (sw)) {
1079                                 xw.WriteStartElement ("z", "root", "http://schemas.microsoft.com/2003/10/Serialization/");
1080                                 dc.WriteObject (xw, new ReferenceWrapper () {T = t, T2 = t});
1081                                 xw.WriteEndElement ();
1082                         }
1083                         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>";
1084                         Assert.AreEqual (xml.Replace ('\'', '"'), sw.ToString (), "#1");
1085
1086                         ReferenceWrapper w;
1087                         using (XmlReader r = XmlReader.Create (new StringReader (xml)))
1088         {
1089                                 r.ReadStartElement ();
1090                                 w = (ReferenceWrapper) dc.ReadObject (r);
1091                                 r.ReadEndElement ();
1092                         }
1093                         Assert.AreEqual (w.T, w.T2, "#2");
1094                 }
1095
1096                 [Test]
1097                 public void GenericSerialization ()
1098                 {
1099                         var sw = new StringWriter ();
1100                         var ser  = new DataContractSerializer (typeof (Foo<string,int,int>));
1101                         using (var xw = XmlWriter.Create (sw))
1102                                 ser.WriteObject (xw, new Foo<string,int,int> () {Field = "f"
1103                         });
1104                         var s = sw.ToString ();
1105
1106                         var ret = (Foo<string,int,int>) ser.ReadObject (XmlReader.Create (new StringReader (s)));
1107                         Assert.AreEqual ("f", ret.Field);
1108                 }
1109
1110                 [Test]
1111                 public void GenericCollectionSerialization ()
1112                 {
1113                         var l = new MyList ();
1114                         l.Add ("foo");
1115                         l.Add ("bar");
1116                         var ds = new DataContractSerializer (typeof (MyList));
1117                         var sw = new StringWriter ();
1118                         using (var xw = XmlWriter.Create (sw))
1119                                 ds.WriteObject (xw, l);
1120                         l = (MyList) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
1121                         Assert.AreEqual (2, l.Count);
1122                 }
1123
1124                 [Test]
1125                 public void GenericListOfKeyValuePairSerialization ()
1126                 {
1127                         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 ('\'', '"');
1128
1129                         var ds = new DataContractSerializer (typeof (List<KeyValuePair<string,string>>));
1130                         var d = new List<KeyValuePair<string,string>> ();
1131                         d.Add (new KeyValuePair<string,string> ("foo", "bar"));
1132                         var sw = new StringWriter ();
1133                         using (var xw = XmlWriter.Create (sw))
1134                                 ds.WriteObject (xw, d);
1135                         Assert.AreEqual (xml, sw.ToString (), "#1");
1136                         d = (List<KeyValuePair<string,string>>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1137                         Assert.AreEqual (1, d.Count, "#2");
1138                         Assert.AreEqual ("bar", d [0].Value, "#3");
1139                 }
1140
1141                 [Test]
1142                 public void GenericListOfDictionaryEntrySerialization ()
1143                 {
1144                         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 ('\'', '"');
1145
1146                         var ds = new DataContractSerializer (typeof (List<DictionaryEntry>));
1147                         var d = new List<DictionaryEntry> ();
1148                         d.Add (new DictionaryEntry ("foo", "bar"));
1149                         var sw = new StringWriter ();
1150                         using (var xw = XmlWriter.Create (sw))
1151                                 ds.WriteObject (xw, d);
1152                         Assert.AreEqual (xml, sw.ToString (), "#1");
1153                         Assert.IsTrue (sw.ToString ().IndexOf ("i:type") >= 0);
1154                         d = (List<DictionaryEntry>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1155                         Assert.AreEqual (1, d.Count, "#2");
1156                         Assert.AreEqual ("bar", d [0].Value, "#3");
1157                 }
1158
1159                 [Test]
1160                 public void GenericDictionarySerialization ()
1161                 {
1162                         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 ('\'', '"');
1163
1164                         var ds = new DataContractSerializer (typeof (Dictionary<string,string>));
1165                         var d = new Dictionary<string,string> ();
1166                         d ["foo"] = "bar";
1167                         var sw = new StringWriter ();
1168                         using (var xw = XmlWriter.Create (sw))
1169                                 ds.WriteObject (xw, d);
1170                         Assert.AreEqual (xml, sw.ToString (), "#1");
1171                         d = (Dictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1172                         Assert.AreEqual (1, d.Count, "#2");
1173                         Assert.AreEqual ("bar", d ["foo"], "#3");
1174                 }
1175
1176                 [Test]
1177                 public void HashtableSerialization ()
1178                 {
1179                         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 ('\'', '"');
1180
1181                         var ds = new DataContractSerializer (typeof (Hashtable));
1182                         var d = new Hashtable ();
1183                         d ["foo"] = "bar";
1184                         var sw = new StringWriter ();
1185                         using (var xw = XmlWriter.Create (sw))
1186                                 ds.WriteObject (xw, d);
1187                         Assert.AreEqual (xml, sw.ToString (), "#1");
1188                         d = (Hashtable) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1189                         Assert.AreEqual (1, d.Count, "#2");
1190                         Assert.AreEqual ("bar", d ["foo"], "#3");
1191                 }
1192
1193                 [Test]
1194                 public void CollectionContarctDictionarySerialization ()
1195                 {
1196                         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 ('\'', '"');
1197
1198                         var ds = new DataContractSerializer (typeof (MyDictionary<string,string>));
1199                         var d = new MyDictionary<string,string> ();
1200                         d ["foo"] = "bar";
1201                         var sw = new StringWriter ();
1202                         using (var xw = XmlWriter.Create (sw))
1203                                 ds.WriteObject (xw, d);
1204                         Assert.AreEqual (xml, sw.ToString (), "#1");
1205                         d = (MyDictionary<string,string>) ds.ReadObject (XmlReader.Create (new StringReader (xml)));
1206                         Assert.AreEqual (1, d.Count, "#2");
1207                         Assert.AreEqual ("bar", d ["foo"], "#3");
1208                 }
1209
1210                 [Test]
1211                 public void SerializeInterfaceCollection ()
1212                 {
1213                         var ser = new DataContractSerializer (typeof (InterfaceCollectionType));
1214                         var sw = new StringWriter ();
1215                         var obj = new InterfaceCollectionType ();
1216                         using (var xw = XmlWriter.Create (sw))
1217                                 ser.WriteObject (xw, obj);
1218                         using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
1219                                 obj = (InterfaceCollectionType) ser.ReadObject (xr);
1220                                 Assert.IsNull (obj.Array, "#1");
1221                         }
1222
1223                         sw = new StringWriter ();
1224                         obj.Array = new List<int> ();
1225                         obj.Array.Add (5);
1226                         using (var xw = XmlWriter.Create (sw))
1227                                 ser.WriteObject (xw, obj);
1228                         using (var xr = XmlReader.Create (new StringReader (sw.ToString ()))) {
1229                                 obj = (InterfaceCollectionType) ser.ReadObject (xr);
1230                                 Assert.AreEqual (5, obj.Array [0], "#2");
1231                         }
1232                 }
1233
1234                 [Test]
1235                 public void EmptyChildren ()
1236                 {
1237                 string xml = @"
1238 <DummyPlaylist xmlns='http://example.com/schemas/asx'>
1239         <Entries>
1240                 <DummyEntry>
1241                         <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
1242                         <Href>http://vmsservices.example.com:8080/VideoService.svc?crid=45541/part=1/guid=ae968b5d-e4a5-41fe-9b23-ed631b27cd21/</Href>
1243                 </DummyEntry>
1244         </Entries>
1245 </DummyPlaylist>
1246 ";
1247                         var reader = XmlReader.Create (new StringReader (xml));
1248                         DummyPlaylist playlist = (DummyPlaylist) new DataContractSerializer (typeof (DummyPlaylist)).ReadObject (reader);
1249                         Assert.AreEqual (1, playlist.entries.Count, "#1");
1250                         Assert.IsTrue (playlist.entries [0] is DummyEntry, "#2");
1251                         Assert.IsNotNull (playlist.entries [0].Href, "#3");
1252                 }
1253
1254                 [Test]
1255                 public void BaseKnownTypeAttributes ()
1256                 {
1257                         // bug #524088
1258                         string xml = @"
1259 <DummyPlaylist xmlns='http://example.com/schemas/asx'>
1260   <Entries>
1261     <DummyEntry>
1262       <EntryInfo xmlns:i='http://www.w3.org/2001/XMLSchema-instance' i:type='PartDummyEntryInfo'/>
1263     </DummyEntry>
1264   </Entries>
1265 </DummyPlaylist>";
1266
1267                         using (XmlReader reader = XmlReader.Create (new StringReader (xml))) {
1268                                 DummyPlaylist playlist = new DataContractSerializer(typeof(DummyPlaylist)).ReadObject(reader) as DummyPlaylist;
1269                                 Assert.IsNotNull (playlist);
1270                         }
1271                 }
1272
1273                 [Test]
1274                 public void Bug524083 ()
1275                 {
1276                         string xml = @"
1277 <AsxEntryInfo xmlns='http://example.com/schemas/asx'>
1278         <AdvertPrompt/>
1279 </AsxEntryInfo>";
1280                                                 
1281                         using (XmlReader reader = XmlReader.Create (new StringReader (xml)))
1282                                 new DataContractSerializer(typeof (AsxEntryInfo)).ReadObject (reader);
1283                 }
1284                 
1285                 [Test]
1286                 public void Bug539563 ()
1287                 {
1288                         new DataContractSerializer (typeof (NestedContractType));
1289                 }
1290
1291                 [Test]
1292                 public void Bug560155 ()
1293                 {
1294                         var g = Guid.NewGuid ();
1295                         Person p1 = new Person ("UserName", g);
1296                         Assert.AreEqual ("name=UserName,id=" + g, p1.ToString (), "#1");
1297                         MemoryStream memStream = new MemoryStream ();
1298                         DataContractSerializer ser =  new DataContractSerializer (typeof (Person));
1299
1300                         ser.WriteObject (memStream, p1);
1301                         memStream.Seek (0, SeekOrigin.Begin);
1302                         Person p2 = (Person) ser.ReadObject (memStream);
1303                         Assert.AreEqual ("name=UserName,id=" + g, p2.ToString (), "#1");
1304                 }
1305
1306                 private T Deserialize<T> (string xml)
1307                 {
1308                         return Deserialize<T> (xml, typeof (T));
1309                 }
1310
1311                 private T Deserialize<T> (string xml, Type runtimeType)
1312                 {
1313                         DataContractSerializer ser = new DataContractSerializer (typeof (T));
1314                         XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
1315                         object o = ser.ReadObject (xr);
1316                         Assert.AreEqual (runtimeType, o.GetType (), "#DS0");
1317                         return (T)o;
1318                 }
1319
1320                 public Dictionary<string, object> GenericDictionary (Dictionary<string, object> settings)
1321                 {
1322                         using (MemoryStream ms = new MemoryStream ()) {
1323                                 DataContractSerializer save = new DataContractSerializer (settings.GetType ());
1324                                 save.WriteObject (ms, settings);
1325
1326                                 ms.Position = 0;
1327
1328                                 DataContractSerializer load = new DataContractSerializer (typeof (Dictionary<string, object>));
1329                                 return (Dictionary<string, object>) load.ReadObject (ms);
1330                         }
1331                 }
1332
1333                 [Test]
1334                 public void GenericDictionaryEmpty ()
1335                 {
1336                         Dictionary<string, object> in_settings = new Dictionary<string, object> ();
1337                         Dictionary<string, object> out_settings = GenericDictionary (in_settings);
1338                         out_settings.Clear ();
1339                 }
1340
1341                 [Test]
1342                 public void GenericDictionaryOneElement ()
1343                 {
1344                         Dictionary<string, object> in_settings = new Dictionary<string, object> ();
1345                         in_settings.Add ("one", "ONE");
1346                         Dictionary<string, object> out_settings = GenericDictionary (in_settings);
1347                         Assert.AreEqual ("ONE", out_settings ["one"], "out");
1348                         out_settings.Clear ();
1349                 }
1350
1351                 [Test]
1352                 public void IgnoreDataMember ()
1353                 {
1354                         var ser = new DataContractSerializer (typeof (MemberIgnored));
1355                         var sw = new StringWriter ();
1356                         using (var w = XmlWriter.Create (sw, settings)) {
1357                                 ser.WriteObject (w, new MemberIgnored ());
1358                         }
1359                         Assert.AreEqual (@"<MemberIgnored xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><body><Bar>bar</Bar></body></MemberIgnored>", sw.ToString (), "#1");
1360                 }
1361
1362                 [Test]
1363                 public void DeserializeEmptyArray ()
1364                 {
1365                         var ds = new DataContractSerializer (typeof (string []));
1366                         var sw = new StringWriter ();
1367                         var xw = XmlWriter.Create (sw);
1368                         ds.WriteObject (xw, new string [] {});
1369                         xw.Close ();
1370                         Console.WriteLine (sw.ToString ());
1371                         var sr = new StringReader (sw.ToString ());
1372                         var xr = XmlReader.Create (sr);
1373                         var ret = ds.ReadObject (xr);
1374                         Assert.AreEqual (typeof (string []), ret.GetType (), "#1");
1375                 }
1376                 
1377                 [Test]
1378                 public void ContractNamespaceAttribute ()
1379                 {
1380                         var ds = new DataContractSerializer (typeof (U2U.DataContracts.Person));
1381                         string xml = "<?xml version='1.0' encoding='utf-16'?><Person xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.u2u.be/samples/wcf/2009'><Name>Rupert</Name><Occupation><Description>Monkey</Description></Occupation></Person>";
1382                         var person = new U2U.DataContracts.Person () {
1383                                 Name = "Rupert",
1384                                 Occupation = new U2U.DataContracts.Job () { Description = "Monkey" }
1385                                 };
1386                         var sw = new StringWriter ();
1387                         using (var xw = XmlWriter.Create (sw))
1388                                 ds.WriteObject (xw, person);
1389                         Assert.AreEqual (xml, sw.ToString ().Replace ('"', '\''), "#1");
1390                 }
1391
1392                 [Test]
1393                 public void Bug610036 ()
1394                 {
1395                         var ms = new MemoryStream ();
1396                         Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
1397
1398                         var ds = new DataContractSerializer (typeof (Root), "Root", "Company.Foo", knownTypes, 1000, false, true, null);
1399
1400                         var root = new Root ("root");
1401                         var bar1 = new Bar ("bar1");
1402                         var bar2 = new Bar ("bar2");
1403                         var bar3 = new Bar ("bar3");
1404                         
1405                         var foo1 = new Foo ("foo1");
1406                         var foo2 = new Foo ("foo2");
1407                         
1408                         foo1.FDict.Add (bar1);
1409                         foo1.FDict.Add (bar2);
1410                         
1411                         foo2.FDict.Add (bar1);
1412                         foo2.FDict.Add (bar3);
1413                         
1414                         root.FDict.Add (foo1);
1415                         root.FDict.Add (foo2);
1416
1417                         ds.WriteObject (ms, root);
1418                         string result = Encoding.UTF8.GetString (ms.ToArray ());
1419                         ms.Position = 0;
1420
1421                         root = (Root) ds.ReadObject (ms);
1422
1423                         Assert.AreEqual (2, root.FDict.Count, "#1");
1424                         int idx = result.IndexOf ("foo1");
1425                         Assert.IsTrue (idx >= 0, "#2");
1426                         // since "foo1" is stored as z:Ref for string, it must not occur twice.
1427                         int idx2 = result.IndexOf ("foo1", idx + 1);
1428                         Assert.IsTrue (idx2 < 0, "idx2 should not occur at " + idx2);
1429                 }
1430
1431                 [Test]
1432                 public void IXmlSerializableCallConstructor ()
1433                 {
1434                         IXmlSerializableCallConstructor  (false);
1435                         IXmlSerializableCallConstructor (true);
1436                 }
1437                 
1438                 void IXmlSerializableCallConstructor (bool binary)
1439                 {
1440                         Stream s = IXmlSerializableCallConstructorSerialize (binary);
1441                         var a = new byte [s.Length];
1442                         s.Position = 0;
1443                         s.Read (a, 0, a.Length);
1444                         s.Position = 0;
1445                         IXmlSerializableCallConstructorDeserialize (s, binary);
1446                 }
1447
1448                 public Stream IXmlSerializableCallConstructorSerialize (bool binary)
1449                 {
1450                         var ds = new DataSet ("ds");
1451                         var dt = new DataTable ("dt");
1452                         ds.Tables.Add (dt);
1453                         dt.Columns.Add ("n", typeof (int));
1454                         dt.Columns.Add ("s", typeof (string));
1455                         
1456                         dt.Rows.Add (5, "five");
1457                         dt.Rows.Add (10, "ten");
1458                         
1459                         ds.AcceptChanges ();
1460                         
1461                         var s = new MemoryStream ();
1462                         
1463                         var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
1464                         
1465                         var x = new DataContractSerializer (typeof (DataSet));
1466                         x.WriteObject (w, ds);
1467                         w.Flush ();
1468         
1469                         return s;
1470                 }
1471                 
1472                 public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
1473                 {
1474                         var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
1475                                 : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
1476                         
1477                         var x = new DataContractSerializer (typeof (DataSet));
1478                         
1479                         var ds = (DataSet) x.ReadObject (r);
1480                 }
1481         }
1482         
1483         [DataContract]
1484         public class MemberIgnored
1485         {
1486                 [DataMember]
1487                 MemberIgnoredBody body = new MemberIgnoredBody ();
1488         }
1489
1490         public class MemberIgnoredBody
1491         {
1492                 [IgnoreDataMember]
1493                 public string Foo = "foo";
1494
1495                 public string Bar = "bar";
1496         }
1497
1498         public enum Colors {
1499                 Red, Green, Blue
1500         }
1501
1502         [Flags]
1503         public enum Colors2 {
1504                 Red, Green, Blue
1505         }
1506
1507         [DataContract (Name = "_ColorsWithDC")]
1508         public enum ColorsWithDC {
1509
1510                 [EnumMember (Value = "_Red")]
1511                 Red, 
1512                 [EnumMember]
1513                 Green, 
1514                 Blue
1515         }
1516
1517
1518         public enum ColorsEnumMemberNoDC {
1519                 [EnumMember (Value = "_Red")]
1520                 Red, 
1521                 [EnumMember]
1522                 Green, 
1523                 Blue
1524         }
1525
1526         [DataContract]
1527         public class DCWithEnum {
1528                 [DataMember (Name = "_colors")]
1529                 public Colors colors;
1530         }
1531
1532         [DataContract]
1533         public class DCWithTwoEnums {
1534                 [DataMember]
1535                 public Colors colors;
1536                 [DataMember]
1537                 public Colors colors2;
1538         }
1539
1540         [DataContract]
1541         public class DCWithNullableEnum {
1542                 [DataMember]
1543                 public Colors? colors;
1544         }
1545
1546
1547         [DataContract (Namespace = "Base")]
1548         public class BaseDC {
1549                 [DataMember]
1550                 public int baseVal;
1551         }
1552
1553         [DataContract (Namespace = "Derived")]
1554         public class DerivedDC : BaseDC {
1555                 [DataMember]
1556                 public int derivedVal;
1557         }
1558
1559         [DataContract]
1560         public class NestedDC {
1561                 public NestedDC (string name) { this.Name = name; }
1562
1563                 [DataMember]
1564                 public string Name;
1565         }
1566
1567         [DataContract]
1568         public class NestingDC {
1569                 [DataMember]
1570                 public NestedDC Field1;
1571                 [DataMember]
1572                 public NestedDC Field2;
1573         }
1574
1575         [DataContract (Namespace = "test1")]
1576         public class NestedDC2 {
1577                 public NestedDC2 (string name) { this.Name = name; }
1578
1579                 [DataMember]
1580                 public string Name;
1581         }
1582
1583         [DataContract (Namespace = "test2")]
1584         public class NestingDC2 {
1585                 [DataMember]
1586                 public NestedDC2 Field;
1587         }
1588
1589         [DataContract]
1590         public class DCEmpty
1591         {
1592                 // serializer doesn't touch it.
1593                 public string Foo = "TEST";
1594         }
1595
1596         [DataContract (Namespace = "")]
1597         public class DCEmptyNoNS
1598         {
1599         }
1600
1601         [DataContract]
1602         public class DCSimple1
1603         {
1604                 [DataMember]
1605                 public string Foo = "TEST";
1606         }
1607
1608         [DataContract]
1609         public class DCHasNonDC
1610         {
1611                 [DataMember]
1612                 public NonDC Hoge= new NonDC ();
1613         }
1614
1615         public class NonDC
1616         {
1617                 public string Whee = "whee!";
1618         }
1619
1620         [DataContract]
1621         public class DCHasSerializable
1622         {
1623                 [DataMember]
1624                 public SimpleSer1 Ser = new SimpleSer1 ();
1625         }
1626
1627         [DataContract (Name = "Foo")]
1628         public class DCWithName
1629         {
1630                 [DataMember (Name = "FooMember")]
1631                 public string DMWithName = "value";
1632         }
1633
1634         [DataContract (Name = "")]
1635         public class DCWithEmptyName
1636         {
1637         }
1638
1639         [DataContract (Name = null)]
1640         public class DCWithNullName
1641         {
1642         }
1643
1644         [DataContract (Namespace = "")]
1645         public class DCWithEmptyNamespace
1646         {
1647         }
1648
1649         [Serializable]
1650         public class SimpleSer1
1651         {
1652                 public string Doh = "doh!";
1653                 [NonSerialized]
1654                 public string Bah = "bah!";
1655         }
1656
1657         public class Wrapper
1658         {
1659                 [DataContract]
1660                 public class DCWrapped
1661                 {
1662                 }
1663         }
1664
1665         [DataContract]
1666         public class CollectionContainer
1667         {
1668                 Collection<string> items = new Collection<string> ();
1669
1670                 [DataMember]
1671                 public Collection<string> Items {
1672                         get { return items; }
1673                 }
1674         }
1675
1676         [CollectionDataContract]
1677         public class DataCollection<T> : Collection<T>
1678         {
1679         }
1680
1681         [DataContract]
1682         public class DataCollectionContainer
1683         {
1684                 DataCollection<string> items = new DataCollection<string> ();
1685
1686                 [DataMember]
1687                 public DataCollection<string> Items {
1688                         get { return items; }
1689                 }
1690         }
1691
1692         [DataContract]
1693         class SerializeNonDCArrayType
1694         {
1695                 [DataMember]
1696                 public NonDCItem [] IPAddresses = new NonDCItem [0];
1697         }
1698
1699         public class NonDCItem
1700         {
1701                 public int [] Data { get; set; }
1702         }
1703
1704         [DataContract]
1705         public class VerifyObjectNameTestData
1706         {
1707                 [DataMember]
1708                 string Member1 = "foo";
1709
1710                 public string GetMember() { return Member1; }
1711         }
1712
1713         [XmlRoot(ElementName = "simple", Namespace = "")]
1714         public class SimpleXml : IXmlSerializable 
1715         {
1716                 void IXmlSerializable.ReadXml (XmlReader reader)
1717                 {
1718                 }
1719
1720                 void IXmlSerializable.WriteXml (XmlWriter writer)
1721                 {
1722                 }
1723
1724                 XmlSchema IXmlSerializable.GetSchema ()
1725                 {
1726                         return null;
1727                 }
1728
1729         }
1730
1731         [DataContract]
1732         public class ReferenceWrapper
1733         {
1734                 [DataMember (Order = 1)]
1735                 public ReferenceType T;
1736
1737                 [DataMember (Order = 2)]
1738                 public ReferenceType T2;
1739         }
1740
1741         [DataContract (IsReference = true)]
1742         public class ReferenceType
1743         {
1744                 [DataMember]
1745                 public string F = "x";
1746         }
1747
1748         public class MyList : IList<string>
1749         {
1750                 List<string> l = new List<string> ();
1751                 public void Clear () { l.Clear (); }
1752                 public void Add(string s) { l.Add (s);}
1753                 public void Insert(int idx, string s) { l.Insert(idx,s);}
1754                 public bool Contains(string s) { return l.Contains(s); }
1755                 public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
1756                 IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
1757                 public bool Remove(string s) { return l.Remove(s); }
1758                 public void RemoveAt(int i) { l.RemoveAt (i);}
1759                 public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
1760                 public int IndexOf (string s) { return l.IndexOf (s); }
1761         
1762                 public int Count { get { return l.Count; } }
1763                 public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
1764                 public string this [int index] { get { return l [index]; } set { l [index] = value; } }
1765         }
1766
1767         [DataContract]
1768         internal class InterfaceCollectionType
1769         {
1770                 [DataMember]
1771                 public IList<int> Array { get; set; }
1772         }
1773
1774         [DataContract]
1775         public class NestedContractType
1776         {
1777                 [DataMember]
1778                 public NestedContractType Nested;
1779                 [DataMember]
1780                 public string X = "x";
1781         }
1782 }
1783
1784 [DataContract]
1785 class GlobalSample1
1786 {
1787 }
1788
1789 [DataContract]
1790 class Foo<X,Y,Z>
1791 {
1792         [DataMember]
1793         public X Field;
1794 }
1795
1796 [CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
1797 public class MyDictionary<K,V> : Dictionary<K,V>
1798 {
1799 }
1800
1801 // bug #524086
1802 [DataContract(Namespace="http://example.com/schemas/asx")]
1803 public class DummyEntry
1804 {
1805     [DataMember]
1806     public DummyEntryInfo EntryInfo { get; set; }
1807     [DataMember]
1808     public string Href { get; set; }
1809 }
1810
1811 [DataContract(Namespace="http://example.com/schemas/asx"),
1812 KnownType(typeof(PartDummyEntryInfo))]
1813 public abstract class DummyEntryInfo
1814 {
1815 }
1816
1817 [DataContract(Namespace="http://example.com/schemas/asx")]
1818 public class DummyPlaylist
1819 {
1820     public IList<DummyEntry> entries = new List<DummyEntry> ();
1821
1822     [DataMember]
1823     public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
1824 }
1825
1826 [DataContract(Namespace="http://example.com/schemas/asx")]
1827 public class PartDummyEntryInfo : DummyEntryInfo
1828 {
1829     public PartDummyEntryInfo() {}
1830 }
1831
1832 // bug #524088
1833
1834 [DataContract(Namespace="http://example.com/schemas/asx")]
1835 public class AsxEntryInfo
1836 {
1837     [DataMember]
1838     public string AdvertPrompt { get; set; }
1839 }
1840
1841 // bug #560155
1842
1843 [DataContract]
1844 public class Person
1845 {
1846         [DataMember]
1847         readonly public string name;
1848         [DataMember]
1849         readonly public Guid Id = Guid.Empty;
1850
1851         public Person (string nameIn, Guid idIn)
1852         {
1853                 name = nameIn;
1854                 Id = idIn;
1855         }
1856
1857         public override string ToString()
1858         {
1859                 return string.Format ("name={0},id={1}", name, Id);
1860         }
1861 }
1862
1863 // bug #599889
1864 namespace U2U.DataContracts
1865 {
1866         [DataContract]
1867         public class Person
1868         {
1869                 [DataMember]
1870                 public string Name { get; set; }
1871
1872                 [DataMember]
1873                 public Job Occupation { get; set; }
1874         }
1875
1876         [DataContract]
1877         public class Job
1878         {
1879                 [DataMember]
1880                 public string Description { get; set; }
1881         }
1882 }
1883
1884 #region bug #610036
1885 //parent class with a name property
1886 [DataContract (Namespace = "Company.Foo")]
1887 public abstract class ParentClass
1888 {
1889         
1890         //constructor
1891         public ParentClass (string name)
1892         {
1893                 Name = name;
1894         }
1895         
1896         //the name
1897         [DataMember]
1898         public string Name{ get; set; }
1899
1900 }
1901
1902 //root object
1903 [DataContract (Namespace = "Company.Foo")]
1904 public class Root : ParentClass
1905 {
1906         //dict
1907         [DataMember]
1908         public Dict<Foo> FDict; 
1909         
1910         //constructor
1911         public Root (string name)
1912                 : base (name)
1913         {
1914                 FDict = new Dict<Foo> ();
1915         }
1916 }
1917
1918
1919 //subclass
1920 [DataContract (Namespace = "Company.Foo")]
1921 public class Foo : ParentClass
1922 {
1923         //here is one dict
1924         [DataMember]
1925         public Dict<Bar> FDict;
1926         
1927         //constructor
1928         public Foo (string name) 
1929                 : base (name)
1930         {
1931                 FDict = new Dict<Bar> ();
1932         }
1933         
1934 }
1935
1936 //another sublass
1937 [DataContract (Namespace = "Company.Foo")]
1938 public class Bar : ParentClass
1939 {
1940         //constructor
1941         public Bar (string name)
1942                 : base (name)
1943         {
1944         }
1945         
1946 }
1947 //the custom dictionary
1948 [CollectionDataContract (ItemName = "DictItem", Namespace = "Company.Foo")]
1949 public class Dict<T> : Dictionary<string, T> where T : ParentClass
1950 {
1951         public void Add (T item)
1952         {
1953                 Add (item.Name, item);
1954         }
1955         
1956 }
1957 #endregion