2
0

ComplexDataStructure.cs 21 KB

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