ComplexDataStructure.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. //
  2. // ComplexDataStructure.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc.
  8. //
  9. //
  10. using System;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15. using System.Collections;
  16. using System.ComponentModel;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.XmlSerialization
  19. {
  20. [TestFixture]
  21. public class ComplexDataStructure
  22. {
  23. [Test]
  24. [NUnit.Framework.Category("NotDotNet")] // FDBK50639
  25. public void WriteLiteral ()
  26. {
  27. Test data = BuildTestObject ();
  28. XmlSerializer ss = new XmlSerializer (GetLiteralTypeMapping ());
  29. XmlSerializerNamespaces nams = new XmlSerializerNamespaces ();
  30. StringWriter sw = new StringWriter();
  31. ss.Serialize (sw,data,nams);
  32. string serialized = sw.ToString ();
  33. serialized = XmlSerializerTests.Infoset (serialized);
  34. StreamReader sr = new StreamReader ("Test/XmlFiles/literal-data.xml");
  35. string expected = sr.ReadToEnd ();
  36. sr.Close ();
  37. expected = XmlSerializerTests.Infoset (expected);
  38. Assert.AreEqual (expected, serialized);
  39. }
  40. [Test]
  41. [NUnit.Framework.Category ("NotDotNet")] // MS.NET results in compilation error (probably it generates bogus source.)
  42. public void ReadLiteral ()
  43. {
  44. XmlSerializer ss = new XmlSerializer (GetLiteralTypeMapping ());
  45. XmlSerializerNamespaces nams = new XmlSerializerNamespaces ();
  46. StreamReader sr = new StreamReader ("Test/XmlFiles/literal-data.xml");
  47. Test data = (Test) ss.Deserialize (sr);
  48. sr.Close ();
  49. CheckObjectContent (BuildTestObject(), data);
  50. }
  51. XmlTypeMapping GetLiteralTypeMapping ()
  52. {
  53. XmlRootAttribute root = new XmlRootAttribute("rootroot");
  54. Type[] types = new Type[] {typeof(UknTestPart), typeof(AnotherTestPart), typeof(DblStringContainer) };
  55. XmlReflectionImporter ri = new XmlReflectionImporter ();
  56. foreach (Type t in types) ri.IncludeType (t);
  57. return ri.ImportTypeMapping (typeof(Test), root);
  58. }
  59. XmlTypeMapping GetEncodedTypeMapping ()
  60. {
  61. SoapReflectionImporter sri = new SoapReflectionImporter ();
  62. sri.IncludeType (typeof(UknTestPart));
  63. sri.IncludeType (typeof(AnotherTestPart));
  64. sri.IncludeType (typeof(DblStringContainer));
  65. return sri.ImportTypeMapping (typeof(Test));
  66. }
  67. Test BuildTestObject ()
  68. {
  69. XmlDocument doc = new XmlDocument();
  70. Test t = new UknTestPart();
  71. t.a = 1;
  72. t.b = "hola";
  73. t.bbis = t.b;
  74. t.c = 44;
  75. t.parts = new TestPart[3];
  76. t.parts[0] = new TestPart();
  77. t.parts[0].name = "un";
  78. t.parts[0].bval = true;
  79. t.parts[1] = new TestPart();
  80. t.parts[1].name = "dos";
  81. t.parts[1].bval = false;
  82. t.parts[2] = t.parts[0];
  83. t.part = t.parts[1];
  84. t.strings = new string[] { "un", "dos", null, "tres" };
  85. t.ushorts = new ushort[] { 1,2,3 };
  86. t.ta = new TB();
  87. t.ta.extraTextNodes = new XmlNode[] { doc.CreateTextNode ("AA"), doc.CreateTextNode ("BB") };
  88. t.tam2 = new TA[][][]
  89. {
  90. new TA[][] { new TA[] {new TA(), new TA()}, new TA[] {new TA(), new TA()}},
  91. new TA[][] { new TA[] {new TB(), new TA()}, new TA[] {new TB(), new TA()}},
  92. new TA[][] { new TA[] {new TA(), new TB()}, new TA[] {new TA(), new TA()}}
  93. };
  94. t.tam3 = t.tam2;
  95. t.flatParts = t.parts;
  96. t.flatParts2 = new TA[] {new TA(), new TB(), null, new TB()};
  97. t.anot = new AnotherTestPart ();
  98. ((AnotherTestPart)t.anot).lo = 1234567890;
  99. t.ob = t.parts[1];
  100. t.ob2 = t.parts[1];
  101. XmlElement e1 = doc.CreateElement ("explicitElement");
  102. XmlElement e2 = doc.CreateElement ("subElement");
  103. e2.SetAttribute ("unAtrib","val");
  104. doc.AppendChild (e1);
  105. e1.AppendChild (e2);
  106. t.oneElem = e1;
  107. t.oneElem2 = e1;
  108. t.someElems = new XmlNode[3];
  109. t.someElems[0] = e1;
  110. t.someElems[1] = null;
  111. t.someElems[2] = e2;
  112. t.extraElems = new XmlElement[1];
  113. t.extraElems[0] = doc.CreateElement ("extra1");
  114. t.extraElems[0].SetAttribute ("val","1");
  115. t.extraElems23 = new XmlElement[2];
  116. t.extraElems23[0] = doc.CreateElement ("extra2");
  117. t.extraElems23[0].SetAttribute ("val","2");
  118. t.extraElems23[1] = doc.CreateElement ("extra3");
  119. t.extraElems23[1].SetAttribute ("val","3");
  120. t.extraElemsRest = doc.CreateElement ("extra4");
  121. t.extraElemsRest.SetAttribute ("val","4");
  122. t.uktester = new UnknownAttributeTester();
  123. t.uktester.aa = "hihi";
  124. t.uktester.extraAtts = new XmlAttribute[3];
  125. t.uktester.extraAtts[0] = doc.CreateAttribute ("extraAtt1");
  126. t.uktester.extraAtts[0].Value = "val1";
  127. t.uktester.extraAtts[1] = doc.CreateAttribute ("extraAtt2");
  128. t.uktester.extraAtts[1].Value = "val2";
  129. t.uktester.extraAtts[2] = doc.CreateAttribute ("extraAtt3");
  130. t.uktester.extraAtts[2].Value = "val3";
  131. t.ob3 = 12345;
  132. t.ob4 = (float)54321.12;
  133. t.op1 = option.AA;
  134. t.opArray = new option[] { option.CC, option.BB, option.AA };
  135. t.ukOpt = option.DD;
  136. t.opAtt = option.BB;
  137. t.byteArray = new byte[] { 11,33,55,222 };
  138. t.byteByteArray = new byte[][] { t.byteArray, t.byteArray };
  139. t.ttList = new ArrayList();
  140. t.ttList.Add ("two");
  141. t.ttList.Add ("strings");
  142. // t.extraText = "Additional text";
  143. t.RoList = new ArrayList ();
  144. t.RoList.Add (t.parts[0]);
  145. t.RoList.Add (t.parts[1]);
  146. /* t.struc = new OneStruct();
  147. t.struc.aa = 776655;
  148. t.struc.cc = "this is a struct";
  149. */
  150. t.multiList = new ArrayList[2];
  151. t.multiList[0] = new ArrayList ();
  152. t.multiList[0].Add (22);
  153. t.multiList[0].Add (33);
  154. t.multiList[1] = new ArrayList ();
  155. t.multiList[1].Add (888);
  156. t.multiList[1].Add (999);
  157. // XmlSerializer does not deserialize default values explicitly.
  158. //t.defElem = "theDefValue";
  159. //t.defAttr = "theDefValue";
  160. t.special = new CustomHashtable ();
  161. t.special.Add ("one","1");
  162. t.special.Add ("two","2");
  163. t.special.Add ("three","3");
  164. t.attqname = new XmlQualifiedName ("thename","thenamespace");
  165. DblStringContainer dbc = new DblStringContainer ();
  166. dbc.doublestring = new string [][] { null, new string[] {"hello"} };
  167. AnotherTestPart at = new AnotherTestPart ();
  168. at.lo = 567;
  169. dbc.at = at;
  170. DblStringContainerAnm dbca = new DblStringContainerAnm ();
  171. dbca.at = dbc;
  172. t.dbscontainer = dbca;
  173. return t;
  174. }
  175. void CheckObjectContent (Test exp, Test t)
  176. {
  177. Assert.AreEqual (exp.a, t.a, "t.a");
  178. Assert.AreEqual (exp.b, t.b, "t.b");
  179. Assert.AreEqual (exp.bbis, t.bbis, "t.bbis");
  180. Assert.AreEqual (exp.c, t.c, "t.c");
  181. Assert.IsNotNull (t.parts, "t.parts");
  182. CheckParts ("t.parts", exp.parts, t.parts);
  183. TestPart.AssertEquals ("t.part", exp.part, t.part);
  184. AssertionHelper.AssertEqualsArray ("t.strings", exp.strings, t.strings);
  185. AssertionHelper.AssertEqualsArray ("t.ushorts", exp.ushorts, t.ushorts);
  186. TA.AssertEquals ("t.ta", exp.ta, t.ta);
  187. Assert.IsNotNull (t.tam2, "t.tam2");
  188. CheckTaArray ("t.tam2", exp.tam2, t.tam2);
  189. Assert.IsNotNull (t.tam3, "t.tam3");
  190. CheckTaArray ("t.tam3", exp.tam3, t.tam3);
  191. Assert.IsNotNull (t.flatParts, "t.flatParts");
  192. CheckParts ("t.flatParts", exp.flatParts, t.flatParts);
  193. // Null element is ignored
  194. Assert.IsNotNull (t.flatParts2, "t.flatParts2");
  195. Assert.AreEqual (3, t.flatParts2.Length, "t.flatParts2.Length");
  196. TA.AssertEquals ("t.flatParts2 0", exp.flatParts2[0], t.flatParts2[0]);
  197. TA.AssertEquals ("t.flatParts2 1", exp.flatParts2[1], t.flatParts2[1]);
  198. TA.AssertEquals ("t.flatParts2 2", exp.flatParts2[3], t.flatParts2[2]);
  199. Assert.IsNotNull (t.anot, "t.anot");
  200. Assert.AreEqual (((AnotherTestPart)exp.anot).lo, ((AnotherTestPart)t.anot).lo, "t.anot.lo");
  201. TestPart.AssertEquals ("t.ob", exp.ob as TestPart, t.ob as TestPart);
  202. TestPart.AssertEquals ("t.ob2", exp.ob2 as TestPart, t.ob2 as TestPart);
  203. AssertionHelper.AssertEqualsXml ("t.oneElem", exp.oneElem, t.oneElem);
  204. AssertionHelper.AssertEqualsXml ("t.oneElem2", exp.oneElem2, t.oneElem2);
  205. // One of the elements was null and it is ignored
  206. Assert.IsNotNull (t.someElems, "t.someElems");
  207. Assert.AreEqual (2, t.someElems.Length, "t.someElems.Length");
  208. AssertionHelper.AssertEqualsXml ("t.someElems[0]", exp.someElems[0], t.someElems[0]);
  209. AssertionHelper.AssertEqualsXml ("t.someElems[1]", exp.someElems[2], t.someElems[1]);
  210. Assert.IsNotNull (t.extraElems, "t.extraElems");
  211. Assert.AreEqual (exp.extraElems.Length, t.extraElems.Length, "t.extraElems.Length");
  212. for (int n=0; n<exp.extraElems.Length; n++)
  213. AssertionHelper.AssertEqualsXml ("t.extraElems[" + n + "]", exp.extraElems[n], t.extraElems[n]);
  214. Assert.IsNotNull (t.extraElems23, "t.extraElems23");
  215. Assert.AreEqual (exp.extraElems23.Length, t.extraElems23.Length, "t.extraElems23.Length");
  216. for (int n=0; n<t.extraElems23.Length; n++)
  217. AssertionHelper.AssertEqualsXml ("t.extraElems23[" + n + "]", exp.extraElems23[n], t.extraElems23[n]);
  218. AssertionHelper.AssertEqualsXml ("t.extraElemsRest", exp.extraElemsRest, t.extraElemsRest);
  219. UnknownAttributeTester.AssertEquals ("t.uktester", exp.uktester, t.uktester);
  220. Assert.AreEqual (exp.ob3, t.ob3, "t.ob3");
  221. Assert.AreEqual (exp.ob4, t.ob4, "t.ob4");
  222. Assert.AreEqual (exp.op1, t.op1, "t.op1");
  223. AssertionHelper.AssertEqualsArray ("t.opArray", exp.opArray, t.opArray);
  224. Assert.AreEqual (exp.ukOpt, t.ukOpt, "t.ukOpt");
  225. Assert.AreEqual (exp.opAtt, t.opAtt, "t.opAtt");
  226. AssertionHelper.AssertEqualsArray ("t.byteArray", exp.byteArray, t.byteArray);
  227. AssertionHelper.AssertEqualsArray ("t.byteByteArray", exp.byteByteArray, t.byteByteArray);
  228. Assert.IsNotNull (t.ttList, "t.ttList");
  229. AssertionHelper.AssertEqualsArray ("t.ttList", exp.ttList.ToArray(), t.ttList.ToArray());
  230. Assert.IsNotNull (t.RoList, "t.RoList");
  231. // Assert.AreEqual (exp.RoList.Count, t.RoList.Count, "t.RoList.Count");
  232. for (int n=0; n<t.RoList.Count; n++)
  233. TestPart.AssertEquals ("t.RoList " + n, (TestPart)exp.RoList[n], (TestPart)t.RoList[n]);
  234. Assert.AreEqual (exp.struc.aa, t.struc.aa, "t.struc.aa");
  235. Assert.AreSame (exp.struc.cc, t.struc.cc, "t.struc.cc");
  236. Assert.IsNull (t.multiList, "t.multiList");
  237. // Assert.AreEqual (exp.multiList.Length, t.multiList.Length, "t.multiList.Count");
  238. // for (int n=0; n<exp.multiList.Length; n++)
  239. // AssertionHelper.AssertEqualsArray ("t.multiList " + n, exp.multiList[n].ToArray(), t.multiList[n].ToArray());
  240. Assert.AreEqual (exp.defElem, t.defElem, "t.defElem");
  241. Assert.AreEqual (exp.defAttr, t.defAttr, "t.defAttr");
  242. CustomHashtable.AssertEquals ("t.special", exp.special, t.special);
  243. Assert.AreEqual (exp.attqname, t.attqname, "t.attqname");
  244. Assert.IsNull (t.dbscontainer, "t.dbscontainer");
  245. // DblStringContainer tdbca = t.dbscontainer.at as DblStringContainer;
  246. // DblStringContainer expdbca = exp.dbscontainer.at as DblStringContainer;
  247. // Assert.IsNotNull (tdbca, "t.dbscontainer.at");
  248. // Assert.IsNotNull (tdbca, "t.dbscontainer.dbca");
  249. // AssertionHelper.AssertEqualsArray ("t.dbscontainer.at.doublestring", expdbca.doublestring, tdbca.doublestring);
  250. // AnotherTestPart tat = tdbca.at as AnotherTestPart;
  251. // AnotherTestPart expat = expdbca.at as AnotherTestPart;
  252. // Assert.IsNotNull (tat, "t.dbscontainer.dbca.at");
  253. // Assert.AreEqual (expat.lo, tat.lo, "t.dbscontainer.dbca.at.lo");
  254. }
  255. void CheckParts (string id, TestPart[] exp, TestPart[] parts)
  256. {
  257. AssertionHelper.AssertType (id, exp, parts);
  258. Assert.AreEqual (exp.Length, parts.Length, id + " Len");
  259. for (int n=0; n<exp.Length; n++)
  260. TestPart.AssertEquals (id + "[" + n + "]", exp[n], parts[n]);
  261. }
  262. void CheckTaArray (string id, TA[][][] exp, TA[][][] arr)
  263. {
  264. AssertionHelper.AssertType (id, exp, arr);
  265. Assert.AreEqual (exp.Length, arr.Length, id + " Len");
  266. for (int n=0; n<exp.Length; n++)
  267. {
  268. TA[][] tar = arr[n];
  269. TA[][] expar = exp[n];
  270. AssertionHelper.AssertType (id, expar, tar);
  271. Assert.AreEqual (expar.Length, tar.Length);
  272. for (int m=0; m<expar.Length; m++)
  273. {
  274. TA[] tar2 = tar[m];
  275. TA[] expar2 = expar[m];
  276. AssertionHelper.AssertType (id, expar2, tar2);
  277. Assert.AreEqual (expar2.Length, tar2.Length, id);
  278. for (int i=0; i<expar2.Length; i++)
  279. TA.AssertEquals (id + "[" + n + "][" + m + "][" + i + "]", expar2[i], tar2[i]);
  280. }
  281. }
  282. }
  283. }
  284. public class AssertionHelper
  285. {
  286. public static bool AssertType (string id, object exp, object ob)
  287. {
  288. if (exp == null) {
  289. Assert.IsNull (ob, id);
  290. return false;
  291. }
  292. else {
  293. Assert.IsNotNull (ob, id);
  294. Assert.AreEqual (exp.GetType(), ob.GetType(), id + " type");
  295. return true;
  296. }
  297. }
  298. public static void AssertEqualsXml (string id, XmlNode exp, XmlNode ob)
  299. {
  300. if (!AssertType (id, exp, ob)) return;
  301. Assert.AreEqual (XmlSerializerTests.Infoset (exp), XmlSerializerTests.Infoset (ob), id);
  302. }
  303. public static void AssertEqualsArray (string id, Array exp, Array ob)
  304. {
  305. if (!AssertType (id, exp, ob)) return;
  306. Assert.AreEqual (exp.GetLength(0), ob.GetLength(0), id + " Length");
  307. for (int n=0; n<exp.GetLength(0); n++) {
  308. object it = exp.GetValue(n);
  309. if (it is Array)
  310. AssertEqualsArray (id + "[" + n + "]", it as Array, ob.GetValue(n) as Array);
  311. else
  312. Assert.AreEqual (it, ob.GetValue(n), id + "[" + n + "]");
  313. }
  314. }
  315. }
  316. [XmlType(TypeName="")]
  317. [XmlRoot(ElementName="aaaaaa",Namespace="")]
  318. [XmlInclude(typeof(UknTestPart))]
  319. [SoapInclude(typeof(UknTestPart))]
  320. public class Test
  321. {
  322. // public option op;elem.SchemaTypeName
  323. public object anot;
  324. [SoapElement(ElementName="suba")]
  325. [XmlElement(ElementName="suba",Namespace="kk")]
  326. public int a;
  327. public string b;
  328. public string bbis;
  329. [SoapAttribute]
  330. [XmlAttribute (Namespace="attribns")]
  331. public byte c;
  332. [XmlElement(Namespace="oo")]
  333. public TestPart part;
  334. public TA ta;
  335. public TestPart[] parts;
  336. [SoapElement(ElementName="multita")]
  337. [XmlArray(ElementName="multita")]
  338. // [XmlArrayItem(ElementName="itema",NestingLevel=1)]
  339. [XmlArrayItem(ElementName="itema",Type=typeof(TA),NestingLevel=1)]
  340. [XmlArrayItem(ElementName="itemb",Type=typeof(TB),NestingLevel=1)]
  341. public TA[][] tam = new TA[][] { new TA[] {new TA(), new TB()}, new TA[] {new TA(), new TA()}};
  342. // [SoapElement(ElementName="multita2")]
  343. [SoapIgnore]
  344. [XmlArray(ElementName="multita2")]
  345. [XmlArrayItem(ElementName="data1",NestingLevel=0)]
  346. [XmlArrayItem(ElementName="data2",NestingLevel=1,Namespace="da2")]
  347. [XmlArrayItem(ElementName="data3a",Type=typeof(TA),NestingLevel=2,Namespace="da3")]
  348. [XmlArrayItem(ElementName="data3b",Type=typeof(TB),NestingLevel=2,Namespace="da3")]
  349. public TA[][][] tam2;
  350. [SoapIgnore]
  351. public TA[][][] tam3;
  352. [SoapElement(IsNullable=true)]
  353. [XmlElement(IsNullable=true)]
  354. public string mayBeNull;
  355. public string[] strings;
  356. [XmlArray(Namespace="arrayNamespace")]
  357. public ushort[] ushorts;
  358. [XmlElement]
  359. public TestPart[] flatParts;
  360. [SoapElement (ElementName="flatTAs")]
  361. [XmlElement (ElementName="flatTAs")]
  362. public TA[] flatParts2;
  363. public object ob;
  364. [XmlElement (Namespace="uimp")]
  365. public object ob2;
  366. public object ob3;
  367. public object ob4;
  368. [SoapIgnore]
  369. public XmlElement oneElem;
  370. [SoapIgnore]
  371. [XmlElement (ElementName="unElement", Namespace="elemns")]
  372. public XmlElement oneElem2;
  373. [SoapIgnore]
  374. [XmlElement (ElementName="unsElements", Namespace="elemns")]
  375. public XmlNode[] someElems;
  376. [SoapIgnore]
  377. [XmlAnyElement ("extra1")]
  378. public XmlElement[] extraElems;
  379. [SoapIgnore]
  380. [XmlAnyElement ("extra2")]
  381. [XmlAnyElement ("extra3")]
  382. [XmlAnyElement ("extra3","nnn")]
  383. public XmlElement[] extraElems23;
  384. [SoapIgnore]
  385. [XmlAnyElement]
  386. public XmlElement extraElemsRest;
  387. public UnknownAttributeTester uktester;
  388. public option op1;
  389. public option[] opArray;
  390. public object ukOpt;
  391. [XmlAttribute]
  392. [SoapIgnore]
  393. public option opAtt;
  394. public byte[] byteArray;
  395. [SoapIgnore]
  396. public byte[][] byteByteArray;
  397. [XmlElement(Type=typeof(string))]
  398. [XmlElement(ElementName="kk",Type=typeof(int))]
  399. public object[] tt = new object[] { "aa",22 };
  400. public ArrayList ttList;
  401. ArrayList roList;
  402. public ArrayList RoList
  403. {
  404. get { return roList; }
  405. set { roList = value; }
  406. }
  407. [SoapIgnore]
  408. [XmlIgnore] // Causes NRE in System.Xml.Serialization.CodeGenerator.GetVariableType (System.Object var)
  409. public ArrayList[] multiList;
  410. [SoapIgnore]
  411. [XmlIgnore]
  412. public OneStruct struc;
  413. [DefaultValue("theDefValue")]
  414. public string defElem;
  415. [XmlAttribute]
  416. [DefaultValue("theDefValue")]
  417. public string defAttr;
  418. [XmlText (Type=typeof(string))]
  419. [XmlElement (Type=typeof(int))]
  420. public object[] xmltext = new object[] {"aa",33,"bb",776};
  421. [SoapIgnore]
  422. public CustomHashtable special;
  423. [XmlAttribute]
  424. public XmlQualifiedName attqname;
  425. [XmlAttribute]
  426. public DateTime[] arrayAttribute;
  427. [XmlArray (Namespace="mm")]
  428. public string[][] dummyStringArray = new string[][] {null,null};
  429. [XmlElement (Namespace="mm")]
  430. public DblStringContainerAnm dbscontainer;
  431. }
  432. public class DblStringContainerAnm
  433. {
  434. public object at;
  435. }
  436. [XmlType(Namespace="mm")]
  437. public class DblStringContainer
  438. {
  439. [XmlArrayItem (NestingLevel=1, IsNullable=true)]
  440. public string [][] doublestring;
  441. public object at;
  442. }
  443. [SoapType(TypeName="TC")]
  444. [XmlType(TypeName="TC")]
  445. [XmlInclude(typeof(TB))]
  446. [SoapInclude(typeof(TB))]
  447. public class TA
  448. {
  449. public int xx = 1;
  450. [XmlText]
  451. [SoapIgnore]
  452. public XmlNode[] extraTextNodes;
  453. public static void AssertEquals (string id, TA expected, TA ob)
  454. {
  455. if (!AssertionHelper.AssertType (id, expected, ob)) return;
  456. Assert.AreEqual (expected.xx, ob.xx, id + " xx");
  457. // TODO: extraTextNodes
  458. }
  459. }
  460. public class TB: TA
  461. {
  462. public int yy = 2;
  463. public static void AssertEquals (string id, TB expected, TB ob)
  464. {
  465. if (!AssertionHelper.AssertType (id, expected, ob)) return;
  466. Assert.AreEqual (expected.yy, ob.yy, id + " yy");
  467. TA.AssertEquals (id + " base", expected, ob);
  468. }
  469. }
  470. public class UnknownAttributeTester
  471. {
  472. [SoapAttribute]
  473. [XmlAttribute]
  474. public string aa;
  475. [SoapIgnore]
  476. [XmlAnyAttribute]
  477. public XmlAttribute[] extraAtts;
  478. // [XmlText(Type=typeof(XmlNode))]
  479. // public XmlNode extraText;
  480. public static void AssertEquals (string id, UnknownAttributeTester exp, UnknownAttributeTester ob)
  481. {
  482. if (!AssertionHelper.AssertType (id, exp, ob)) return;
  483. Assert.AreEqual (exp.aa, ob.aa, id + " aa");
  484. if (!AssertionHelper.AssertType (id + " extraAtts", exp.extraAtts, ob.extraAtts)) return;
  485. int p = 0;
  486. for (int n=0; n<ob.extraAtts.Length; n++)
  487. {
  488. XmlAttribute at = ob.extraAtts [n];
  489. if (at.NamespaceURI == "http://www.w3.org/2000/xmlns/") continue;
  490. Assert.IsTrue (p < exp.extraAtts.Length, id + " extraAtts length");
  491. AssertionHelper.AssertEqualsXml (id + ".extraAtts " + n, exp.extraAtts [p], ob.extraAtts[n]);
  492. p++;
  493. }
  494. }
  495. }
  496. [SoapType(TypeName="UnTestPart", Namespace="mm")]
  497. [XmlType(TypeName="UnTestPart", Namespace="mm")]
  498. public class TestPart
  499. {
  500. public string name;
  501. public bool bval;
  502. public static void AssertEquals (string id, TestPart expected, TestPart ob)
  503. {
  504. if (!AssertionHelper.AssertType (id, expected, ob)) return;
  505. Assert.AreEqual (expected.name, ob.name, id + " name");
  506. Assert.AreEqual (expected.bval, ob.bval, id + " bval");
  507. }
  508. }
  509. [SoapType(Namespace="mm")]
  510. [XmlType(Namespace="mm")]
  511. public class AnotherTestPart
  512. {
  513. [XmlText]
  514. public long lo;
  515. }
  516. public class UknTestPart : Test
  517. {
  518. [XmlElement(IsNullable=true)]
  519. public string uname;
  520. public bool uval;
  521. }
  522. public struct OneStruct
  523. {
  524. public int aa;
  525. public string cc;
  526. }
  527. // [XmlType(Namespace="enum_namespace")]
  528. public enum option
  529. {
  530. AA,
  531. [SoapEnum(Name="xmlBB")]
  532. [XmlEnum(Name="xmlBB")]
  533. BB,
  534. CC,
  535. DD
  536. }
  537. public class CustomHashtable : IXmlSerializable
  538. {
  539. Hashtable data = new Hashtable ();
  540. public void Add (string key, string value)
  541. {
  542. data.Add (key, value);
  543. }
  544. public static void AssertEquals (string id, CustomHashtable exp, CustomHashtable ob)
  545. {
  546. if (!AssertionHelper.AssertType (id, exp, ob)) return;
  547. if (!AssertionHelper.AssertType (id, exp.data, ob.data)) return;
  548. Assert.AreEqual (exp.data.Count, ob.data.Count, id + " data Count");
  549. foreach (DictionaryEntry entry in exp.data)
  550. Assert.AreEqual (entry.Value, ob.data[entry.Key]);
  551. }
  552. public void ReadXml (XmlReader reader)
  553. {
  554. // Read the element enclosing the object
  555. reader.ReadStartElement();
  556. reader.MoveToContent ();
  557. // Reads the "data" element
  558. reader.ReadStartElement();
  559. reader.MoveToContent ();
  560. while (reader.NodeType != XmlNodeType.EndElement)
  561. {
  562. if (reader.NodeType == XmlNodeType.Element)
  563. {
  564. string key = reader.LocalName;
  565. data [key] = reader.ReadElementString ();
  566. }
  567. else
  568. reader.Skip ();
  569. reader.MoveToContent ();
  570. }
  571. reader.ReadEndElement ();
  572. }
  573. public void WriteXml (XmlWriter writer)
  574. {
  575. writer.WriteStartElement ("data");
  576. foreach (DictionaryEntry entry in data)
  577. {
  578. writer.WriteElementString ((string)entry.Key, (string)entry.Value);
  579. }
  580. writer.WriteEndElement ();
  581. }
  582. public XmlSchema GetSchema ()
  583. {
  584. XmlSchema s = new XmlSchema ();
  585. s.TargetNamespace = "http://www.go-mono.org/schemas";
  586. s.Id = "monoschema";
  587. XmlSchemaElement e = new XmlSchemaElement ();
  588. e.Name = "data";
  589. s.Items.Add (e);
  590. XmlSchemaComplexType cs = new XmlSchemaComplexType ();
  591. XmlSchemaSequence seq = new XmlSchemaSequence ();
  592. XmlSchemaAny any = new XmlSchemaAny ();
  593. any.MinOccurs = 0;
  594. any.MaxOccurs = decimal.MaxValue;
  595. seq.Items.Add (any);
  596. cs.Particle = seq;
  597. e.SchemaType = cs;
  598. return s;
  599. }
  600. }
  601. }