Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / XmlObjectSerializerTest.cs
1 //
2 // XmlObjectSerializerTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Ankit Jain <JAnkit@novell.com>
7 //
8 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 //
32 // This test code contains tests for both DataContractSerializer and
33 // NetDataContractSerializer. The code could be mostly common.
34 //
35
36 using System;
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
40 using System.Data;
41 using System.IO;
42 using System.Linq;
43 using System.Net;
44 using System.Runtime.Serialization;
45 using System.Text;
46 using System.Xml;
47 using System.Xml.Schema;
48 using System.Xml.Serialization;
49 using NUnit.Framework;
50
51 [assembly: ContractNamespace ("http://www.u2u.be/samples/wcf/2009", ClrNamespace = "U2U.DataContracts")] // bug #599889
52
53 namespace MonoTests.System.Runtime.Serialization
54 {
55         [TestFixture]
56         public class DataContractSerializerTest
57         {
58                 static readonly XmlWriterSettings settings;
59
60                 static DataContractSerializerTest ()
61                 {
62                         settings = new XmlWriterSettings ();
63                         settings.OmitXmlDeclaration = true;
64                 }
65
66                 [DataContract]
67                 class Sample1
68                 {
69                         [DataMember]
70                         public string Member1;
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentNullException))]
75                 public void ConstructorTypeNull ()
76                 {
77                         new DataContractSerializer (null);
78                 }
79
80                 [Test]
81                 public void ConstructorKnownTypesNull ()
82                 {
83                         // null knownTypes is allowed. Though the property is filled.
84                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), null).KnownTypes, "#1");
85                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), "Foo", String.Empty, null).KnownTypes, "#2");
86                         Assert.IsNotNull (new DataContractSerializer (typeof (Sample1), new XmlDictionary ().Add ("Foo"), XmlDictionaryString.Empty, null).KnownTypes, "#3");
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentNullException))]
91                 public void ConstructorNameNull ()
92                 {
93                         new DataContractSerializer (typeof (Sample1), null, String.Empty);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentNullException))]
98                 public void ConstructorNamespaceNull ()
99                 {
100                         new DataContractSerializer (typeof (Sample1), "foo", null);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
105                 public void ConstructorNegativeMaxObjects ()
106                 {
107                         new DataContractSerializer (typeof (Sample1), null,
108                                 -1, false, false, null);
109                 }
110
111                 [Test]
112                 public void ConstructorMisc ()
113                 {
114                         new DataContractSerializer (typeof (GlobalSample1));
115                 }
116
117                 [Test]
118                 public void WriteObjectContent ()
119                 {
120                         StringWriter sw = new StringWriter ();
121                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
122                                 DataContractSerializer ser =
123                                         new DataContractSerializer (typeof (string));
124                                 xw.WriteStartElement ("my-element");
125                                 ser.WriteObjectContent (xw, "TEST STRING");
126                                 xw.WriteEndElement ();
127                         }
128                         Assert.AreEqual ("<my-element>TEST STRING</my-element>",
129                                 sw.ToString ());
130                 }
131
132                 [Test]
133                 public void WriteObjectToStream ()
134                 {
135                         DataContractSerializer ser =
136                                 new DataContractSerializer (typeof (int));
137                         MemoryStream sw = new MemoryStream ();
138                         ser.WriteObject (sw, 1);
139                         string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
140                         byte[] buf = sw.ToArray ();
141                         Assert.AreEqual (expected, Encoding.UTF8.GetString (buf, 0, buf.Length));
142                 }
143
144                 [Test]
145                 public void ReadObjectFromStream ()
146                 {
147                         DataContractSerializer ser =
148                                 new DataContractSerializer (typeof (int));
149                         string expected = "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>";
150                         byte[] buf = Encoding.UTF8.GetBytes (expected);
151                         MemoryStream sw = new MemoryStream (buf);
152                         object res = ser.ReadObject (sw);
153                         Assert.AreEqual (1, res);
154                 }
155
156                 // int
157
158                 [Test]
159                 public void SerializeInt ()
160                 {
161                         DataContractSerializer ser =
162                                 new DataContractSerializer (typeof (int));
163                         SerializeInt (ser, "<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">1</int>");
164                 }
165
166
167                 [Test]
168                 [Category ("NotWorking")]
169                 public void NetSerializeInt ()
170                 {
171                         NetDataContractSerializer ser =
172                                 new NetDataContractSerializer ();
173                         // z:Assembly="0" ???
174                         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));
175                 }
176
177                 void SerializeInt (XmlObjectSerializer ser, string expected)
178                 {
179                         StringWriter sw = new StringWriter ();
180                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
181                                 ser.WriteObject (w, 1);
182                         }
183                         Assert.AreEqual (expected, sw.ToString ());
184                 }
185
186                 // pass typeof(DCEmpty), serialize int
187
188                 [Test]
189                 public void SerializeIntForDCEmpty ()
190                 {
191                         DataContractSerializer ser =
192                                 new DataContractSerializer (typeof (DCEmpty));
193                         // tricky!
194                         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>");
195                 }
196
197                 void SerializeIntForDCEmpty (XmlObjectSerializer ser, string expected)
198                 {
199                         StringWriter sw = new StringWriter ();
200                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
201                                 ser.WriteObject (w, 1);
202                         }
203                         XmlComparer.AssertAreEqual (expected, sw.ToString ());
204                 }
205
206                 // DCEmpty
207
208                 [Test]
209                 public void SerializeEmptyClass ()
210                 {
211                         DataContractSerializer ser =
212                                 new DataContractSerializer (typeof (DCEmpty));
213                         SerializeEmptyClass (ser, "<DCEmpty xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
214                 }
215
216                 [Test]
217                 [Category ("NotWorking")]
218                 public void NetSerializeEmptyClass ()
219                 {
220                         NetDataContractSerializer ser =
221                                 new NetDataContractSerializer ();
222                         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));
223                 }
224
225                 void SerializeEmptyClass (XmlObjectSerializer ser, string expected)
226                 {
227                         StringWriter sw = new StringWriter ();
228                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
229                                 ser.WriteObject (w, new DCEmpty ());
230                         }
231                         Assert.AreEqual (expected, sw.ToString ());
232                 }
233
234                 // DCEmpty
235
236                 [Test]
237                 public void SerializeEmptyNoNSClass ()
238                 {
239                         var ser = new DataContractSerializer (typeof (DCEmptyNoNS));
240                         SerializeEmptyNoNSClass (ser, "<DCEmptyNoNS xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />");
241                 }
242
243                 void SerializeEmptyNoNSClass (XmlObjectSerializer ser, string expected)
244                 {
245                         var sw = new StringWriter ();
246                         using (var w = XmlWriter.Create (sw, settings)) {
247                                 ser.WriteObject (w, new DCEmptyNoNS ());
248                         }
249                         Assert.AreEqual (expected, sw.ToString ());
250                 }
251                 // string (primitive)
252
253                 [Test]
254                 public void SerializePrimitiveString ()
255                 {
256                         XmlObjectSerializer ser =
257                                 new DataContractSerializer (typeof (string));
258                         SerializePrimitiveString (ser, "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">TEST</string>");
259                 }
260
261                 [Test]
262                 [Category ("NotWorking")]
263                 public void NetSerializePrimitiveString ()
264                 {
265                         XmlObjectSerializer ser = new NetDataContractSerializer ();
266                         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>");
267                 }
268
269                 void SerializePrimitiveString (XmlObjectSerializer ser, string expected)
270                 {
271                         StringWriter sw = new StringWriter ();
272                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
273                                 ser.WriteObject (w, "TEST");
274                         }
275                         Assert.AreEqual (expected, sw.ToString ());
276                 }
277
278                 // QName (primitive but ...)
279
280                 [Test]
281                 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
282                 public void SerializePrimitiveQName ()
283                 {
284                         XmlObjectSerializer ser =
285                                 new DataContractSerializer (typeof (XmlQualifiedName));
286                         SerializePrimitiveQName (ser, "<z:QName xmlns:d7=\"urn:foo\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\">d7:foo</z:QName>");
287                 }
288
289                 [Test]
290                 [Ignore ("These tests would not make any sense right now since it's populated prefix is not testable.")]
291                 public void NetSerializePrimitiveQName ()
292                 {
293                         XmlObjectSerializer ser = new NetDataContractSerializer ();
294                         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>");
295                 }
296
297                 void SerializePrimitiveQName (XmlObjectSerializer ser, string expected)
298                 {
299                         StringWriter sw = new StringWriter ();
300                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
301                                 ser.WriteObject (w, new XmlQualifiedName ("foo", "urn:foo"));
302                         }
303                         Assert.AreEqual (expected, sw.ToString ());
304                 }
305
306                 // DCSimple1
307
308                 [Test]
309                 public void SerializeSimpleClass1 ()
310                 {
311                         DataContractSerializer ser =
312                                 new DataContractSerializer (typeof (DCSimple1));
313                         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>");
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (SerializationException))]
318                 public void SerializeSimpleXml ()
319                 {
320                         DataContractSerializer ser =
321                                 new DataContractSerializer (typeof (SimpleXml));
322                         SerializeSimpleClass1 (ser, @"<simple i:type=""d1p1:DCSimple1"" xmlns:d1p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><d1p1:Foo>TEST</d1p1:Foo></simple>");
323                 }
324
325                 [Test]
326                 [Category ("NotWorking")]
327                 public void NetSerializeSimpleClass1 ()
328                 {
329                         NetDataContractSerializer ser =
330                                 new NetDataContractSerializer ();
331                         SerializeSimpleClass1 (ser, String.Format ("<DCSimple1 xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.DCSimple1\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Foo z:Id=\"2\">TEST</Foo></DCSimple1>", this.GetType ().Assembly.FullName));
332                 }
333
334                 void SerializeSimpleClass1 (XmlObjectSerializer ser, string expected)
335                 {
336                         StringWriter sw = new StringWriter ();
337                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
338                                 ser.WriteObject (w, new DCSimple1 ());
339                         }
340                         Console.WriteLine(sw.ToString());
341                         Assert.AreEqual (expected, sw.ToString ());
342                 }
343
344                 // NonDC (behavior changed in 3.5/SP1; not it's not rejected)
345
346                 [Test]
347                 public void SerializeNonDC ()
348                 {
349                         DataContractSerializer ser = new DataContractSerializer (typeof (NonDC));
350                         var sw = new StringWriter ();
351                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
352                                 ser.WriteObject (w, new NonDC ());
353                         }
354                         Assert.AreEqual ("<NonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Whee>whee!</Whee></NonDC>".Replace ('\'', '"'), sw.ToString ());
355                 }
356
357                 // DCHasNonDC
358
359                 [Test]
360                 public void SerializeDCHasNonDC ()
361                 {
362                         DataContractSerializer ser = new DataContractSerializer (typeof (DCHasNonDC));
363                         var sw = new StringWriter ();
364                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
365                                 ser.WriteObject (w, new DCHasNonDC ());
366                         }
367                         Assert.AreEqual ("<DCHasNonDC xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Hoge><Whee>whee!</Whee></Hoge></DCHasNonDC>".Replace ('\'', '"'), sw.ToString ());
368                 }
369
370                 // DCHasSerializable
371
372                 [Test]
373                 // DCHasSerializable itself is DataContract and has a field
374                 // whose type is not contract but serializable.
375                 public void SerializeSimpleSerializable1 ()
376                 {
377                         DataContractSerializer ser = new DataContractSerializer (typeof (DCHasSerializable));
378                         StringWriter sw = new StringWriter ();
379                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
380                                 ser.WriteObject (w, new DCHasSerializable ());
381                         }
382                         Assert.AreEqual ("<DCHasSerializable xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><Ser><Doh>doh!</Doh></Ser></DCHasSerializable>", sw.ToString ());
383                 }
384
385                 [Test]
386                 public void SerializeDCWithName ()
387                 {
388                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
389                         StringWriter sw = new StringWriter ();
390                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
391                                 ser.WriteObject (w, new DCWithName ());
392                         }
393                         Assert.AreEqual ("<Foo xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\"><FooMember>value</FooMember></Foo>", sw.ToString ());
394                 }
395
396                 [Test]
397                 public void SerializeDCWithEmptyName1 ()
398                 {
399                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyName));
400                         StringWriter sw = new StringWriter ();
401                         DCWithEmptyName dc = new DCWithEmptyName ();
402                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
403                                 try {
404                                         ser.WriteObject (w, dc);
405                                 } catch (InvalidDataContractException) {
406                                         return;
407                                 }
408                         }
409                         Assert.Fail ("Expected InvalidDataContractException");
410                 }
411
412                 [Test]
413                 [Category ("NotWorking")]
414                 public void SerializeDCWithEmptyName2 ()
415                 {
416                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithName));
417                         StringWriter sw = new StringWriter ();
418
419                         /* DataContractAttribute.Name == "", not valid */
420                         DCWithEmptyName dc = new DCWithEmptyName ();
421                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
422                                 try {
423                                         ser.WriteObject (w, dc);
424                                 } catch (InvalidDataContractException) {
425                                         return;
426                                 }
427                         }
428                         Assert.Fail ("Expected InvalidDataContractException");
429                 }
430
431                 [Test]
432                 [Category ("NotWorking")]
433                 public void SerializeDCWithNullName ()
434                 {
435                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithNullName));
436                         StringWriter sw = new StringWriter ();
437                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
438                                 try {
439                                         /* DataContractAttribute.Name == "", not valid */
440                                         ser.WriteObject (w, new DCWithNullName ());
441                                 } catch (InvalidDataContractException) {
442                                         return;
443                                 }
444                         }
445                         Assert.Fail ("Expected InvalidDataContractException");
446                 }
447
448                 [Test]
449                 public void SerializeDCWithEmptyNamespace1 ()
450                 {
451                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEmptyNamespace));
452                         StringWriter sw = new StringWriter ();
453                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
454                                 ser.WriteObject (w, new DCWithEmptyNamespace ());
455                         }
456                 }
457
458                 // Wrapper.DCWrapped
459
460                 [Test]
461                 public void SerializeWrappedClass ()
462                 {
463                         DataContractSerializer ser =
464                                 new DataContractSerializer (typeof (Wrapper.DCWrapped));
465                         SerializeWrappedClass (ser, "<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />");
466                 }
467
468                 [Test]
469                 [Category ("NotWorking")]
470                 public void NetSerializeWrappedClass ()
471                 {
472                         NetDataContractSerializer ser =
473                                 new NetDataContractSerializer ();
474                         SerializeWrappedClass (ser, String.Format ("<Wrapper.DCWrapped xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" z:Id=\"1\" z:Type=\"MonoTests.System.Runtime.Serialization.Wrapper+DCWrapped\" z:Assembly=\"{0}\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" xmlns=\"http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization\" />", this.GetType ().Assembly.FullName));
475                 }
476
477                 void SerializeWrappedClass (XmlObjectSerializer ser, string expected)
478                 {
479                         StringWriter sw = new StringWriter ();
480                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
481                                 ser.WriteObject (w, new Wrapper.DCWrapped ());
482                         }
483                         Assert.AreEqual (expected, sw.ToString ());
484                 }
485
486                 [Test]
487                 /* old code
488                 // CollectionContainer : Items must have a setter.
489                 [ExpectedException (typeof (InvalidDataContractException))]
490                 [Category ("NotWorking")]
491                 */
492                 public void SerializeReadOnlyCollectionMember ()
493                 {
494                         DataContractSerializer ser =
495                                 new DataContractSerializer (typeof (CollectionContainer));
496
497                         StringWriter sw = new StringWriter ();
498                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
499                                 ser.WriteObject (w, null);
500                         }
501                         Assert.AreEqual ("<CollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
502
503                         sw = new StringWriter ();
504                         var c = new CollectionContainer ();
505                         c.Items.Add ("foo");
506                         c.Items.Add ("bar");
507                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
508                                 ser.WriteObject (w, c);
509                         }
510                         Assert.AreEqual ("<CollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items xmlns:d2p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><d2p1:string>foo</d2p1:string><d2p1:string>bar</d2p1:string></Items></CollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
511                 }
512
513                 // DataCollectionContainer : Items must have a setter.
514                 [Test]
515                 //[ExpectedException (typeof (InvalidDataContractException))]
516                 public void SerializeReadOnlyDataCollectionMember ()
517                 {
518                         DataContractSerializer ser =
519                                 new DataContractSerializer (typeof (DataCollectionContainer));
520                         StringWriter sw = new StringWriter ();
521                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
522                                 ser.WriteObject (w, null);
523                         }
524                         Assert.AreEqual ("<DataCollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");
525
526                         sw = new StringWriter ();
527                         var c = new DataCollectionContainer ();
528                         c.Items.Add ("foo");
529                         c.Items.Add ("bar");
530                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
531                                 ser.WriteObject (w, c);
532                         }
533                         // LAMESPEC: this is bogus behavior. .NET serializes 
534                         // System.String as "string" without overriding its 
535                         // element namespace, but then it must be regarded as
536                         // in parent's namespace. What if there already is an
537                         // element definition for "string" with the same
538                         // namespace?
539                         Assert.AreEqual ("<DataCollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items><string>foo</string><string>bar</string></Items></DataCollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
540                 }
541
542                 [Test]
543                 public void SerializeGuid ()
544                 {
545                         DataContractSerializer ser = new DataContractSerializer (typeof (Guid));
546                         StringWriter sw = new StringWriter ();
547                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
548                                 ser.WriteObject (w, Guid.Empty);
549                         }
550                         Assert.AreEqual (
551                                 "<guid xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">00000000-0000-0000-0000-000000000000</guid>",
552                                 sw.ToString ());
553                 }
554
555                 [Test]
556                 public void SerializeEnum ()
557                 {
558                         DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
559                         StringWriter sw = new StringWriter ();
560                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
561                                 ser.WriteObject (w, new Colors ());
562                         }
563
564                         Assert.AreEqual (
565                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>",
566                                 sw.ToString ());
567                 }
568
569                 [Test]
570                 public void SerializeEnum2 ()
571                 {
572                         DataContractSerializer ser = new DataContractSerializer (typeof (Colors));
573                         StringWriter sw = new StringWriter ();
574                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
575                                 ser.WriteObject (w, 0);
576                         }
577
578                         XmlComparer.AssertAreEqual (
579                                 @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">0</Colors>",
580                                 sw.ToString ());
581                 }
582
583                 [Test]
584                 public void SerializeEnumWithDC ()
585                 {
586                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
587                         StringWriter sw = new StringWriter ();
588                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
589                                 ser.WriteObject (w, new ColorsWithDC ());
590                         }
591
592                         Assert.AreEqual (
593                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>",
594                                 sw.ToString ());
595                 }
596
597                 [Test]
598                 public void SerializeEnumWithNoDC ()
599                 {
600                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsEnumMemberNoDC));
601                         StringWriter sw = new StringWriter ();
602                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
603                                 ser.WriteObject (w, new ColorsEnumMemberNoDC ());
604                         }
605
606                         Assert.AreEqual (
607                                 @"<ColorsEnumMemberNoDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</ColorsEnumMemberNoDC>",
608                                 sw.ToString ());
609                 }
610
611                 [Test]
612                 public void SerializeEnumWithDC2 ()
613                 {
614                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
615                         StringWriter sw = new StringWriter ();
616                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
617                                 ser.WriteObject (w, 3);
618                         }
619
620                         XmlComparer.AssertAreEqual (
621                                 @"<_ColorsWithDC xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">3</_ColorsWithDC>",
622                                 sw.ToString ());
623                 }
624
625                 [Test]
626                 [ExpectedException (typeof (SerializationException))]
627                 public void SerializeEnumWithDCInvalid ()
628                 {
629                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
630                         StringWriter sw = new StringWriter ();
631                         ColorsWithDC cdc = ColorsWithDC.Blue;
632                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
633                                 ser.WriteObject (w, cdc);
634                         }
635                 }
636
637                 [Test]
638                 public void SerializeDCWithEnum ()
639                 {
640                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum));
641                         StringWriter sw = new StringWriter ();
642                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
643                                 ser.WriteObject (w, new DCWithEnum ());
644                         }
645  
646                         Assert.AreEqual (
647                                 @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>",
648                                 sw.ToString ());
649                 }
650
651                 [Test]
652                 public void SerializeDCWithTwoEnums ()
653                 {
654                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithTwoEnums));
655                         StringWriter sw = new StringWriter ();
656                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
657                                 DCWithTwoEnums e = new DCWithTwoEnums ();
658                                 e.colors = Colors.Blue;
659                                 e.colors2 = Colors.Green;
660                                 ser.WriteObject (w, e);
661                         }
662  
663                         Assert.AreEqual (
664                                 @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors>Blue</colors><colors2>Green</colors2></DCWithTwoEnums>",
665                                 sw.ToString ());
666                 }
667
668                 [Test]
669                 public void SerializeNestingDC2 ()
670                 {
671                         DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC2));
672                         StringWriter sw = new StringWriter ();
673                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
674                                 NestingDC2 e = new NestingDC2 ();
675                                 e.Field = new NestedDC2 ("Something");
676                                 ser.WriteObject (w, e);
677                         }
678  
679                         Assert.AreEqual (
680                                 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>",
681                                 sw.ToString ());
682                 }
683
684                 [Test]
685                 public void SerializeNestingDC ()
686                 {
687                         DataContractSerializer ser = new DataContractSerializer (typeof (NestingDC));
688                         StringWriter sw = new StringWriter ();
689                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
690                                 NestingDC e = new NestingDC ();
691                                 e.Field1 = new NestedDC ("test1");
692                                 e.Field2 = new NestedDC ("test2");
693                                 ser.WriteObject (w, e);
694                         }
695  
696                         Assert.AreEqual (
697                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>",
698                                 sw.ToString ());
699                         sw = new StringWriter ();
700                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
701                                 NestingDC e = new NestingDC ();
702                                 ser.WriteObject (w, e);
703                         }
704  
705                         Assert.AreEqual (
706                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>",
707                                 sw.ToString ());
708                 }
709
710                 [Test]
711                 public void SerializeDerivedDC ()
712                 {
713                         DataContractSerializer ser = new DataContractSerializer (typeof (DerivedDC));
714                         StringWriter sw = new StringWriter ();
715                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
716                                 DerivedDC e = new DerivedDC ();
717                                 ser.WriteObject (w, e);
718                         }
719  
720                         Assert.AreEqual (
721                                 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">0</baseVal><derivedVal>0</derivedVal></DerivedDC>",
722                                 sw.ToString ());
723                 }
724
725                 [Test]
726                 public void SerializerDCArray ()
727                 {
728                         DataContractSerializer ser = new DataContractSerializer (typeof (DCWithEnum []));
729                         StringWriter sw = new StringWriter ();
730                         DCWithEnum [] arr = new DCWithEnum [2];
731                         arr [0] = new DCWithEnum (); arr [0].colors = Colors.Red;
732                         arr [1] = new DCWithEnum (); arr [1].colors = Colors.Green;
733                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
734                                 ser.WriteObject (w, arr);
735                         }
736
737                         XmlComparer.AssertAreEqual (
738                                 @"<ArrayOfDCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><DCWithEnum><_colors>Red</_colors></DCWithEnum><DCWithEnum><_colors>Green</_colors></DCWithEnum></ArrayOfDCWithEnum>",
739                                 sw.ToString ());
740                 }
741
742                 [Test]
743                 public void SerializerDCArray2 ()
744                 {
745                         List<Type> known = new List<Type> ();
746                         known.Add (typeof (DCWithEnum));
747                         known.Add (typeof (DCSimple1));
748                         DataContractSerializer ser = new DataContractSerializer (typeof (object []), known);
749                         StringWriter sw = new StringWriter ();
750                         object [] arr = new object [2];
751                         arr [0] = new DCWithEnum (); ((DCWithEnum)arr [0]).colors = Colors.Red;
752                         arr [1] = new DCSimple1 (); ((DCSimple1) arr [1]).Foo = "hello";
753
754                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
755                                 ser.WriteObject (w, arr);
756                         }
757
758                         XmlComparer.AssertAreEqual (
759                                 @"<ArrayOfanyType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCWithEnum""><d2p1:_colors>Red</d2p1:_colors></anyType><anyType xmlns:d2p1=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"" i:type=""d2p1:DCSimple1""><d2p1:Foo>hello</d2p1:Foo></anyType></ArrayOfanyType>",
760                                 sw.ToString ());
761                 }
762
763                 [Test]
764                 public void SerializerDCArray3 ()
765                 {
766                         DataContractSerializer ser = new DataContractSerializer (typeof (int []));
767                         StringWriter sw = new StringWriter ();
768                         int [] arr = new int [2];
769                         arr [0] = 1; arr [1] = 2;
770
771                         using (XmlWriter w = XmlWriter.Create (sw, settings)) {
772                                 ser.WriteObject (w, arr);
773                         }
774
775                         XmlComparer.AssertAreEqual (
776                                 @"<ArrayOfint xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><int>1</int><int>2</int></ArrayOfint>",
777                                 sw.ToString ());
778                 }
779
780                 [Test]
781                 public void SerializeNonDCArray ()
782                 {
783                         DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
784                         StringWriter sw = new StringWriter ();
785                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
786                                 ser.WriteObject (xw, new SerializeNonDCArrayType ());
787                         }
788                         Assert.AreEqual (@"<SerializeNonDCArrayType xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><IPAddresses /></SerializeNonDCArrayType>",
789                                 sw.ToString ());
790                 }
791
792                 [Test]
793                 public void SerializeNonDCArrayItems ()
794                 {
795                         DataContractSerializer ser = new DataContractSerializer (typeof (SerializeNonDCArrayType));
796                         StringWriter sw = new StringWriter ();
797                         using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
798                                 SerializeNonDCArrayType obj = new SerializeNonDCArrayType ();
799                                 obj.IPAddresses = new NonDCItem [] {new NonDCItem () { Data = new int [] {1, 2, 3, 4} } };
800                                 ser.WriteObject (xw, obj);
801                         }
802
803                         XmlDocument doc = new XmlDocument ();
804                         doc.LoadXml (sw.ToString ());
805                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
806                         nsmgr.AddNamespace ("s", "http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization");
807                         nsmgr.AddNamespace ("n", "http://schemas.datacontract.org/2004/07/System.Net");
808                         nsmgr.AddNamespace ("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
809
810                         Assert.AreEqual (1, doc.SelectNodes ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem", nsmgr).Count, "#1");
811                         XmlElement el = doc.SelectSingleNode ("/s:SerializeNonDCArrayType/s:IPAddresses/s:NonDCItem/s:Data", nsmgr) as XmlElement;
812                         Assert.IsNotNull (el, "#3");
813                         Assert.AreEqual (4, el.SelectNodes ("a:int", nsmgr).Count, "#4");
814                 }
815
816                 [Test]
817                 public void DeserializeEnum ()
818                 {
819                         Colors c = Deserialize<Colors> (
820                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">Red</Colors>");
821
822                         Assert.AreEqual (Colors.Red, c, "#de2");
823                 }
824
825                 [Test]
826                 public void DeserializeEnum2 ()
827                 {
828                         Colors c = Deserialize<Colors> (
829                                 @"<Colors xmlns:d1p1=""http://www.w3.org/2001/XMLSchema"" i:type=""d1p1:int"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">1</Colors>",
830                                 typeof (int));
831
832                         Assert.AreEqual (Colors.Green, c, "#de4");
833                 }
834                 
835                 [Test]
836                 [ExpectedException (typeof (SerializationException))]
837                 public void DeserializeEnumInvalid1 ()
838                 {
839                         Deserialize<Colors> (
840                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></Colors>");
841                 }
842
843                 [Test]
844                 [ExpectedException (typeof (SerializationException))]
845                 public void DeserializeEnumInvalid2 ()
846                 {
847                         Deserialize<Colors> (
848                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
849                 }
850
851                 [Test]
852                 [ExpectedException (typeof (SerializationException))]
853                 public void DeserializeEnumInvalid3 ()
854                 {
855                         //"red" instead of "Red"
856                         Deserialize<Colors> (
857                                 @"<Colors xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">red</Colors>");
858                 }
859
860                 [Test]
861                 public void DeserializeEnumFlags ()
862                 {
863                         Deserialize<Colors2> (
864                                 @"<Colors2 xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
865                 }
866
867                 [Test]
868                 public void DeserializeEnumWithDC ()
869                 {
870                         ColorsWithDC cdc = Deserialize<ColorsWithDC> (
871                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC>");
872                         
873                         Assert.AreEqual (ColorsWithDC.Red, cdc, "#de6");
874                 }
875
876                 [Test]
877                 [ExpectedException (typeof (SerializationException))]
878                 public void DeserializeEnumWithDCInvalid ()
879                 {
880                         Deserialize<ColorsWithDC> (
881                                 @"<_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">NonExistant</_ColorsWithDC>");
882                 }
883
884                 [Test]
885                 public void DeserializeDCWithEnum ()
886                 {
887                         DCWithEnum dc = Deserialize<DCWithEnum> (
888                                 @"<DCWithEnum xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><_colors>Red</_colors></DCWithEnum>");
889
890                         Assert.AreEqual (Colors.Red, dc.colors, "#de8");
891                 }
892
893                 [Test]
894                 public void DeserializeNestingDC ()
895                 {
896                         NestingDC dc = Deserialize<NestingDC> (
897                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1><Name>test1</Name></Field1><Field2><Name>test2</Name></Field2></NestingDC>");
898
899                         Assert.IsNotNull (dc.Field1, "#N1: Field1 should not be null.");
900                         Assert.IsNotNull (dc.Field2, "#N2: Field2 should not be null.");
901                         Assert.AreEqual ("test1", dc.Field1.Name, "#1");
902                         Assert.AreEqual ("test2", dc.Field2.Name, "#2");
903                 }
904
905                 [Test]
906                 public void DeserializeNestingDC2 ()
907                 {
908                         NestingDC2 dc = Deserialize<NestingDC2> (
909                                 @"<NestingDC2 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""test2""><Field xmlns:d2p1=""test1""><d2p1:Name>Something</d2p1:Name></Field></NestingDC2>");
910
911                         Assert.IsNotNull (dc.Field, "#N1: Field should not be null.");
912                         Assert.AreEqual ("Something", dc.Field.Name, "#N2");
913                 }
914
915                 [Test]
916                 public void DeserializeDerivedDC ()
917                 {
918                         DerivedDC dc = Deserialize<DerivedDC> (
919                                 @"<DerivedDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""Derived""><baseVal xmlns=""Base"">1</baseVal><derivedVal>2</derivedVal></DerivedDC>");
920
921                         Assert.AreEqual (1, dc.baseVal, "#N1");
922                         Assert.AreEqual (2, dc.derivedVal, "#N2");
923                 }
924
925                 [Test]
926                 public void DeserializeTwice ()
927                 {
928                         string xml = 
929                                 @"<any><_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC> <_ColorsWithDC xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization"">_Red</_ColorsWithDC></any>";
930                         DataContractSerializer ser = new DataContractSerializer (typeof (ColorsWithDC));
931                         XmlReader xr = XmlReader.Create (new StringReader (xml), new XmlReaderSettings ());
932                         xr.ReadStartElement ();
933                         object o = ser.ReadObject (xr);
934                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
935                         ColorsWithDC cdc = (ColorsWithDC) o;
936                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
937
938                         o = ser.ReadObject (xr);
939                         Assert.AreEqual (typeof (ColorsWithDC), o.GetType (), "#de5");
940                         cdc = (ColorsWithDC) o;
941                         Assert.AreEqual (ColorsWithDC.Red, o, "#de6");
942                         Assert.AreEqual (XmlNodeType.EndElement, xr.NodeType, "#de6");
943                         Assert.AreEqual ("any", xr.LocalName, "#de6");
944                         xr.ReadEndElement ();
945                 }
946
947
948                 [Test]
949                 public void DeserializeEmptyNestingDC ()
950                 {
951                         NestingDC dc = Deserialize<NestingDC> (
952                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""></NestingDC>");
953
954                         Assert.IsNotNull (dc, "#A0: The object should not be null.");
955                         Assert.IsNull (dc.Field1, "#A1: Field1 should be null.");
956                         Assert.IsNull (dc.Field2, "#A2: Field2 should be null.");
957
958                         dc = Deserialize<NestingDC> (
959                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""/>");
960
961                         Assert.IsNotNull (dc, "#B0: The object should not be null.");
962                         Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
963                         Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
964
965                         dc = Deserialize<NestingDC> (
966                                 @"<NestingDC xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><Field1 i:nil=""true"" /><Field2 i:nil=""true"" /></NestingDC>");
967
968                         Assert.IsNotNull (dc, "#B0: The object should not be null.");
969                         Assert.IsNull (dc.Field1, "#B1: Field1 should be null.");
970                         Assert.IsNull (dc.Field2, "#B2: Field2 should be null.");
971                 }
972
973                 [Test]
974                 [ExpectedException (typeof (SerializationException))]
975                 public void DeserializeEmptyDCWithTwoEnums ()
976                 {
977                         Deserialize<DCWithTwoEnums> (
978                                 @"<DCWithTwoEnums xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization""><colors i:nil=""true""/><colors2 i:nil=""true""/></DCWithTwoEnums>");
979                 }
980
981                 [Test]
982                 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                         string result = 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                         int idx = result.IndexOf ("foo1");
1424                         Assert.IsTrue (idx >= 0, "#2");
1425                         // since "foo1" is stored as z:Ref for string, it must not occur twice.
1426                         int idx2 = result.IndexOf ("foo1", idx + 1);
1427                         Assert.IsTrue (idx2 < 0, "idx2 should not occur at " + idx2);
1428                 }
1429
1430                 [Test]
1431                 public void AncestralReference ()
1432                 {
1433                         // Reference to Parent comes inside the Parent itself.
1434                         // In this case, adding reference after complete deserialization won't work (but it should).
1435                         var ms = new MemoryStream ();
1436                         Type [] knownTypes = new Type [] { typeof (ParentClass), typeof (Foo), typeof (Bar) };
1437
1438                         var ds = new DataContractSerializer (typeof (Parent));
1439
1440                         var org = new Parent ();
1441                         ds.WriteObject (ms, org);
1442                         string result = Encoding.UTF8.GetString (ms.ToArray ());
1443                         ms.Position = 0;
1444
1445                         var parent = (Parent) ds.ReadObject (ms);
1446
1447                         Assert.IsNotNull (parent.Child, "#1");
1448                         Assert.AreEqual (parent, parent.Child.Parent, "#2");
1449                 }
1450
1451                 [Test]
1452                 public void IXmlSerializableCallConstructor ()
1453                 {
1454                         IXmlSerializableCallConstructor  (false);
1455                         IXmlSerializableCallConstructor (true);
1456                 }
1457                 
1458                 void IXmlSerializableCallConstructor (bool binary)
1459                 {
1460                         Stream s = IXmlSerializableCallConstructorSerialize (binary);
1461                         var a = new byte [s.Length];
1462                         s.Position = 0;
1463                         s.Read (a, 0, a.Length);
1464                         s.Position = 0;
1465                         IXmlSerializableCallConstructorDeserialize (s, binary);
1466                 }
1467
1468                 public Stream IXmlSerializableCallConstructorSerialize (bool binary)
1469                 {
1470                         var ds = new DataSet ("ds");
1471                         var dt = new DataTable ("dt");
1472                         ds.Tables.Add (dt);
1473                         dt.Columns.Add ("n", typeof (int));
1474                         dt.Columns.Add ("s", typeof (string));
1475                         
1476                         dt.Rows.Add (5, "five");
1477                         dt.Rows.Add (10, "ten");
1478                         
1479                         ds.AcceptChanges ();
1480                         
1481                         var s = new MemoryStream ();
1482                         
1483                         var w = binary ? XmlDictionaryWriter.CreateBinaryWriter (s) : XmlDictionaryWriter.CreateTextWriter (s);
1484                         
1485                         var x = new DataContractSerializer (typeof (DataSet));
1486                         x.WriteObject (w, ds);
1487                         w.Flush ();
1488         
1489                         return s;
1490                 }
1491                 
1492                 public void IXmlSerializableCallConstructorDeserialize (Stream s, bool binary)
1493                 {
1494                         var r = binary ? XmlDictionaryReader.CreateBinaryReader (s, XmlDictionaryReaderQuotas.Max)
1495                                 : XmlDictionaryReader.CreateTextReader (s, XmlDictionaryReaderQuotas.Max);
1496                         
1497                         var x = new DataContractSerializer (typeof (DataSet));
1498                         
1499                         var ds = (DataSet) x.ReadObject (r);
1500                 }
1501
1502                 [Test]
1503                 [ExpectedException (typeof (InvalidDataContractException))] // BaseConstraintType1 is neither DataContract nor Serializable.
1504                 public void BaseConstraint1 ()
1505                 {
1506                         new DataContractSerializer (typeof (BaseConstraintType3)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType3 ());
1507                 }
1508
1509                 [Test]
1510                 public void BaseConstraint2 ()
1511                 {
1512                         new DataContractSerializer (typeof (BaseConstraintType4)).WriteObject (XmlWriter.Create (TextWriter.Null), new BaseConstraintType4 ());
1513                 }
1514
1515                 [Test] // bug #652331
1516                 public void MembersNamespacesInBaseType ()
1517                 {
1518                         string xml1 = @"<Currency>JPY</Currency><Description i:nil=""true"" />";
1519                         string xml2 = @"<Currency xmlns=""http://schemas.datacontract.org/2004/07/SLProto5"">JPY</Currency><Description i:nil=""true"" xmlns=""http://schemas.datacontract.org/2004/07/SLProto5"" />";
1520                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmount> (new SLProto5.CashAmount () { Currency = "JPY" }, xml1, "#1").Currency, "r#1");
1521                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmount> (new SLProto5_Different.CashAmount () { Currency = "JPY" }, xml2, "#2").Currency, "r#2");
1522                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5.CashAmountDC> (new SLProto5.CashAmountDC () { Currency = "JPY" }, xml1, "#3").Currency, "r#3");
1523                         Assert.AreEqual ("JPY", MembersNamespacesInBaseType_Part<SLProto5_Different.CashAmountDC> (new SLProto5_Different.CashAmountDC () { Currency = "JPY" }, xml2, "#4").Currency, "r#4");
1524                 }
1525
1526                 T MembersNamespacesInBaseType_Part<T> (T instance, string expectedPart, string assert)
1527                 {
1528                         var ds = new DataContractSerializer (typeof (T));
1529                         var sw = new StringWriter ();
1530                         using (var w = XmlWriter.Create (sw))
1531                                 ds.WriteObject (w, instance);
1532                         Assert.IsTrue (sw.ToString ().IndexOf (expectedPart) > 0, assert);
1533                         return (T) ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ())));
1534                 }
1535
1536                 [Test]
1537                 public void DateTimeOffsetSerialization ()
1538                 {
1539                         var ds = new DataContractSerializer (typeof (DateTimeOffset));
1540                         var sw = new StringWriter ();
1541                         string xml = "<DateTimeOffset xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/System'><DateTime>2011-03-01T02:05:06.078Z</DateTime><OffsetMinutes>120</OffsetMinutes></DateTimeOffset>".Replace ('\'', '"');
1542                                                 
1543                         var v = new DateTimeOffset (new DateTime (2011, 3, 1, 4, 5, 6, 78), TimeSpan.FromMinutes (120));
1544                         using (var xw = XmlWriter.Create (sw, settings)) {
1545                                 ds.WriteObject (xw, v);
1546                         }
1547                         Assert.AreEqual (xml, sw.ToString (), "#1");
1548                         Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
1549                 }
1550                 
1551                 [Test]
1552                 public void DateTimeOffsetNullableSerialization ()
1553                 {
1554                         var ds = new DataContractSerializer (typeof (DateTimeOffset?));
1555                         var sw = new StringWriter ();
1556                         string xml = "<DateTimeOffset xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/System\"><DateTime>2012-05-04T00:34:00Z</DateTime><OffsetMinutes>120</OffsetMinutes></DateTimeOffset>";
1557                         
1558                         var v = new DateTimeOffset (new DateTime (2012, 05, 04, 02, 34, 0), TimeSpan.FromMinutes (120));
1559                         using (var xw = XmlWriter.Create (sw, settings)) {
1560                                 ds.WriteObject (xw, v);
1561                         }
1562                         Assert.AreEqual (xml, sw.ToString (), "#1");
1563                         Assert.AreEqual (v, ds.ReadObject (XmlReader.Create (new StringReader (sw.ToString ()))), "#2");
1564                 }
1565
1566                 [Test]
1567                 public void XmlDocumentSupport ()
1568                 {
1569                         var xml = "<XmlDocumentContract xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:foo'><Content><Root xmlns=''>Hello, world!</Root></Content><Nodes><child1 xmlns='' /><child2 xmlns='' /></Nodes></XmlDocumentContract>".Replace ('\'', '"');
1570                         var xml2 = "<Root>Hello, world!</Root>";
1571                         var obj = new XmlDocumentContract ();
1572                         var doc = new XmlDocument ();
1573                         doc.LoadXml (xml2);
1574                         obj.Content = doc.DocumentElement;
1575                         doc = new XmlDocument ();
1576                         doc.LoadXml ("<root><child1/><child2/></root>");
1577                         var l = new List<XmlNode> ();
1578                         foreach (XmlNode node in doc.DocumentElement.ChildNodes)
1579                                 l.Add (node);
1580                         obj.Nodes = l.ToArray ();
1581                         var serializer = new DataContractSerializer (typeof (XmlDocumentContract))
1582                         ;
1583                         var sb = new StringBuilder ();
1584                         using (var writer = new StringWriter (sb))
1585                                 serializer.WriteObject (new XmlTextWriter (writer), obj);
1586                         Assert.AreEqual (xml, sb.ToString (), "#1");
1587                         using (var reader = new StringReader (sb.ToString ()))
1588                                 obj = serializer.ReadObject (new XmlTextReader (reader)) as XmlDocumentContract;
1589                         Assert.AreEqual ("Hello, world!", obj.Content != null ? obj.Content.InnerText : String.Empty, "#2");
1590                         Assert.AreEqual (2, obj.Nodes != null ? obj.Nodes.Length : -1, "#3");
1591                 }
1592
1593                 [Test]
1594                 public void ArrayAsEnumerableAsRoot ()
1595                 {
1596                         var ds = new DataContractSerializer (typeof (IEnumerable<Guid>));
1597                         var sw = new StringWriter ();
1598                         using (var xw = XmlWriter.Create (sw, settings))
1599                                 ds.WriteObject (xw, new Guid [] {Guid.Empty});
1600                         string xml = "<ArrayOfguid xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><guid>00000000-0000-0000-0000-000000000000</guid></ArrayOfguid>".Replace ('\'', '"');
1601                         Assert.AreEqual (xml, sw.ToString (), "#1");
1602                 }
1603         }
1604         
1605         [DataContract]
1606         public class MemberIgnored
1607         {
1608                 [DataMember]
1609                 MemberIgnoredBody body = new MemberIgnoredBody ();
1610         }
1611
1612         public class MemberIgnoredBody
1613         {
1614                 [IgnoreDataMember]
1615                 public string Foo = "foo";
1616
1617                 public string Bar = "bar";
1618         }
1619
1620         public enum Colors {
1621                 Red, Green, Blue
1622         }
1623
1624         [Flags]
1625         public enum Colors2 {
1626                 Red, Green, Blue
1627         }
1628
1629         [DataContract (Name = "_ColorsWithDC")]
1630         public enum ColorsWithDC {
1631
1632                 [EnumMember (Value = "_Red")]
1633                 Red, 
1634                 [EnumMember]
1635                 Green, 
1636                 Blue
1637         }
1638
1639
1640         public enum ColorsEnumMemberNoDC {
1641                 [EnumMember (Value = "_Red")]
1642                 Red, 
1643                 [EnumMember]
1644                 Green, 
1645                 Blue
1646         }
1647
1648         [DataContract]
1649         public class DCWithEnum {
1650                 [DataMember (Name = "_colors")]
1651                 public Colors colors;
1652         }
1653
1654         [DataContract]
1655         public class DCWithTwoEnums {
1656                 [DataMember]
1657                 public Colors colors;
1658                 [DataMember]
1659                 public Colors colors2;
1660         }
1661
1662         [DataContract]
1663         public class DCWithNullableEnum {
1664                 [DataMember]
1665                 public Colors? colors;
1666         }
1667
1668
1669         [DataContract (Namespace = "Base")]
1670         public class BaseDC {
1671                 [DataMember]
1672                 public int baseVal;
1673         }
1674
1675         [DataContract (Namespace = "Derived")]
1676         public class DerivedDC : BaseDC {
1677                 [DataMember]
1678                 public int derivedVal;
1679         }
1680
1681         [DataContract]
1682         public class NestedDC {
1683                 public NestedDC (string name) { this.Name = name; }
1684
1685                 [DataMember]
1686                 public string Name;
1687         }
1688
1689         [DataContract]
1690         public class NestingDC {
1691                 [DataMember]
1692                 public NestedDC Field1;
1693                 [DataMember]
1694                 public NestedDC Field2;
1695         }
1696
1697         [DataContract (Namespace = "test1")]
1698         public class NestedDC2 {
1699                 public NestedDC2 (string name) { this.Name = name; }
1700
1701                 [DataMember]
1702                 public string Name;
1703         }
1704
1705         [DataContract (Namespace = "test2")]
1706         public class NestingDC2 {
1707                 [DataMember]
1708                 public NestedDC2 Field;
1709         }
1710
1711         [DataContract]
1712         public class DCEmpty
1713         {
1714                 // serializer doesn't touch it.
1715                 public string Foo = "TEST";
1716         }
1717
1718         [DataContract (Namespace = "")]
1719         public class DCEmptyNoNS
1720         {
1721         }
1722
1723         [DataContract]
1724         public class DCSimple1
1725         {
1726                 [DataMember]
1727                 public string Foo = "TEST";
1728         }
1729
1730         [DataContract]
1731         public class DCHasNonDC
1732         {
1733                 [DataMember]
1734                 public NonDC Hoge= new NonDC ();
1735         }
1736
1737         public class NonDC
1738         {
1739                 public string Whee = "whee!";
1740         }
1741
1742         [DataContract]
1743         public class DCHasSerializable
1744         {
1745                 [DataMember]
1746                 public SimpleSer1 Ser = new SimpleSer1 ();
1747         }
1748
1749         [DataContract (Name = "Foo")]
1750         public class DCWithName
1751         {
1752                 [DataMember (Name = "FooMember")]
1753                 public string DMWithName = "value";
1754         }
1755
1756         [DataContract (Name = "")]
1757         public class DCWithEmptyName
1758         {
1759         }
1760
1761         [DataContract (Name = null)]
1762         public class DCWithNullName
1763         {
1764         }
1765
1766         [DataContract (Namespace = "")]
1767         public class DCWithEmptyNamespace
1768         {
1769         }
1770
1771         [Serializable]
1772         public class SimpleSer1
1773         {
1774                 public string Doh = "doh!";
1775                 [NonSerialized]
1776                 public string Bah = "bah!";
1777         }
1778
1779         public class Wrapper
1780         {
1781                 [DataContract]
1782                 public class DCWrapped
1783                 {
1784                 }
1785         }
1786
1787         [DataContract]
1788         public class CollectionContainer
1789         {
1790                 Collection<string> items = new Collection<string> ();
1791
1792                 [DataMember]
1793                 public Collection<string> Items {
1794                         get { return items; }
1795                 }
1796         }
1797
1798         [CollectionDataContract]
1799         public class DataCollection<T> : Collection<T>
1800         {
1801         }
1802
1803         [DataContract]
1804         public class DataCollectionContainer
1805         {
1806                 DataCollection<string> items = new DataCollection<string> ();
1807
1808                 [DataMember]
1809                 public DataCollection<string> Items {
1810                         get { return items; }
1811                 }
1812         }
1813
1814         [DataContract]
1815         class SerializeNonDCArrayType
1816         {
1817                 [DataMember]
1818                 public NonDCItem [] IPAddresses = new NonDCItem [0];
1819         }
1820
1821         public class NonDCItem
1822         {
1823                 public int [] Data { get; set; }
1824         }
1825
1826         [DataContract]
1827         public class VerifyObjectNameTestData
1828         {
1829                 [DataMember]
1830                 string Member1 = "foo";
1831
1832                 public string GetMember() { return Member1; }
1833         }
1834
1835         [XmlRoot(ElementName = "simple", Namespace = "")]
1836         public class SimpleXml : IXmlSerializable 
1837         {
1838                 void IXmlSerializable.ReadXml (XmlReader reader)
1839                 {
1840                 }
1841
1842                 void IXmlSerializable.WriteXml (XmlWriter writer)
1843                 {
1844                 }
1845
1846                 XmlSchema IXmlSerializable.GetSchema ()
1847                 {
1848                         return null;
1849                 }
1850
1851         }
1852
1853         [DataContract]
1854         public class ReferenceWrapper
1855         {
1856                 [DataMember (Order = 1)]
1857                 public ReferenceType T;
1858
1859                 [DataMember (Order = 2)]
1860                 public ReferenceType T2;
1861         }
1862
1863         [DataContract (IsReference = true)]
1864         public class ReferenceType
1865         {
1866                 [DataMember]
1867                 public string F = "x";
1868         }
1869
1870         public class MyList : IList<string>
1871         {
1872                 List<string> l = new List<string> ();
1873                 public void Clear () { l.Clear (); }
1874                 public void Add(string s) { l.Add (s);}
1875                 public void Insert(int idx, string s) { l.Insert(idx,s);}
1876                 public bool Contains(string s) { return l.Contains(s); }
1877                 public IEnumerator<string> GetEnumerator () { return l.GetEnumerator (); }
1878                 IEnumerator IEnumerable.GetEnumerator () { return l.GetEnumerator (); }
1879                 public bool Remove(string s) { return l.Remove(s); }
1880                 public void RemoveAt(int i) { l.RemoveAt (i);}
1881                 public void CopyTo (string [] arr, int index) { l.CopyTo (arr, index);}
1882                 public int IndexOf (string s) { return l.IndexOf (s); }
1883         
1884                 public int Count { get { return l.Count; } }
1885                 public bool IsReadOnly { get { return ((IList<string>) l).IsReadOnly; } }
1886                 public string this [int index] { get { return l [index]; } set { l [index] = value; } }
1887         }
1888
1889         [DataContract]
1890         internal class InterfaceCollectionType
1891         {
1892                 [DataMember]
1893                 public IList<int> Array { get; set; }
1894         }
1895
1896         [DataContract]
1897         public class NestedContractType
1898         {
1899                 [DataMember]
1900                 public NestedContractType Nested;
1901                 [DataMember]
1902                 public string X = "x";
1903         }
1904
1905         class BaseConstraintType1 // non-serializable
1906         {
1907         }
1908         
1909         [Serializable]
1910         class BaseConstraintType2
1911         {
1912         }
1913         
1914         [DataContract]
1915         class BaseConstraintType3 : BaseConstraintType1
1916         {
1917         }
1918         
1919         [DataContract]
1920         class BaseConstraintType4 : BaseConstraintType2
1921         {
1922         }
1923
1924         [DataContract (Namespace = "urn:foo")]
1925         public class XmlDocumentContract
1926         {
1927                 [DataMember (Name = "Content")]
1928                 private XmlElement content;
1929
1930                 public XmlElement Content {
1931                         get { return content; }
1932                         set { content = value; }
1933                 }
1934
1935                 [DataMember (Name = "Nodes")]
1936                 private XmlNode [] nodes;
1937
1938                 public XmlNode [] Nodes {
1939                         get { return nodes; }
1940                         set { nodes = value; }
1941                 }
1942         }
1943 }
1944
1945 [DataContract]
1946 class GlobalSample1
1947 {
1948 }
1949
1950 [DataContract]
1951 class Foo<X,Y,Z>
1952 {
1953         [DataMember]
1954         public X Field;
1955 }
1956
1957 [CollectionDataContract (Name = "NAME", Namespace = "urn:foo", ItemName = "ITEM", KeyName = "KEY", ValueName = "VALUE")]
1958 public class MyDictionary<K,V> : Dictionary<K,V>
1959 {
1960 }
1961
1962 // bug #524086
1963 [DataContract(Namespace="http://example.com/schemas/asx")]
1964 public class DummyEntry
1965 {
1966     [DataMember]
1967     public DummyEntryInfo EntryInfo { get; set; }
1968     [DataMember]
1969     public string Href { get; set; }
1970 }
1971
1972 [DataContract(Namespace="http://example.com/schemas/asx"),
1973 KnownType(typeof(PartDummyEntryInfo))]
1974 public abstract class DummyEntryInfo
1975 {
1976 }
1977
1978 [DataContract(Namespace="http://example.com/schemas/asx")]
1979 public class DummyPlaylist
1980 {
1981     public IList<DummyEntry> entries = new List<DummyEntry> ();
1982
1983     [DataMember]
1984     public IList<DummyEntry> Entries { get { return entries; } set {entries = value;} }
1985 }
1986
1987 [DataContract(Namespace="http://example.com/schemas/asx")]
1988 public class PartDummyEntryInfo : DummyEntryInfo
1989 {
1990     public PartDummyEntryInfo() {}
1991 }
1992
1993 // bug #524088
1994
1995 [DataContract(Namespace="http://example.com/schemas/asx")]
1996 public class AsxEntryInfo
1997 {
1998     [DataMember]
1999     public string AdvertPrompt { get; set; }
2000 }
2001
2002 // bug #560155
2003
2004 [DataContract]
2005 public class Person
2006 {
2007         [DataMember]
2008         readonly public string name;
2009         [DataMember]
2010         readonly public Guid Id = Guid.Empty;
2011
2012         public Person (string nameIn, Guid idIn)
2013         {
2014                 name = nameIn;
2015                 Id = idIn;
2016         }
2017
2018         public override string ToString()
2019         {
2020                 return string.Format ("name={0},id={1}", name, Id);
2021         }
2022 }
2023
2024 // bug #599889
2025 namespace U2U.DataContracts
2026 {
2027         [DataContract]
2028         public class Person
2029         {
2030                 [DataMember]
2031                 public string Name { get; set; }
2032
2033                 [DataMember]
2034                 public Job Occupation { get; set; }
2035         }
2036
2037         [DataContract]
2038         public class Job
2039         {
2040                 [DataMember]
2041                 public string Description { get; set; }
2042         }
2043 }
2044
2045 #region bug #610036
2046 //parent class with a name property
2047 [DataContract (Namespace = "Company.Foo")]
2048 public abstract class ParentClass
2049 {
2050         
2051         //constructor
2052         public ParentClass (string name)
2053         {
2054                 Name = name;
2055         }
2056         
2057         //the name
2058         [DataMember]
2059         public string Name{ get; set; }
2060
2061 }
2062
2063 //root object
2064 [DataContract (Namespace = "Company.Foo")]
2065 public class Root : ParentClass
2066 {
2067         //dict
2068         [DataMember]
2069         public Dict<Foo> FDict; 
2070         
2071         //constructor
2072         public Root (string name)
2073                 : base (name)
2074         {
2075                 FDict = new Dict<Foo> ();
2076         }
2077 }
2078
2079
2080 //subclass
2081 [DataContract (Namespace = "Company.Foo")]
2082 public class Foo : ParentClass
2083 {
2084         //here is one dict
2085         [DataMember]
2086         public Dict<Bar> FDict;
2087         
2088         //constructor
2089         public Foo (string name) 
2090                 : base (name)
2091         {
2092                 FDict = new Dict<Bar> ();
2093         }
2094         
2095 }
2096
2097 //another sublass
2098 [DataContract (Namespace = "Company.Foo")]
2099 public class Bar : ParentClass
2100 {
2101         //constructor
2102         public Bar (string name)
2103                 : base (name)
2104         {
2105         }
2106         
2107 }
2108 //the custom dictionary
2109 [CollectionDataContract (ItemName = "DictItem", Namespace = "Company.Foo")]
2110 public class Dict<T> : Dictionary<string, T> where T : ParentClass
2111 {
2112         public void Add (T item)
2113         {
2114                 Add (item.Name, item);
2115         }
2116         
2117 }
2118
2119 [DataContract (IsReference = true)]
2120 public class Parent
2121 {
2122         //constructor
2123         public Parent ()
2124         {
2125                 Child = new Child (this);
2126         }
2127
2128         [DataMember]
2129         public Child Child;
2130 }
2131
2132 [DataContract]
2133 public class Child
2134 {
2135         public Child ()
2136         {
2137         }
2138         
2139         public Child (Parent parent)
2140         {
2141                 this.Parent = parent;
2142         }
2143         
2144         [DataMember]
2145         public Parent Parent;
2146 }
2147
2148 namespace SLProto5
2149 {
2150         public class CashAmount : Amount
2151         {
2152         }
2153
2154         [DataContract]
2155         public class CashAmountDC : AmountDC
2156         {
2157         }
2158
2159         public class Amount
2160         {
2161                 public string Currency { get; set; }
2162                 public string Description { get; set; }
2163         }
2164
2165         [DataContract]
2166         public class AmountDC
2167         {
2168                 [DataMember]
2169                 public string Currency { get; set; }
2170                 [DataMember]
2171                 public string Description { get; set; }
2172         }
2173 }
2174
2175 namespace SLProto5_Different
2176 {
2177         public class CashAmount : SLProto5.Amount
2178         {
2179         }
2180
2181         [DataContract]
2182         public class CashAmountDC : SLProto5.AmountDC
2183         {
2184         }
2185 }
2186
2187 #endregion