XmlSerializerTestClasses.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. [SoapEnum ("PuBlIc")]
  166. Public = 0,
  167. [XmlEnum("protected")]
  168. Protected = 1,
  169. }
  170. public class MyList : ArrayList
  171. {
  172. object container;
  173. // NOTE: MyList has no public constructor
  174. public MyList (object container) : base()
  175. {
  176. this.container = container;
  177. }
  178. }
  179. public class Container
  180. {
  181. public MyList Items;
  182. public Container () {
  183. Items = new MyList(this);
  184. }
  185. }
  186. public class Container2
  187. {
  188. public MyList Items;
  189. public Container2 () {
  190. }
  191. public Container2 (bool b) {
  192. Items = new MyList(this);
  193. }
  194. }
  195. public class MyElem: XmlElement
  196. {
  197. public MyElem (XmlDocument doc): base ("","myelem","", doc)
  198. {
  199. SetAttribute ("aa","1");
  200. }
  201. [XmlAttribute]
  202. public int kk=1;
  203. }
  204. public class MyDocument: XmlDocument
  205. {
  206. public MyDocument ()
  207. {
  208. }
  209. [XmlAttribute]
  210. public int kk=1;
  211. }
  212. public class CDataContainer
  213. {
  214. public XmlCDataSection cdata;
  215. }
  216. public class NodeContainer
  217. {
  218. public XmlNode node;
  219. }
  220. public class Choices
  221. {
  222. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  223. [XmlElementAttribute("ChoiceOne", typeof(string), IsNullable=false)]
  224. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  225. [XmlChoiceIdentifier("ItemType")]
  226. public string MyChoice;
  227. [XmlIgnore]
  228. public ItemChoiceType ItemType;
  229. }
  230. [XmlType(IncludeInSchema = false)]
  231. public enum ItemChoiceType
  232. {
  233. ChoiceZero,
  234. [XmlEnum ("ChoiceOne")]
  235. StrangeOne,
  236. ChoiceTwo,
  237. }
  238. public class WrongChoices
  239. {
  240. [XmlElementAttribute("ChoiceZero", typeof(string), IsNullable=false)]
  241. [XmlElementAttribute("StrangeOne", typeof(string), IsNullable=false)]
  242. [XmlElementAttribute("ChoiceTwo", typeof(string), IsNullable=false)]
  243. [XmlChoiceIdentifier("ItemType")]
  244. public string MyChoice;
  245. [XmlIgnore]
  246. public ItemChoiceType ItemType;
  247. }
  248. [XmlType ("Type with space")]
  249. public class TestSpace
  250. {
  251. [XmlElement (ElementName = "Element with space")]
  252. public int elem;
  253. [XmlAttribute (AttributeName = "Attribute with space")]
  254. public int attr;
  255. }
  256. [Serializable]
  257. public class ReadOnlyProperties {
  258. string[] strArr = new string[2] { "string1", "string2" };
  259. public string[] StrArr {
  260. get { return strArr; }
  261. }
  262. public string dat {
  263. get { return "fff"; }
  264. }
  265. }
  266. [XmlRoot("root")]
  267. public class ListDefaults
  268. {
  269. public ListDefaults ()
  270. {
  271. ed = new SimpleClass ();
  272. str = "hola";
  273. }
  274. public ArrayList list2;
  275. public MyList list3;
  276. public string[] list4;
  277. [XmlElement("e", typeof(SimpleClass))]
  278. public ArrayList list5;
  279. [DefaultValue (null)]
  280. public SimpleClass ed;
  281. [DefaultValue (null)]
  282. public string str;
  283. }
  284. public class clsPerson
  285. {
  286. public IList EmailAccounts;
  287. }
  288. public class ArrayClass
  289. {
  290. public object names = new object[] { "un","dos" };
  291. }
  292. public class CompositeValueType
  293. {
  294. public void Init ()
  295. {
  296. Items = new object[] { 1, 2 };
  297. ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.In, ItemsChoiceType.Es };
  298. }
  299. [XmlElementAttribute("Es", typeof(int))]
  300. [XmlElementAttribute("In", typeof(int))]
  301. [XmlChoiceIdentifierAttribute("ItemsElementName")]
  302. public object[] Items;
  303. [XmlElementAttribute("ItemsElementName")]
  304. [XmlIgnoreAttribute()]
  305. public ItemsChoiceType[] ItemsElementName;
  306. }
  307. public enum ItemsChoiceType {
  308. In, Es
  309. }
  310. public class ArrayAttributeWithType
  311. {
  312. [XmlAttribute (DataType="anyURI")]
  313. public string[] at = new string [] { "a","b" };
  314. [XmlAttribute (DataType="base64Binary")]
  315. public byte[][] bin1 = new byte[][] { new byte[]{1,2}, new byte[]{1,2}};
  316. [XmlAttribute (DataType="base64Binary")]
  317. public byte[] bin2 = new byte[] { 1,2 };
  318. }
  319. public class ArrayAttributeWithWrongType
  320. {
  321. [XmlAttribute (DataType="int")]
  322. public string[] at = new string [] { "a","b" };
  323. }
  324. [XmlType ("Container")]
  325. public class EntityContainer
  326. {
  327. EntityCollection collection1;
  328. EntityCollection collection2;
  329. EntityCollection collection3 = new EntityCollection ("root");
  330. EntityCollection collection4 = new EntityCollection ("root");
  331. [XmlArray (IsNullable=true)]
  332. public EntityCollection Collection1 {
  333. get { return collection1; }
  334. set { collection1 = value; collection1.Container = "assigned"; }
  335. }
  336. [XmlArray (IsNullable=false)]
  337. public EntityCollection Collection2 {
  338. get { return collection2; }
  339. set { collection2 = value; collection2.Container = "assigned"; }
  340. }
  341. [XmlArray (IsNullable=true)]
  342. public EntityCollection Collection3 {
  343. get { return collection3; }
  344. set { collection3 = value; collection3.Container = "assigned"; }
  345. }
  346. [XmlArray (IsNullable=false)]
  347. public EntityCollection Collection4 {
  348. get { return collection4; }
  349. set { collection4 = value; collection4.Container = "assigned"; }
  350. }
  351. }
  352. [XmlType ("Container")]
  353. public class ArrayEntityContainer
  354. {
  355. Entity[] collection1;
  356. Entity[] collection2;
  357. Entity[] collection3 = new Entity [0];
  358. Entity[] collection4 = new Entity [0];
  359. [XmlArray (IsNullable=true)]
  360. public Entity[] Collection1 {
  361. get { return collection1; }
  362. set { collection1 = value; }
  363. }
  364. [XmlArray (IsNullable=false)]
  365. public Entity[] Collection2 {
  366. get { return collection2; }
  367. set { collection2 = value; }
  368. }
  369. [XmlArray (IsNullable=true)]
  370. public Entity[] Collection3 {
  371. get { return collection3; }
  372. set { collection3 = value; }
  373. }
  374. [XmlArray (IsNullable=false)]
  375. public Entity[] Collection4 {
  376. get { return collection4; }
  377. set { collection4 = value; }
  378. }
  379. }
  380. public class Entity
  381. {
  382. private string _name = string.Empty;
  383. private string _parent = null;
  384. [XmlAttribute]
  385. public string Name {
  386. get { return _name; }
  387. set { _name = value; }
  388. }
  389. [XmlIgnore]
  390. public string Parent {
  391. get { return _parent; }
  392. set { _parent = value; }
  393. }
  394. }
  395. public class EntityCollection : ArrayList
  396. {
  397. public string _container;
  398. public EntityCollection ()
  399. {
  400. }
  401. public EntityCollection (string c)
  402. {
  403. _container = c;
  404. }
  405. public string Container {
  406. get { return _container; }
  407. set { _container = value; }
  408. }
  409. public int Add (Entity value)
  410. {
  411. if(_container != null)
  412. value.Parent = _container;
  413. return base.Add(value);
  414. }
  415. public new Entity this[int index]
  416. {
  417. get { return (Entity) base[index]; }
  418. set { base[index] = value; }
  419. }
  420. }
  421. [XmlType ("Container")]
  422. public class ObjectWithReadonlyCollection
  423. {
  424. EntityCollection collection1 = new EntityCollection ("root");
  425. public EntityCollection Collection1 {
  426. get { return collection1; }
  427. }
  428. }
  429. [XmlType ("Container")]
  430. public class ObjectWithReadonlyNulCollection
  431. {
  432. EntityCollection collection1;
  433. public EntityCollection Collection1 {
  434. get { return collection1; }
  435. }
  436. }
  437. [XmlType ("Container")]
  438. public class ObjectWithReadonlyArray
  439. {
  440. Entity[] collection1 = new Entity [0];
  441. public Entity[] Collection1 {
  442. get { return collection1; }
  443. }
  444. }
  445. [XmlInclude (typeof(SubclassTestSub))]
  446. public class SubclassTestBase
  447. {
  448. }
  449. public class SubclassTestSub: SubclassTestBase
  450. {
  451. }
  452. public class SubclassTestExtra
  453. {
  454. }
  455. public class SubclassTestContainer
  456. {
  457. [XmlElement ("a", typeof(SubclassTestBase))]
  458. [XmlElement ("b", typeof(SubclassTestExtra))]
  459. public object data;
  460. }
  461. public class DictionaryWithIndexer : DictionaryBase
  462. {
  463. public TimeSpan this[int index] {
  464. get { return TimeSpan.MinValue; }
  465. }
  466. public void Add (TimeSpan value)
  467. {
  468. }
  469. }
  470. [XmlRoot(Namespace="some:urn")]
  471. [SoapTypeAttribute (Namespace="another:urn")]
  472. public class PrimitiveTypesContainer
  473. {
  474. public PrimitiveTypesContainer ()
  475. {
  476. Number = 2004;
  477. Name = "some name";
  478. Index = (byte) 56;
  479. Password = new byte[] { 243, 15 };
  480. PathSeparatorCharacter = '/';
  481. }
  482. public int Number;
  483. public string Name;
  484. public byte Index;
  485. public byte[] Password;
  486. public char PathSeparatorCharacter;
  487. }
  488. public class TestSchemaForm1
  489. {
  490. public PrintTypeResponse p1;
  491. [XmlElement(Namespace="urn:oo")]
  492. public PrintTypeResponse p2;
  493. }
  494. [XmlType (Namespace="urn:testForm")]
  495. public class TestSchemaForm2
  496. {
  497. public PrintTypeResponse p1;
  498. [XmlElement(Namespace="urn:oo")]
  499. public PrintTypeResponse p2;
  500. }
  501. [XmlType (Namespace="urn:responseTypes")]
  502. public class PrintTypeResponse {
  503. [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
  504. public OutputType result;
  505. public PrintTypeResponse intern;
  506. public void Init ()
  507. {
  508. result = new OutputType ();
  509. result.data = "data1";
  510. intern = new PrintTypeResponse ();
  511. intern.result = new OutputType ();
  512. intern.result.data = "data2";
  513. }
  514. }
  515. [XmlType (Namespace="urn:responseTypes")]
  516. public class OutputType {
  517. [XmlElement (Form=XmlSchemaForm.Unqualified, IsNullable=true)]
  518. public string data;
  519. }
  520. [XmlRootAttribute ("testDefault", Namespace="urn:myNS", IsNullable=false)]
  521. [SoapType("testDefault", Namespace="urn:myNS")]
  522. public class TestDefault
  523. {
  524. public string str;
  525. [DefaultValue ("Default Value")]
  526. public string strDefault = "Default Value";
  527. [DefaultValue (true)]
  528. public bool boolT = true;
  529. [DefaultValue (false)]
  530. public bool boolF = false;
  531. [DefaultValue (typeof (decimal), "10")]
  532. public decimal decimalval = 10m;
  533. [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
  534. public FlagEnum flag = (FlagEnum.e1 | FlagEnum.e4);
  535. [DefaultValue (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4)]
  536. public FlagEnum_Encoded flagencoded = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4);
  537. }
  538. [XmlType ("optionalValueType", Namespace="some:urn")]
  539. [XmlRootAttribute ("optionalValue", Namespace="another:urn", IsNullable=false)]
  540. public class OptionalValueTypeContainer
  541. {
  542. [DefaultValue (FlagEnum.e1 | FlagEnum.e4)]
  543. public FlagEnum Attributes = FlagEnum.e1 | FlagEnum.e4;
  544. [DefaultValue (FlagEnum.e1)]
  545. public FlagEnum Flags = FlagEnum.e1;
  546. [XmlIgnore]
  547. [SoapIgnore]
  548. public bool FlagsSpecified;
  549. [DefaultValue (false)]
  550. public bool IsEmpty;
  551. [XmlIgnore]
  552. [SoapIgnore]
  553. public bool IsEmptySpecified {
  554. get { return _isEmptySpecified; }
  555. set { _isEmptySpecified = value; }
  556. }
  557. [DefaultValue (false)]
  558. public bool IsNull;
  559. private bool _isEmptySpecified;
  560. }
  561. public class Group
  562. {
  563. [SoapAttribute (Namespace = "http://www.cpandl.com")]
  564. public string GroupName;
  565. [SoapAttribute (DataType = "base64Binary")]
  566. public Byte [] GroupNumber;
  567. [SoapAttribute (DataType = "date", AttributeName = "CreationDate")]
  568. public DateTime Today;
  569. [SoapElement (DataType = "nonNegativeInteger", ElementName = "PosInt")]
  570. public string PostitiveInt;
  571. [SoapIgnore]
  572. public bool IgnoreThis;
  573. [DefaultValue (GroupType.B)]
  574. public GroupType Grouptype;
  575. public Vehicle MyVehicle;
  576. [SoapInclude (typeof (Car))]
  577. public Vehicle myCar (string licNumber)
  578. {
  579. Vehicle v;
  580. if (licNumber == string.Empty) {
  581. v = new Car();
  582. v.licenseNumber = "!!!!!!";
  583. } else {
  584. v = new Car();
  585. v.licenseNumber = licNumber;
  586. }
  587. return v;
  588. }
  589. }
  590. [SoapInclude (typeof (Car))]
  591. public abstract class Vehicle
  592. {
  593. public string licenseNumber;
  594. [SoapElement (DataType = "date")]
  595. public DateTime makeDate;
  596. [DefaultValue ("450")]
  597. public string weight;
  598. }
  599. public class Car: Vehicle
  600. {
  601. }
  602. public enum GroupType
  603. {
  604. [SoapEnum ("Small")]
  605. A,
  606. [SoapEnum ("Large")]
  607. B
  608. }
  609. }