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