XmlSerializerTestClasses.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. [Flags]
  24. public enum FlagEnum {
  25. [XmlEnum ("one")]
  26. e1 = 1,
  27. [XmlEnum ("two")]
  28. e2 = 2,
  29. [XmlEnum ("four")]
  30. e4 = 4 }
  31. [Flags]
  32. public enum ZeroFlagEnum {
  33. [XmlEnum ("zero")]
  34. e0 = 0,
  35. [XmlEnum ("o<n>e")]
  36. e1 = 1,
  37. [XmlEnum ("tns:t<w>o")]
  38. e2 = 2,
  39. [XmlEnum ("four")]
  40. [XmlIgnore]
  41. e4 = 4
  42. }
  43. public class SimpleClass
  44. {
  45. public string something = null;
  46. }
  47. public class StringCollection : CollectionBase
  48. {
  49. public void Add (String parameter)
  50. {
  51. List.Insert (Count, parameter);
  52. }
  53. public String this [int index]
  54. {
  55. get
  56. {
  57. if (index < 0 || index > Count)
  58. throw new ArgumentOutOfRangeException ();
  59. return (String) List [index];
  60. }
  61. set { List [index] = value; }
  62. }
  63. }
  64. public class StringCollectionContainer
  65. {
  66. StringCollection messages = new StringCollection();
  67. public StringCollection Messages
  68. {
  69. get { return messages; }
  70. }
  71. }
  72. public class ArrayContainer
  73. {
  74. public object [] items = null;
  75. }
  76. public class ClassArrayContainer
  77. {
  78. public SimpleClass [] items = null;
  79. }
  80. [XmlRoot("simple")]
  81. public class SimpleClassWithXmlAttributes
  82. {
  83. [XmlAttribute("member")]
  84. public string something = null;
  85. }
  86. [XmlRoot("field")]
  87. public class Field
  88. {
  89. [XmlAttribute("modifiers")]
  90. public MapModifiers Modifiers;
  91. [XmlAttribute ("modifiers2", Form=XmlSchemaForm.Unqualified)]
  92. public MapModifiers Modifiers2;
  93. [XmlAttribute ("modifiers3")]
  94. [DefaultValue (0)]
  95. public MapModifiers Modifiers3;
  96. [XmlAttribute ("modifiers4", Form=XmlSchemaForm.Unqualified)]
  97. [DefaultValue (0)]
  98. public MapModifiers Modifiers4;
  99. [XmlAttribute ("names")]
  100. public string[] Names;
  101. [XmlAttribute ("street")]
  102. public string Street;
  103. }
  104. [Flags]
  105. public enum MapModifiers
  106. {
  107. [XmlEnum("public")]
  108. Public = 0,
  109. [XmlEnum("protected")]
  110. Protected = 1,
  111. }
  112. public class MyList : ArrayList
  113. {
  114. object container;
  115. // NOTE: MyList has no public constructor
  116. public MyList (object container) : base()
  117. {
  118. this.container = container;
  119. }
  120. }
  121. public class Container
  122. {
  123. public MyList Items;
  124. public Container () {
  125. Items = new MyList(this);
  126. }
  127. }
  128. public class Container2
  129. {
  130. public MyList Items;
  131. public Container2 () {
  132. }
  133. public Container2 (bool b) {
  134. Items = new MyList(this);
  135. }
  136. }
  137. public class MyElem: XmlElement
  138. {
  139. public MyElem (XmlDocument doc): base ("","myelem","", doc)
  140. {
  141. SetAttribute ("aa","1");
  142. }
  143. [XmlAttribute]
  144. public int kk=1;
  145. }
  146. public class MyDocument: XmlDocument
  147. {
  148. public MyDocument ()
  149. {
  150. }
  151. [XmlAttribute]
  152. public int kk=1;
  153. }
  154. public class CDataContainer
  155. {
  156. public XmlCDataSection cdata;
  157. }
  158. public class NodeContainer
  159. {
  160. public XmlNode node;
  161. }
  162. public class Choices
  163. {
  164. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  165. [XmlElementAttribute("ChoiceOne", typeof(string), IsNullable=false)]
  166. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  167. [XmlChoiceIdentifier("ItemType")]
  168. public string MyChoice;
  169. [XmlIgnore]
  170. public ItemChoiceType ItemType;
  171. }
  172. [XmlType(IncludeInSchema = false)]
  173. public enum ItemChoiceType
  174. {
  175. ChoiceZero,
  176. [XmlEnum ("ChoiceOne")]
  177. StrangeOne,
  178. ChoiceTwo,
  179. }
  180. public class WrongChoices
  181. {
  182. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  183. [XmlElementAttribute("StrangeOne", typeof(string), IsNullable=false)]
  184. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  185. [XmlChoiceIdentifier("ItemType")]
  186. public string MyChoice;
  187. [XmlIgnore]
  188. public ItemChoiceType ItemType;
  189. }
  190. [XmlType ("Type with space")]
  191. public class TestSpace
  192. {
  193. [XmlElement (ElementName = "Element with space")]
  194. public int elem;
  195. [XmlAttribute (AttributeName = "Attribute with space")]
  196. public int attr;
  197. }
  198. [Serializable]
  199. public class ReadOnlyProperties {
  200. string[] strArr = new string[2] { "string1", "string2" };
  201. public string[] StrArr {
  202. get { return strArr; }
  203. }
  204. public string dat {
  205. get { return "fff"; }
  206. }
  207. }
  208. [XmlRoot("root")]
  209. public class ListDefaults
  210. {
  211. public ListDefaults ()
  212. {
  213. ed = new SimpleClass ();
  214. str = "hola";
  215. }
  216. public ArrayList list2;
  217. public MyList list3;
  218. public string[] list4;
  219. [XmlElement("e", typeof(SimpleClass))]
  220. public ArrayList list5;
  221. [DefaultValue (null)]
  222. public SimpleClass ed;
  223. [DefaultValue (null)]
  224. public string str;
  225. }
  226. public class clsPerson
  227. {
  228. public IList EmailAccounts;
  229. }
  230. public class ArrayClass
  231. {
  232. public object names = new object[] { "un","dos" };
  233. }
  234. public class CompositeValueType
  235. {
  236. public void Init ()
  237. {
  238. Items = new object[] { 1, 2 };
  239. ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
  240. }
  241. [XmlElementAttribute("Es", typeof(int))]
  242. [XmlElementAttribute("In", typeof(int))]
  243. [XmlChoiceIdentifierAttribute("ItemsElementName")]
  244. public object[] Items;
  245. [XmlElementAttribute("ItemsElementName")]
  246. [XmlIgnoreAttribute()]
  247. public ItemsChoiceType[] ItemsElementName;
  248. }
  249. public enum ItemsChoiceType {
  250. In, Es
  251. }
  252. public class ArrayAttributeWithType
  253. {
  254. [XmlAttribute (DataType="anyURI")]
  255. public string[] at = new string [] { "a","b" };
  256. [XmlAttribute (DataType="base64Binary")]
  257. public byte[][] bin1 = new byte[][] { new byte[]{1,2}, new byte[]{1,2}};
  258. [XmlAttribute (DataType="base64Binary")]
  259. public byte[] bin2 = new byte[] { 1,2 };
  260. }
  261. public class ArrayAttributeWithWrongType
  262. {
  263. [XmlAttribute (DataType="int")]
  264. public string[] at = new string [] { "a","b" };
  265. }
  266. [XmlType ("Container")]
  267. public class EntityContainer
  268. {
  269. EntityCollection collection1;
  270. EntityCollection collection2;
  271. EntityCollection collection3 = new EntityCollection ("root");
  272. EntityCollection collection4 = new EntityCollection ("root");
  273. [XmlArray (IsNullable=true)]
  274. public EntityCollection Collection1 {
  275. get { return collection1; }
  276. set { collection1 = value; collection1.Container = "assigned"; }
  277. }
  278. [XmlArray (IsNullable=false)]
  279. public EntityCollection Collection2 {
  280. get { return collection2; }
  281. set { collection2 = value; collection2.Container = "assigned"; }
  282. }
  283. [XmlArray (IsNullable=true)]
  284. public EntityCollection Collection3 {
  285. get { return collection3; }
  286. set { collection3 = value; collection3.Container = "assigned"; }
  287. }
  288. [XmlArray (IsNullable=false)]
  289. public EntityCollection Collection4 {
  290. get { return collection4; }
  291. set { collection4 = value; collection4.Container = "assigned"; }
  292. }
  293. }
  294. [XmlType ("Container")]
  295. public class ArrayEntityContainer
  296. {
  297. Entity[] collection1;
  298. Entity[] collection2;
  299. Entity[] collection3 = new Entity [0];
  300. Entity[] collection4 = new Entity [0];
  301. [XmlArray (IsNullable=true)]
  302. public Entity[] Collection1 {
  303. get { return collection1; }
  304. set { collection1 = value; }
  305. }
  306. [XmlArray (IsNullable=false)]
  307. public Entity[] Collection2 {
  308. get { return collection2; }
  309. set { collection2 = value; }
  310. }
  311. [XmlArray (IsNullable=true)]
  312. public Entity[] Collection3 {
  313. get { return collection3; }
  314. set { collection3 = value; }
  315. }
  316. [XmlArray (IsNullable=false)]
  317. public Entity[] Collection4 {
  318. get { return collection4; }
  319. set { collection4 = value; }
  320. }
  321. }
  322. public class Entity
  323. {
  324. private string _name = string.Empty;
  325. private string _parent = null;
  326. [XmlAttribute]
  327. public string Name {
  328. get { return _name; }
  329. set { _name = value; }
  330. }
  331. [XmlIgnore]
  332. public string Parent {
  333. get { return _parent; }
  334. set { _parent = value; }
  335. }
  336. }
  337. public class EntityCollection : ArrayList
  338. {
  339. public string _container;
  340. public EntityCollection ()
  341. {
  342. }
  343. public EntityCollection (string c)
  344. {
  345. _container = c;
  346. }
  347. public string Container {
  348. get { return _container; }
  349. set { _container = value; }
  350. }
  351. public int Add (Entity value)
  352. {
  353. if(_container != null)
  354. value.Parent = _container;
  355. return base.Add(value);
  356. }
  357. public new Entity this[int index]
  358. {
  359. get { return (Entity) base[index]; }
  360. set { base[index] = value; }
  361. }
  362. }
  363. [XmlType ("Container")]
  364. public class ObjectWithReadonlyCollection
  365. {
  366. EntityCollection collection1 = new EntityCollection ("root");
  367. public EntityCollection Collection1 {
  368. get { return collection1; }
  369. }
  370. }
  371. [XmlType ("Container")]
  372. public class ObjectWithReadonlyNulCollection
  373. {
  374. EntityCollection collection1;
  375. public EntityCollection Collection1 {
  376. get { return collection1; }
  377. }
  378. }
  379. [XmlType ("Container")]
  380. public class ObjectWithReadonlyArray
  381. {
  382. Entity[] collection1 = new Entity [0];
  383. public Entity[] Collection1 {
  384. get { return collection1; }
  385. }
  386. }
  387. [XmlInclude (typeof(SubclassTestSub))]
  388. public class SubclassTestBase
  389. {
  390. }
  391. public class SubclassTestSub: SubclassTestBase
  392. {
  393. }
  394. public class SubclassTestExtra
  395. {
  396. }
  397. public class SubclassTestContainer
  398. {
  399. [XmlElement ("a", typeof(SubclassTestBase))]
  400. [XmlElement ("b", typeof(SubclassTestExtra))]
  401. public object data;
  402. }
  403. public class DictionaryWithIndexer : DictionaryBase
  404. {
  405. public TimeSpan this[int index] {
  406. get { return TimeSpan.MinValue; }
  407. }
  408. public void Add (TimeSpan value)
  409. {
  410. }
  411. }
  412. [XmlRoot(Namespace="some:urn")]
  413. [SoapTypeAttribute (Namespace="another:urn")]
  414. public class PrimitiveTypesContainer
  415. {
  416. public PrimitiveTypesContainer ()
  417. {
  418. Number = 2004;
  419. Name = "some name";
  420. Index = (byte) 56;
  421. Password = new byte[] { 243, 15 };
  422. PathSeparatorCharacter = '/';
  423. }
  424. public int Number;
  425. public string Name;
  426. public byte Index;
  427. public byte[] Password;
  428. public char PathSeparatorCharacter;
  429. }
  430. }