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