1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
//
// System.Xml.XmlSerializerTests
//
// Author:
// Erik LeBel <eriklebel@yahoo.ca>
//
// (C) 2003 Erik LeBel
//
//
// NOTES:
// Where possible, these tests avoid testing the order of
// an object's members serialization. Mono and .NET do not
// reflect members in the same order.
//
// Only serializations tests so far, no deserialization.
//
// FIXME
// test XmlArrayAttribute
// test XmlArrayItemAttribute
// test serialization of decimal type
// test serialization of Guid type
// test XmlNode serialization with and without modifying attributes.
// test deserialization
// FIXMEs found in this file
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using NUnit.Framework;
using MonoTests.System.Xml.TestClasses;
namespace MonoTests.System.XmlSerialization
{
[TestFixture]
public class XmlSerializerTests : Assertion
{
StringWriter sw;
XmlTextWriter xtw;
XmlSerializer xs;
private void SetUpWriter()
{
sw = new StringWriter ();
xtw = new XmlTextWriter (sw);
xtw.QuoteChar = '\'';
xtw.Formatting = Formatting.None;
}
private string WriterText
{
get
{
string val = sw.GetStringBuilder().ToString();
int offset = val.IndexOf('>') + 1;
val = val.Substring(offset);
return Infoset(val);
}
}
private void Serialize(object o)
{
SetUpWriter();
xs = new XmlSerializer(o.GetType());
xs.Serialize(xtw, o);
}
private void Serialize(object o, Type type)
{
SetUpWriter();
xs = new XmlSerializer(type);
xs.Serialize(xtw, o);
}
private void Serialize(object o, XmlSerializerNamespaces ns)
{
SetUpWriter();
xs = new XmlSerializer(o.GetType());
xs.Serialize(xtw, o, ns);
}
private void Serialize(object o, XmlAttributeOverrides ao)
{
SetUpWriter();
xs = new XmlSerializer(o.GetType(), ao);
xs.Serialize(xtw, o);
}
private void Serialize(object o, XmlRootAttribute root)
{
SetUpWriter();
xs = new XmlSerializer(o.GetType(), root);
xs.Serialize(xtw, o);
}
// test constructors
#if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
[Test]
public void TestConstructor()
{
XmlSerializer ser = new XmlSerializer (null, "");
}
#else
#endif
// test basic types ////////////////////////////////////////////////////////
[Test]
public void TestSerializeInt()
{
Serialize(10);
AssertEquals(Infoset("<int>10</int>"), WriterText);
}
[Test]
public void TestSerializeBool()
{
Serialize(true);
AssertEquals(Infoset("<boolean>true</boolean>"), WriterText);
Serialize(false);
AssertEquals(Infoset("<boolean>false</boolean>"), WriterText);
}
[Test]
public void TestSerializeString()
{
Serialize("hello");
AssertEquals(Infoset("<string>hello</string>"), WriterText);
}
[Test]
public void TestSerializeEmptyString()
{
Serialize(String.Empty);
AssertEquals(Infoset("<string />"), WriterText);
}
[Test]
public void TestSerializeNullObject()
{
Serialize(null, typeof(object));
AssertEquals(Infoset("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
}
[Test]
[Ignore ("The generated XML is not exact but it is equivalent")]
public void TestSerializeNullString()
{
Serialize(null, typeof(string));
Console.WriteLine (WriterText);
AssertEquals (Infoset("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
}
[Test]
public void TestSerializeIntArray()
{
Serialize(new int[] {1, 2, 3, 4});
AssertEquals (Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);
}
[Test]
public void TestSerializeEmptyArray()
{
Serialize(new int[] {});
AssertEquals(Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
}
[Test]
public void TestSerializeChar()
{
Serialize('A');
AssertEquals(Infoset("<char>65</char>"), WriterText);
Serialize('\0');
AssertEquals(Infoset("<char>0</char>"), WriterText);
Serialize('\n');
AssertEquals(Infoset("<char>10</char>"), WriterText);
Serialize('\uFF01');
AssertEquals(Infoset("<char>65281</char>"), WriterText);
}
[Test]
public void TestSerializeFloat()
{
Serialize(10.78);
AssertEquals(Infoset("<double>10.78</double>"), WriterText);
Serialize(-1e8);
AssertEquals(Infoset("<double>-100000000</double>"), WriterText);
// FIXME test INF and other boundary conditions that may exist with floats
}
[Test]
public void TestSerializeEnumeration()
{
Serialize(SimpleEnumeration.FIRST);
AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
Serialize(SimpleEnumeration.SECOND);
AssertEquals(Infoset("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);
}
[Test]
public void TestSerializeEnumDefaultValue() {
Serialize(new EnumDefaultValue());
AssertEquals(Infoset("<EnumDefaultValue />"), WriterText);
Serialize(new EnumDefaultValueNF());
AssertEquals(Infoset("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
Serialize(new SimpleEnumeration());
AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
}
[Test]
public void TestSerializeQualifiedName()
{
Serialize(new XmlQualifiedName("me", "home.urn"));
AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
}
[Test]
public void TestSerializeBytes()
{
Serialize((byte)0xAB);
AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);
Serialize((byte)15);
AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);
}
[Test]
public void TestSerializeByteArrays()
{
Serialize(new byte[] {});
AssertEquals(Infoset("<base64Binary />"), WriterText);
Serialize(new byte[] {0xAB, 0xCD});
AssertEquals(Infoset("<base64Binary>q80=</base64Binary>"), WriterText);
}
[Test]
public void TestSerializeDateTime()
{
DateTime d = new DateTime();
Serialize(d);
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan off = tz.GetUtcOffset (d);
string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
}
/*
FIXME
- decimal
- Guid
- XmlNode objects
[Test]
public void TestSerialize()
{
Serialize();
AssertEquals(WriterText, "");
}
*/
// test basic class serialization /////////////////////////////////////
[Test]
public void TestSerializeSimpleClass()
{
SimpleClass simple = new SimpleClass();
Serialize(simple);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
simple.something = "hello";
Serialize(simple);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
}
[Test]
public void TestSerializeStringCollection()
{
StringCollection strings = new StringCollection();
Serialize(strings);
AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
strings.Add("hello");
strings.Add("goodbye");
Serialize(strings);
AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
}
[Test]
public void TestSerializePlainContainer()
{
StringCollectionContainer container = new StringCollectionContainer();
Serialize(container);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
container.Messages.Add("hello");
container.Messages.Add("goodbye");
Serialize(container);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
}
[Test]
public void TestSerializeArrayContainer()
{
ArrayContainer container = new ArrayContainer();
Serialize(container);
AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
container.items = new object[] {10, 20};
Serialize(container);
AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"),WriterText);
container.items = new object[] {10, "hello"};
Serialize(container);
AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"),WriterText);
}
[Test]
public void TestSerializeClassArrayContainer()
{
ClassArrayContainer container = new ClassArrayContainer();
Serialize(container);
AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
SimpleClass simple1 = new SimpleClass();
simple1.something = "hello";
SimpleClass simple2 = new SimpleClass();
simple2.something = "hello";
container.items = new SimpleClass[2];
container.items[0] = simple1;
container.items[1] = simple2;
Serialize(container);
AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"),WriterText);
}
// test basic attributes ///////////////////////////////////////////////
[Test]
public void TestSerializeSimpleClassWithXmlAttributes()
{
SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
Serialize(simple);
AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
simple.something = "hello";
Serialize(simple);
AssertEquals (Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
}
// test overrides ///////////////////////////////////////////////////////
[Test]
public void TestSerializeSimpleClassWithOverrides()
{
// Also tests XmlIgnore
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
attr.XmlIgnore = true;
overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
}
[Test]
public void TestSerializeSchema ()
{
XmlSchema schema = new XmlSchema ();
schema.Items.Add (new XmlSchemaAttribute ());
schema.Items.Add (new XmlSchemaAttributeGroup ());
schema.Items.Add (new XmlSchemaComplexType ());
schema.Items.Add (new XmlSchemaNotation ());
schema.Items.Add (new XmlSchemaSimpleType ());
schema.Items.Add (new XmlSchemaGroup ());
schema.Items.Add (new XmlSchemaElement ());
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.QuoteChar = '\'';
xtw.Formatting = Formatting.Indented;
XmlSerializer xs = new XmlSerializer (schema.GetType ());
xs.Serialize (xtw, schema);
AssertEquals (string.Format(CultureInfo.InvariantCulture,
"<?xml version='1.0' encoding='utf-16'?>{0}" +
"<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
" <xsd:attribute />{0}" +
" <xsd:attributeGroup />{0}" +
" <xsd:complexType />{0}" +
" <xsd:notation />{0}" +
" <xsd:simpleType />{0}" +
" <xsd:group />{0}" +
" <xsd:element />{0}" +
"</xsd:schema>", Environment.NewLine), sw.ToString ());
}
// test xmlText //////////////////////////////////////////////////////////
[Test]
public void TestSerializeXmlTextAttribute()
{
SimpleClass simple = new SimpleClass();
simple.something = "hello";
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
overrides.Add(typeof(SimpleClass), "something", attr);
attr.XmlText = new XmlTextAttribute();
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
attr.XmlText = new XmlTextAttribute(typeof(string));
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
try
{
attr.XmlText = new XmlTextAttribute(typeof(byte[]));
Serialize(simple, overrides);
Fail("XmlText.Type does not match the type it serializes: this should have failed");
}
catch (Exception)
{
}
try
{
attr.XmlText = new XmlTextAttribute();
attr.XmlText.DataType = "sometype";
Serialize(simple, overrides);
Fail("XmlText.DataType does not match the type it serializes: this should have failed");
}
catch (Exception)
{
}
}
// test xmlRoot //////////////////////////////////////////////////////////
[Test]
public void TestSerializeXmlRootAttribute()
{
// constructor override & element name
XmlRootAttribute root = new XmlRootAttribute();
root.ElementName = "renamed";
SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
Serialize(simpleWithAttributes, root);
AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
SimpleClass simple = null;
root.IsNullable = false;
try
{
Serialize(simple, root);
Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
}
catch (Exception)
{
}
root.IsNullable = true;
try
{
Serialize(simple, root);
Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
}
catch (Exception)
{
}
simple = new SimpleClass();
root.ElementName = null;
root.Namespace = "some.urn";
Serialize(simple, root);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
}
[Test]
public void TestSerializeXmlRootAttributeOnMember()
{
// nested root
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes childAttr = new XmlAttributes();
childAttr.XmlRoot = new XmlRootAttribute("simple");
overrides.Add(typeof(SimpleClass), childAttr);
XmlAttributes attr = new XmlAttributes();
attr.XmlRoot = new XmlRootAttribute("simple");
overrides.Add(typeof(ClassArrayContainer), attr);
ClassArrayContainer container = new ClassArrayContainer();
container.items = new SimpleClass[1];
container.items[0] = new SimpleClass();
Serialize(container, overrides);
AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"),WriterText);
// FIXME test data type
}
// test XmlAttribute /////////////////////////////////////////////////////
[Test]
public void TestSerializeXmlAttributeAttribute()
{
// null
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
attr.XmlAttribute = new XmlAttributeAttribute();
overrides.Add(typeof(SimpleClass), "something", attr);
SimpleClass simple = new SimpleClass();;
Serialize(simple, overrides);
AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// regular
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);
// AttributeName
attr.XmlAttribute.AttributeName = "somethingelse";
Serialize(simple, overrides);
AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);
// Type
// FIXME this should work, shouldnt it?
// attr.XmlAttribute.Type = typeof(string);
// Serialize(simple, overrides);
// Assert(WriterText.EndsWith(" something='hello' />"));
// Namespace
attr.XmlAttribute.Namespace = "some:urn";
Serialize(simple, overrides);
AssertEquals ("#4", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText);
// FIXME DataType
// FIXME XmlSchemaForm Form
// FIXME write XmlQualifiedName as attribute
}
// test XmlElement ///////////////////////////////////////////////////////
[Test]
public void TestSerializeXmlElementAttribute()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
XmlElementAttribute element = new XmlElementAttribute();
attr.XmlElements.Add(element);
overrides.Add(typeof(SimpleClass), "something", attr);
// null
SimpleClass simple = new SimpleClass();;
Serialize(simple, overrides);
AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// not null
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
//ElementName
element.ElementName = "saying";
Serialize(simple, overrides);
AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);
//IsNullable
element.IsNullable = false;
simple.something = null;
Serialize(simple, overrides);
AssertEquals("#4", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
element.IsNullable = true;
simple.something = null;
Serialize(simple, overrides);
AssertEquals ("#5", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText);
//Namespace
element.ElementName = null;
element.IsNullable = false;
element.Namespace = "some:urn";
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals ("#6", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText);
//FIXME DataType
//FIXME Form
//FIXME Type
}
// test XmlElementAttribute with arrays and collections //////////////////
[Test]
public void TestSerializeCollectionWithXmlElementAttribute()
{
// the rule is:
// if no type is specified or the specified type
// matches the contents of the collection,
// serialize each element in an element named after the member.
// if the type does not match, or matches the collection itself,
// create a base wrapping element for the member, and then
// wrap each collection item in its own wrapping element based on type.
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
XmlElementAttribute element = new XmlElementAttribute();
attr.XmlElements.Add(element);
overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
// empty collection & no type info in XmlElementAttribute
StringCollectionContainer container = new StringCollectionContainer();
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// non-empty collection & no type info in XmlElementAttribute
container.Messages.Add("hello");
Serialize(container, overrides);
AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
// non-empty collection & only type info in XmlElementAttribute
element.Type = typeof(StringCollection);
Serialize(container, overrides);
AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText);
// non-empty collection & only type info in XmlElementAttribute
element.Type = typeof(string);
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
// two elements
container.Messages.Add("goodbye");
element.Type = null;
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText);
}
// test DefaultValue /////////////////////////////////////////////////////
[Test]
public void TestSerializeDefaultValueAttribute()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
string defaultValueInstance = "nothing";
attr.XmlDefaultValue = defaultValueInstance;
overrides.Add(typeof(SimpleClass), "something", attr);
// use the default
SimpleClass simple = new SimpleClass();
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// same value as default
simple.something = defaultValueInstance;
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// some other value
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
}
// test XmlEnum //////////////////////////////////////////////////////////
[Test]
public void TestSerializeXmlEnumAttribute()
{
// technically this has an XmlIgnore attribute,
// but it is not being serialized as a member.
Serialize(XmlSchemaForm.None);
AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
Serialize(XmlSchemaForm.Qualified);
AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
Serialize(XmlSchemaForm.Unqualified);
AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
}
[Test]
public void TestSerializeXmlNodeArray ()
{
XmlDocument doc = new XmlDocument ();
Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
AssertEquals(Infoset("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
}
[Test]
public void TestSerializeXmlElement ()
{
XmlDocument doc = new XmlDocument ();
Serialize (doc.CreateElement("elem"), typeof(XmlElement));
AssertEquals(Infoset("<elem/>"), WriterText);
}
[Test]
public void TestSerializeXmlElementSubclass ()
{
XmlDocument doc = new XmlDocument ();
Serialize (new MyElem (doc), typeof(XmlElement));
AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
Serialize (new MyElem (doc), typeof(MyElem));
AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
}
[Test]
public void TestSerializeXmlCDataSection ()
{
XmlDocument doc = new XmlDocument ();
CDataContainer c = new CDataContainer ();
c.cdata = doc.CreateCDataSection("data section contents");
Serialize (c);
AssertEquals(Infoset("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
}
[Test]
public void TestSerializeXmlNode ()
{
XmlDocument doc = new XmlDocument ();
NodeContainer c = new NodeContainer ();
c.node = doc.CreateTextNode("text");
Serialize (c);
AssertEquals(Infoset("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
}
[Test]
public void TestSerializeChoice ()
{
Choices ch = new Choices ();
ch.MyChoice = "choice text";
ch.ItemType = ItemChoiceType.ChoiceZero;
Serialize (ch);
AssertEquals(Infoset("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText);
ch.ItemType = ItemChoiceType.StrangeOne;
Serialize (ch);
AssertEquals(Infoset("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText);
ch.ItemType = ItemChoiceType.ChoiceTwo;
Serialize (ch);
AssertEquals(Infoset("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText);
}
[Test]
public void TestSerializeNamesWithSpaces ()
{
TestSpace ts = new TestSpace();
ts.elem = 4;
ts.attr = 5;
Serialize (ts);
AssertEquals(Infoset("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
}
[Test]
public void TestSerializeReadOnlyProps ()
{
ReadOnlyProperties ts = new ReadOnlyProperties();
Serialize (ts);
AssertEquals(Infoset("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
}
[Test]
public void TestSerializeIList()
{
clsPerson k = new clsPerson();
k.EmailAccounts = new ArrayList();
k.EmailAccounts.Add("a");
k.EmailAccounts.Add("b");
Serialize (k);
AssertEquals (Infoset("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
}
[Test]
public void TestSerializeArrayEnc ()
{
SoapReflectionImporter imp = new SoapReflectionImporter ();
XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
XmlSerializer ser = new XmlSerializer (map);
StringWriter sw = new StringWriter ();
XmlTextWriter tw = new XmlTextWriter (sw);
tw.WriteStartElement ("aa");
ser.Serialize (tw, new ArrayClass ());
tw.WriteEndElement ();
}
[Test]
public void TestIncludeType()
{
// Test for bug #76049
XmlReflectionImporter imp = new XmlReflectionImporter ();
XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
imp.IncludeType (typeof(TestSpace));
XmlSerializer ser = new XmlSerializer (map);
ser.Serialize (new StringWriter (), new TestSpace ());
}
[Test]
public void TestSerializeChoiceArray()
{
CompositeValueType v = new CompositeValueType ();
v.Init ();
Serialize (v);
AssertEquals (Infoset("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
}
[Test]
public void TestArrayAttributeWithDataType ()
{
Serialize (new ArrayAttributeWithType ());
string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
AssertEquals (Infoset(res), WriterText);
}
[Test]
[ExpectedException (typeof(InvalidOperationException))]
public void TestArrayAttributeWithWrongDataType ()
{
Serialize (new ArrayAttributeWithWrongType ());
}
// Helper methods
public static string Infoset (string sx)
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (sx);
StringBuilder sb = new StringBuilder ();
GetInfoset (doc.DocumentElement, sb);
return sb.ToString ();
}
public static string Infoset (XmlNode nod)
{
StringBuilder sb = new StringBuilder ();
GetInfoset (nod, sb);
return sb.ToString ();
}
static void GetInfoset (XmlNode nod, StringBuilder sb)
{
switch (nod.NodeType)
{
case XmlNodeType.Attribute:
if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
break;
case XmlNodeType.Element:
XmlElement elem = (XmlElement) nod;
sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
ArrayList ats = new ArrayList ();
foreach (XmlAttribute at in elem.Attributes)
ats.Add (at.LocalName + " " + at.NamespaceURI);
ats.Sort ();
foreach (string name in ats)
{
string[] nn = name.Split (' ');
GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
}
sb.Append (">");
foreach (XmlNode cn in elem.ChildNodes)
GetInfoset (cn, sb);
sb.Append ("</>");
break;
default:
sb.Append (nod.OuterXml);
break;
}
}
}
}
|