XmlSerializerTestClasses.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. }
  32. [Flags]
  33. [SoapType ("flagenum")]
  34. public enum FlagEnum_Encoded
  35. {
  36. [SoapEnum ("one")]
  37. e1 = 1,
  38. [SoapEnum ("two")]
  39. e2 = 2,
  40. [SoapEnum ("four")]
  41. e4 = 4
  42. }
  43. [Flags]
  44. public enum ZeroFlagEnum {
  45. [XmlEnum ("zero")]
  46. e0 = 0,
  47. [XmlEnum ("o<n>e")]
  48. e1 = 1,
  49. [XmlEnum ("tns:t<w>o")]
  50. e2 = 2,
  51. [XmlEnum ("four")]
  52. [XmlIgnore]
  53. e4 = 4
  54. }
  55. public class SimpleClass
  56. {
  57. public string something = null;
  58. }
  59. public class StringCollection : CollectionBase
  60. {
  61. public void Add (String parameter)
  62. {
  63. List.Insert (Count, parameter);
  64. }
  65. public String this [int index]
  66. {
  67. get
  68. {
  69. if (index < 0 || index > Count)
  70. throw new ArgumentOutOfRangeException ();
  71. return (String) List [index];
  72. }
  73. set { List [index] = value; }
  74. }
  75. }
  76. public class StringCollectionContainer
  77. {
  78. StringCollection messages = new StringCollection();
  79. public StringCollection Messages
  80. {
  81. get { return messages; }
  82. }
  83. }
  84. public class ArrayContainer
  85. {
  86. public object [] items = null;
  87. }
  88. public class ClassArrayContainer
  89. {
  90. public SimpleClass [] items = null;
  91. }
  92. [XmlRoot("simple")]
  93. public class SimpleClassWithXmlAttributes
  94. {
  95. [XmlAttribute("member")]
  96. public string something = null;
  97. }
  98. [XmlRoot("field")]
  99. public class Field
  100. {
  101. [XmlAttribute ("flag1")]
  102. [DefaultValue (1)]
  103. public FlagEnum Flags1;
  104. [XmlAttribute ("flag2")]
  105. [DefaultValue (FlagEnum.e1)]
  106. public FlagEnum Flags2;
  107. [XmlAttribute ("flag3", Form = XmlSchemaForm.Qualified)]
  108. [DefaultValue (FlagEnum.e1 | FlagEnum.e2)]
  109. public FlagEnum Flags3;
  110. [XmlAttribute ("flag4")]
  111. public FlagEnum Flags4;
  112. [XmlAttribute("modifiers")]
  113. public MapModifiers Modifiers;
  114. [XmlAttribute ("modifiers2", Form=XmlSchemaForm.Unqualified)]
  115. public MapModifiers Modifiers2;
  116. [XmlAttribute ("modifiers3")]
  117. [DefaultValue (0)]
  118. public MapModifiers Modifiers3;
  119. [XmlAttribute ("modifiers4", Form=XmlSchemaForm.Unqualified)]
  120. [DefaultValue (MapModifiers.Protected)]
  121. public MapModifiers Modifiers4;
  122. [XmlAttribute ("modifiers5", Form = XmlSchemaForm.Qualified)]
  123. [DefaultValue (MapModifiers.Public)]
  124. public MapModifiers Modifiers5;
  125. [XmlAttribute ("names")]
  126. public string[] Names;
  127. [XmlAttribute ("street")]
  128. public string Street;
  129. }
  130. [SoapType ("field", Namespace = "some:urn")]
  131. public class Field_Encoded
  132. {
  133. [SoapAttribute ("flag1")]
  134. [DefaultValue (FlagEnum_Encoded.e1)]
  135. public FlagEnum_Encoded Flags1;
  136. [SoapAttribute ("flag2")]
  137. [DefaultValue (FlagEnum_Encoded.e1)]
  138. public FlagEnum_Encoded Flags2;
  139. [SoapAttribute ("flag3")]
  140. [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2)]
  141. public FlagEnum_Encoded Flags3;
  142. [SoapAttribute ("flag4")]
  143. public FlagEnum_Encoded Flags4;
  144. [SoapAttribute ("modifiers")]
  145. public MapModifiers Modifiers;
  146. [SoapAttribute ("modifiers2")]
  147. public MapModifiers Modifiers2;
  148. [SoapAttribute ("modifiers3")]
  149. [DefaultValue (MapModifiers.Public)]
  150. public MapModifiers Modifiers3;
  151. [SoapAttribute ("modifiers4")]
  152. [DefaultValue (MapModifiers.Protected)]
  153. public MapModifiers Modifiers4;
  154. [SoapAttribute ("modifiers5")]
  155. [DefaultValue (MapModifiers.Public)]
  156. public MapModifiers Modifiers5;
  157. public string[] Names;
  158. [SoapAttribute ("street")]
  159. public string Street;
  160. }
  161. [Flags]
  162. public enum MapModifiers
  163. {
  164. [XmlEnum("public")]
  165. Public = 0,
  166. [XmlEnum("protected")]
  167. Protected = 1,
  168. }
  169. public class MyList : ArrayList
  170. {
  171. object container;
  172. // NOTE: MyList has no public constructor
  173. public MyList (object container) : base()
  174. {
  175. this.container = container;
  176. }
  177. }
  178. public class Container
  179. {
  180. public MyList Items;
  181. public Container () {
  182. Items = new MyList(this);
  183. }
  184. }
  185. public class Container2
  186. {
  187. public MyList Items;
  188. public Container2 () {
  189. }
  190. public Container2 (bool b) {
  191. Items = new MyList(this);
  192. }
  193. }
  194. public class MyElem: XmlElement
  195. {
  196. public MyElem (XmlDocument doc): base ("","myelem","", doc)
  197. {
  198. SetAttribute ("aa","1");
  199. }
  200. [XmlAttribute]
  201. public int kk=1;
  202. }
  203. public class MyDocument: XmlDocument
  204. {
  205. public MyDocument ()
  206. {
  207. }
  208. [XmlAttribute]
  209. public int kk=1;
  210. }
  211. public class CDataContainer
  212. {
  213. public XmlCDataSection cdata;
  214. }
  215. public class NodeContainer
  216. {
  217. public XmlNode node;
  218. }
  219. public class Choices
  220. {
  221. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  222. [XmlElementAttribute("ChoiceOne", typeof(string), IsNullable=false)]
  223. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  224. [XmlChoiceIdentifier("ItemType")]
  225. public string MyChoice;
  226. [XmlIgnore]
  227. public ItemChoiceType ItemType;
  228. }
  229. [XmlType(IncludeInSchema = false)]
  230. public enum ItemChoiceType
  231. {
  232. ChoiceZero,
  233. [XmlEnum ("ChoiceOne")]
  234. StrangeOne,
  235. ChoiceTwo,
  236. }
  237. public class WrongChoices
  238. {
  239. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  240. [XmlElementAttribute("StrangeOne", typeof(string), IsNullable=false)]
  241. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  242. [XmlChoiceIdentifier("ItemType")]
  243. public string MyChoice;
  244. [XmlIgnore]
  245. public ItemChoiceType ItemType;
  246. }
  247. [XmlType ("Type with space")]
  248. public class TestSpace
  249. {
  250. [XmlElement (ElementName = "Element with space")]
  251. public int elem;
  252. [XmlAttribute (AttributeName = "Attribute with space")]
  253. public int attr;
  254. }
  255. [Serializable]
  256. public class ReadOnlyProperties {
  257. string[] strArr = new string[2] { "string1", "string2" };
  258. public string[] StrArr {
  259. get { return strArr; }
  260. }
  261. public string dat {
  262. get { return "fff"; }
  263. }
  264. }
  265. [XmlRoot("root")]
  266. public class ListDefaults
  267. {
  268. public ListDefaults ()
  269. {
  270. ed = new SimpleClass ();
  271. str = "hola";
  272. }
  273. public ArrayList list2;
  274. public MyList list3;
  275. public string[] list4;
  276. [XmlElement("e", typeof(SimpleClass))]
  277. public ArrayList list5;
  278. [DefaultValue (null)]
  279. public SimpleClass ed;
  280. [DefaultValue (null)]
  281. public string str;
  282. }
  283. public class clsPerson
  284. {
  285. public IList EmailAccounts;
  286. }
  287. public class ArrayClass
  288. {
  289. public object names = new object[] { "un","dos" };
  290. }
  291. public class CompositeValueType
  292. {
  293. public void Init ()
  294. {
  295. Items = new object[] { 1, 2 };
  296. ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
  297. }
  298. [XmlElementAttribute("Es", typeof(int))]
  299. [XmlElementAttribute("In", typeof(int))]
  300. [XmlChoiceIdentifierAttribute("ItemsElementName")]
  301. public object[] Items;
  302. [XmlElementAttribute("ItemsElementName")]
  303. [XmlIgnoreAttribute()]
  304. public ItemsChoiceType[] ItemsElementName;
  305. }
  306. public enum ItemsChoiceType {
  307. In, Es
  308. }
  309. public class ArrayAttributeWithType
  310. {
  311. [XmlAttribute (DataType="anyURI")]
  312. public string[] at = new string [] { "a","b" };
  313. [XmlAttribute (DataType="base64Binary")]
  314. public byte[][] bin1 = new byte[][] { new byte[]{1,2}, new byte[]{1,2}};
  315. [XmlAttribute (DataType="base64Binary")]
  316. public byte[] bin2 = new byte[] { 1,2 };
  317. }
  318. public class ArrayAttributeWithWrongType
  319. {
  320. [XmlAttribute (DataType="int")]
  321. public string[] at = new string [] { "a","b" };
  322. }
  323. [XmlType ("Container")]
  324. public class EntityContainer
  325. {
  326. EntityCollection collection1;
  327. EntityCollection collection2;
  328. EntityCollection collection3 = new EntityCollection ("root");
  329. EntityCollection collection4 = new EntityCollection ("root");
  330. [XmlArray (IsNullable=true)]
  331. public EntityCollection Collection1 {
  332. get { return collection1; }
  333. set { collection1 = value; collection1.Container = "assigned"; }
  334. }
  335. [XmlArray (IsNullable=false)]
  336. public EntityCollection Collection2 {
  337. get { return collection2; }
  338. set { collection2 = value; collection2.Container = "assigned"; }
  339. }
  340. [XmlArray (IsNullable=true)]
  341. public EntityCollection Collection3 {
  342. get { return collection3; }
  343. set { collection3 = value; collection3.Container = "assigned"; }
  344. }
  345. [XmlArray (IsNullable=false)]
  346. public EntityCollection Collection4 {
  347. get { return collection4; }
  348. set { collection4 = value; collection4.Container = "assigned"; }
  349. }
  350. }
  351. [XmlType ("Container")]
  352. public class ArrayEntityContainer
  353. {
  354. Entity[] collection1;
  355. Entity[] collection2;
  356. Entity[] collection3 = new Entity [0];
  357. Entity[] collection4 = new Entity [0];
  358. [XmlArray (IsNullable=true)]
  359. public Entity[] Collection1 {
  360. get { return collection1; }
  361. set { collection1 = value; }
  362. }
  363. [XmlArray (IsNullable=false)]
  364. public Entity[] Collection2 {
  365. get { return collection2; }
  366. set { collection2 = value; }
  367. }
  368. [XmlArray (IsNullable=true)]
  369. public Entity[] Collection3 {
  370. get { return collection3; }
  371. set { collection3 = value; }
  372. }
  373. [XmlArray (IsNullable=false)]
  374. public Entity[] Collection4 {
  375. get { return collection4; }
  376. set { collection4 = value; }
  377. }
  378. }
  379. public class Entity
  380. {
  381. private string _name = string.Empty;
  382. private string _parent = null;
  383. [XmlAttribute]
  384. public string Name {
  385. get { return _name; }
  386. set { _name = value; }
  387. }
  388. [XmlIgnore]
  389. public string Parent {
  390. get { return _parent; }
  391. set { _parent = value; }
  392. }
  393. }
  394. public class EntityCollection : ArrayList
  395. {
  396. public string _container;
  397. public EntityCollection ()
  398. {
  399. }
  400. public EntityCollection (string c)
  401. {
  402. _container = c;
  403. }
  404. public string Container {
  405. get { return _container; }
  406. set { _container = value; }
  407. }
  408. public int Add (Entity value)
  409. {
  410. if(_container != null)
  411. value.Parent = _container;
  412. return base.Add(value);
  413. }
  414. public new Entity this[int index]
  415. {
  416. get { return (Entity) base[index]; }
  417. set { base[index] = value; }
  418. }
  419. }
  420. [XmlType ("Container")]
  421. public class ObjectWithReadonlyCollection
  422. {
  423. EntityCollection collection1 = new EntityCollection ("root");
  424. public EntityCollection Collection1 {
  425. get { return collection1; }
  426. }
  427. }
  428. [XmlType ("Container")]
  429. public class ObjectWithReadonlyNulCollection
  430. {
  431. EntityCollection collection1;
  432. public EntityCollection Collection1 {
  433. get { return collection1; }
  434. }
  435. }
  436. [XmlType ("Container")]
  437. public class ObjectWithReadonlyArray
  438. {
  439. Entity[] collection1 = new Entity [0];
  440. public Entity[] Collection1 {
  441. get { return collection1; }
  442. }
  443. }
  444. [XmlInclude (typeof(SubclassTestSub))]
  445. public class SubclassTestBase
  446. {
  447. }
  448. public class SubclassTestSub: SubclassTestBase
  449. {
  450. }
  451. public class SubclassTestExtra
  452. {
  453. }
  454. public class SubclassTestContainer
  455. {
  456. [XmlElement ("a", typeof(SubclassTestBase))]
  457. [XmlElement ("b", typeof(SubclassTestExtra))]
  458. public object data;
  459. }
  460. public class DictionaryWithIndexer : DictionaryBase
  461. {
  462. public TimeSpan this[int index] {
  463. get { return TimeSpan.MinValue; }
  464. }
  465. public void Add (TimeSpan value)
  466. {
  467. }
  468. }
  469. [XmlRoot(Namespace="some:urn")]
  470. [SoapTypeAttribute (Namespace="another:urn")]
  471. public class PrimitiveTypesContainer
  472. {
  473. public PrimitiveTypesContainer ()
  474. {
  475. Number = 2004;
  476. Name = "some name";
  477. Index = (byte) 56;
  478. Password = new byte[] { 243, 15 };
  479. PathSeparatorCharacter = '/';
  480. }
  481. public int Number;
  482. public string Name;
  483. public byte Index;
  484. public byte[] Password;
  485. public char PathSeparatorCharacter;
  486. }
  487. public class TestSchemaForm1
  488. {
  489. public PrintTypeResponse p1;
  490. [XmlElement(Namespace="urn:oo")]
  491. public PrintTypeResponse p2;
  492. }
  493. [XmlType (Namespace="urn:testForm")]
  494. public class TestSchemaForm2
  495. {
  496. public PrintTypeResponse p1;
  497. [XmlElement(Namespace="urn:oo")]
  498. public PrintTypeResponse p2;
  499. }
  500. [XmlType (Namespace="urn:responseTypes")]
  501. public class PrintTypeResponse {
  502. [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
  503. public OutputType result;
  504. public PrintTypeResponse intern;
  505. public void Init ()
  506. {
  507. result = new OutputType ();
  508. result.data = "data1";
  509. intern = new PrintTypeResponse ();
  510. intern.result = new OutputType ();
  511. intern.result.data = "data2";
  512. }
  513. }
  514. [XmlType (Namespace="urn:responseTypes")]
  515. public class OutputType {
  516. [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
  517. public string data;
  518. }
  519. [XmlRootAttribute ("testDefault", Namespace="urn:myNS", IsNullable=false)]
  520. [SoapType("testDefault", Namespace="urn:myNS")]
  521. public class TestDefault
  522. {
  523. public string str;
  524. [DefaultValue ("Default Value")]
  525. public string strDefault = "Default Value";
  526. [DefaultValue (true)]
  527. public bool boolT = true;
  528. [DefaultValue (false)]
  529. public bool boolF = false;
  530. [DefaultValue (typeof (decimal), "10")]
  531. public decimal decimalval = 10m;
  532. [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
  533. public FlagEnum flag = (FlagEnum.e1 | FlagEnum.e4);
  534. [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4)]
  535. public FlagEnum_Encoded flagencoded = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4);
  536. }
  537. [XmlType ("optionalValueType", Namespace="some:urn")]
  538. [XmlRootAttribute ("optionalValue", Namespace="another:urn", IsNullable=false)]
  539. public class OptionalValueTypeContainer
  540. {
  541. [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
  542. public FlagEnum Attributes = FlagEnum.e1 | FlagEnum.e4;
  543. [DefaultValue (FlagEnum.e1)]
  544. public FlagEnum Flags = FlagEnum.e1;
  545. [XmlIgnore]
  546. [SoapIgnore]
  547. public bool FlagsSpecified;
  548. [DefaultValue (false)]
  549. public bool IsEmpty;
  550. [XmlIgnore]
  551. [SoapIgnore]
  552. public bool IsEmptySpecified {
  553. get { return _isEmptySpecified; }
  554. set { _isEmptySpecified = value; }
  555. }
  556. [DefaultValue (false)]
  557. public bool IsNull;
  558. private bool _isEmptySpecified;
  559. }
  560. }