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;
468 // public MyList list3;
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 SubclassTestList
716 [XmlElement ("a", typeof (SimpleClass))]
717 [XmlElement ("b", typeof (SubclassTestBase))]
718 public List<object> Items;
721 public class DictionaryWithIndexer : DictionaryBase
723 public TimeSpan this[int index]
725 get { return TimeSpan.MinValue; }
728 public void Add (TimeSpan value)
733 [XmlRoot (Namespace = "some:urn")]
734 [SoapTypeAttribute (Namespace = "another:urn")]
735 public class PrimitiveTypesContainer
737 public PrimitiveTypesContainer ()
742 Password = new byte[] { 243, 15 };
743 PathSeparatorCharacter = '/';
749 public byte[] Password;
750 public char PathSeparatorCharacter;
753 public class TestSchemaForm1
755 public PrintTypeResponse p1;
757 [XmlElement (Namespace = "urn:oo")]
758 public PrintTypeResponse p2;
761 [XmlType (Namespace = "urn:testForm")]
762 public class TestSchemaForm2
764 public PrintTypeResponse p1;
766 [XmlElement (Namespace = "urn:oo")]
767 public PrintTypeResponse p2;
770 [XmlType (Namespace = "urn:responseTypes")]
771 public class PrintTypeResponse
773 [XmlElement (Form = XmlSchemaForm.Unqualified, IsNullable = true)]
774 public OutputType result;
775 public PrintTypeResponse intern;
779 result = new OutputType ();
780 result.data = "data1";
781 intern = new PrintTypeResponse ();
782 intern.result = new OutputType ();
783 intern.result.data = "data2";
787 [XmlType (Namespace = "urn:responseTypes")]
788 public class OutputType
791 [XmlElement (Form = XmlSchemaForm.Unqualified, IsNullable = true)]
795 [XmlRootAttribute ("testDefault", Namespace = "urn:myNS", IsNullable = false)]
796 [SoapType ("testDefault", Namespace = "urn:myNS")]
797 public class TestDefault
801 [DefaultValue ("Default Value")]
802 public string strDefault = "Default Value";
804 [DefaultValue (true)]
805 public bool boolT = true;
807 [DefaultValue (false)]
808 public bool boolF = false;
810 [DefaultValue (typeof (decimal), "10")]
811 public decimal decimalval = 10m;
813 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
814 public FlagEnum flag = (FlagEnum.e1 | FlagEnum.e4);
816 [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4)]
817 public FlagEnum_Encoded flagencoded = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4);
820 [XmlType ("optionalValueType", Namespace = "some:urn")]
821 [XmlRootAttribute ("optionalValue", Namespace = "another:urn", IsNullable = false)]
822 public class OptionalValueTypeContainer
824 [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
825 public FlagEnum Attributes = FlagEnum.e1 | FlagEnum.e4;
827 [DefaultValue (FlagEnum.e1)]
828 public FlagEnum Flags = FlagEnum.e1;
832 public bool FlagsSpecified;
834 [DefaultValue (false)]
839 public bool IsEmptySpecified
841 get { return _isEmptySpecified; }
842 set { _isEmptySpecified = value; }
845 [DefaultValue (false)]
848 private bool _isEmptySpecified;
853 [SoapAttribute (Namespace = "http://www.cpandl.com")]
854 public string GroupName;
856 [SoapAttribute (DataType = "base64Binary")]
857 public Byte[] GroupNumber;
859 [SoapAttribute (DataType = "date", AttributeName = "CreationDate")]
860 public DateTime Today;
862 [SoapElement (DataType = "nonNegativeInteger", ElementName = "PosInt")]
863 public string PostitiveInt;
866 public bool IgnoreThis;
868 [DefaultValue (GroupType.B)]
869 public GroupType Grouptype;
870 public Vehicle MyVehicle;
872 [SoapInclude (typeof (Car))]
873 public Vehicle myCar (string licNumber)
876 if (licNumber == string.Empty) {
878 v.licenseNumber = "!!!!!!";
882 v.licenseNumber = licNumber;
888 [SoapInclude (typeof (Car))]
889 public abstract class Vehicle
891 public string licenseNumber;
892 [SoapElement (DataType = "date")]
893 public DateTime makeDate;
894 [DefaultValue ("450")]
895 public string weight;
898 public class Car : Vehicle
902 public enum GroupType
910 public class ErrorneousGetSchema : IXmlSerializable
912 public XmlSchema GetSchema ()
914 throw new ApplicationException ("unexpected");
917 public void ReadXml (XmlReader reader)
921 public void WriteXml (XmlWriter writer)
925 // it should be serialized IF it is NOT IXmlSerializable.
926 public string Whoa = "whoa";
929 [XmlRoot ("DefaultDateTimeContainer", Namespace = "urn:foo")]
930 public class DefaultDateTimeContainer // bug #378696
932 public DateTime SimpleDateTime;
934 [DefaultValue(typeof(DateTime), "2001-02-03T04:05:06")]
935 public DateTime FancyDateTime;
937 [DefaultValue (typeof (int), "123456")]
941 public class XmlSerializableImplicitConvertible
943 public BaseClass B = new DerivedClass ();
945 public class XmlSerializable : IXmlSerializable
947 public void WriteXml (XmlWriter writer)
951 public void ReadXml (XmlReader reader)
955 public XmlSchema GetSchema ()
961 public class BaseClass
963 public static implicit operator XmlSerializable (BaseClass b)
965 return new XmlSerializable ();
969 public static implicit operator BaseClass (XmlSerializable x)
971 return new BaseClass ();
975 public class DerivedClass : BaseClass
980 public class Bug704813Type
982 IEnumerable<string> foo = new List<string> ();
983 public IEnumerable<string> Foo {
988 public class Bug708178Type
990 List<string> foo = new List<string> ();
992 [XmlArray("Foo"), XmlArrayItem("Foo", typeof(string))]
993 public List<string> Foo {
999 public class ExplicitlyOrderedMembersType1
1001 [XmlElement("child0", Order = 4)]
1002 public string Child0;
1004 [XmlElement("child", Order = 0)]
1005 public string Child1;
1007 [XmlElement("child", Order = 2)]
1008 public string Child2;
1012 public class ExplicitlyOrderedMembersType2
1014 [XmlElement("child0", Order = 4)]
1015 public string Child0;
1017 [XmlElement("child")] // wrong. Needs to be Ordered as well.
1018 public string Child1;
1020 [XmlElement("child", Order = 2)]
1021 public string Child2;
1025 public class ExplicitlyOrderedMembersType3
1027 [XmlElement("child0", Order = 1)] // it's between 0 and 2. After two "child" elements, child0 is not recognized as this member.
1028 public string Child0;
1030 [XmlElement("child", Order = 0)]
1031 public string Child1;
1033 [XmlElement("child", Order = 2)]
1034 public string Child2;
1038 public class ExplicitlyOrderedMembersType4
1040 [XmlElement("child0", Order = 1)] // it's between 0 and 2. After two "child" elements, child0 is not recognized as this member.
1041 public string Child0;
1043 [XmlElement("child", Order = 0)]
1044 public string Child1;
1046 [XmlElement("child", Order = 2)]
1047 public string Child2;
1050 public string Child3;
1054 public class NullableDatesAndTimes {
1055 [XmlElementAttribute ("MyTime", DataType = "time", IsNullable = false)]
1056 public DateTime MyTime;
1058 [XmlElementAttribute ("MyTimeNullable", DataType = "time", IsNullable = true)]
1059 public DateTime? MyTimeNullable;
1061 [XmlElementAttribute ("MyDate", DataType = "date", IsNullable = false)]
1062 public DateTime MyDate;
1064 [XmlElementAttribute ("MyDateNullable", DataType = "date", IsNullable = true)]
1065 public DateTime? MyDateNullable;
1068 public class NotExactDateParseClass
1070 [XmlElementAttribute (DataType = "date")]
1071 public DateTime SomeDate;
1074 public class Bug8468BaseClass
1079 public class Bug8468MidClass: Bug8468BaseClass
1084 [XmlRoot("Test", Namespace="http://test-namespace")]
1085 public class Bug8468Subclass: Bug8468MidClass
1090 public class Bug8468SubclassNoNamespace: Bug8468MidClass
1094 [XmlRoot("Test", Namespace="")]
1095 public class Bug8468BaseClassV2
1100 public class Bug8468MidClassV2: Bug8468BaseClassV2
1105 [XmlRoot("Test", Namespace="http://test-namespace")]
1106 public class Bug8468SubclassV2: Bug8468MidClassV2
1111 public class Bug8468SubclassNoNamespaceV2: Bug8468MidClassV2
1116 public class Bug9193Class
1118 [XmlElement ("Data", Order=0)]
1119 public string[] Data;
1120 [XmlElement ("Extra", Order=1)]
1121 public string[] Extra;
1124 public class SimpleObjectA
1132 public static implicit operator SimpleObjectA (SimpleObjectB o)
1134 return new SimpleObjectA { Text = o.Text };
1137 public static implicit operator SimpleObjectB (SimpleObjectA o)
1139 return new SimpleObjectB { Text = o.Text };
1143 public class SimpleObjectB
1152 public class ObjectWithElementRequiringImplicitCast
1154 public ObjectWithElementRequiringImplicitCast () { }
1155 public ObjectWithElementRequiringImplicitCast (string text)
1157 Object = new SimpleObjectB { Text = text };
1160 [XmlElement(Type = typeof (SimpleObjectA))]
1161 public SimpleObjectB Object
1167 public class ObjectWithNullableArrayItems
1169 [XmlArrayItem ("Element", IsNullable = true)]
1170 public List<SimpleClass> Elements;
1173 public class ObjectWithNonNullableArrayItems
1175 [XmlArrayItem ("Element", IsNullable = false)]
1176 public List<SimpleClass> Elements;
1179 public class ObjectWithNotSpecifiedNullableArrayItems
1181 [XmlArrayItem ("Element")]
1182 public List<SimpleClass> Elements;
1186 public sealed class ClassWithDefaultTextNotNull
1189 public string Value;
1191 public const string DefaultValue = "NotNull";
1193 public ClassWithDefaultTextNotNull (string v) {
1197 public ClassWithDefaultTextNotNull () {
1198 Value = DefaultValue;