XmlSerializerTests.cs 137 KB

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