2
0

XmlSerializerTests.cs 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155
  1. //
  2. // System.Xml.XmlSerializerTests
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. // Hagit Yidov <[email protected]>
  7. //
  8. // (C) 2003 Erik LeBel
  9. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  10. //
  11. //
  12. // NOTES:
  13. // Where possible, these tests avoid testing the order of
  14. // an object's members serialization. Mono and .NET do not
  15. // reflect members in the same order.
  16. //
  17. // Only serializations tests so far, no deserialization.
  18. //
  19. // FIXME
  20. // test XmlArrayAttribute
  21. // test XmlArrayItemAttribute
  22. // test serialization of decimal type
  23. // test serialization of Guid type
  24. // test XmlNode serialization with and without modifying attributes.
  25. // test deserialization
  26. // FIXMEs found in this file
  27. using System;
  28. using System.Collections;
  29. using System.Globalization;
  30. using System.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Data;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. #if NET_2_0
  37. using System.Collections.Generic;
  38. #endif
  39. using NUnit.Framework;
  40. using MonoTests.System.Xml.TestClasses;
  41. namespace MonoTests.System.XmlSerialization
  42. {
  43. [TestFixture]
  44. public class XmlSerializerTests
  45. {
  46. const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  47. const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
  48. const string ANamespace = "some:urn";
  49. const string AnotherNamespace = "another:urn";
  50. StringWriter sw;
  51. XmlTextWriter xtw;
  52. XmlSerializer xs;
  53. private void SetUpWriter ()
  54. {
  55. sw = new StringWriter ();
  56. xtw = new XmlTextWriter (sw);
  57. xtw.QuoteChar = '\'';
  58. xtw.Formatting = Formatting.None;
  59. }
  60. private string WriterText
  61. {
  62. get
  63. {
  64. string val = sw.GetStringBuilder ().ToString ();
  65. int offset = val.IndexOf ('>') + 1;
  66. val = val.Substring (offset);
  67. return Infoset (val);
  68. }
  69. }
  70. private void Serialize (object o)
  71. {
  72. SetUpWriter ();
  73. xs = new XmlSerializer (o.GetType ());
  74. xs.Serialize (xtw, o);
  75. }
  76. private void Serialize (object o, Type type)
  77. {
  78. SetUpWriter ();
  79. xs = new XmlSerializer (type);
  80. xs.Serialize (xtw, o);
  81. }
  82. private void Serialize (object o, XmlSerializerNamespaces ns)
  83. {
  84. SetUpWriter ();
  85. xs = new XmlSerializer (o.GetType ());
  86. xs.Serialize (xtw, o, ns);
  87. }
  88. private void Serialize (object o, XmlAttributeOverrides ao)
  89. {
  90. SetUpWriter ();
  91. xs = new XmlSerializer (o.GetType (), ao);
  92. xs.Serialize (xtw, o);
  93. }
  94. private void Serialize (object o, XmlAttributeOverrides ao, string defaultNamespace)
  95. {
  96. SetUpWriter ();
  97. xs = new XmlSerializer (o.GetType (), ao, Type.EmptyTypes,
  98. (XmlRootAttribute) null, defaultNamespace);
  99. xs.Serialize (xtw, o);
  100. }
  101. private void Serialize (object o, XmlRootAttribute root)
  102. {
  103. SetUpWriter ();
  104. xs = new XmlSerializer (o.GetType (), root);
  105. xs.Serialize (xtw, o);
  106. }
  107. private void Serialize (object o, XmlTypeMapping typeMapping)
  108. {
  109. SetUpWriter ();
  110. xs = new XmlSerializer (typeMapping);
  111. xs.Serialize (xtw, o);
  112. }
  113. private void SerializeEncoded (object o)
  114. {
  115. SerializeEncoded (o, o.GetType ());
  116. }
  117. private void SerializeEncoded (object o, SoapAttributeOverrides ao)
  118. {
  119. XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao);
  120. SetUpWriter ();
  121. xs = new XmlSerializer (mapping);
  122. xs.Serialize (xtw, o);
  123. }
  124. private void SerializeEncoded (object o, SoapAttributeOverrides ao, string defaultNamespace)
  125. {
  126. XmlTypeMapping mapping = CreateSoapMapping (o.GetType (), ao, defaultNamespace);
  127. SetUpWriter ();
  128. xs = new XmlSerializer (mapping);
  129. xs.Serialize (xtw, o);
  130. }
  131. private void SerializeEncoded (object o, Type type)
  132. {
  133. XmlTypeMapping mapping = CreateSoapMapping (type);
  134. SetUpWriter ();
  135. xs = new XmlSerializer (mapping);
  136. xs.Serialize (xtw, o);
  137. }
  138. private void SerializeEncoded (XmlTextWriter xtw, object o, Type type)
  139. {
  140. XmlTypeMapping mapping = CreateSoapMapping (type);
  141. xs = new XmlSerializer (mapping);
  142. xs.Serialize (xtw, o);
  143. }
  144. // test constructors
  145. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  146. [Test]
  147. public void TestConstructor()
  148. {
  149. XmlSerializer ser = new XmlSerializer (null, "");
  150. }
  151. #else
  152. #endif
  153. // test basic types ////////////////////////////////////////////////////////
  154. [Test]
  155. public void TestSerializeInt ()
  156. {
  157. Serialize (10);
  158. Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
  159. }
  160. [Test]
  161. public void TestSerializeBool ()
  162. {
  163. Serialize (true);
  164. Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
  165. Serialize (false);
  166. Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
  167. }
  168. [Test]
  169. public void TestSerializeString ()
  170. {
  171. Serialize ("hello");
  172. Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
  173. }
  174. [Test]
  175. public void TestSerializeEmptyString ()
  176. {
  177. Serialize (String.Empty);
  178. Assert.AreEqual (Infoset ("<string />"), WriterText);
  179. }
  180. [Test]
  181. public void TestSerializeNullObject ()
  182. {
  183. Serialize (null, typeof (object));
  184. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  185. "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  186. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  187. }
  188. [Test]
  189. [Ignore ("The generated XML is not exact but it is equivalent")]
  190. public void TestSerializeNullString ()
  191. {
  192. Serialize (null, typeof (string));
  193. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  194. "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  195. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  196. }
  197. [Test]
  198. public void TestSerializeIntArray ()
  199. {
  200. Serialize (new int[] { 1, 2, 3, 4 });
  201. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  202. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
  203. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  204. }
  205. [Test]
  206. public void TestSerializeEmptyArray ()
  207. {
  208. Serialize (new int[] { });
  209. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  210. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
  211. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  212. }
  213. [Test]
  214. public void TestSerializeChar ()
  215. {
  216. Serialize ('A');
  217. Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
  218. Serialize ('\0');
  219. Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
  220. Serialize ('\n');
  221. Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
  222. Serialize ('\uFF01');
  223. Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
  224. }
  225. [Test]
  226. public void TestSerializeFloat ()
  227. {
  228. Serialize (10.78);
  229. Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
  230. Serialize (-1e8);
  231. Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
  232. // FIXME test INF and other boundary conditions that may exist with floats
  233. }
  234. [Test]
  235. public void TestSerializeEnumeration_FromValue ()
  236. {
  237. Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  238. Assert.AreEqual (
  239. "<?xml version='1.0' encoding='utf-16'?>" +
  240. "<SimpleEnumeration>SECOND</SimpleEnumeration>",
  241. sw.ToString ());
  242. }
  243. [Test]
  244. [Category ("NotWorking")]
  245. public void TestSerializeEnumeration_FromValue_Encoded ()
  246. {
  247. SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  248. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  249. "<?xml version='1.0' encoding='utf-16'?>" +
  250. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  251. XmlSchema.InstanceNamespace), sw.ToString ());
  252. }
  253. [Test]
  254. public void TestSerializeEnumeration ()
  255. {
  256. Serialize (SimpleEnumeration.FIRST);
  257. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
  258. Serialize (SimpleEnumeration.SECOND);
  259. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
  260. }
  261. [Test]
  262. public void TestSerializeEnumeration_Encoded ()
  263. {
  264. SerializeEncoded (SimpleEnumeration.FIRST);
  265. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  266. "<?xml version='1.0' encoding='utf-16'?>" +
  267. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  268. XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
  269. SerializeEncoded (SimpleEnumeration.SECOND);
  270. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  271. "<?xml version='1.0' encoding='utf-16'?>" +
  272. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  273. XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
  274. }
  275. [Test]
  276. public void TestSerializeEnumDefaultValue ()
  277. {
  278. Serialize (new EnumDefaultValue ());
  279. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
  280. Serialize (new SimpleEnumeration ());
  281. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
  282. Serialize (3, typeof (EnumDefaultValue));
  283. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
  284. Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  285. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
  286. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  287. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
  288. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  289. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
  290. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  291. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
  292. Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  293. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
  294. Serialize (3, typeof (FlagEnum));
  295. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
  296. Serialize (5, typeof (FlagEnum));
  297. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
  298. Serialize (FlagEnum.e4, typeof (FlagEnum));
  299. Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
  300. Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  301. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
  302. Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  303. Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
  304. Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  305. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
  306. Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  307. Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
  308. Serialize (3, typeof (EnumDefaultValueNF));
  309. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
  310. Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  311. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
  312. Serialize (2, typeof (ZeroFlagEnum));
  313. Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
  314. Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
  315. Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
  316. }
  317. [Test]
  318. [Category ("NotWorking")]
  319. public void TestSerializeEnumDefaultValue_Encoded ()
  320. {
  321. SerializeEncoded (new EnumDefaultValue ());
  322. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  323. "<?xml version='1.0' encoding='utf-16'?>" +
  324. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}' />",
  325. XmlSchema.InstanceNamespace), sw.ToString (), "#1");
  326. SerializeEncoded (new SimpleEnumeration ());
  327. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  328. "<?xml version='1.0' encoding='utf-16'?>" +
  329. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  330. XmlSchema.InstanceNamespace), sw.ToString (), "#2");
  331. SerializeEncoded (3, typeof (EnumDefaultValue));
  332. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  333. "<?xml version='1.0' encoding='utf-16'?>" +
  334. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  335. XmlSchema.InstanceNamespace), sw.ToString (), "#3");
  336. SerializeEncoded (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  337. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  338. "<?xml version='1.0' encoding='utf-16'?>" +
  339. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  340. XmlSchema.InstanceNamespace), sw.ToString (), "#4");
  341. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  342. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  343. "<?xml version='1.0' encoding='utf-16'?>" +
  344. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  345. XmlSchema.InstanceNamespace), sw.ToString (), "#5");
  346. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  347. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  348. "<?xml version='1.0' encoding='utf-16'?>" +
  349. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  350. XmlSchema.InstanceNamespace), sw.ToString (), "#6");
  351. SerializeEncoded (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  352. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  353. "<?xml version='1.0' encoding='utf-16'?>" +
  354. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  355. XmlSchema.InstanceNamespace), sw.ToString (), "#7");
  356. SerializeEncoded (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  357. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  358. "<?xml version='1.0' encoding='utf-16'?>" +
  359. "<EnumDefaultValue d1p1:type='EnumDefaultValue' xmlns:d1p1='{0}'>e3</EnumDefaultValue>",
  360. XmlSchema.InstanceNamespace), sw.ToString (), "#8");
  361. SerializeEncoded (3, typeof (FlagEnum));
  362. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  363. "<?xml version='1.0' encoding='utf-16'?>" +
  364. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  365. XmlSchema.InstanceNamespace), sw.ToString (), "#9");
  366. SerializeEncoded (5, typeof (FlagEnum));
  367. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  368. "<?xml version='1.0' encoding='utf-16'?>" +
  369. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  370. XmlSchema.InstanceNamespace), sw.ToString (), "#10");
  371. SerializeEncoded (FlagEnum.e4, typeof (FlagEnum));
  372. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  373. "<?xml version='1.0' encoding='utf-16'?>" +
  374. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e4</FlagEnum>",
  375. XmlSchema.InstanceNamespace), sw.ToString (), "#11");
  376. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  377. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  378. "<?xml version='1.0' encoding='utf-16'?>" +
  379. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2</FlagEnum>",
  380. XmlSchema.InstanceNamespace), sw.ToString (), "#12");
  381. SerializeEncoded (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  382. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  383. "<?xml version='1.0' encoding='utf-16'?>" +
  384. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e2 e4</FlagEnum>",
  385. XmlSchema.InstanceNamespace), sw.ToString (), "#13");
  386. SerializeEncoded (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  387. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  388. "<?xml version='1.0' encoding='utf-16'?>" +
  389. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e1 e4</FlagEnum>",
  390. XmlSchema.InstanceNamespace), sw.ToString (), "#14");
  391. SerializeEncoded (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  392. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  393. "<?xml version='1.0' encoding='utf-16'?>" +
  394. "<FlagEnum d1p1:type='FlagEnum' xmlns:d1p1='{0}'>e2 e4</FlagEnum>",
  395. XmlSchema.InstanceNamespace), sw.ToString (), "#15");
  396. SerializeEncoded (3, typeof (EnumDefaultValueNF));
  397. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  398. "<?xml version='1.0' encoding='utf-16'?>" +
  399. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e3</EnumDefaultValueNF>",
  400. XmlSchema.InstanceNamespace), sw.ToString (), "#16");
  401. SerializeEncoded (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  402. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  403. "<?xml version='1.0' encoding='utf-16'?>" +
  404. "<EnumDefaultValueNF d1p1:type='EnumDefaultValueNF' xmlns:d1p1='{0}'>e2</EnumDefaultValueNF>",
  405. XmlSchema.InstanceNamespace), sw.ToString (), "#17");
  406. SerializeEncoded (2, typeof (ZeroFlagEnum));
  407. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  408. "<?xml version='1.0' encoding='utf-16'?>" +
  409. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e2</ZeroFlagEnum>",
  410. XmlSchema.InstanceNamespace), sw.ToString (), "#18");
  411. SerializeEncoded (new ZeroFlagEnum ()); // enum actually has a field with value 0
  412. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  413. "<?xml version='1.0' encoding='utf-16'?>" +
  414. "<ZeroFlagEnum d1p1:type='ZeroFlagEnum' xmlns:d1p1='{0}'>e0</ZeroFlagEnum>",
  415. XmlSchema.InstanceNamespace), sw.ToString (), "#19");
  416. }
  417. [Test]
  418. public void TestSerializeEnumDefaultValue_InvalidValue1 ()
  419. {
  420. try {
  421. Serialize ("b", typeof (EnumDefaultValue));
  422. Assert.Fail ("#A1");
  423. } catch (InvalidOperationException ex) {
  424. Assert.IsNotNull (ex.InnerException, "#A2");
  425. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  426. }
  427. try {
  428. Serialize ("e1", typeof (EnumDefaultValue));
  429. Assert.Fail ("#B1");
  430. } catch (InvalidOperationException ex) {
  431. Assert.IsNotNull (ex.InnerException, "#B2");
  432. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  433. }
  434. try {
  435. Serialize ("e1,e2", typeof (EnumDefaultValue));
  436. Assert.Fail ("#C1");
  437. } catch (InvalidOperationException ex) {
  438. Assert.IsNotNull (ex.InnerException, "#C2");
  439. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  440. }
  441. try {
  442. Serialize (string.Empty, typeof (EnumDefaultValue));
  443. Assert.Fail ("#D1");
  444. } catch (InvalidOperationException ex) {
  445. Assert.IsNotNull (ex.InnerException, "#D2");
  446. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  447. }
  448. try {
  449. Serialize ("1", typeof (EnumDefaultValue));
  450. Assert.Fail ("#E1");
  451. } catch (InvalidOperationException ex) {
  452. Assert.IsNotNull (ex.InnerException, "#E2");
  453. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  454. }
  455. try {
  456. Serialize ("0", typeof (EnumDefaultValue));
  457. Assert.Fail ("#F1");
  458. } catch (InvalidOperationException ex) {
  459. Assert.IsNotNull (ex.InnerException, "#F2");
  460. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#F3");
  461. }
  462. try {
  463. Serialize (new SimpleClass (), typeof (EnumDefaultValue));
  464. Assert.Fail ("#G1");
  465. } catch (InvalidOperationException ex) {
  466. Assert.IsNotNull (ex.InnerException, "#G2");
  467. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#G3");
  468. }
  469. }
  470. [Test]
  471. public void TestSerializeEnumDefaultValue_InvalidValue2 ()
  472. {
  473. #if NET_2_0
  474. try {
  475. Serialize (5, typeof (EnumDefaultValue));
  476. Assert.Fail ("#1");
  477. } catch (InvalidOperationException ex) {
  478. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  479. Assert.IsNotNull (ex.InnerException, "#3");
  480. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  481. Assert.IsNotNull (ex.InnerException.Message, "#5");
  482. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'5'") != -1, "#6");
  483. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValue).FullName) != -1, "#7");
  484. }
  485. #else
  486. Serialize (5, typeof (EnumDefaultValue));
  487. Assert.AreEqual (Infoset ("<EnumDefaultValue>5</EnumDefaultValue>"), WriterText);
  488. #endif
  489. }
  490. [Test]
  491. public void TestSerializeEnumDefaultValueNF_InvalidValue1 ()
  492. {
  493. #if NET_2_0
  494. try {
  495. Serialize (new EnumDefaultValueNF ());
  496. Assert.Fail ("#1");
  497. } catch (InvalidOperationException ex) {
  498. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  499. Assert.IsNotNull (ex.InnerException, "#3");
  500. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  501. Assert.IsNotNull (ex.InnerException.Message, "#5");
  502. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  503. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  504. }
  505. #else
  506. Serialize (new EnumDefaultValueNF ());
  507. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  508. #endif
  509. }
  510. [Test]
  511. public void TestSerializeEnumDefaultValueNF_InvalidValue2 ()
  512. {
  513. #if NET_2_0
  514. try {
  515. Serialize (15, typeof (EnumDefaultValueNF));
  516. Assert.Fail ("#1");
  517. } catch (InvalidOperationException ex) {
  518. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  519. Assert.IsNotNull (ex.InnerException, "#3");
  520. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  521. Assert.IsNotNull (ex.InnerException.Message, "#5");
  522. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'15'") != -1, "#6");
  523. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).FullName) != -1, "#7");
  524. }
  525. #else
  526. Serialize (15, typeof (EnumDefaultValueNF));
  527. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>15</EnumDefaultValueNF>"), WriterText);
  528. #endif
  529. }
  530. [Test]
  531. public void TestSerializeEnumDefaultValueNF_InvalidValue3 ()
  532. {
  533. try {
  534. Serialize ("b", typeof (EnumDefaultValueNF));
  535. Assert.Fail ("#A1");
  536. } catch (InvalidOperationException ex) {
  537. Assert.IsNotNull (ex.InnerException, "#A2");
  538. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#A3");
  539. }
  540. try {
  541. Serialize ("e2", typeof (EnumDefaultValueNF));
  542. Assert.Fail ("#B1");
  543. } catch (InvalidOperationException ex) {
  544. Assert.IsNotNull (ex.InnerException, "#B2");
  545. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#B3");
  546. }
  547. try {
  548. Serialize (string.Empty, typeof (EnumDefaultValueNF));
  549. Assert.Fail ("#C1");
  550. } catch (InvalidOperationException ex) {
  551. Assert.IsNotNull (ex.InnerException, "#C2");
  552. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#C3");
  553. }
  554. try {
  555. Serialize ("1", typeof (EnumDefaultValueNF));
  556. Assert.Fail ("#D1");
  557. } catch (InvalidOperationException ex) {
  558. Assert.IsNotNull (ex.InnerException, "#D2");
  559. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#D3");
  560. }
  561. try {
  562. Serialize ("0", typeof (EnumDefaultValueNF));
  563. Assert.Fail ("#E1");
  564. } catch (InvalidOperationException ex) {
  565. Assert.IsNotNull (ex.InnerException, "#E2");
  566. Assert.AreEqual (typeof (InvalidCastException), ex.InnerException.GetType (), "#E3");
  567. }
  568. }
  569. [Test]
  570. public void TestSerializeField ()
  571. {
  572. Field f = new Field ();
  573. Serialize (f, typeof (Field));
  574. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  575. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag3=''" +
  576. " flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
  577. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#A");
  578. f.Flags1 = FlagEnum.e1;
  579. f.Flags2 = FlagEnum.e1;
  580. f.Flags3 = FlagEnum.e2;
  581. f.Modifiers = MapModifiers.Protected;
  582. f.Modifiers2 = MapModifiers.Public;
  583. f.Modifiers3 = MapModifiers.Public;
  584. f.Modifiers4 = MapModifiers.Protected;
  585. f.Modifiers5 = MapModifiers.Public;
  586. Serialize (f, typeof (Field));
  587. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  588. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4=''" +
  589. " modifiers='protected' modifiers2='public' />",
  590. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#B");
  591. f.Flags1 = (FlagEnum) 1;
  592. f.Flags1 = FlagEnum.e2;
  593. f.Flags2 = FlagEnum.e2;
  594. f.Flags3 = FlagEnum.e1 | FlagEnum.e2;
  595. f.Modifiers = MapModifiers.Public;
  596. f.Modifiers2 = MapModifiers.Protected;
  597. f.Modifiers3 = MapModifiers.Protected;
  598. f.Modifiers4 = MapModifiers.Public;
  599. f.Modifiers5 = MapModifiers.Protected;
  600. Serialize (f, typeof (Field));
  601. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  602. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two'" +
  603. " flag4='' modifiers='public' modifiers2='protected'" +
  604. " modifiers3='protected' modifiers4='public'" +
  605. " modifiers5='protected' />",
  606. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#C");
  607. f.Flags1 = FlagEnum.e1 | FlagEnum.e2;
  608. f.Flags2 = FlagEnum.e2;
  609. f.Flags3 = FlagEnum.e4;
  610. f.Flags4 = FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4;
  611. f.Modifiers3 = MapModifiers.Public;
  612. f.Modifiers4 = MapModifiers.Protected;
  613. f.Modifiers5 = MapModifiers.Public;
  614. f.Names = new string[] { "a", "b" };
  615. Serialize (f, typeof (Field));
  616. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  617. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='two'" +
  618. " flag3='four' flag4='one two four' modifiers='public'" +
  619. " modifiers2='protected' names='a b' />",
  620. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#D");
  621. f.Flags2 = (FlagEnum) 444;
  622. f.Flags3 = (FlagEnum) 555;
  623. f.Modifiers = (MapModifiers) 666;
  624. f.Modifiers2 = (MapModifiers) 777;
  625. f.Modifiers3 = (MapModifiers) 0;
  626. f.Modifiers4 = (MapModifiers) 888;
  627. f.Modifiers5 = (MapModifiers) 999;
  628. #if NET_2_0
  629. try {
  630. Serialize (f, typeof (Field));
  631. Assert.Fail ("#E1");
  632. } catch (InvalidOperationException ex) {
  633. // There was an error generating the XML document
  634. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
  635. Assert.IsNotNull (ex.Message, "#E3");
  636. Assert.IsNotNull (ex.InnerException, "#E4");
  637. // Instance validation error: '444' is not a valid value for
  638. // MonoTests.System.Xml.TestClasses.FlagEnum
  639. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
  640. Assert.IsNotNull (ex.InnerException.Message, "#E6");
  641. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#E7");
  642. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
  643. Assert.IsNull (ex.InnerException.InnerException, "#E9");
  644. }
  645. #else
  646. Serialize (f, typeof (Field));
  647. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  648. "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='one two' flag2='444'" +
  649. " flag3='555' flag4='one two four' modifiers='666' modifiers2='777'" +
  650. " modifiers4='888' modifiers5='999' names='a b' />",
  651. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText, "#E");
  652. #endif
  653. }
  654. [Test]
  655. [Category ("NotDotNet")] // MS bug
  656. public void TestSerializeField_Encoded ()
  657. {
  658. Field_Encoded f = new Field_Encoded ();
  659. SerializeEncoded (f, typeof (Field_Encoded));
  660. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  661. "<?xml version='1.0' encoding='utf-16'?>" +
  662. #if NET_2_0
  663. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1=''" +
  664. " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
  665. " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
  666. #else
  667. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1=''" +
  668. " flag2='' flag3='' flag4='' modifiers='PuBlIc'" +
  669. " modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='some:urn' />",
  670. #endif
  671. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  672. sw.GetStringBuilder ().ToString (), "#A");
  673. f.Flags1 = FlagEnum_Encoded.e1;
  674. f.Flags2 = FlagEnum_Encoded.e1;
  675. f.Flags3 = FlagEnum_Encoded.e2;
  676. f.Modifiers = MapModifiers.Protected;
  677. f.Modifiers2 = MapModifiers.Public;
  678. f.Modifiers3 = MapModifiers.Public;
  679. f.Modifiers4 = MapModifiers.Protected;
  680. f.Modifiers5 = MapModifiers.Public;
  681. SerializeEncoded (f, typeof (Field_Encoded));
  682. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  683. "<?xml version='1.0' encoding='utf-16'?>" +
  684. #if NET_2_0
  685. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag3='two'" +
  686. " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
  687. " xmlns:q1='some:urn' />",
  688. #else
  689. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two'" +
  690. " flag4='' modifiers='Protected' modifiers2='PuBlIc'" +
  691. " xmlns:q1='some:urn' />",
  692. #endif
  693. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  694. sw.GetStringBuilder ().ToString (), "#B");
  695. f.Flags1 = FlagEnum_Encoded.e2;
  696. f.Flags2 = FlagEnum_Encoded.e2;
  697. f.Flags3 = FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2;
  698. f.Modifiers = MapModifiers.Public;
  699. f.Modifiers2 = MapModifiers.Protected;
  700. f.Modifiers3 = MapModifiers.Protected;
  701. f.Modifiers4 = MapModifiers.Public;
  702. f.Modifiers5 = MapModifiers.Protected;
  703. SerializeEncoded (f, typeof (Field_Encoded));
  704. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  705. "<?xml version='1.0' encoding='utf-16'?>" +
  706. #if NET_2_0
  707. "<q1:field xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' flag1='two'" +
  708. " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
  709. " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
  710. " xmlns:q1='some:urn' />",
  711. #else
  712. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two'" +
  713. " flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected'" +
  714. " modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected'" +
  715. " xmlns:q1='some:urn' />",
  716. #endif
  717. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  718. sw.GetStringBuilder ().ToString (), "#C");
  719. f.Flags1 = (FlagEnum_Encoded) 1;
  720. f.Flags2 = (FlagEnum_Encoded) 444;
  721. f.Flags3 = (FlagEnum_Encoded) 555;
  722. f.Modifiers = (MapModifiers) 666;
  723. f.Modifiers2 = (MapModifiers) 777;
  724. f.Modifiers3 = (MapModifiers) 0;
  725. f.Modifiers4 = (MapModifiers) 888;
  726. f.Modifiers5 = (MapModifiers) 999;
  727. #if NET_2_0
  728. try {
  729. #endif
  730. SerializeEncoded (f, typeof (Field_Encoded));
  731. #if NET_2_0
  732. Assert.Fail ("#D1");
  733. } catch (InvalidOperationException ex) {
  734. // There was an error generating the XML document
  735. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  736. Assert.IsNotNull (ex.Message, "#D3");
  737. Assert.IsNotNull (ex.InnerException, "#D4");
  738. // Instance validation error: '444' is not a valid value for
  739. // MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
  740. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
  741. Assert.IsNotNull (ex.InnerException.Message, "#D6");
  742. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
  743. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
  744. Assert.IsNull (ex.InnerException.InnerException, "#D9");
  745. }
  746. #else
  747. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  748. "<?xml version='1.0' encoding='utf-16'?>" +
  749. "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444'" +
  750. " flag3='555' flag4='' modifiers='666' modifiers2='777'" +
  751. " modifiers4='888' modifiers5='999' xmlns:q1='some:urn' />",
  752. XmlSchema.Namespace, XmlSchema.InstanceNamespace),
  753. sw.GetStringBuilder ().ToString (), "#D");
  754. #endif
  755. }
  756. [Test]
  757. #if TARGET_JVM
  758. [Ignore ("JVM returns fields in different order")]
  759. #endif
  760. public void TestSerializeGroup ()
  761. {
  762. Group myGroup = new Group ();
  763. myGroup.GroupName = ".NET";
  764. Byte[] hexByte = new Byte[] { 0x64, 0x32 };
  765. myGroup.GroupNumber = hexByte;
  766. DateTime myDate = new DateTime (2002, 5, 2);
  767. myGroup.Today = myDate;
  768. myGroup.PostitiveInt = "10000";
  769. myGroup.IgnoreThis = true;
  770. Car thisCar = (Car) myGroup.myCar ("1234566");
  771. myGroup.MyVehicle = thisCar;
  772. SetUpWriter ();
  773. xtw.WriteStartDocument (true);
  774. xtw.WriteStartElement ("Wrapper");
  775. SerializeEncoded (xtw, myGroup, typeof (Group));
  776. xtw.WriteEndElement ();
  777. xtw.Close ();
  778. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  779. "<Wrapper>" +
  780. "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
  781. "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
  782. "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
  783. "<MyVehicle href='#id2' />" +
  784. "</Group>" +
  785. "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
  786. "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
  787. "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
  788. "</Car>" +
  789. "</Wrapper>",
  790. XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  791. WriterText, "#1");
  792. myGroup.GroupName = null;
  793. myGroup.Grouptype = GroupType.B;
  794. myGroup.MyVehicle.licenseNumber = null;
  795. myGroup.MyVehicle.weight = "450";
  796. SetUpWriter ();
  797. xtw.WriteStartDocument (true);
  798. xtw.WriteStartElement ("Wrapper");
  799. SerializeEncoded (xtw, myGroup, typeof (Group));
  800. xtw.WriteEndElement ();
  801. xtw.Close ();
  802. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  803. "<Wrapper>" +
  804. "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
  805. "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
  806. "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
  807. "<MyVehicle href='#id2' />" +
  808. "</Group>" +
  809. "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
  810. "<makeDate xmlns:q1='{0}' d2p1:type='q1:date'>0001-01-01</makeDate>" +
  811. "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
  812. "</Car>" +
  813. "</Wrapper>",
  814. XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  815. WriterText, "#2");
  816. }
  817. [Test]
  818. public void TestSerializeZeroFlagEnum_InvalidValue ()
  819. {
  820. #if NET_2_0
  821. try {
  822. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  823. Assert.Fail ("#1");
  824. } catch (InvalidOperationException ex) {
  825. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  826. Assert.IsNotNull (ex.InnerException, "#3");
  827. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  828. Assert.IsNotNull (ex.InnerException.Message, "#5");
  829. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'4'") != -1, "#6");
  830. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#7");
  831. }
  832. #else
  833. Serialize (4, typeof (ZeroFlagEnum)); // corresponding enum field is marked XmlIgnore
  834. Assert.AreEqual (Infoset ("<ZeroFlagEnum>4</ZeroFlagEnum>"), WriterText);
  835. #endif
  836. }
  837. [Test]
  838. public void TestSerializeQualifiedName ()
  839. {
  840. Serialize (new XmlQualifiedName ("me", "home.urn"));
  841. Assert.AreEqual (Infoset ("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  842. }
  843. [Test]
  844. public void TestSerializeBytes ()
  845. {
  846. Serialize ((byte) 0xAB);
  847. Assert.AreEqual (Infoset ("<unsignedByte>171</unsignedByte>"), WriterText);
  848. Serialize ((byte) 15);
  849. Assert.AreEqual (Infoset ("<unsignedByte>15</unsignedByte>"), WriterText);
  850. }
  851. [Test]
  852. public void TestSerializeByteArrays ()
  853. {
  854. Serialize (new byte[] { });
  855. Assert.AreEqual (Infoset ("<base64Binary />"), WriterText);
  856. Serialize (new byte[] { 0xAB, 0xCD });
  857. Assert.AreEqual (Infoset ("<base64Binary>q80=</base64Binary>"), WriterText);
  858. }
  859. [Test]
  860. public void TestSerializeDateTime ()
  861. {
  862. DateTime d = new DateTime ();
  863. Serialize (d);
  864. TimeZone tz = TimeZone.CurrentTimeZone;
  865. TimeSpan off = tz.GetUtcOffset (d);
  866. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  867. #if NET_2_0
  868. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00</dateTime>"), WriterText);
  869. #else
  870. Assert.AreEqual (Infoset ("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  871. #endif
  872. }
  873. /*
  874. FIXME
  875. - decimal
  876. - Guid
  877. - XmlNode objects
  878. [Test]
  879. public void TestSerialize()
  880. {
  881. Serialize();
  882. Assert.AreEqual (WriterText, "");
  883. }
  884. */
  885. // test basic class serialization /////////////////////////////////////
  886. [Test]
  887. public void TestSerializeSimpleClass ()
  888. {
  889. SimpleClass simple = new SimpleClass ();
  890. Serialize (simple);
  891. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  892. simple.something = "hello";
  893. Serialize (simple);
  894. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  895. }
  896. [Test]
  897. public void TestSerializeStringCollection ()
  898. {
  899. StringCollection strings = new StringCollection ();
  900. Serialize (strings);
  901. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  902. strings.Add ("hello");
  903. strings.Add ("goodbye");
  904. Serialize (strings);
  905. Assert.AreEqual (Infoset ("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);
  906. }
  907. [Test]
  908. public void TestSerializeOptionalValueTypeContainer ()
  909. {
  910. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  911. XmlAttributes attr;
  912. OptionalValueTypeContainer optionalValue = new OptionalValueTypeContainer ();
  913. Serialize (optionalValue);
  914. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  915. "<?xml version='1.0' encoding='utf-16'?>" +
  916. #if NET_2_0
  917. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}' />",
  918. #else
  919. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}' />",
  920. #endif
  921. XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace),
  922. sw.ToString (), "#1");
  923. attr = new XmlAttributes ();
  924. // remove the DefaultValue attribute on the Flags member
  925. overrides.Add (typeof (OptionalValueTypeContainer), "Flags", attr);
  926. // remove the DefaultValue attribute on the Attributes member
  927. overrides.Add (typeof (OptionalValueTypeContainer), "Attributes", attr);
  928. Serialize (optionalValue, overrides);
  929. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  930. "<?xml version='1.0' encoding='utf-16'?>" +
  931. #if NET_2_0
  932. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
  933. #else
  934. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
  935. #endif
  936. "<Attributes xmlns='{3}'>one four</Attributes>" +
  937. "</optionalValue>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  938. AnotherNamespace, ANamespace), sw.ToString (), "#2");
  939. optionalValue.FlagsSpecified = true;
  940. Serialize (optionalValue, overrides);
  941. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  942. "<?xml version='1.0' encoding='utf-16'?>" +
  943. #if NET_2_0
  944. "<optionalValue xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='{2}'>" +
  945. #else
  946. "<optionalValue xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
  947. #endif
  948. "<Attributes xmlns='{3}'>one four</Attributes>" +
  949. "<Flags xmlns='{3}'>one</Flags>" +
  950. "</optionalValue>",
  951. XmlSchema.Namespace, XmlSchema.InstanceNamespace, AnotherNamespace,
  952. ANamespace), sw.ToString (), "#3");
  953. }
  954. [Test]
  955. public void TestSerializePlainContainer ()
  956. {
  957. StringCollectionContainer container = new StringCollectionContainer ();
  958. Serialize (container);
  959. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  960. container.Messages.Add ("hello");
  961. container.Messages.Add ("goodbye");
  962. Serialize (container);
  963. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);
  964. }
  965. [Test]
  966. public void TestSerializeArrayContainer ()
  967. {
  968. ArrayContainer container = new ArrayContainer ();
  969. Serialize (container);
  970. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  971. container.items = new object[] { 10, 20 };
  972. Serialize (container);
  973. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"), WriterText);
  974. container.items = new object[] { 10, "hello" };
  975. Serialize (container);
  976. Assert.AreEqual (Infoset ("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"), WriterText);
  977. }
  978. [Test]
  979. public void TestSerializeClassArrayContainer ()
  980. {
  981. ClassArrayContainer container = new ClassArrayContainer ();
  982. Serialize (container);
  983. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  984. SimpleClass simple1 = new SimpleClass ();
  985. simple1.something = "hello";
  986. SimpleClass simple2 = new SimpleClass ();
  987. simple2.something = "hello";
  988. container.items = new SimpleClass[2];
  989. container.items[0] = simple1;
  990. container.items[1] = simple2;
  991. Serialize (container);
  992. Assert.AreEqual (Infoset ("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"), WriterText);
  993. }
  994. // test basic attributes ///////////////////////////////////////////////
  995. [Test]
  996. public void TestSerializeSimpleClassWithXmlAttributes ()
  997. {
  998. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
  999. Serialize (simple);
  1000. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1001. simple.something = "hello";
  1002. Serialize (simple);
  1003. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  1004. }
  1005. // test overrides ///////////////////////////////////////////////////////
  1006. [Test]
  1007. public void TestSerializeSimpleClassWithOverrides ()
  1008. {
  1009. // Also tests XmlIgnore
  1010. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1011. XmlAttributes attr = new XmlAttributes ();
  1012. attr.XmlIgnore = true;
  1013. overrides.Add (typeof (SimpleClassWithXmlAttributes), "something", attr);
  1014. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes ();
  1015. simple.something = "hello";
  1016. Serialize (simple, overrides);
  1017. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1018. }
  1019. [Test]
  1020. public void TestSerializeSchema ()
  1021. {
  1022. XmlSchema schema = new XmlSchema ();
  1023. schema.Items.Add (new XmlSchemaAttribute ());
  1024. schema.Items.Add (new XmlSchemaAttributeGroup ());
  1025. schema.Items.Add (new XmlSchemaComplexType ());
  1026. schema.Items.Add (new XmlSchemaNotation ());
  1027. schema.Items.Add (new XmlSchemaSimpleType ());
  1028. schema.Items.Add (new XmlSchemaGroup ());
  1029. schema.Items.Add (new XmlSchemaElement ());
  1030. StringWriter sw = new StringWriter ();
  1031. XmlTextWriter xtw = new XmlTextWriter (sw);
  1032. xtw.QuoteChar = '\'';
  1033. xtw.Formatting = Formatting.Indented;
  1034. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  1035. xs.Serialize (xtw, schema);
  1036. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1037. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  1038. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  1039. " <xsd:attribute />{0}" +
  1040. " <xsd:attributeGroup />{0}" +
  1041. " <xsd:complexType />{0}" +
  1042. " <xsd:notation />{0}" +
  1043. " <xsd:simpleType />{0}" +
  1044. " <xsd:group />{0}" +
  1045. " <xsd:element />{0}" +
  1046. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  1047. }
  1048. // test xmlText //////////////////////////////////////////////////////////
  1049. [Test]
  1050. public void TestSerializeXmlTextAttribute ()
  1051. {
  1052. SimpleClass simple = new SimpleClass ();
  1053. simple.something = "hello";
  1054. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1055. XmlAttributes attr = new XmlAttributes ();
  1056. overrides.Add (typeof (SimpleClass), "something", attr);
  1057. attr.XmlText = new XmlTextAttribute ();
  1058. Serialize (simple, overrides);
  1059. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#1");
  1060. attr.XmlText = new XmlTextAttribute (typeof (string));
  1061. Serialize (simple, overrides);
  1062. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText, "#2");
  1063. try {
  1064. attr.XmlText = new XmlTextAttribute (typeof (byte[]));
  1065. Serialize (simple, overrides);
  1066. Assert.Fail ("#A1: XmlText.Type does not match the type it serializes: this should have failed");
  1067. } catch (InvalidOperationException ex) {
  1068. // there was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'
  1069. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1070. Assert.IsNotNull (ex.Message, "#A3");
  1071. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#A4");
  1072. Assert.IsNotNull (ex.InnerException, "#A5");
  1073. // there was an error reflecting field 'something'.
  1074. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#A6");
  1075. Assert.IsNotNull (ex.InnerException.Message, "#A7");
  1076. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#A8");
  1077. Assert.IsNotNull (ex.InnerException.InnerException, "#A9");
  1078. // the type for XmlText may not be specified for primitive types.
  1079. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#A10");
  1080. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#A11");
  1081. Assert.IsNull (ex.InnerException.InnerException.InnerException, "#A12");
  1082. }
  1083. try {
  1084. attr.XmlText = new XmlTextAttribute ();
  1085. attr.XmlText.DataType = "sometype";
  1086. Serialize (simple, overrides);
  1087. Assert.Fail ("#B1: XmlText.DataType does not match the type it serializes: this should have failed");
  1088. } catch (InvalidOperationException ex) {
  1089. // There was an error reflecting type 'MonoTests.System.Xml.TestClasses.SimpleClass'.
  1090. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1091. Assert.IsNotNull (ex.Message, "#B3");
  1092. Assert.IsTrue (ex.Message.IndexOf (typeof (SimpleClass).FullName) != -1, "#B4");
  1093. Assert.IsNotNull (ex.InnerException, "#B5");
  1094. // There was an error reflecting field 'something'.
  1095. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B6");
  1096. Assert.IsNotNull (ex.InnerException.Message, "#B7");
  1097. Assert.IsTrue (ex.InnerException.Message.IndexOf ("something") != -1, "#B8");
  1098. Assert.IsNotNull (ex.InnerException.InnerException, "#B9");
  1099. //FIXME
  1100. /*
  1101. // There was an error reflecting type 'System.String'.
  1102. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.GetType (), "#B10");
  1103. Assert.IsNotNull (ex.InnerException.InnerException.Message, "#B11");
  1104. Assert.IsTrue (ex.InnerException.InnerException.Message.IndexOf (typeof (string).FullName) != -1, "#B12");
  1105. Assert.IsNotNull (ex.InnerException.InnerException.InnerException, "#B13");
  1106. // Value 'sometype' cannot be used for the XmlElementAttribute.DataType property.
  1107. // The datatype 'http://www.w3.org/2001/XMLSchema:sometype' is missing.
  1108. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.InnerException.InnerException.GetType (), "#B14");
  1109. Assert.IsNotNull (ex.InnerException.InnerException.InnerException.Message, "#B15");
  1110. Assert.IsTrue (ex.InnerException.InnerException.InnerException.Message.IndexOf ("http://www.w3.org/2001/XMLSchema:sometype") != -1, "#B16");
  1111. Assert.IsNull (ex.InnerException.InnerException.InnerException.InnerException, "#B17");
  1112. */
  1113. }
  1114. }
  1115. // test xmlRoot //////////////////////////////////////////////////////////
  1116. [Test]
  1117. public void TestSerializeXmlRootAttribute ()
  1118. {
  1119. // constructor override & element name
  1120. XmlRootAttribute root = new XmlRootAttribute ();
  1121. root.ElementName = "renamed";
  1122. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes ();
  1123. Serialize (simpleWithAttributes, root);
  1124. Assert.AreEqual (Infoset ("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1125. SimpleClass simple = null;
  1126. root.IsNullable = false;
  1127. try {
  1128. Serialize (simple, root);
  1129. Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == false");
  1130. } catch (NullReferenceException) {
  1131. }
  1132. root.IsNullable = true;
  1133. try {
  1134. Serialize (simple, root);
  1135. Assert.Fail ("Cannot serialize null object if XmlRoot's IsNullable == true");
  1136. } catch (NullReferenceException) {
  1137. }
  1138. simple = new SimpleClass ();
  1139. root.ElementName = null;
  1140. root.Namespace = "some.urn";
  1141. Serialize (simple, root);
  1142. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  1143. }
  1144. [Test]
  1145. public void TestSerializeXmlRootAttributeOnMember ()
  1146. {
  1147. // nested root
  1148. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1149. XmlAttributes childAttr = new XmlAttributes ();
  1150. childAttr.XmlRoot = new XmlRootAttribute ("simple");
  1151. overrides.Add (typeof (SimpleClass), childAttr);
  1152. XmlAttributes attr = new XmlAttributes ();
  1153. attr.XmlRoot = new XmlRootAttribute ("simple");
  1154. overrides.Add (typeof (ClassArrayContainer), attr);
  1155. ClassArrayContainer container = new ClassArrayContainer ();
  1156. container.items = new SimpleClass[1];
  1157. container.items[0] = new SimpleClass ();
  1158. Serialize (container, overrides);
  1159. Assert.AreEqual (Infoset ("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"), WriterText);
  1160. // FIXME test data type
  1161. }
  1162. // test XmlAttribute /////////////////////////////////////////////////////
  1163. [Test]
  1164. public void TestSerializeXmlAttributeAttribute ()
  1165. {
  1166. // null
  1167. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1168. XmlAttributes attr = new XmlAttributes ();
  1169. attr.XmlAttribute = new XmlAttributeAttribute ();
  1170. overrides.Add (typeof (SimpleClass), "something", attr);
  1171. SimpleClass simple = new SimpleClass (); ;
  1172. Serialize (simple, overrides);
  1173. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1174. // regular
  1175. simple.something = "hello";
  1176. Serialize (simple, overrides);
  1177. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText, "#2");
  1178. // AttributeName
  1179. attr.XmlAttribute.AttributeName = "somethingelse";
  1180. Serialize (simple, overrides);
  1181. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText, "#3");
  1182. // Type
  1183. // FIXME this should work, shouldnt it?
  1184. // attr.XmlAttribute.Type = typeof(string);
  1185. // Serialize(simple, overrides);
  1186. // Assert(WriterText.EndsWith(" something='hello' />"));
  1187. // Namespace
  1188. attr.XmlAttribute.Namespace = "some:urn";
  1189. Serialize (simple, overrides);
  1190. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText, "#4");
  1191. // FIXME DataType
  1192. // FIXME XmlSchemaForm Form
  1193. // FIXME write XmlQualifiedName as attribute
  1194. }
  1195. // test XmlElement ///////////////////////////////////////////////////////
  1196. [Test]
  1197. public void TestSerializeXmlElementAttribute ()
  1198. {
  1199. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1200. XmlAttributes attr = new XmlAttributes ();
  1201. XmlElementAttribute element = new XmlElementAttribute ();
  1202. attr.XmlElements.Add (element);
  1203. overrides.Add (typeof (SimpleClass), "something", attr);
  1204. // null
  1205. SimpleClass simple = new SimpleClass (); ;
  1206. Serialize (simple, overrides);
  1207. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1208. // not null
  1209. simple.something = "hello";
  1210. Serialize (simple, overrides);
  1211. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#2");
  1212. //ElementName
  1213. element.ElementName = "saying";
  1214. Serialize (simple, overrides);
  1215. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText, "#3");
  1216. //IsNullable
  1217. element.IsNullable = false;
  1218. simple.something = null;
  1219. Serialize (simple, overrides);
  1220. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#4");
  1221. element.IsNullable = true;
  1222. simple.something = null;
  1223. Serialize (simple, overrides);
  1224. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText, "#5");
  1225. //Namespace
  1226. element.ElementName = null;
  1227. element.IsNullable = false;
  1228. element.Namespace = "some:urn";
  1229. simple.something = "hello";
  1230. Serialize (simple, overrides);
  1231. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText, "#6");
  1232. //FIXME DataType
  1233. //FIXME Form
  1234. //FIXME Type
  1235. }
  1236. // test XmlElementAttribute with arrays and collections //////////////////
  1237. [Test]
  1238. public void TestSerializeCollectionWithXmlElementAttribute ()
  1239. {
  1240. // the rule is:
  1241. // if no type is specified or the specified type
  1242. // matches the contents of the collection,
  1243. // serialize each element in an element named after the member.
  1244. // if the type does not match, or matches the collection itself,
  1245. // create a base wrapping element for the member, and then
  1246. // wrap each collection item in its own wrapping element based on type.
  1247. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1248. XmlAttributes attr = new XmlAttributes ();
  1249. XmlElementAttribute element = new XmlElementAttribute ();
  1250. attr.XmlElements.Add (element);
  1251. overrides.Add (typeof (StringCollectionContainer), "Messages", attr);
  1252. // empty collection & no type info in XmlElementAttribute
  1253. StringCollectionContainer container = new StringCollectionContainer ();
  1254. Serialize (container, overrides);
  1255. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#1");
  1256. // non-empty collection & no type info in XmlElementAttribute
  1257. container.Messages.Add ("hello");
  1258. Serialize (container, overrides);
  1259. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#2");
  1260. // non-empty collection & only type info in XmlElementAttribute
  1261. element.Type = typeof (StringCollection);
  1262. Serialize (container, overrides);
  1263. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText, "#3");
  1264. // non-empty collection & only type info in XmlElementAttribute
  1265. element.Type = typeof (string);
  1266. Serialize (container, overrides);
  1267. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText, "#4");
  1268. // two elements
  1269. container.Messages.Add ("goodbye");
  1270. element.Type = null;
  1271. Serialize (container, overrides);
  1272. Assert.AreEqual (Infoset ("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText, "#5");
  1273. }
  1274. // test DefaultValue /////////////////////////////////////////////////////
  1275. [Test]
  1276. public void TestSerializeDefaultValueAttribute ()
  1277. {
  1278. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  1279. XmlAttributes attr = new XmlAttributes ();
  1280. string defaultValueInstance = "nothing";
  1281. attr.XmlDefaultValue = defaultValueInstance;
  1282. overrides.Add (typeof (SimpleClass), "something", attr);
  1283. // use the default
  1284. SimpleClass simple = new SimpleClass ();
  1285. Serialize (simple, overrides);
  1286. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
  1287. // same value as default
  1288. simple.something = defaultValueInstance;
  1289. Serialize (simple, overrides);
  1290. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
  1291. // some other value
  1292. simple.something = "hello";
  1293. Serialize (simple, overrides);
  1294. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText, "#A3");
  1295. overrides = new XmlAttributeOverrides ();
  1296. attr = new XmlAttributes ();
  1297. attr.XmlAttribute = new XmlAttributeAttribute ();
  1298. attr.XmlDefaultValue = defaultValueInstance;
  1299. overrides.Add (typeof (SimpleClass), "something", attr);
  1300. // use the default
  1301. simple = new SimpleClass ();
  1302. Serialize (simple, overrides);
  1303. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
  1304. // same value as default
  1305. simple.something = defaultValueInstance;
  1306. Serialize (simple, overrides);
  1307. Assert.AreEqual (Infoset ("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B2");
  1308. // some other value
  1309. simple.something = "hello";
  1310. Serialize (simple, overrides);
  1311. Assert.AreEqual (Infoset ("<SimpleClass something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B3");
  1312. overrides = new XmlAttributeOverrides ();
  1313. attr = new XmlAttributes ();
  1314. attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
  1315. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1316. // use the default
  1317. TestDefault testDefault = new TestDefault ();
  1318. Serialize (testDefault);
  1319. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C1");
  1320. // use the default with overrides
  1321. Serialize (testDefault, overrides);
  1322. Assert.AreEqual (Infoset ("<testDefault flagenc='e1 e4' xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C2");
  1323. overrides = new XmlAttributeOverrides ();
  1324. attr = new XmlAttributes ();
  1325. attr.XmlAttribute = new XmlAttributeAttribute ("flagenc");
  1326. attr.XmlDefaultValue = (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e4); // add default again
  1327. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1328. // use the default with overrides
  1329. Serialize (testDefault, overrides);
  1330. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C3");
  1331. // use the default with overrides and default namspace
  1332. Serialize (testDefault, overrides, AnotherNamespace);
  1333. Assert.AreEqual (Infoset ("<testDefault xmlns='urn:myNS' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#C4");
  1334. // non-default values
  1335. testDefault.strDefault = "Some Text";
  1336. testDefault.boolT = false;
  1337. testDefault.boolF = true;
  1338. testDefault.decimalval = 20m;
  1339. testDefault.flag = FlagEnum.e2;
  1340. testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
  1341. Serialize (testDefault);
  1342. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1343. "<testDefault xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1344. " <strDefault>Some Text</strDefault>" +
  1345. " <boolT>false</boolT>" +
  1346. " <boolF>true</boolF>" +
  1347. " <decimalval>20</decimalval>" +
  1348. " <flag>two</flag>" +
  1349. " <flagencoded>e1 e2</flagencoded>" +
  1350. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1351. WriterText, "#C5");
  1352. Serialize (testDefault, overrides);
  1353. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1354. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1355. " <strDefault>Some Text</strDefault>" +
  1356. " <boolT>false</boolT>" +
  1357. " <boolF>true</boolF>" +
  1358. " <decimalval>20</decimalval>" +
  1359. " <flag>two</flag>" +
  1360. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1361. WriterText, "#C6");
  1362. Serialize (testDefault, overrides, AnotherNamespace);
  1363. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1364. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1365. " <strDefault>Some Text</strDefault>" +
  1366. " <boolT>false</boolT>" +
  1367. " <boolF>true</boolF>" +
  1368. " <decimalval>20</decimalval>" +
  1369. " <flag>two</flag>" +
  1370. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1371. WriterText, "#C7");
  1372. attr = new XmlAttributes ();
  1373. XmlTypeAttribute xmlType = new XmlTypeAttribute ("flagenum");
  1374. xmlType.Namespace = "yetanother:urn";
  1375. attr.XmlType = xmlType;
  1376. overrides.Add (typeof (FlagEnum_Encoded), attr);
  1377. Serialize (testDefault, overrides, AnotherNamespace);
  1378. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1379. "<testDefault flagenc='e1 e2' xmlns='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1380. " <strDefault>Some Text</strDefault>" +
  1381. " <boolT>false</boolT>" +
  1382. " <boolF>true</boolF>" +
  1383. " <decimalval>20</decimalval>" +
  1384. " <flag>two</flag>" +
  1385. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1386. WriterText, "#C8");
  1387. attr = new XmlAttributes ();
  1388. attr.XmlType = new XmlTypeAttribute ("testDefault");
  1389. overrides.Add (typeof (TestDefault), attr);
  1390. Serialize (testDefault, overrides, AnotherNamespace);
  1391. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1392. "<testDefault flagenc='e1 e2' xmlns='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1393. " <strDefault>Some Text</strDefault>" +
  1394. " <boolT>false</boolT>" +
  1395. " <boolF>true</boolF>" +
  1396. " <decimalval>20</decimalval>" +
  1397. " <flag>two</flag>" +
  1398. "</testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1399. AnotherNamespace)), WriterText, "#C9");
  1400. }
  1401. [Test]
  1402. [Category ("NotWorking")] // SerializationCodeGenerator outputs wrong xsi:type for flagencoded in #C1
  1403. public void TestSerializeDefaultValueAttribute_Encoded ()
  1404. {
  1405. SoapAttributeOverrides overrides = new SoapAttributeOverrides ();
  1406. SoapAttributes attr = new SoapAttributes ();
  1407. attr.SoapAttribute = new SoapAttributeAttribute ();
  1408. string defaultValueInstance = "nothing";
  1409. attr.SoapDefaultValue = defaultValueInstance;
  1410. overrides.Add (typeof (SimpleClass), "something", attr);
  1411. // use the default
  1412. SimpleClass simple = new SimpleClass ();
  1413. SerializeEncoded (simple, overrides);
  1414. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A1");
  1415. // same value as default
  1416. simple.something = defaultValueInstance;
  1417. SerializeEncoded (simple, overrides);
  1418. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A2");
  1419. // some other value
  1420. simple.something = "hello";
  1421. SerializeEncoded (simple, overrides);
  1422. Assert.AreEqual (Infoset ("<SimpleClass id='id1' something='hello' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#A3");
  1423. attr.SoapAttribute = null;
  1424. attr.SoapElement = new SoapElementAttribute ();
  1425. // use the default
  1426. simple = new SimpleClass ();
  1427. SerializeEncoded (simple, overrides);
  1428. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText, "#B1");
  1429. // same value as default
  1430. simple.something = defaultValueInstance;
  1431. SerializeEncoded (simple, overrides);
  1432. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>nothing</something></SimpleClass>"), WriterText, "#B2");
  1433. // some other value
  1434. simple.something = "hello";
  1435. SerializeEncoded (simple, overrides);
  1436. Assert.AreEqual (Infoset ("<SimpleClass id='id1' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xsi:type='xsd:string'>hello</something></SimpleClass>"), WriterText, "#B3");
  1437. overrides = new SoapAttributeOverrides ();
  1438. attr = new SoapAttributes ();
  1439. attr.SoapElement = new SoapElementAttribute ("flagenc");
  1440. overrides.Add (typeof (TestDefault), "flagencoded", attr);
  1441. // use the default (from MS KB325691)
  1442. TestDefault testDefault = new TestDefault ();
  1443. SerializeEncoded (testDefault);
  1444. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1445. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1446. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1447. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1448. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1449. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1450. " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
  1451. " <flagencoded xsi:type='flagenum'>one four</flagencoded>" +
  1452. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1453. WriterText, "#C1");
  1454. SerializeEncoded (testDefault, overrides);
  1455. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1456. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1457. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1458. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1459. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1460. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1461. " <flag xsi:type='FlagEnum'>e1 e4</flag>" +
  1462. " <flagenc xsi:type='flagenum'>one four</flagenc>" +
  1463. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1464. WriterText, "#C2");
  1465. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1466. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1467. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1468. " <strDefault xsi:type='xsd:string'>Default Value</strDefault>" +
  1469. " <boolT xsi:type='xsd:boolean'>true</boolT>" +
  1470. " <boolF xsi:type='xsd:boolean'>false</boolF>" +
  1471. " <decimalval xsi:type='xsd:decimal'>10</decimalval>" +
  1472. " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e1 e4</flag>" +
  1473. " <flagenc xmlns:q3='{2}' xsi:type='q3:flagenum'>one four</flagenc>" +
  1474. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1475. AnotherNamespace)), WriterText, "#C3");
  1476. // non-default values
  1477. testDefault.strDefault = "Some Text";
  1478. testDefault.boolT = false;
  1479. testDefault.boolF = true;
  1480. testDefault.decimalval = 20m;
  1481. testDefault.flag = FlagEnum.e2;
  1482. testDefault.flagencoded = FlagEnum_Encoded.e2 | FlagEnum_Encoded.e1;
  1483. SerializeEncoded (testDefault);
  1484. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1485. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1486. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1487. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1488. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1489. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1490. " <flag xsi:type='FlagEnum'>e2</flag>" +
  1491. " <flagencoded xsi:type='flagenum'>one two</flagencoded>" +
  1492. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1493. WriterText, "#C4");
  1494. SerializeEncoded (testDefault, overrides);
  1495. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1496. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1497. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1498. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1499. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1500. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1501. " <flag xsi:type='FlagEnum'>e2</flag>" +
  1502. " <flagenc xsi:type='flagenum'>one two</flagenc>" +
  1503. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)),
  1504. WriterText, "#C5");
  1505. attr = new SoapAttributes ();
  1506. attr.SoapType = new SoapTypeAttribute ("flagenum", "yetanother:urn");
  1507. overrides.Add (typeof (FlagEnum_Encoded), attr);
  1508. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1509. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1510. "<q1:testDefault id='id1' xmlns:q1='urn:myNS' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1511. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1512. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1513. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1514. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1515. " <flag xmlns:q2='{2}' xsi:type='q2:FlagEnum'>e2</flag>" +
  1516. " <flagenc xmlns:q3='yetanother:urn' xsi:type='q3:flagenum'>one two</flagenc>" +
  1517. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1518. AnotherNamespace)), WriterText, "#C6");
  1519. attr = new SoapAttributes ();
  1520. attr.SoapType = new SoapTypeAttribute ("testDefault");
  1521. overrides.Add (typeof (TestDefault), attr);
  1522. SerializeEncoded (testDefault, overrides, AnotherNamespace);
  1523. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  1524. "<q1:testDefault id='id1' xmlns:q1='{2}' xmlns:xsd='{0}' xmlns:xsi='{1}'>" +
  1525. " <strDefault xsi:type='xsd:string'>Some Text</strDefault>" +
  1526. " <boolT xsi:type='xsd:boolean'>false</boolT>" +
  1527. " <boolF xsi:type='xsd:boolean'>true</boolF>" +
  1528. " <decimalval xsi:type='xsd:decimal'>20</decimalval>" +
  1529. " <flag xsi:type='q1:FlagEnum'>e2</flag>" +
  1530. " <flagenc xmlns:q2='yetanother:urn' xsi:type='q2:flagenum'>one two</flagenc>" +
  1531. "</q1:testDefault>", XmlSchema.Namespace, XmlSchema.InstanceNamespace,
  1532. AnotherNamespace)), WriterText, "#C7");
  1533. }
  1534. // test XmlEnum //////////////////////////////////////////////////////////
  1535. [Test]
  1536. public void TestSerializeXmlEnumAttribute ()
  1537. {
  1538. Serialize (XmlSchemaForm.Qualified);
  1539. Assert.AreEqual (Infoset ("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText, "#1");
  1540. Serialize (XmlSchemaForm.Unqualified);
  1541. Assert.AreEqual (Infoset ("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText, "#2");
  1542. }
  1543. [Test]
  1544. public void TestSerializeXmlEnumAttribute_IgnoredValue ()
  1545. {
  1546. // technically XmlSchemaForm.None has an XmlIgnore attribute,
  1547. // but it is not being serialized as a member.
  1548. #if NET_2_0
  1549. try {
  1550. Serialize (XmlSchemaForm.None);
  1551. Assert.Fail ("#1");
  1552. } catch (InvalidOperationException ex) {
  1553. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  1554. Assert.IsNotNull (ex.InnerException, "#3");
  1555. Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#4");
  1556. Assert.IsNotNull (ex.InnerException.Message, "#5");
  1557. Assert.IsTrue (ex.InnerException.Message.IndexOf ("'0'") != -1, "#6");
  1558. Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (XmlSchemaForm).FullName) != -1, "#7");
  1559. }
  1560. #else
  1561. Serialize (XmlSchemaForm.None);
  1562. Assert.AreEqual (Infoset ("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  1563. #endif
  1564. }
  1565. [Test]
  1566. public void TestSerializeXmlNodeArray ()
  1567. {
  1568. XmlDocument doc = new XmlDocument ();
  1569. Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
  1570. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1571. }
  1572. [Test]
  1573. public void TestSerializeXmlNodeArray2 ()
  1574. {
  1575. XmlDocument doc = new XmlDocument ();
  1576. Serialize (new XmlNode[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
  1577. Assert.AreEqual (Infoset (String.Format ("<ArrayOfXmlNode xmlns:xsd='{0}' xmlns:xsi='{1}'><XmlNode><elem1/></XmlNode><XmlNode><elem2/></XmlNode></ArrayOfXmlNode>", XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  1578. }
  1579. [Test]
  1580. [ExpectedException (typeof (InvalidOperationException))]
  1581. [Category ("NotWorking")]
  1582. public void TestSerializeXmlNodeArrayIncludesAttribute ()
  1583. {
  1584. XmlDocument doc = new XmlDocument ();
  1585. Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
  1586. }
  1587. [Test]
  1588. public void TestSerializeXmlElementArray ()
  1589. {
  1590. XmlDocument doc = new XmlDocument ();
  1591. Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
  1592. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1593. }
  1594. #if NET_2_0
  1595. [Test]
  1596. [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
  1597. public void TestSerializeGenericListOfNode ()
  1598. {
  1599. XmlDocument doc = new XmlDocument ();
  1600. Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1601. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1602. }
  1603. [Test]
  1604. [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
  1605. public void TestSerializeGenericListOfElement ()
  1606. {
  1607. XmlDocument doc = new XmlDocument ();
  1608. Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1609. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1610. }
  1611. #endif
  1612. [Test]
  1613. public void TestSerializeXmlDocument ()
  1614. {
  1615. XmlDocument doc = new XmlDocument ();
  1616. doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
  1617. Serialize (doc, typeof (XmlDocument));
  1618. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
  1619. sw.GetStringBuilder ().ToString ());
  1620. }
  1621. [Test]
  1622. public void TestSerializeXmlElement ()
  1623. {
  1624. XmlDocument doc = new XmlDocument ();
  1625. Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
  1626. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  1627. }
  1628. [Test]
  1629. public void TestSerializeXmlElementSubclass ()
  1630. {
  1631. XmlDocument doc = new XmlDocument ();
  1632. Serialize (new MyElem (doc), typeof (XmlElement));
  1633. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
  1634. Serialize (new MyElem (doc), typeof (MyElem));
  1635. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
  1636. }
  1637. [Test]
  1638. public void TestSerializeXmlCDataSection ()
  1639. {
  1640. XmlDocument doc = new XmlDocument ();
  1641. CDataContainer c = new CDataContainer ();
  1642. c.cdata = doc.CreateCDataSection ("data section contents");
  1643. Serialize (c);
  1644. Assert.AreEqual (Infoset ("<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>"), WriterText);
  1645. }
  1646. [Test]
  1647. public void TestSerializeXmlNode ()
  1648. {
  1649. XmlDocument doc = new XmlDocument ();
  1650. NodeContainer c = new NodeContainer ();
  1651. c.node = doc.CreateTextNode ("text");
  1652. Serialize (c);
  1653. Assert.AreEqual (Infoset ("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
  1654. }
  1655. [Test]
  1656. public void TestSerializeChoice ()
  1657. {
  1658. Choices ch = new Choices ();
  1659. ch.MyChoice = "choice text";
  1660. ch.ItemType = ItemChoiceType.ChoiceZero;
  1661. Serialize (ch);
  1662. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceZero>choice text</ChoiceZero></Choices>"), WriterText, "#1");
  1663. ch.ItemType = ItemChoiceType.StrangeOne;
  1664. Serialize (ch);
  1665. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceOne>choice text</ChoiceOne></Choices>"), WriterText, "#2");
  1666. ch.ItemType = ItemChoiceType.ChoiceTwo;
  1667. Serialize (ch);
  1668. Assert.AreEqual (Infoset ("<Choices xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><ChoiceTwo>choice text</ChoiceTwo></Choices>"), WriterText, "#3");
  1669. }
  1670. [Test]
  1671. public void TestSerializeNamesWithSpaces ()
  1672. {
  1673. TestSpace ts = new TestSpace ();
  1674. ts.elem = 4;
  1675. ts.attr = 5;
  1676. Serialize (ts);
  1677. Assert.AreEqual (Infoset ("<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>"), WriterText);
  1678. }
  1679. [Test]
  1680. public void TestSerializeReadOnlyProps ()
  1681. {
  1682. ReadOnlyProperties ts = new ReadOnlyProperties ();
  1683. Serialize (ts);
  1684. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1685. }
  1686. [Test]
  1687. public void TestSerializeIList ()
  1688. {
  1689. clsPerson k = new clsPerson ();
  1690. k.EmailAccounts = new ArrayList ();
  1691. k.EmailAccounts.Add ("a");
  1692. k.EmailAccounts.Add ("b");
  1693. Serialize (k);
  1694. Assert.AreEqual (Infoset ("<clsPerson xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><EmailAccounts><anyType xsi:type=\"xsd:string\">a</anyType><anyType xsi:type=\"xsd:string\">b</anyType></EmailAccounts></clsPerson>"), WriterText);
  1695. }
  1696. [Test]
  1697. public void TestSerializeArrayEnc ()
  1698. {
  1699. SoapReflectionImporter imp = new SoapReflectionImporter ();
  1700. XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
  1701. XmlSerializer ser = new XmlSerializer (map);
  1702. StringWriter sw = new StringWriter ();
  1703. XmlTextWriter tw = new XmlTextWriter (sw);
  1704. tw.WriteStartElement ("aa");
  1705. ser.Serialize (tw, new ArrayClass ());
  1706. tw.WriteEndElement ();
  1707. }
  1708. [Test] // bug #76049
  1709. public void TestIncludeType ()
  1710. {
  1711. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1712. XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
  1713. imp.IncludeType (typeof (TestSpace));
  1714. XmlSerializer ser = new XmlSerializer (map);
  1715. ser.Serialize (new StringWriter (), new TestSpace ());
  1716. }
  1717. [Test]
  1718. public void TestSerializeChoiceArray ()
  1719. {
  1720. CompositeValueType v = new CompositeValueType ();
  1721. v.Init ();
  1722. Serialize (v);
  1723. Assert.AreEqual (Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>"), WriterText);
  1724. }
  1725. [Test]
  1726. public void TestArrayAttributeWithDataType ()
  1727. {
  1728. Serialize (new ArrayAttributeWithType ());
  1729. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  1730. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  1731. Assert.AreEqual (Infoset (res), WriterText);
  1732. }
  1733. [Test]
  1734. public void TestSubclassElementType ()
  1735. {
  1736. SubclassTestContainer c = new SubclassTestContainer ();
  1737. c.data = new SubclassTestSub ();
  1738. Serialize (c);
  1739. string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
  1740. res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
  1741. Assert.AreEqual (Infoset (res), WriterText);
  1742. }
  1743. [Test]
  1744. [ExpectedException (typeof (InvalidOperationException))]
  1745. public void TestArrayAttributeWithWrongDataType ()
  1746. {
  1747. Serialize (new ArrayAttributeWithWrongType ());
  1748. }
  1749. [Test]
  1750. [Category ("NotWorking")]
  1751. public void TestSerializePrimitiveTypesContainer ()
  1752. {
  1753. Serialize (new PrimitiveTypesContainer ());
  1754. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1755. "<?xml version='1.0' encoding='utf-16'?>" +
  1756. #if NET_2_0
  1757. "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
  1758. #else
  1759. "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='some:urn'>" +
  1760. #endif
  1761. "<Number>2004</Number>" +
  1762. "<Name>some name</Name>" +
  1763. "<Index>56</Index>" +
  1764. "<Password>8w8=</Password>" +
  1765. "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
  1766. "</PrimitiveTypesContainer>", XmlSchema.Namespace,
  1767. XmlSchema.InstanceNamespace), sw.ToString (), "#1");
  1768. SerializeEncoded (new PrimitiveTypesContainer ());
  1769. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1770. "<?xml version='1.0' encoding='utf-16'?>" +
  1771. #if NET_2_0
  1772. "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
  1773. #else
  1774. "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
  1775. #endif
  1776. "<Number xsi:type='xsd:int'>2004</Number>" +
  1777. "<Name xsi:type='xsd:string'>some name</Name>" +
  1778. "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
  1779. "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
  1780. "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
  1781. "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
  1782. XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
  1783. sw.ToString (), "#2");
  1784. }
  1785. [Test]
  1786. public void TestSchemaForm ()
  1787. {
  1788. TestSchemaForm1 t1 = new TestSchemaForm1 ();
  1789. t1.p1 = new PrintTypeResponse ();
  1790. t1.p1.Init ();
  1791. t1.p2 = new PrintTypeResponse ();
  1792. t1.p2.Init ();
  1793. TestSchemaForm2 t2 = new TestSchemaForm2 ();
  1794. t2.p1 = new PrintTypeResponse ();
  1795. t2.p1.Init ();
  1796. t2.p2 = new PrintTypeResponse ();
  1797. t2.p2.Init ();
  1798. Serialize (t1);
  1799. string res = "";
  1800. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1801. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1802. res += " <p1>";
  1803. res += " <result>";
  1804. res += " <data>data1</data>";
  1805. res += " </result>";
  1806. res += " <intern xmlns=\"urn:responseTypes\">";
  1807. res += " <result xmlns=\"\">";
  1808. res += " <data>data2</data>";
  1809. res += " </result>";
  1810. res += " </intern>";
  1811. res += " </p1>";
  1812. res += " <p2 xmlns=\"urn:oo\">";
  1813. res += " <result xmlns=\"\">";
  1814. res += " <data>data1</data>";
  1815. res += " </result>";
  1816. res += " <intern xmlns=\"urn:responseTypes\">";
  1817. res += " <result xmlns=\"\">";
  1818. res += " <data>data2</data>";
  1819. res += " </result>";
  1820. res += " </intern>";
  1821. res += " </p2>";
  1822. res += "</TestSchemaForm1>";
  1823. Assert.AreEqual (Infoset (res), WriterText);
  1824. Serialize (t2);
  1825. res = "";
  1826. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1827. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1828. res += " <p1 xmlns=\"urn:testForm\">";
  1829. res += " <result xmlns=\"\">";
  1830. res += " <data>data1</data>";
  1831. res += " </result>";
  1832. res += " <intern xmlns=\"urn:responseTypes\">";
  1833. res += " <result xmlns=\"\">";
  1834. res += " <data>data2</data>";
  1835. res += " </result>";
  1836. res += " </intern>";
  1837. res += " </p1>";
  1838. res += " <p2 xmlns=\"urn:oo\">";
  1839. res += " <result xmlns=\"\">";
  1840. res += " <data>data1</data>";
  1841. res += " </result>";
  1842. res += " <intern xmlns=\"urn:responseTypes\">";
  1843. res += " <result xmlns=\"\">";
  1844. res += " <data>data2</data>";
  1845. res += " </result>";
  1846. res += " </intern>";
  1847. res += " </p2>";
  1848. res += "</TestSchemaForm2>";
  1849. Assert.AreEqual (Infoset (res), WriterText);
  1850. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1851. XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
  1852. Serialize (t1, map);
  1853. res = "";
  1854. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1855. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1856. res += " <p1>";
  1857. res += " <result xmlns=\"\">";
  1858. res += " <data>data1</data>";
  1859. res += " </result>";
  1860. res += " <intern xmlns=\"urn:responseTypes\">";
  1861. res += " <result xmlns=\"\">";
  1862. res += " <data>data2</data>";
  1863. res += " </result>";
  1864. res += " </intern>";
  1865. res += " </p1>";
  1866. res += " <p2 xmlns=\"urn:oo\">";
  1867. res += " <result xmlns=\"\">";
  1868. res += " <data>data1</data>";
  1869. res += " </result>";
  1870. res += " <intern xmlns=\"urn:responseTypes\">";
  1871. res += " <result xmlns=\"\">";
  1872. res += " <data>data2</data>";
  1873. res += " </result>";
  1874. res += " </intern>";
  1875. res += " </p2>";
  1876. res += "</TestSchemaForm1>";
  1877. Assert.AreEqual (Infoset (res), WriterText);
  1878. imp = new XmlReflectionImporter ();
  1879. map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
  1880. Serialize (t2, map);
  1881. res = "";
  1882. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1883. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1884. res += " <p1 xmlns=\"urn:testForm\">";
  1885. res += " <result xmlns=\"\">";
  1886. res += " <data>data1</data>";
  1887. res += " </result>";
  1888. res += " <intern xmlns=\"urn:responseTypes\">";
  1889. res += " <result xmlns=\"\">";
  1890. res += " <data>data2</data>";
  1891. res += " </result>";
  1892. res += " </intern>";
  1893. res += " </p1>";
  1894. res += " <p2 xmlns=\"urn:oo\">";
  1895. res += " <result xmlns=\"\">";
  1896. res += " <data>data1</data>";
  1897. res += " </result>";
  1898. res += " <intern xmlns=\"urn:responseTypes\">";
  1899. res += " <result xmlns=\"\">";
  1900. res += " <data>data2</data>";
  1901. res += " </result>";
  1902. res += " </intern>";
  1903. res += " </p2>";
  1904. res += "</TestSchemaForm2>";
  1905. Assert.AreEqual (Infoset (res), WriterText);
  1906. }
  1907. [Test] // bug #78536
  1908. public void CDataTextNodes ()
  1909. {
  1910. XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
  1911. ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
  1912. string xml = @"<CDataTextNodesType>
  1913. <foo><![CDATA[
  1914. (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
  1915. \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
  1916. (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
  1917. ]]></foo>
  1918. </CDataTextNodesType>";
  1919. ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
  1920. }
  1921. #if !TARGET_JVM && !MOBILE
  1922. [Test]
  1923. public void GenerateSerializerGenerics ()
  1924. {
  1925. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1926. Type type = typeof (List<int>);
  1927. XmlSerializer.GenerateSerializer (
  1928. new Type [] {type},
  1929. new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
  1930. }
  1931. #endif
  1932. [Test]
  1933. public void Nullable ()
  1934. {
  1935. XmlSerializer ser = new XmlSerializer (typeof (int?));
  1936. int? nullableType = 5;
  1937. sw = new StringWriter ();
  1938. xtw = new XmlTextWriter (sw);
  1939. ser.Serialize (xtw, nullableType);
  1940. xtw.Close ();
  1941. string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
  1942. Assert.AreEqual (Infoset (expected), WriterText);
  1943. int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
  1944. Assert.AreEqual (5, i);
  1945. }
  1946. [Test]
  1947. public void NullableEnums ()
  1948. {
  1949. WithNulls w = new WithNulls ();
  1950. XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
  1951. StringWriter tw = new StringWriter ();
  1952. ser.Serialize (tw, w);
  1953. string expected = "<?xml version='1.0' encoding='utf-16'?>" +
  1954. "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  1955. "<nint xsi:nil='true' />" +
  1956. "<nenum xsi:nil='true' />" +
  1957. "<ndate xsi:nil='true' />" +
  1958. "</WithNulls>";
  1959. Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
  1960. StringReader sr = new StringReader (tw.ToString ());
  1961. w = (WithNulls) ser.Deserialize (sr);
  1962. Assert.IsFalse (w.nint.HasValue);
  1963. Assert.IsFalse (w.nenum.HasValue);
  1964. Assert.IsFalse (w.ndate.HasValue);
  1965. DateTime t = new DateTime (2008,4,1);
  1966. w.nint = 4;
  1967. w.ndate = t;
  1968. w.nenum = TestEnumWithNulls.bb;
  1969. tw = new StringWriter ();
  1970. ser.Serialize (tw, w);
  1971. expected = "<?xml version='1.0' encoding='utf-16'?>" +
  1972. "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  1973. "<nint>4</nint>" +
  1974. "<nenum>bb</nenum>" +
  1975. "<ndate>2008-04-01T00:00:00</ndate>" +
  1976. "</WithNulls>";
  1977. Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
  1978. sr = new StringReader (tw.ToString ());
  1979. w = (WithNulls) ser.Deserialize (sr);
  1980. Assert.IsTrue (w.nint.HasValue);
  1981. Assert.IsTrue (w.nenum.HasValue);
  1982. Assert.IsTrue (w.ndate.HasValue);
  1983. Assert.AreEqual (4, w.nint.Value);
  1984. Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
  1985. Assert.AreEqual (t, w.ndate.Value);
  1986. }
  1987. [Test]
  1988. public void SerializeBase64Binary()
  1989. {
  1990. XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
  1991. sw = new StringWriter ();
  1992. XmlTextWriter xtw = new XmlTextWriter (sw);
  1993. ser.Serialize (xtw, new Base64Binary ());
  1994. xtw.Close ();
  1995. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><Base64Binary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""AQID"" />";
  1996. Assert.AreEqual (Infoset (expected), WriterText);
  1997. Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
  1998. Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
  1999. }
  2000. [Test] // bug #79989, #79990
  2001. public void SerializeHexBinary ()
  2002. {
  2003. XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
  2004. sw = new StringWriter ();
  2005. XmlTextWriter xtw = new XmlTextWriter (sw);
  2006. ser.Serialize (xtw, new HexBinary ());
  2007. xtw.Close ();
  2008. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><HexBinary xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" Data=""010203"" />";
  2009. Assert.AreEqual (Infoset (expected), WriterText);
  2010. HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
  2011. Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
  2012. }
  2013. [Test]
  2014. [ExpectedException (typeof (InvalidOperationException))]
  2015. public void XmlArrayAttributeOnInt ()
  2016. {
  2017. new XmlSerializer (typeof (XmlArrayOnInt));
  2018. }
  2019. [Test]
  2020. [ExpectedException (typeof (InvalidOperationException))]
  2021. public void XmlArrayAttributeUnqualifiedWithNamespace ()
  2022. {
  2023. new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
  2024. }
  2025. [Test]
  2026. [ExpectedException (typeof (InvalidOperationException))]
  2027. public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
  2028. {
  2029. new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
  2030. }
  2031. [Test] // bug #78042
  2032. public void XmlArrayAttributeOnArray ()
  2033. {
  2034. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
  2035. sw = new StringWriter ();
  2036. XmlTextWriter xtw = new XmlTextWriter (sw);
  2037. ser.Serialize (xtw, new XmlArrayOnArray ());
  2038. xtw.Close ();
  2039. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArray xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><string xmlns=""urn:foo"">foo</string><string xmlns=""urn:foo"">bar</string></Sane><Mids xmlns=""""><ArrayItemInXmlArray xmlns=""urn:foo""><Whee xmlns=""""><string xmlns=""urn:gyabo"">foo</string><string xmlns=""urn:gyabo"">bar</string></Whee></ArrayItemInXmlArray></Mids></XmlArrayOnArray>";
  2040. Assert.AreEqual (Infoset (expected), WriterText);
  2041. }
  2042. [Test]
  2043. public void XmlArrayAttributeOnCollection ()
  2044. {
  2045. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
  2046. XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
  2047. inst.Sane.Add ("abc");
  2048. inst.Sane.Add (1);
  2049. sw = new StringWriter ();
  2050. XmlTextWriter xtw = new XmlTextWriter (sw);
  2051. ser.Serialize (xtw, inst);
  2052. xtw.Close ();
  2053. string expected = @"<?xml version=""1.0"" encoding=""utf-16""?><XmlArrayOnArrayList xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""urn:foo""><Sane xmlns=""""><anyType xsi:type=""xsd:string"" xmlns=""urn:foo"">abc</anyType><anyType xsi:type=""xsd:int"" xmlns=""urn:foo"">1</anyType></Sane></XmlArrayOnArrayList>";
  2054. Assert.AreEqual (Infoset (expected), WriterText);
  2055. }
  2056. [Test] // bug #338705
  2057. public void SerializeTimeSpan ()
  2058. {
  2059. // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
  2060. XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
  2061. ser.Serialize (TextWriter.Null, TimeSpan.Zero);
  2062. }
  2063. [Test]
  2064. public void SerializeDurationToString ()
  2065. {
  2066. XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
  2067. ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
  2068. }
  2069. [Test]
  2070. [ExpectedException (typeof (InvalidOperationException))]
  2071. public void SerializeDurationToTimeSpan ()
  2072. {
  2073. XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
  2074. ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
  2075. }
  2076. [Test]
  2077. [ExpectedException (typeof (InvalidOperationException))]
  2078. public void SerializeInvalidDataType ()
  2079. {
  2080. XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
  2081. ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
  2082. }
  2083. [Test]
  2084. #if !NET_2_0
  2085. [ExpectedException (typeof (ApplicationException))]
  2086. #endif
  2087. public void SerializeErrorneousIXmlSerializable ()
  2088. {
  2089. Serialize (new ErrorneousGetSchema ());
  2090. Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
  2091. }
  2092. #if NET_2_0
  2093. public void DateTimeRoundtrip ()
  2094. {
  2095. // bug #337729
  2096. XmlSerializer ser = new XmlSerializer (typeof (DateTime));
  2097. StringWriter sw = new StringWriter ();
  2098. ser.Serialize (sw, DateTime.UtcNow);
  2099. DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
  2100. Assert.AreEqual (DateTimeKind.Utc, d.Kind);
  2101. }
  2102. #endif
  2103. [Test]
  2104. public void SupportIXmlSerializableImplicitlyConvertible ()
  2105. {
  2106. XmlAttributes attrs = new XmlAttributes ();
  2107. XmlElementAttribute attr = new XmlElementAttribute ();
  2108. attr.ElementName = "XmlSerializable";
  2109. attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
  2110. attrs.XmlElements.Add (attr);
  2111. XmlAttributeOverrides attrOverrides = new
  2112. XmlAttributeOverrides ();
  2113. attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
  2114. XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
  2115. new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
  2116. }
  2117. [Test] // bug #566370
  2118. public void SerializeEnumWithCSharpKeyword ()
  2119. {
  2120. var ser = new XmlSerializer (typeof (DoxCompoundKind));
  2121. for (int i = 0; i < 100; i++) // test serialization code generator
  2122. ser.Serialize (Console.Out, DoxCompoundKind.@class);
  2123. }
  2124. public enum DoxCompoundKind
  2125. {
  2126. [XmlEnum("class")]
  2127. @class,
  2128. [XmlEnum("struct")]
  2129. @struct,
  2130. union,
  2131. [XmlEnum("interface")]
  2132. @interface,
  2133. protocol,
  2134. category,
  2135. exception,
  2136. file,
  2137. [XmlEnum("namespace")]
  2138. @namespace,
  2139. group,
  2140. page,
  2141. example,
  2142. dir
  2143. }
  2144. #region GenericsSeralizationTests
  2145. #if NET_2_0
  2146. [Test]
  2147. public void TestSerializeGenSimpleClassString ()
  2148. {
  2149. GenSimpleClass<string> simple = new GenSimpleClass<string> ();
  2150. Serialize (simple);
  2151. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  2152. simple.something = "hello";
  2153. Serialize (simple);
  2154. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>"), WriterText);
  2155. }
  2156. [Test]
  2157. public void TestSerializeGenSimpleClassBool ()
  2158. {
  2159. GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
  2160. Serialize (simple);
  2161. Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>"), WriterText);
  2162. simple.something = true;
  2163. Serialize (simple);
  2164. Assert.AreEqual (Infoset ("<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>"), WriterText);
  2165. }
  2166. [Test]
  2167. public void TestSerializeGenSimpleStructInt ()
  2168. {
  2169. GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
  2170. Serialize (simple);
  2171. Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>"), WriterText);
  2172. simple.something = 123;
  2173. Serialize (simple);
  2174. Assert.AreEqual (Infoset ("<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>"), WriterText);
  2175. }
  2176. [Test]
  2177. public void TestSerializeGenListClassString ()
  2178. {
  2179. GenListClass<string> genlist = new GenListClass<string> ();
  2180. Serialize (genlist);
  2181. Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>"), WriterText);
  2182. genlist.somelist.Add ("Value1");
  2183. genlist.somelist.Add ("Value2");
  2184. Serialize (genlist);
  2185. Assert.AreEqual (Infoset ("<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>"), WriterText);
  2186. }
  2187. [Test]
  2188. public void TestSerializeGenListClassFloat ()
  2189. {
  2190. GenListClass<float> genlist = new GenListClass<float> ();
  2191. Serialize (genlist);
  2192. Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>"), WriterText);
  2193. genlist.somelist.Add (1);
  2194. genlist.somelist.Add (2.2F);
  2195. Serialize (genlist);
  2196. Assert.AreEqual (Infoset ("<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>"), WriterText);
  2197. }
  2198. [Test]
  2199. public void TestSerializeGenListClassList ()
  2200. {
  2201. GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
  2202. Serialize (genlist);
  2203. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
  2204. GenListClass<int> inlist1 = new GenListClass<int> ();
  2205. inlist1.somelist.Add (1);
  2206. inlist1.somelist.Add (2);
  2207. GenListClass<int> inlist2 = new GenListClass<int> ();
  2208. inlist2.somelist.Add (10);
  2209. inlist2.somelist.Add (20);
  2210. genlist.somelist.Add (inlist1);
  2211. genlist.somelist.Add (inlist2);
  2212. Serialize (genlist);
  2213. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>"), WriterText);
  2214. }
  2215. [Test]
  2216. public void TestSerializeGenListClassArray ()
  2217. {
  2218. GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
  2219. Serialize (genlist);
  2220. Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
  2221. GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
  2222. genarr1.arr[0] = 'a';
  2223. genarr1.arr[1] = 'b';
  2224. genlist.somelist.Add (genarr1);
  2225. GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
  2226. genarr2.arr[0] = 'd';
  2227. genarr2.arr[1] = 'e';
  2228. genarr2.arr[2] = 'f';
  2229. genlist.somelist.Add (genarr2);
  2230. Serialize (genlist);
  2231. Assert.AreEqual (Infoset ("<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>"), WriterText);
  2232. }
  2233. [Test]
  2234. public void TestSerializeGenTwoClassCharDouble ()
  2235. {
  2236. GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
  2237. Serialize (gentwo);
  2238. Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>"), WriterText);
  2239. gentwo.something1 = 'a';
  2240. gentwo.something2 = 2.2;
  2241. Serialize (gentwo);
  2242. Assert.AreEqual (Infoset ("<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>"), WriterText);
  2243. }
  2244. [Test]
  2245. public void TestSerializeGenDerivedClassDecimalShort ()
  2246. {
  2247. GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
  2248. Serialize (derived);
  2249. Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
  2250. derived.something1 = "Value1";
  2251. derived.something2 = 1;
  2252. derived.another1 = 1.1M;
  2253. derived.another2 = -22;
  2254. Serialize (derived);
  2255. Assert.AreEqual (Infoset ("<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>"), WriterText);
  2256. }
  2257. [Test]
  2258. public void TestSerializeGenDerivedSecondClassByteUlong ()
  2259. {
  2260. GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
  2261. Serialize (derived2);
  2262. Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
  2263. derived2.something1 = 1;
  2264. derived2.something2 = 222;
  2265. derived2.another1 = 111;
  2266. derived2.another2 = 222222;
  2267. Serialize (derived2);
  2268. Assert.AreEqual (Infoset ("<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>"), WriterText);
  2269. }
  2270. [Test]
  2271. public void TestSerializeGenNestedClass ()
  2272. {
  2273. GenNestedClass<string, int>.InnerClass<bool> nested =
  2274. new GenNestedClass<string, int>.InnerClass<bool> ();
  2275. Serialize (nested);
  2276. Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>"), WriterText);
  2277. nested.inner = 5;
  2278. nested.something = true;
  2279. Serialize (nested);
  2280. Assert.AreEqual (Infoset ("<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>"), WriterText);
  2281. }
  2282. [Test]
  2283. public void TestSerializeGenListClassListNested ()
  2284. {
  2285. GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
  2286. new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
  2287. Serialize (genlist);
  2288. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
  2289. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
  2290. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2291. GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
  2292. inval1.inner = 1;
  2293. inval1.something = "ONE";
  2294. inlist1.somelist.Add (inval1);
  2295. GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
  2296. inval2.inner = 2;
  2297. inval2.something = "TWO";
  2298. inlist1.somelist.Add (inval2);
  2299. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
  2300. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2301. GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
  2302. inval3.inner = 30;
  2303. inval3.something = "THIRTY";
  2304. inlist2.somelist.Add (inval3);
  2305. genlist.somelist.Add (inlist1);
  2306. genlist.somelist.Add (inlist2);
  2307. Serialize (genlist);
  2308. Assert.AreEqual (Infoset ("<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>"), WriterText);
  2309. }
  2310. public enum Myenum { one, two, three, four, five, six };
  2311. [Test]
  2312. public void TestSerializeGenArrayClassEnum ()
  2313. {
  2314. GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
  2315. Serialize (genarr);
  2316. Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
  2317. genarr.arr[0] = Myenum.one;
  2318. genarr.arr[1] = Myenum.three;
  2319. genarr.arr[2] = Myenum.five;
  2320. Serialize (genarr);
  2321. Assert.AreEqual (Infoset ("<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>"), WriterText);
  2322. }
  2323. [Test]
  2324. public void TestSerializeGenArrayStruct ()
  2325. {
  2326. GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
  2327. Serialize (genarr);
  2328. Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></><:GenSimpleStructOfUInt32><:something>0</></></></>", WriterText);
  2329. GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
  2330. genstruct.something = 111;
  2331. genarr.arr[0] = genstruct;
  2332. genstruct.something = 222;
  2333. genarr.arr[1] = genstruct;
  2334. genstruct.something = 333;
  2335. genarr.arr[2] = genstruct;
  2336. Serialize (genarr);
  2337. Assert.AreEqual ("<:GenArrayClassOfGenSimpleStructOfUInt32 http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenSimpleStructOfUInt32><:something>111</></><:GenSimpleStructOfUInt32><:something>222</></><:GenSimpleStructOfUInt32><:something>333</></></></>", WriterText);
  2338. }
  2339. [Test]
  2340. public void TestSerializeGenArrayList ()
  2341. {
  2342. GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
  2343. Serialize (genarr);
  2344. Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></><:GenListClassOfString http://www.w3.org/2001/XMLSchema-instance:nil='true'></></></>", WriterText);
  2345. GenListClass<string> genlist1 = new GenListClass<string> ();
  2346. genlist1.somelist.Add ("list1-val1");
  2347. genlist1.somelist.Add ("list1-val2");
  2348. genarr.arr[0] = genlist1;
  2349. GenListClass<string> genlist2 = new GenListClass<string> ();
  2350. genlist2.somelist.Add ("list2-val1");
  2351. genlist2.somelist.Add ("list2-val2");
  2352. genlist2.somelist.Add ("list2-val3");
  2353. genlist2.somelist.Add ("list2-val4");
  2354. genarr.arr[1] = genlist2;
  2355. GenListClass<string> genlist3 = new GenListClass<string> ();
  2356. genlist3.somelist.Add ("list3val");
  2357. genarr.arr[2] = genlist3;
  2358. Serialize (genarr);
  2359. Assert.AreEqual ("<:GenArrayClassOfGenListClassOfString http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:arr><:GenListClassOfString><:somelist><:string>list1-val1</><:string>list1-val2</></></><:GenListClassOfString><:somelist><:string>list2-val1</><:string>list2-val2</><:string>list2-val3</><:string>list2-val4</></></><:GenListClassOfString><:somelist><:string>list3val</></></></></>", WriterText);
  2360. }
  2361. [Test]
  2362. public void TestSerializeGenComplexStruct ()
  2363. {
  2364. GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
  2365. Serialize (complex);
  2366. Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>0</><:simpleclass><:something>0</></><:simplestruct><:something>0</></><:listclass><:somelist></></><:arrayclass><:arr><:int>0</><:int>0</><:int>0</></></><:twoclass><:something1>0</></><:derivedclass><:something2>0</><:another1>0</></><:derived2><:something1>0</><:another1>0</></><:nestedouter><:outer>0</></><:nestedinner><:something>0</></></>", WriterText);
  2367. complex.something = 123;
  2368. complex.simpleclass.something = 456;
  2369. complex.simplestruct.something = 789;
  2370. GenListClass<int> genlist = new GenListClass<int> ();
  2371. genlist.somelist.Add (100);
  2372. genlist.somelist.Add (200);
  2373. complex.listclass = genlist;
  2374. GenArrayClass<int> genarr = new GenArrayClass<int> ();
  2375. genarr.arr[0] = 11;
  2376. genarr.arr[1] = 22;
  2377. genarr.arr[2] = 33;
  2378. complex.arrayclass = genarr;
  2379. complex.twoclass.something1 = 10;
  2380. complex.twoclass.something2 = "Ten";
  2381. complex.derivedclass.another1 = 1;
  2382. complex.derivedclass.another2 = "one";
  2383. complex.derivedclass.something1 = "two";
  2384. complex.derivedclass.something2 = 2;
  2385. complex.derived2.another1 = 3;
  2386. complex.derived2.another2 = "three";
  2387. complex.derived2.something1 = 4;
  2388. complex.derived2.something2 = "four";
  2389. complex.nestedouter.outer = 5;
  2390. complex.nestedinner.inner = "six";
  2391. complex.nestedinner.something = 6;
  2392. Serialize (complex);
  2393. Assert.AreEqual ("<:GenComplexStructOfInt32String http://www.w3.org/2000/xmlns/:xsd='http://www.w3.org/2001/XMLSchema' http://www.w3.org/2000/xmlns/:xsi='http://www.w3.org/2001/XMLSchema-instance'><:something>123</><:simpleclass><:something>456</></><:simplestruct><:something>789</></><:listclass><:somelist><:int>100</><:int>200</></></><:arrayclass><:arr><:int>11</><:int>22</><:int>33</></></><:twoclass><:something1>10</><:something2>Ten</></><:derivedclass><:something1>two</><:something2>2</><:another1>1</><:another2>one</></><:derived2><:something1>4</><:something2>four</><:another1>3</><:another2>three</></><:nestedouter><:outer>5</></><:nestedinner><:inner>six</><:something>6</></></>", WriterText);
  2394. }
  2395. [Test] // bug #80759
  2396. public void HasNullableField ()
  2397. {
  2398. Bug80759 foo = new Bug80759 ();
  2399. foo.Test = "BAR";
  2400. foo.NullableInt = 10;
  2401. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
  2402. MemoryStream stream = new MemoryStream ();
  2403. serializer.Serialize (stream, foo);
  2404. stream.Position = 0;
  2405. foo = (Bug80759) serializer.Deserialize (stream);
  2406. }
  2407. [Test] // bug #80759, with fieldSpecified.
  2408. [ExpectedException (typeof (InvalidOperationException))]
  2409. [Category ("NotWorking")]
  2410. public void HasFieldSpecifiedButIrrelevant ()
  2411. {
  2412. Bug80759_2 foo = new Bug80759_2 ();
  2413. foo.Test = "BAR";
  2414. foo.NullableInt = 10;
  2415. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
  2416. MemoryStream stream = new MemoryStream ();
  2417. serializer.Serialize (stream, foo);
  2418. stream.Position = 0;
  2419. foo = (Bug80759_2) serializer.Deserialize (stream);
  2420. }
  2421. [Test]
  2422. public void HasNullableField2 ()
  2423. {
  2424. Bug80759 foo = new Bug80759 ();
  2425. foo.Test = "BAR";
  2426. foo.NullableInt = 10;
  2427. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
  2428. MemoryStream stream = new MemoryStream ();
  2429. serializer.Serialize (stream, foo);
  2430. stream.Position = 0;
  2431. foo = (Bug80759) serializer.Deserialize (stream);
  2432. Assert.AreEqual ("BAR", foo.Test, "#1");
  2433. Assert.AreEqual (10, foo.NullableInt, "#2");
  2434. foo.NullableInt = null;
  2435. stream = new MemoryStream ();
  2436. serializer.Serialize (stream, foo);
  2437. stream.Position = 0;
  2438. foo = (Bug80759) serializer.Deserialize (stream);
  2439. Assert.AreEqual ("BAR", foo.Test, "#3");
  2440. Assert.IsNull (foo.NullableInt, "#4");
  2441. }
  2442. [Test]
  2443. public void SupportPrivateCtorOnly ()
  2444. {
  2445. XmlSerializer xs =
  2446. new XmlSerializer (typeof (PrivateCtorOnly));
  2447. StringWriter sw = new StringWriter ();
  2448. xs.Serialize (sw, PrivateCtorOnly.Instance);
  2449. xs.Deserialize (new StringReader (sw.ToString ()));
  2450. }
  2451. [Test]
  2452. public void XmlSchemaProviderQNameBecomesRootName ()
  2453. {
  2454. xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
  2455. Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
  2456. Assert.AreEqual (Infoset ("<foo />"), WriterText);
  2457. xs.Deserialize (new StringReader ("<foo/>"));
  2458. }
  2459. [Test]
  2460. public void XmlSchemaProviderQNameBecomesRootName2 ()
  2461. {
  2462. string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
  2463. xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
  2464. Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
  2465. Assert.AreEqual (Infoset (xml), WriterText);
  2466. xs.Deserialize (new StringReader (xml));
  2467. }
  2468. [Test]
  2469. public void XmlAnyElementForObjects () // bug #553032
  2470. {
  2471. new XmlSerializer (typeof (XmlAnyElementForObjectsType));
  2472. }
  2473. [Test]
  2474. [ExpectedException (typeof (InvalidOperationException))]
  2475. public void XmlAnyElementForObjects2 () // bug #553032-2
  2476. {
  2477. new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
  2478. }
  2479. public class Bug2893 {
  2480. public Bug2893 ()
  2481. {
  2482. Contents = new XmlDataDocument();
  2483. }
  2484. [XmlAnyElement("Contents")]
  2485. public XmlNode Contents;
  2486. }
  2487. // Bug Xamarin #2893
  2488. [Test]
  2489. public void XmlAnyElementForXmlNode ()
  2490. {
  2491. var obj = new Bug2893 ();
  2492. XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
  2493. XmlWriterSettings settings = new XmlWriterSettings();
  2494. var xsn = new XmlSerializerNamespaces();
  2495. xsn.Add(string.Empty, string.Empty);
  2496. byte[] buffer = new byte[2048];
  2497. var ms = new MemoryStream(buffer);
  2498. using (var xw = XmlWriter.Create(ms, settings))
  2499. {
  2500. mySerializer.Serialize(xw, obj, xsn);
  2501. xw.Flush();
  2502. }
  2503. mySerializer.Serialize(ms, obj);
  2504. }
  2505. [Test]
  2506. public void XmlRootOverridesSchemaProviderQName ()
  2507. {
  2508. var obj = new XmlRootOverridesSchemaProviderQNameType ();
  2509. XmlSerializer xs = new XmlSerializer (obj.GetType ());
  2510. var sw = new StringWriter ();
  2511. using (XmlWriter xw = XmlWriter.Create (sw))
  2512. xs.Serialize (xw, obj);
  2513. Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
  2514. }
  2515. #endif
  2516. #endregion //GenericsSeralizationTests
  2517. public class XmlArrayOnInt
  2518. {
  2519. [XmlArray]
  2520. public int Bogus;
  2521. }
  2522. public class XmlArrayUnqualifiedWithNamespace
  2523. {
  2524. [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2525. public ArrayList Sane = new ArrayList ();
  2526. }
  2527. public class XmlArrayItemUnqualifiedWithNamespace
  2528. {
  2529. [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2530. public ArrayList Sane = new ArrayList ();
  2531. }
  2532. [XmlRoot (Namespace = "urn:foo")]
  2533. public class XmlArrayOnArrayList
  2534. {
  2535. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2536. public ArrayList Sane = new ArrayList ();
  2537. }
  2538. [XmlRoot (Namespace = "urn:foo")]
  2539. public class XmlArrayOnArray
  2540. {
  2541. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2542. public string[] Sane = new string[] { "foo", "bar" };
  2543. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2544. public ArrayItemInXmlArray[] Mids =
  2545. new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
  2546. }
  2547. [XmlType (Namespace = "urn:gyabo")]
  2548. public class ArrayItemInXmlArray
  2549. {
  2550. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2551. public string[] Whee = new string[] { "foo", "bar" };
  2552. }
  2553. [XmlRoot ("Base64Binary")]
  2554. public class Base64Binary
  2555. {
  2556. [XmlAttribute (DataType = "base64Binary")]
  2557. public byte [] Data = new byte [] {1, 2, 3};
  2558. }
  2559. [XmlRoot ("HexBinary")]
  2560. public class HexBinary
  2561. {
  2562. [XmlAttribute (DataType = "hexBinary")]
  2563. public byte[] Data = new byte[] { 1, 2, 3 };
  2564. }
  2565. [XmlRoot ("PrivateCtorOnly")]
  2566. public class PrivateCtorOnly
  2567. {
  2568. public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
  2569. private PrivateCtorOnly ()
  2570. {
  2571. }
  2572. }
  2573. public class CDataTextNodesType
  2574. {
  2575. public CDataTextNodesInternal foo;
  2576. }
  2577. public class CDataTextNodesInternal
  2578. {
  2579. [XmlText]
  2580. public string Value;
  2581. }
  2582. public class InvalidTypeContainer
  2583. {
  2584. [XmlElement (DataType = "invalid")]
  2585. public string InvalidTypeItem = "aaa";
  2586. }
  2587. public class TimeSpanContainer1
  2588. {
  2589. [XmlElement (DataType = "duration")]
  2590. public string StringDuration = "aaa";
  2591. }
  2592. public class TimeSpanContainer2
  2593. {
  2594. [XmlElement (DataType = "duration")]
  2595. public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
  2596. }
  2597. #if NET_2_0
  2598. public class Bug80759
  2599. {
  2600. public string Test;
  2601. public int? NullableInt;
  2602. }
  2603. public class Bug80759_2
  2604. {
  2605. public string Test;
  2606. public int? NullableInt;
  2607. [XmlIgnore]
  2608. public bool NullableIntSpecified {
  2609. get { return NullableInt.HasValue; }
  2610. }
  2611. }
  2612. [XmlSchemaProvider ("GetXsdType")]
  2613. public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
  2614. {
  2615. public XmlSchema GetSchema ()
  2616. {
  2617. return null;
  2618. }
  2619. public void ReadXml (XmlReader reader)
  2620. {
  2621. reader.Skip ();
  2622. }
  2623. public void WriteXml (XmlWriter writer)
  2624. {
  2625. }
  2626. public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
  2627. {
  2628. if (xss.Count == 0) {
  2629. XmlSchema xs = new XmlSchema ();
  2630. XmlSchemaComplexType ct = new XmlSchemaComplexType ();
  2631. ct.Name = "foo";
  2632. xs.Items.Add (ct);
  2633. xss.Add (xs);
  2634. }
  2635. return new XmlQualifiedName ("foo");
  2636. }
  2637. }
  2638. public class XmlSchemaProviderQNameBecomesRootNameType2
  2639. {
  2640. [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
  2641. public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
  2642. }
  2643. public class XmlAnyElementForObjectsType
  2644. {
  2645. [XmlAnyElement]
  2646. public object [] arr = new object [] {3,4,5};
  2647. }
  2648. [XmlRoot ("foo")]
  2649. [XmlSchemaProvider ("GetSchema")]
  2650. public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
  2651. {
  2652. public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
  2653. {
  2654. var xs = new XmlSchema ();
  2655. var xse = new XmlSchemaComplexType () { Name = "bar" };
  2656. xs.Items.Add (xse);
  2657. xss.Add (xs);
  2658. return new XmlQualifiedName ("bar");
  2659. }
  2660. XmlSchema IXmlSerializable.GetSchema ()
  2661. {
  2662. return null;
  2663. }
  2664. void IXmlSerializable.ReadXml (XmlReader reader)
  2665. {
  2666. }
  2667. void IXmlSerializable.WriteXml (XmlWriter writer)
  2668. {
  2669. }
  2670. }
  2671. #endif
  2672. void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
  2673. {
  2674. Assert.Fail ();
  2675. }
  2676. // Helper methods
  2677. public static string Infoset (string sx)
  2678. {
  2679. XmlDocument doc = new XmlDocument ();
  2680. doc.LoadXml (sx);
  2681. StringBuilder sb = new StringBuilder ();
  2682. GetInfoset (doc.DocumentElement, sb);
  2683. return sb.ToString ();
  2684. }
  2685. public static string Infoset (XmlNode nod)
  2686. {
  2687. StringBuilder sb = new StringBuilder ();
  2688. GetInfoset (nod, sb);
  2689. return sb.ToString ();
  2690. }
  2691. static void GetInfoset (XmlNode nod, StringBuilder sb)
  2692. {
  2693. switch (nod.NodeType) {
  2694. case XmlNodeType.Attribute:
  2695. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  2696. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  2697. break;
  2698. case XmlNodeType.Element:
  2699. XmlElement elem = (XmlElement) nod;
  2700. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  2701. ArrayList ats = new ArrayList ();
  2702. foreach (XmlAttribute at in elem.Attributes)
  2703. ats.Add (at.LocalName + " " + at.NamespaceURI);
  2704. ats.Sort ();
  2705. foreach (string name in ats) {
  2706. string[] nn = name.Split (' ');
  2707. GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
  2708. }
  2709. sb.Append (">");
  2710. foreach (XmlNode cn in elem.ChildNodes)
  2711. GetInfoset (cn, sb);
  2712. sb.Append ("</>");
  2713. break;
  2714. default:
  2715. sb.Append (nod.OuterXml);
  2716. break;
  2717. }
  2718. }
  2719. static XmlTypeMapping CreateSoapMapping (Type type)
  2720. {
  2721. SoapReflectionImporter importer = new SoapReflectionImporter ();
  2722. return importer.ImportTypeMapping (type);
  2723. }
  2724. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
  2725. {
  2726. SoapReflectionImporter importer = new SoapReflectionImporter (ao);
  2727. return importer.ImportTypeMapping (type);
  2728. }
  2729. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
  2730. {
  2731. SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
  2732. return importer.ImportTypeMapping (type);
  2733. }
  2734. }
  2735. }