2 // System.Xml.XmlSerializerTestClasses
5 // Erik LeBel <eriklebel@yahoo.ca>
6 // Hagit Yidov <hagity@mainsoft.com>
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Classes to use in the testing of the XmlSerializer
15 using System.Collections;
16 using System.Collections.Generic;
17 using System.ComponentModel;
19 using System.Xml.Schema;
20 using System.Xml.Serialization;
22 namespace MonoTests.System.Xml.TestClasses
24 public enum SimpleEnumeration { FIRST, SECOND };
27 public enum EnumDefaultValue { e1 = 1, e2 = 2, e3 = 3 }
28 public enum EnumDefaultValueNF { e1 = 1, e2 = 2, e3 = 3 }
42 [SoapType ("flagenum")]
43 public enum FlagEnum_Encoded
54 public enum ZeroFlagEnum
60 [XmlEnum ("tns:t<w>o")]
67 #region GenericsTestClasses
69 public class GenSimpleClass<T>
71 public T something = default (T);
74 public struct GenSimpleStruct<T>
77 public GenSimpleStruct (int dummy)
79 something = default (T);
83 public class GenListClass<T>
85 public List<T> somelist = new List<T> ();
88 public class GenArrayClass<T>
90 public T[] arr = new T[3];
93 public class GenTwoClass<T1, T2>
95 public T1 something1 = default (T1);
96 public T2 something2 = default (T2);
99 public class GenDerivedClass<T1, T2> : GenTwoClass<string, int>
101 public T1 another1 = default (T1);
102 public T2 another2 = default (T2);
105 public class GenDerived2Class<T1, T2> : GenTwoClass<T1, T2>
107 public T1 another1 = default (T1);
108 public T2 another2 = default (T2);
111 public class GenNestedClass<TO, TI>
113 public TO outer = default (TO);
114 public class InnerClass<T>
116 public TI inner = default (TI);
117 public T something = default (T);
121 public struct GenComplexStruct<T1, T2>
124 public GenSimpleClass<T1> simpleclass;
125 public GenSimpleStruct<T1> simplestruct;
126 public GenListClass<T1> listclass;
127 public GenArrayClass<T1> arrayclass;
128 public GenTwoClass<T1, T2> twoclass;
129 public GenDerivedClass<T1, T2> derivedclass;
130 public GenDerived2Class<T1, T2> derived2;
131 public GenNestedClass<T1, T2> nestedouter;
132 public GenNestedClass<T1, T2>.InnerClass<T1> nestedinner;
133 public GenComplexStruct (int dummy)
135 something = default (T1);
136 simpleclass = new GenSimpleClass<T1> ();
137 simplestruct = new GenSimpleStruct<T1> ();
138 listclass = new GenListClass<T1> ();
139 arrayclass = new GenArrayClass<T1> ();
140 twoclass = new GenTwoClass<T1, T2> ();
141 derivedclass = new GenDerivedClass<T1, T2> ();
142 derived2 = new GenDerived2Class<T1, T2> ();
143 nestedouter = new GenNestedClass<T1, T2> ();
144 nestedinner = new GenNestedClass<T1, T2>.InnerClass<T1> ();
148 public class WithNulls
150 [XmlElement (IsNullable=true)]
153 [XmlElement (IsNullable=true)]
154 public TestEnumWithNulls? nenum;
156 [XmlElement (IsNullable=true)]
157 public DateTime? ndate;
160 public enum TestEnumWithNulls
167 #endregion // GenericsTestClasses
169 public class SimpleClass
171 public string something = null;
174 public class StringCollection : CollectionBase
176 public void Add (String parameter)
178 List.Insert (Count, parameter);
181 public String this[int index]
185 if (index < 0 || index > Count)
186 throw new ArgumentOutOfRangeException ();
188 return (String) List[index];
190 set { List[index] = value; }
194 public class StringCollectionContainer
196 StringCollection messages = new StringCollection ();
198 public StringCollection Messages
200 get { return messages; }
204 public class ArrayContainer
206 public object[] items = null;
209 public class ClassArrayContainer
211 public SimpleClass[] items = null;
215 public class SimpleClassWithXmlAttributes
217 [XmlAttribute ("member")]
218 public string something = null;
224 [XmlAttribute ("flag1")]
226 public FlagEnum Flags1;
228 [XmlAttribute ("flag2")]
229 [DefaultValue (FlagEnum.e1)]
230 public FlagEnum Flags2;
232 [XmlAttribute ("flag3", Form = XmlSchemaForm.Qualified)]
233 [DefaultValue (FlagEnum.e1 | FlagEnum.e2)]
234 public FlagEnum Flags3;
236 [XmlAttribute ("flag4")]
237 public FlagEnum Flags4;
239 [XmlAttribute ("modifiers")]
240 public MapModifiers Modifiers;
242 [XmlAttribute ("modifiers2", Form = XmlSchemaForm.Unqualified)]
243 public MapModifiers Modifiers2;
245 [XmlAttribute ("modifiers3")]
247 public MapModifiers Modifiers3;
249 [XmlAttribute ("modifiers4", Form = XmlSchemaForm.Unqualified)]
250 [DefaultValue (MapModifiers.Protected)]
251 public MapModifiers Modifiers4;
253 [XmlAttribute ("modifiers5", Form = XmlSchemaForm.Qualified)]
254 [DefaultValue (MapModifiers.Public)]
255 public MapModifiers Modifiers5;
257 [XmlAttribute ("names")]
258 public string[] Names;
260 [XmlAttribute ("street")]
261 public string Street;
264 [SoapType ("field", Namespace = "some:urn")]
265 public class Field_Encoded
267 [SoapAttribute ("flag1")]
268 [DefaultValue (FlagEnum_Encoded.e1)]
269 public FlagEnum_Encoded Flags1;
271 [SoapAttribute ("flag2")]
272 [DefaultValue (FlagEnum_Encoded.e1)]
273 public FlagEnum_Encoded Flags2;
275 [SoapAttribute ("flag3")]
276 [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2)]
277 public FlagEnum_Encoded Flags3;
279 [SoapAttribute ("flag4")]
280 public FlagEnum_Encoded Flags4;
282 [SoapAttribute ("modifiers")]
283 public MapModifiers Modifiers;
285 [SoapAttribute ("modifiers2")]
286 public MapModifiers Modifiers2;
288 [SoapAttribute ("modifiers3")]
289 [DefaultValue (MapModifiers.Public)]
290 public MapModifiers Modifiers3;
292 [SoapAttribute ("modifiers4")]
293 [DefaultValue (MapModifiers.Protected)]
294 public MapModifiers Modifiers4;
296 [SoapAttribute ("modifiers5")]
297 [DefaultValue (MapModifiers.Public)]
298 public MapModifiers Modifiers5;
300 public string[] Names;
302 [SoapAttribute ("street")]
303 public string Street;
307 public enum MapModifiers
310 [SoapEnum ("PuBlIc")]
312 [XmlEnum ("protected")]
316 public class MyList : ArrayList
320 // NOTE: MyList has no public constructor
321 public MyList (object container)
324 this.container = container;
328 public class Container
334 Items = new MyList (this);
338 public class Container2
346 public Container2 (bool b)
348 Items = new MyList (this);
352 public class MyElem : XmlElement
354 public MyElem (XmlDocument doc)
355 : base ("", "myelem", "", doc)
357 SetAttribute ("aa", "1");
364 public class MyDocument : XmlDocument
374 public class CDataContainer
376 public XmlCDataSection cdata;
379 public class NodeContainer
386 [XmlElementAttribute ("ChoiceZero", typeof (string), IsNullable = false)]
387 [XmlElementAttribute ("ChoiceOne", typeof (string), IsNullable = false)]
388 [XmlElementAttribute ("ChoiceTwo", typeof (string), IsNullable = false)]
389 [XmlChoiceIdentifier ("ItemType")]
390 public string MyChoice;
393 public ItemChoiceType ItemType;
396 [XmlType (IncludeInSchema = false)]
397 public enum ItemChoiceType
400 [XmlEnum ("ChoiceOne")]
405 public class WrongChoices
407 [XmlElementAttribute ("ChoiceZero", typeof (string), IsNullable = false)]
408 [XmlElementAttribute ("StrangeOne", typeof (string), IsNullable = false)]
409 [XmlElementAttribute ("ChoiceTwo", typeof (string), IsNullable = false)]
410 [XmlChoiceIdentifier ("ItemType")]
411 public string MyChoice;
414 public ItemChoiceType ItemType;
417 [XmlType ("Type with space")]
418 public class TestSpace
420 [XmlElement (ElementName = "Element with space")]
423 [XmlAttribute (AttributeName = "Attribute with space")]
428 public class ReadOnlyProperties
430 string[] strArr = new string[2] { "string1", "string2" };
431 List<string> strList = new List<string> { "listString1" };
433 public string[] StrArr
435 get { return strArr; }
440 get { return "fff"; }
443 public IList<string> StrList { get { return strList; } }
447 public class ReadOnlyListProperty {
448 List<string> strList = new List<string> { "listString1", "listString2" };
450 public List<string> StrList
452 get { return strList; }
458 public class ListDefaults
460 public ListDefaults ()
462 ed = new SimpleClass ();
466 public ArrayList list2;
470 public string[] list4;
472 [XmlElement ("e", typeof (SimpleClass))]
473 public ArrayList list5;
475 [DefaultValue (null)]
476 public SimpleClass ed;
478 [DefaultValue (null)]
482 public class clsPerson
484 public IList EmailAccounts;
487 public class ArrayClass
489 public object names = new object[] { "un", "dos" };
492 public class CompositeValueType
496 Items = new object[] { 1, 2 };
497 ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
500 [XmlElementAttribute ("Es", typeof (int))]
501 [XmlElementAttribute ("In", typeof (int))]
502 [XmlChoiceIdentifierAttribute ("ItemsElementName")]
503 public object[] Items;
505 [XmlElementAttribute ("ItemsElementName")]
506 [XmlIgnoreAttribute ()]
507 public ItemsChoiceType[] ItemsElementName;
510 public enum ItemsChoiceType
515 public class ArrayAttributeWithType
517 [XmlAttribute (DataType = "anyURI")]
518 public string[] at = new string[] { "a", "b" };
520 [XmlAttribute (DataType = "base64Binary")]
521 public byte[][] bin1 = new byte[][] { new byte[] { 1, 2 }, new byte[] { 1, 2 } };
523 [XmlAttribute (DataType = "base64Binary")]
524 public byte[] bin2 = new byte[] { 1, 2 };
527 public class ArrayAttributeWithWrongType
529 [XmlAttribute (DataType = "int")]
530 public string[] at = new string[] { "a", "b" };
533 [XmlType ("Container")]
534 public class EntityContainer
536 EntityCollection collection1;
537 EntityCollection collection2;
538 EntityCollection collection3 = new EntityCollection ("root");
539 EntityCollection collection4 = new EntityCollection ("root");
541 [XmlArray (IsNullable = true)]
542 public EntityCollection Collection1
544 get { return collection1; }
545 set { collection1 = value; collection1.Container = "assigned"; }
548 [XmlArray (IsNullable = false)]
549 public EntityCollection Collection2
551 get { return collection2; }
552 set { collection2 = value; collection2.Container = "assigned"; }
555 [XmlArray (IsNullable = true)]
556 public EntityCollection Collection3
558 get { return collection3; }
559 set { collection3 = value; collection3.Container = "assigned"; }
562 [XmlArray (IsNullable = false)]
563 public EntityCollection Collection4
565 get { return collection4; }
566 set { collection4 = value; collection4.Container = "assigned"; }
570 [XmlType ("Container")]
571 public class ArrayEntityContainer
573 Entity[] collection1;
574 Entity[] collection2;
575 Entity[] collection3 = new Entity[0];
576 Entity[] collection4 = new Entity[0];
578 [XmlArray (IsNullable = true)]
579 public Entity[] Collection1
581 get { return collection1; }
582 set { collection1 = value; }
585 [XmlArray (IsNullable = false)]
586 public Entity[] Collection2
588 get { return collection2; }
589 set { collection2 = value; }
592 [XmlArray (IsNullable = true)]
593 public Entity[] Collection3
595 get { return collection3; }
596 set { collection3 = value; }
599 [XmlArray (IsNullable = false)]
600 public Entity[] Collection4
602 get { return collection4; }
603 set { collection4 = value; }
609 private string _name = string.Empty;
610 private string _parent = null;
615 get { return _name; }
616 set { _name = value; }
622 get { return _parent; }
623 set { _parent = value; }
627 public class EntityCollection : ArrayList
629 public string _container;
631 public EntityCollection ()
635 public EntityCollection (string c)
640 public string Container
642 get { return _container; }
643 set { _container = value; }
646 public int Add (Entity value)
648 if (_container != null)
649 value.Parent = _container;
651 return base.Add (value);
654 public new Entity this[int index]
656 get { return (Entity) base[index]; }
657 set { base[index] = value; }
661 [XmlType ("Container")]
662 public class ObjectWithReadonlyCollection
664 EntityCollection collection1 = new EntityCollection ("root");
666 public EntityCollection Collection1
668 get { return collection1; }
672 [XmlType ("Container")]
673 public class ObjectWithReadonlyNulCollection
675 EntityCollection collection1;
677 public EntityCollection Collection1
679 get { return collection1; }
683 [XmlType ("Container")]
684 public class ObjectWithReadonlyArray
686 Entity[] collection1 = new Entity[0];
688 public Entity[] Collection1
690 get { return collection1; }
694 [XmlInclude (typeof (SubclassTestSub))]
695 public class SubclassTestBase
699 public class SubclassTestSub : SubclassTestBase
703 public class SubclassTestExtra
707 public class SubclassTestContainer
709 [XmlElement ("a", typeof (SubclassTestBase))]
710 [XmlElement ("b", typeof (SubclassTestExtra))]
714 public class DictionaryWithIndexer : DictionaryBase
716 public TimeSpan this[int index]
718 get { return TimeSpan.MinValue; }
721 public void Add (TimeSpan value)
726 [XmlRoot (Namespace = "some:urn")]
727 [SoapTypeAttribute (Namespace = "another:urn")]
728 public class PrimitiveTypesContainer
730 public PrimitiveTypesContainer ()
735 Password = new byte[] { 243, 15 };
736 PathSeparatorCharacter = '/';
742 public byte[] Password;
743 public char PathSeparatorCharacter;
746 public class TestSchemaForm1
748 public PrintTypeResponse p1;
750 [XmlElement (Namespace = "urn:oo")]
751 public PrintTypeResponse p2;
754 [XmlType (Namespace = "urn:testForm")]
755 public class TestSchemaForm2
757 public PrintTypeResponse p1;
759 [XmlElement (Namespace = "urn:oo")]
760 public PrintTypeResponse p2;
763 [XmlType (Namespace = "urn:responseTypes")]
764 public class PrintTypeResponse
766 [XmlElement (Form = XmlSchemaForm.Unqualified, IsNullable = true)]
767 public OutputType result;
768 public PrintTypeResponse intern;
772 result = new OutputType ();
773 result.data = "data1";
774 intern = new PrintTypeResponse ();
775 intern.result = new OutputType ();
776 intern.result.data = "data2";
780 [XmlType (Namespace = "urn:responseTypes")]
781 public class OutputType
784 [XmlElement (Form = XmlSchemaForm.Unqualified, IsNullable = true)]
788 [XmlRootAttribute ("testDefault", Namespace = "urn:myNS", IsNullable = false)]
789 [SoapType ("testDefault", Namespace = "urn:myNS")]
790 public class TestDefault
794 [DefaultValue ("Default Value")]
795 public string strDefault = "Default Value";
797 [DefaultValue (true)]
798 public bool boolT = true;
800 [DefaultValue (false)]
801 public bool boolF = false;
803 [DefaultValue (typeof (decimal), "10")]
804 public decimal decimalval = 10m;
806 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
807 public FlagEnum flag = (FlagEnum.e1 | FlagEnum.e4);
809 [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4)]
810 public FlagEnum_Encoded flagencoded = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4);
813 [XmlType ("optionalValueType", Namespace = "some:urn")]
814 [XmlRootAttribute ("optionalValue", Namespace = "another:urn", IsNullable = false)]
815 public class OptionalValueTypeContainer
817 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
818 public FlagEnum Attributes = FlagEnum.e1 | FlagEnum.e4;
820 [DefaultValue (FlagEnum.e1)]
821 public FlagEnum Flags = FlagEnum.e1;
825 public bool FlagsSpecified;
827 [DefaultValue (false)]
832 public bool IsEmptySpecified
834 get { return _isEmptySpecified; }
835 set { _isEmptySpecified = value; }
838 [DefaultValue (false)]
841 private bool _isEmptySpecified;
846 [SoapAttribute (Namespace = "http://www.cpandl.com")]
847 public string GroupName;
849 [SoapAttribute (DataType = "base64Binary")]
850 public Byte[] GroupNumber;
852 [SoapAttribute (DataType = "date", AttributeName = "CreationDate")]
853 public DateTime Today;
855 [SoapElement (DataType = "nonNegativeInteger", ElementName = "PosInt")]
856 public string PostitiveInt;
859 public bool IgnoreThis;
861 [DefaultValue (GroupType.B)]
862 public GroupType Grouptype;
863 public Vehicle MyVehicle;
865 [SoapInclude (typeof (Car))]
866 public Vehicle myCar (string licNumber)
869 if (licNumber == string.Empty) {
871 v.licenseNumber = "!!!!!!";
875 v.licenseNumber = licNumber;
881 [SoapInclude (typeof (Car))]
882 public abstract class Vehicle
884 public string licenseNumber;
885 [SoapElement (DataType = "date")]
886 public DateTime makeDate;
887 [DefaultValue ("450")]
888 public string weight;
891 public class Car : Vehicle
895 public enum GroupType
903 public class ErrorneousGetSchema : IXmlSerializable
905 public XmlSchema GetSchema ()
907 throw new ApplicationException ("unexpected");
910 public void ReadXml (XmlReader reader)
914 public void WriteXml (XmlWriter writer)
918 // it should be serialized IF it is NOT IXmlSerializable.
919 public string Whoa = "whoa";
922 [XmlRoot ("DefaultDateTimeContainer", Namespace = "urn:foo")]
923 public class DefaultDateTimeContainer // bug #378696
925 public DateTime SimpleDateTime;
927 [DefaultValue(typeof(DateTime), "2001-02-03T04:05:06")]
928 public DateTime FancyDateTime;
930 [DefaultValue (typeof (int), "123456")]
934 public class XmlSerializableImplicitConvertible
936 public BaseClass B = new DerivedClass ();
938 public class XmlSerializable : IXmlSerializable
940 public void WriteXml (XmlWriter writer)
944 public void ReadXml (XmlReader reader)
948 public XmlSchema GetSchema ()
954 public class BaseClass
956 public static implicit operator XmlSerializable (BaseClass b)
958 return new XmlSerializable ();
962 public static implicit operator BaseClass (XmlSerializable x)
964 return new BaseClass ();
968 public class DerivedClass : BaseClass
973 public class Bug704813Type
975 IEnumerable<string> foo = new List<string> ();
976 public IEnumerable<string> Foo {
981 public class Bug708178Type
983 List<string> foo = new List<string> ();
985 [XmlArray("Foo"), XmlArrayItem("Foo", typeof(string))]
986 public List<string> Foo {
992 public class ExplicitlyOrderedMembersType1
994 [XmlElement("child0", Order = 4)]
995 public string Child0;
997 [XmlElement("child", Order = 0)]
998 public string Child1;
1000 [XmlElement("child", Order = 2)]
1001 public string Child2;
1005 public class ExplicitlyOrderedMembersType2
1007 [XmlElement("child0", Order = 4)]
1008 public string Child0;
1010 [XmlElement("child")] // wrong. Needs to be Ordered as well.
1011 public string Child1;
1013 [XmlElement("child", Order = 2)]
1014 public string Child2;
1018 public class ExplicitlyOrderedMembersType3
1020 [XmlElement("child0", Order = 1)] // it's between 0 and 2. After two "child" elements, child0 is not recognized as this member.
1021 public string Child0;
1023 [XmlElement("child", Order = 0)]
1024 public string Child1;
1026 [XmlElement("child", Order = 2)]
1027 public string Child2;
1031 public class ExplicitlyOrderedMembersType4
1033 [XmlElement("child0", Order = 1)] // it's between 0 and 2. After two "child" elements, child0 is not recognized as this member.
1034 public string Child0;
1036 [XmlElement("child", Order = 0)]
1037 public string Child1;
1039 [XmlElement("child", Order = 2)]
1040 public string Child2;
1043 public string Child3;
1047 public class NullableDatesAndTimes {
1048 [XmlElementAttribute ("MyTime", DataType = "time", IsNullable = false)]
1049 public DateTime MyTime;
1051 [XmlElementAttribute ("MyTimeNullable", DataType = "time", IsNullable = true)]
1052 public DateTime? MyTimeNullable;
1054 [XmlElementAttribute ("MyDate", DataType = "date", IsNullable = false)]
1055 public DateTime MyDate;
1057 [XmlElementAttribute ("MyDateNullable", DataType = "date", IsNullable = true)]
1058 public DateTime? MyDateNullable;
1061 public class NotExactDateParseClass
1063 [XmlElementAttribute (DataType = "date")]
1064 public DateTime SomeDate;
1067 public class Bug8468BaseClass
1072 public class Bug8468MidClass: Bug8468BaseClass
1077 [XmlRoot("Test", Namespace="http://test-namespace")]
1078 public class Bug8468Subclass: Bug8468MidClass
1083 public class Bug8468SubclassNoNamespace: Bug8468MidClass
1087 [XmlRoot("Test", Namespace="")]
1088 public class Bug8468BaseClassV2
1093 public class Bug8468MidClassV2: Bug8468BaseClassV2
1098 [XmlRoot("Test", Namespace="http://test-namespace")]
1099 public class Bug8468SubclassV2: Bug8468MidClassV2
1104 public class Bug8468SubclassNoNamespaceV2: Bug8468MidClassV2
1109 public class Bug9193Class
1111 [XmlElement ("Data", Order=0)]
1112 public string[] Data;
1113 [XmlElement ("Extra", Order=1)]
1114 public string[] Extra;
1117 public class SimpleObjectA
1125 public static implicit operator SimpleObjectA (SimpleObjectB o)
1127 return new SimpleObjectA { Text = o.Text };
1130 public static implicit operator SimpleObjectB (SimpleObjectA o)
1132 return new SimpleObjectB { Text = o.Text };
1136 public class SimpleObjectB
1145 public class ObjectWithElementRequiringImplicitCast
1147 public ObjectWithElementRequiringImplicitCast () { }
1148 public ObjectWithElementRequiringImplicitCast (string text)
1150 Object = new SimpleObjectB { Text = text };
1153 [XmlElement(Type = typeof (SimpleObjectA))]
1154 public SimpleObjectB Object
1160 public class ObjectWithNullableArrayItems
1162 [XmlArrayItem ("Element", IsNullable = true)]
1163 public List<SimpleClass> Elements;
1166 public class ObjectWithNonNullableArrayItems
1168 [XmlArrayItem ("Element", IsNullable = false)]
1169 public List<SimpleClass> Elements;
1172 public class ObjectWithNotSpecifiedNullableArrayItems
1174 [XmlArrayItem ("Element")]
1175 public List<SimpleClass> Elements;
1179 public sealed class ClassWithDefaultTextNotNull
1182 public string Value;
1184 public const string DefaultValue = "NotNull";
1186 public ClassWithDefaultTextNotNull (string v) {
1190 public ClassWithDefaultTextNotNull () {
1191 Value = DefaultValue;