XmlSerializerTestClasses.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //
  2. // System.Xml.XmlSerializerTestClasses
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. //
  7. // (C) 2003 Erik LeBel
  8. //
  9. // Classes to use in the testing of the XmlSerializer
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.Collections;
  14. using System.Xml;
  15. using System.Xml.Schema;
  16. using System.Xml.Serialization;
  17. namespace MonoTests.System.Xml.TestClasses
  18. {
  19. public enum SimpleEnumeration { FIRST, SECOND };
  20. [Flags]
  21. public enum EnumDefaultValue { e1 = 1, e2 = 2, e3 = 3 }
  22. public enum EnumDefaultValueNF { e1 = 1, e2 = 2, e3 = 3 }
  23. public class SimpleClass
  24. {
  25. public string something = null;
  26. }
  27. public class StringCollection : CollectionBase
  28. {
  29. public void Add (String parameter)
  30. {
  31. List.Insert (Count, parameter);
  32. }
  33. public String this [int index]
  34. {
  35. get
  36. {
  37. if (index < 0 || index > Count)
  38. throw new ArgumentOutOfRangeException ();
  39. return (String) List [index];
  40. }
  41. set { List [index] = value; }
  42. }
  43. }
  44. public class StringCollectionContainer
  45. {
  46. StringCollection messages = new StringCollection();
  47. public StringCollection Messages
  48. {
  49. get { return messages; }
  50. }
  51. }
  52. public class ArrayContainer
  53. {
  54. public object [] items = null;
  55. }
  56. public class ClassArrayContainer
  57. {
  58. public SimpleClass [] items = null;
  59. }
  60. [XmlRoot("simple")]
  61. public class SimpleClassWithXmlAttributes
  62. {
  63. [XmlAttribute("member")]
  64. public string something = null;
  65. }
  66. [XmlRoot("field")]
  67. public class Field
  68. {
  69. [XmlAttribute("modifiers")]
  70. public MapModifiers Modifiers;
  71. [XmlAttribute ("modifiers2", Form=XmlSchemaForm.Unqualified)]
  72. public MapModifiers Modifiers2;
  73. [XmlAttribute ("modifiers3")]
  74. [DefaultValue (0)]
  75. public MapModifiers Modifiers3;
  76. [XmlAttribute ("modifiers4", Form=XmlSchemaForm.Unqualified)]
  77. [DefaultValue (0)]
  78. public MapModifiers Modifiers4;
  79. [XmlAttribute ("names")]
  80. public string[] Names;
  81. [XmlAttribute ("street")]
  82. public string Street;
  83. }
  84. [Flags]
  85. public enum MapModifiers
  86. {
  87. [XmlEnum("public")]
  88. Public = 0,
  89. [XmlEnum("protected")]
  90. Protected = 1,
  91. }
  92. public class MyList : ArrayList
  93. {
  94. object container;
  95. // NOTE: MyList has no public constructor
  96. public MyList (object container) : base()
  97. {
  98. this.container = container;
  99. }
  100. }
  101. public class Container
  102. {
  103. public MyList Items;
  104. public Container () {
  105. Items = new MyList(this);
  106. }
  107. }
  108. public class Container2
  109. {
  110. public MyList Items;
  111. public Container2 () {
  112. }
  113. public Container2 (bool b) {
  114. Items = new MyList(this);
  115. }
  116. }
  117. public class MyElem: XmlElement
  118. {
  119. public MyElem (XmlDocument doc): base ("","myelem","", doc)
  120. {
  121. SetAttribute ("aa","1");
  122. }
  123. [XmlAttribute]
  124. public int kk=1;
  125. }
  126. public class MyDocument: XmlDocument
  127. {
  128. public MyDocument ()
  129. {
  130. }
  131. [XmlAttribute]
  132. public int kk=1;
  133. }
  134. public class CDataContainer
  135. {
  136. public XmlCDataSection cdata;
  137. }
  138. public class NodeContainer
  139. {
  140. public XmlNode node;
  141. }
  142. public class Choices
  143. {
  144. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  145. [XmlElementAttribute("ChoiceOne", typeof(string), IsNullable=false)]
  146. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  147. [XmlChoiceIdentifier("ItemType")]
  148. public string MyChoice;
  149. [XmlIgnore]
  150. public ItemChoiceType ItemType;
  151. }
  152. [XmlType(IncludeInSchema = false)]
  153. public enum ItemChoiceType
  154. {
  155. ChoiceZero,
  156. [XmlEnum ("ChoiceOne")]
  157. StrangeOne,
  158. ChoiceTwo,
  159. }
  160. public class WrongChoices
  161. {
  162. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  163. [XmlElementAttribute("StrangeOne", typeof(string), IsNullable=false)]
  164. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  165. [XmlChoiceIdentifier("ItemType")]
  166. public string MyChoice;
  167. [XmlIgnore]
  168. public ItemChoiceType ItemType;
  169. }
  170. [XmlType ("Type with space")]
  171. public class TestSpace
  172. {
  173. [XmlElement (ElementName = "Element with space")]
  174. public int elem;
  175. [XmlAttribute (AttributeName = "Attribute with space")]
  176. public int attr;
  177. }
  178. [Serializable]
  179. public class ReadOnlyProperties {
  180. string[] strArr = new string[2] { "string1", "string2" };
  181. public string[] StrArr {
  182. get { return strArr; }
  183. }
  184. public string dat {
  185. get { return "fff"; }
  186. }
  187. }
  188. [XmlRoot("root")]
  189. public class ListDefaults
  190. {
  191. public ListDefaults ()
  192. {
  193. ed = new SimpleClass ();
  194. str = "hola";
  195. }
  196. public ArrayList list2;
  197. public MyList list3;
  198. public string[] list4;
  199. [XmlElement("e", typeof(SimpleClass))]
  200. public ArrayList list5;
  201. [DefaultValue (null)]
  202. public SimpleClass ed;
  203. [DefaultValue (null)]
  204. public string str;
  205. }
  206. public class clsPerson
  207. {
  208. public IList EmailAccounts;
  209. }
  210. public class ArrayClass
  211. {
  212. public object names = new object[] { "un","dos" };
  213. }
  214. public class CompositeValueType
  215. {
  216. public void Init ()
  217. {
  218. Items = new object[] { 1, 2 };
  219. ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
  220. }
  221. [XmlElementAttribute("Es", typeof(int))]
  222. [XmlElementAttribute("In", typeof(int))]
  223. [XmlChoiceIdentifierAttribute("ItemsElementName")]
  224. public object[] Items;
  225. [XmlElementAttribute("ItemsElementName")]
  226. [XmlIgnoreAttribute()]
  227. public ItemsChoiceType[] ItemsElementName;
  228. }
  229. public enum ItemsChoiceType {
  230. In, Es
  231. }
  232. public class ArrayAttributeWithType
  233. {
  234. [XmlAttribute (DataType="anyURI")]
  235. public string[] at = new string [] { "a","b" };
  236. [XmlAttribute (DataType="base64Binary")]
  237. public byte[][] bin1 = new byte[][] { new byte[]{1,2}, new byte[]{1,2}};
  238. [XmlAttribute (DataType="base64Binary")]
  239. public byte[] bin2 = new byte[] { 1,2 };
  240. }
  241. public class ArrayAttributeWithWrongType
  242. {
  243. [XmlAttribute (DataType="int")]
  244. public string[] at = new string [] { "a","b" };
  245. }
  246. [XmlType ("Container")]
  247. public class EntityContainer
  248. {
  249. EntityCollection collection1;
  250. EntityCollection collection2;
  251. EntityCollection collection3 = new EntityCollection ("root");
  252. EntityCollection collection4 = new EntityCollection ("root");
  253. [XmlArray (IsNullable=true)]
  254. public EntityCollection Collection1 {
  255. get { return collection1; }
  256. set { collection1 = value; collection1.Container = "assigned"; }
  257. }
  258. [XmlArray (IsNullable=false)]
  259. public EntityCollection Collection2 {
  260. get { return collection2; }
  261. set { collection2 = value; collection2.Container = "assigned"; }
  262. }
  263. [XmlArray (IsNullable=true)]
  264. public EntityCollection Collection3 {
  265. get { return collection3; }
  266. set { collection3 = value; collection3.Container = "assigned"; }
  267. }
  268. [XmlArray (IsNullable=false)]
  269. public EntityCollection Collection4 {
  270. get { return collection4; }
  271. set { collection4 = value; collection4.Container = "assigned"; }
  272. }
  273. }
  274. [XmlType ("Container")]
  275. public class ArrayEntityContainer
  276. {
  277. Entity[] collection1;
  278. Entity[] collection2;
  279. Entity[] collection3 = new Entity [0];
  280. Entity[] collection4 = new Entity [0];
  281. [XmlArray (IsNullable=true)]
  282. public Entity[] Collection1 {
  283. get { return collection1; }
  284. set { collection1 = value; }
  285. }
  286. [XmlArray (IsNullable=false)]
  287. public Entity[] Collection2 {
  288. get { return collection2; }
  289. set { collection2 = value; }
  290. }
  291. [XmlArray (IsNullable=true)]
  292. public Entity[] Collection3 {
  293. get { return collection3; }
  294. set { collection3 = value; }
  295. }
  296. [XmlArray (IsNullable=false)]
  297. public Entity[] Collection4 {
  298. get { return collection4; }
  299. set { collection4 = value; }
  300. }
  301. }
  302. public class Entity
  303. {
  304. private string _name = string.Empty;
  305. private string _parent = null;
  306. [XmlAttribute]
  307. public string Name {
  308. get { return _name; }
  309. set { _name = value; }
  310. }
  311. [XmlIgnore]
  312. public string Parent {
  313. get { return _parent; }
  314. set { _parent = value; }
  315. }
  316. }
  317. public class EntityCollection : ArrayList
  318. {
  319. public string _container;
  320. public EntityCollection ()
  321. {
  322. }
  323. public EntityCollection (string c)
  324. {
  325. _container = c;
  326. }
  327. public string Container {
  328. get { return _container; }
  329. set { _container = value; }
  330. }
  331. public int Add (Entity value)
  332. {
  333. if(_container != null)
  334. value.Parent = _container;
  335. return base.Add(value);
  336. }
  337. public new Entity this[int index]
  338. {
  339. get { return (Entity) base[index]; }
  340. set { base[index] = value; }
  341. }
  342. }
  343. [XmlType ("Container")]
  344. public class ObjectWithReadonlyCollection
  345. {
  346. EntityCollection collection1 = new EntityCollection ("root");
  347. public EntityCollection Collection1 {
  348. get { return collection1; }
  349. }
  350. }
  351. [XmlType ("Container")]
  352. public class ObjectWithReadonlyNulCollection
  353. {
  354. EntityCollection collection1;
  355. public EntityCollection Collection1 {
  356. get { return collection1; }
  357. }
  358. }
  359. [XmlType ("Container")]
  360. public class ObjectWithReadonlyArray
  361. {
  362. Entity[] collection1 = new Entity [0];
  363. public Entity[] Collection1 {
  364. get { return collection1; }
  365. }
  366. }
  367. }