XmlSerializerTests.cs 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  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. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  145. [Test]
  146. public void TestConstructor()
  147. {
  148. XmlSerializer ser = new XmlSerializer (null, "");
  149. }
  150. #else
  151. #endif
  152. // test basic types ////////////////////////////////////////////////////////
  153. [Test]
  154. public void TestSerializeInt ()
  155. {
  156. Serialize (10);
  157. Assert.AreEqual (Infoset ("<int>10</int>"), WriterText);
  158. }
  159. [Test]
  160. public void TestSerializeBool ()
  161. {
  162. Serialize (true);
  163. Assert.AreEqual (Infoset ("<boolean>true</boolean>"), WriterText);
  164. Serialize (false);
  165. Assert.AreEqual (Infoset ("<boolean>false</boolean>"), WriterText);
  166. }
  167. [Test]
  168. public void TestSerializeString ()
  169. {
  170. Serialize ("hello");
  171. Assert.AreEqual (Infoset ("<string>hello</string>"), WriterText);
  172. }
  173. [Test]
  174. public void TestSerializeEmptyString ()
  175. {
  176. Serialize (String.Empty);
  177. Assert.AreEqual (Infoset ("<string />"), WriterText);
  178. }
  179. [Test]
  180. public void TestSerializeNullObject ()
  181. {
  182. Serialize (null, typeof (object));
  183. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  184. "<anyType xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  185. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  186. }
  187. [Test]
  188. [Ignore ("The generated XML is not exact but it is equivalent")]
  189. public void TestSerializeNullString ()
  190. {
  191. Serialize (null, typeof (string));
  192. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  193. "<string xmlns:xsd='{0}' xmlns:xsi='{1}' xsi:nil='true' />",
  194. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  195. }
  196. [Test]
  197. public void TestSerializeIntArray ()
  198. {
  199. Serialize (new int[] { 1, 2, 3, 4 });
  200. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  201. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>",
  202. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  203. }
  204. [Test]
  205. public void TestSerializeEmptyArray ()
  206. {
  207. Serialize (new int[] { });
  208. Assert.AreEqual (Infoset (string.Format (CultureInfo.InvariantCulture,
  209. "<ArrayOfInt xmlns:xsd='{0}' xmlns:xsi='{1}' />",
  210. XmlSchema.Namespace, XmlSchema.InstanceNamespace)), WriterText);
  211. }
  212. [Test]
  213. public void TestSerializeChar ()
  214. {
  215. Serialize ('A');
  216. Assert.AreEqual (Infoset ("<char>65</char>"), WriterText);
  217. Serialize ('\0');
  218. Assert.AreEqual (Infoset ("<char>0</char>"), WriterText);
  219. Serialize ('\n');
  220. Assert.AreEqual (Infoset ("<char>10</char>"), WriterText);
  221. Serialize ('\uFF01');
  222. Assert.AreEqual (Infoset ("<char>65281</char>"), WriterText);
  223. }
  224. [Test]
  225. public void TestSerializeFloat ()
  226. {
  227. Serialize (10.78);
  228. Assert.AreEqual (Infoset ("<double>10.78</double>"), WriterText);
  229. Serialize (-1e8);
  230. Assert.AreEqual (Infoset ("<double>-100000000</double>"), WriterText);
  231. // FIXME test INF and other boundary conditions that may exist with floats
  232. }
  233. [Test]
  234. public void TestSerializeEnumeration_FromValue ()
  235. {
  236. Serialize ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  237. Assert.AreEqual (
  238. "<?xml version='1.0' encoding='utf-16'?>" +
  239. "<SimpleEnumeration>SECOND</SimpleEnumeration>",
  240. sw.ToString ());
  241. }
  242. [Test]
  243. public void TestSerializeEnumeration_FromValue_Encoded ()
  244. {
  245. SerializeEncoded ((int) SimpleEnumeration.SECOND, typeof (SimpleEnumeration));
  246. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  247. "<?xml version='1.0' encoding='utf-16'?>" +
  248. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  249. XmlSchema.InstanceNamespace), sw.ToString ());
  250. }
  251. [Test]
  252. public void TestSerializeEnumeration ()
  253. {
  254. Serialize (SimpleEnumeration.FIRST);
  255. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#1");
  256. Serialize (SimpleEnumeration.SECOND);
  257. Assert.AreEqual (Infoset ("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText, "#2");
  258. }
  259. [Test]
  260. public void TestSerializeEnumeration_Encoded ()
  261. {
  262. SerializeEncoded (SimpleEnumeration.FIRST);
  263. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  264. "<?xml version='1.0' encoding='utf-16'?>" +
  265. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>FIRST</SimpleEnumeration>",
  266. XmlSchema.InstanceNamespace), sw.ToString (), "#B1");
  267. SerializeEncoded (SimpleEnumeration.SECOND);
  268. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  269. "<?xml version='1.0' encoding='utf-16'?>" +
  270. "<SimpleEnumeration d1p1:type='SimpleEnumeration' xmlns:d1p1='{0}'>SECOND</SimpleEnumeration>",
  271. XmlSchema.InstanceNamespace), sw.ToString (), "#B2");
  272. }
  273. [Test]
  274. public void TestSerializeEnumDefaultValue ()
  275. {
  276. Serialize (new EnumDefaultValue ());
  277. Assert.AreEqual (Infoset ("<EnumDefaultValue />"), WriterText, "#1");
  278. Serialize (new SimpleEnumeration ());
  279. Assert.AreEqual (Infoset ("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText, "#2");
  280. Serialize (3, typeof (EnumDefaultValue));
  281. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#3");
  282. Serialize (EnumDefaultValue.e3, typeof (EnumDefaultValue));
  283. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#4");
  284. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2, typeof (EnumDefaultValue));
  285. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#5");
  286. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  287. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#6");
  288. Serialize (EnumDefaultValue.e1 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  289. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#7");
  290. Serialize (EnumDefaultValue.e2 | EnumDefaultValue.e3, typeof (EnumDefaultValue));
  291. Assert.AreEqual (Infoset ("<EnumDefaultValue>e3</EnumDefaultValue>"), WriterText, "#8");
  292. Serialize (3, typeof (FlagEnum));
  293. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#9");
  294. Serialize (5, typeof (FlagEnum));
  295. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#10");
  296. Serialize (FlagEnum.e4, typeof (FlagEnum));
  297. Assert.AreEqual (Infoset ("<FlagEnum>four</FlagEnum>"), WriterText, "#11");
  298. Serialize (FlagEnum.e1 | FlagEnum.e2, typeof (FlagEnum));
  299. Assert.AreEqual (Infoset ("<FlagEnum>one two</FlagEnum>"), WriterText, "#12");
  300. Serialize (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  301. Assert.AreEqual (Infoset ("<FlagEnum>one two four</FlagEnum>"), WriterText, "#13");
  302. Serialize (FlagEnum.e1 | FlagEnum.e4, typeof (FlagEnum));
  303. Assert.AreEqual (Infoset ("<FlagEnum>one four</FlagEnum>"), WriterText, "#14");
  304. Serialize (FlagEnum.e2 | FlagEnum.e4, typeof (FlagEnum));
  305. Assert.AreEqual (Infoset ("<FlagEnum>two four</FlagEnum>"), WriterText, "#15");
  306. Serialize (3, typeof (EnumDefaultValueNF));
  307. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e3</EnumDefaultValueNF>"), WriterText, "#16");
  308. Serialize (EnumDefaultValueNF.e2, typeof (EnumDefaultValueNF));
  309. Assert.AreEqual (Infoset ("<EnumDefaultValueNF>e2</EnumDefaultValueNF>"), WriterText, "#17");
  310. Serialize (2, typeof (ZeroFlagEnum));
  311. Assert.AreEqual (Infoset ("<ZeroFlagEnum>tns:t&lt;w&gt;o</ZeroFlagEnum>"), WriterText, "#18");
  312. Serialize (new ZeroFlagEnum ()); // enum actually has a field with value 0
  313. Assert.AreEqual (Infoset ("<ZeroFlagEnum>zero</ZeroFlagEnum>"), WriterText, "#19");
  314. }
  315. [Test]
  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. public void TestSerializeXmlNodeArrayIncludesAttribute ()
  1512. {
  1513. XmlDocument doc = new XmlDocument ();
  1514. Serialize (new XmlNode[] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (XmlNode []));
  1515. }
  1516. [Test]
  1517. public void TestSerializeXmlElementArray ()
  1518. {
  1519. XmlDocument doc = new XmlDocument ();
  1520. Serialize (new XmlElement[] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }, typeof (object));
  1521. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1522. }
  1523. [Test]
  1524. [ExpectedException (typeof (InvalidOperationException))] // List<XmlNode> is not supported
  1525. public void TestSerializeGenericListOfNode ()
  1526. {
  1527. XmlDocument doc = new XmlDocument ();
  1528. Serialize (new List<XmlNode> (new XmlNode [] { doc.CreateAttribute ("at"), doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1529. Assert.AreEqual (Infoset ("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  1530. }
  1531. [Test]
  1532. [ExpectedException (typeof (InvalidOperationException))] // List<XmlElement> is not supported
  1533. public void TestSerializeGenericListOfElement ()
  1534. {
  1535. XmlDocument doc = new XmlDocument ();
  1536. Serialize (new List<XmlElement> (new XmlElement [] { doc.CreateElement ("elem1"), doc.CreateElement ("elem2") }), typeof (object));
  1537. Assert.AreEqual (Infoset ("<anyType><elem1/><elem2/></anyType>"), WriterText);
  1538. }
  1539. [Test]
  1540. public void TestSerializeXmlDocument ()
  1541. {
  1542. XmlDocument doc = new XmlDocument ();
  1543. doc.LoadXml (@"<?xml version=""1.0"" encoding=""utf-8"" ?><root/>");
  1544. Serialize (doc, typeof (XmlDocument));
  1545. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><root />",
  1546. sw.GetStringBuilder ().ToString ());
  1547. }
  1548. [Test]
  1549. public void TestSerializeXmlElement ()
  1550. {
  1551. XmlDocument doc = new XmlDocument ();
  1552. Serialize (doc.CreateElement ("elem"), typeof (XmlElement));
  1553. Assert.AreEqual (Infoset ("<elem/>"), WriterText);
  1554. }
  1555. [Test]
  1556. public void TestSerializeXmlElementSubclass ()
  1557. {
  1558. XmlDocument doc = new XmlDocument ();
  1559. Serialize (new MyElem (doc), typeof (XmlElement));
  1560. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#1");
  1561. Serialize (new MyElem (doc), typeof (MyElem));
  1562. Assert.AreEqual (Infoset ("<myelem aa=\"1\"/>"), WriterText, "#2");
  1563. }
  1564. [Test]
  1565. public void TestSerializeXmlCDataSection ()
  1566. {
  1567. XmlDocument doc = new XmlDocument ();
  1568. CDataContainer c = new CDataContainer ();
  1569. c.cdata = doc.CreateCDataSection ("data section contents");
  1570. Serialize (c);
  1571. 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);
  1572. }
  1573. [Test]
  1574. public void TestSerializeXmlNode ()
  1575. {
  1576. XmlDocument doc = new XmlDocument ();
  1577. NodeContainer c = new NodeContainer ();
  1578. c.node = doc.CreateTextNode ("text");
  1579. Serialize (c);
  1580. 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);
  1581. }
  1582. [Test]
  1583. public void TestSerializeChoice ()
  1584. {
  1585. Choices ch = new Choices ();
  1586. ch.MyChoice = "choice text";
  1587. ch.ItemType = ItemChoiceType.ChoiceZero;
  1588. Serialize (ch);
  1589. 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");
  1590. ch.ItemType = ItemChoiceType.StrangeOne;
  1591. Serialize (ch);
  1592. 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");
  1593. ch.ItemType = ItemChoiceType.ChoiceTwo;
  1594. Serialize (ch);
  1595. 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");
  1596. }
  1597. [Test]
  1598. public void TestSerializeNamesWithSpaces ()
  1599. {
  1600. TestSpace ts = new TestSpace ();
  1601. ts.elem = 4;
  1602. ts.attr = 5;
  1603. Serialize (ts);
  1604. 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);
  1605. }
  1606. [Test]
  1607. public void TestSerializeReadOnlyProps ()
  1608. {
  1609. ReadOnlyProperties ts = new ReadOnlyProperties ();
  1610. Serialize (ts);
  1611. Assert.AreEqual (Infoset ("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  1612. }
  1613. [Test]
  1614. public void TestSerializeReadOnlyListProp ()
  1615. {
  1616. ReadOnlyListProperty ts = new ReadOnlyListProperty ();
  1617. Serialize (ts);
  1618. 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);
  1619. }
  1620. [Test]
  1621. public void TestSerializeIList ()
  1622. {
  1623. clsPerson k = new clsPerson ();
  1624. k.EmailAccounts = new ArrayList ();
  1625. k.EmailAccounts.Add ("a");
  1626. k.EmailAccounts.Add ("b");
  1627. Serialize (k);
  1628. 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);
  1629. }
  1630. [Test]
  1631. public void TestSerializeArrayEnc ()
  1632. {
  1633. SoapReflectionImporter imp = new SoapReflectionImporter ();
  1634. XmlTypeMapping map = imp.ImportTypeMapping (typeof (ArrayClass));
  1635. XmlSerializer ser = new XmlSerializer (map);
  1636. StringWriter sw = new StringWriter ();
  1637. XmlTextWriter tw = new XmlTextWriter (sw);
  1638. tw.WriteStartElement ("aa");
  1639. ser.Serialize (tw, new ArrayClass ());
  1640. tw.WriteEndElement ();
  1641. }
  1642. [Test] // bug #76049
  1643. public void TestIncludeType ()
  1644. {
  1645. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1646. XmlTypeMapping map = imp.ImportTypeMapping (typeof (object));
  1647. imp.IncludeType (typeof (TestSpace));
  1648. XmlSerializer ser = new XmlSerializer (map);
  1649. ser.Serialize (new StringWriter (), new TestSpace ());
  1650. }
  1651. [Test]
  1652. public void TestSerializeChoiceArray ()
  1653. {
  1654. CompositeValueType v = new CompositeValueType ();
  1655. v.Init ();
  1656. Serialize (v);
  1657. 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);
  1658. }
  1659. [Test]
  1660. public void TestArrayAttributeWithDataType ()
  1661. {
  1662. Serialize (new ArrayAttributeWithType ());
  1663. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  1664. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  1665. Assert.AreEqual (Infoset (res), WriterText);
  1666. }
  1667. [Test]
  1668. public void TestSubclassElementType ()
  1669. {
  1670. SubclassTestContainer c = new SubclassTestContainer ();
  1671. c.data = new SubclassTestSub ();
  1672. Serialize (c);
  1673. string res = "<SubclassTestContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
  1674. res += "<a xsi:type=\"SubclassTestSub\"/></SubclassTestContainer>";
  1675. Assert.AreEqual (Infoset (res), WriterText);
  1676. }
  1677. [Test]
  1678. [ExpectedException (typeof (InvalidOperationException))]
  1679. public void TestArrayAttributeWithWrongDataType ()
  1680. {
  1681. Serialize (new ArrayAttributeWithWrongType ());
  1682. }
  1683. [Test]
  1684. [Category ("NotWorking")]
  1685. public void TestSerializePrimitiveTypesContainer ()
  1686. {
  1687. Serialize (new PrimitiveTypesContainer ());
  1688. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1689. "<?xml version='1.0' encoding='utf-16'?>" +
  1690. "<PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' xmlns='some:urn'>" +
  1691. "<Number>2004</Number>" +
  1692. "<Name>some name</Name>" +
  1693. "<Index>56</Index>" +
  1694. "<Password>8w8=</Password>" +
  1695. "<PathSeparatorCharacter>47</PathSeparatorCharacter>" +
  1696. "</PrimitiveTypesContainer>", XmlSchema.Namespace,
  1697. XmlSchema.InstanceNamespace), sw.ToString (), "#1");
  1698. SerializeEncoded (new PrimitiveTypesContainer ());
  1699. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  1700. "<?xml version='1.0' encoding='utf-16'?>" +
  1701. "<q1:PrimitiveTypesContainer xmlns:xsi='{1}' xmlns:xsd='{0}' id='id1' xmlns:q1='{2}'>" +
  1702. "<Number xsi:type='xsd:int'>2004</Number>" +
  1703. "<Name xsi:type='xsd:string'>some name</Name>" +
  1704. "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
  1705. "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
  1706. "<PathSeparatorCharacter xmlns:q2='{3}' xsi:type='q2:char'>47</PathSeparatorCharacter>" +
  1707. "</q1:PrimitiveTypesContainer>", XmlSchema.Namespace,
  1708. XmlSchema.InstanceNamespace, AnotherNamespace, WsdlTypesNamespace),
  1709. sw.ToString (), "#2");
  1710. }
  1711. [Test]
  1712. public void TestSchemaForm ()
  1713. {
  1714. TestSchemaForm1 t1 = new TestSchemaForm1 ();
  1715. t1.p1 = new PrintTypeResponse ();
  1716. t1.p1.Init ();
  1717. t1.p2 = new PrintTypeResponse ();
  1718. t1.p2.Init ();
  1719. TestSchemaForm2 t2 = new TestSchemaForm2 ();
  1720. t2.p1 = new PrintTypeResponse ();
  1721. t2.p1.Init ();
  1722. t2.p2 = new PrintTypeResponse ();
  1723. t2.p2.Init ();
  1724. Serialize (t1);
  1725. string res = "";
  1726. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1727. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1728. res += " <p1>";
  1729. res += " <result>";
  1730. res += " <data>data1</data>";
  1731. res += " </result>";
  1732. res += " <intern xmlns=\"urn:responseTypes\">";
  1733. res += " <result xmlns=\"\">";
  1734. res += " <data>data2</data>";
  1735. res += " </result>";
  1736. res += " </intern>";
  1737. res += " </p1>";
  1738. res += " <p2 xmlns=\"urn:oo\">";
  1739. res += " <result xmlns=\"\">";
  1740. res += " <data>data1</data>";
  1741. res += " </result>";
  1742. res += " <intern xmlns=\"urn:responseTypes\">";
  1743. res += " <result xmlns=\"\">";
  1744. res += " <data>data2</data>";
  1745. res += " </result>";
  1746. res += " </intern>";
  1747. res += " </p2>";
  1748. res += "</TestSchemaForm1>";
  1749. Assert.AreEqual (Infoset (res), WriterText);
  1750. Serialize (t2);
  1751. res = "";
  1752. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1753. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
  1754. res += " <p1 xmlns=\"urn:testForm\">";
  1755. res += " <result xmlns=\"\">";
  1756. res += " <data>data1</data>";
  1757. res += " </result>";
  1758. res += " <intern xmlns=\"urn:responseTypes\">";
  1759. res += " <result xmlns=\"\">";
  1760. res += " <data>data2</data>";
  1761. res += " </result>";
  1762. res += " </intern>";
  1763. res += " </p1>";
  1764. res += " <p2 xmlns=\"urn:oo\">";
  1765. res += " <result xmlns=\"\">";
  1766. res += " <data>data1</data>";
  1767. res += " </result>";
  1768. res += " <intern xmlns=\"urn:responseTypes\">";
  1769. res += " <result xmlns=\"\">";
  1770. res += " <data>data2</data>";
  1771. res += " </result>";
  1772. res += " </intern>";
  1773. res += " </p2>";
  1774. res += "</TestSchemaForm2>";
  1775. Assert.AreEqual (Infoset (res), WriterText);
  1776. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1777. XmlTypeMapping map = imp.ImportTypeMapping (typeof (TestSchemaForm1), "urn:extra");
  1778. Serialize (t1, map);
  1779. res = "";
  1780. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1781. res += "<TestSchemaForm1 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1782. res += " <p1>";
  1783. res += " <result xmlns=\"\">";
  1784. res += " <data>data1</data>";
  1785. res += " </result>";
  1786. res += " <intern xmlns=\"urn:responseTypes\">";
  1787. res += " <result xmlns=\"\">";
  1788. res += " <data>data2</data>";
  1789. res += " </result>";
  1790. res += " </intern>";
  1791. res += " </p1>";
  1792. res += " <p2 xmlns=\"urn:oo\">";
  1793. res += " <result xmlns=\"\">";
  1794. res += " <data>data1</data>";
  1795. res += " </result>";
  1796. res += " <intern xmlns=\"urn:responseTypes\">";
  1797. res += " <result xmlns=\"\">";
  1798. res += " <data>data2</data>";
  1799. res += " </result>";
  1800. res += " </intern>";
  1801. res += " </p2>";
  1802. res += "</TestSchemaForm1>";
  1803. Assert.AreEqual (Infoset (res), WriterText);
  1804. imp = new XmlReflectionImporter ();
  1805. map = imp.ImportTypeMapping (typeof (TestSchemaForm2), "urn:extra");
  1806. Serialize (t2, map);
  1807. res = "";
  1808. res += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  1809. res += "<TestSchemaForm2 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:extra\">";
  1810. res += " <p1 xmlns=\"urn:testForm\">";
  1811. res += " <result xmlns=\"\">";
  1812. res += " <data>data1</data>";
  1813. res += " </result>";
  1814. res += " <intern xmlns=\"urn:responseTypes\">";
  1815. res += " <result xmlns=\"\">";
  1816. res += " <data>data2</data>";
  1817. res += " </result>";
  1818. res += " </intern>";
  1819. res += " </p1>";
  1820. res += " <p2 xmlns=\"urn:oo\">";
  1821. res += " <result xmlns=\"\">";
  1822. res += " <data>data1</data>";
  1823. res += " </result>";
  1824. res += " <intern xmlns=\"urn:responseTypes\">";
  1825. res += " <result xmlns=\"\">";
  1826. res += " <data>data2</data>";
  1827. res += " </result>";
  1828. res += " </intern>";
  1829. res += " </p2>";
  1830. res += "</TestSchemaForm2>";
  1831. Assert.AreEqual (Infoset (res), WriterText);
  1832. }
  1833. [Test] // bug #78536
  1834. public void CDataTextNodes ()
  1835. {
  1836. XmlSerializer ser = new XmlSerializer (typeof (CDataTextNodesType));
  1837. ser.UnknownNode += new XmlNodeEventHandler (CDataTextNodes_BadNode);
  1838. string xml = @"<CDataTextNodesType>
  1839. <foo><![CDATA[
  1840. (?<filename>^([A-Z]:)?[^\(]+)\((?<line>\d+),(?<column>\d+)\):
  1841. \s((?<warning>warning)|(?<error>error))\s[^:]+:(?<message>.+$)|
  1842. (?<error>(fatal\s)?error)[^:]+:(?<message>.+$)
  1843. ]]></foo>
  1844. </CDataTextNodesType>";
  1845. ser.Deserialize (new XmlTextReader (xml, XmlNodeType.Document, null));
  1846. }
  1847. #if !MOBILE
  1848. [Test]
  1849. public void GenerateSerializerGenerics ()
  1850. {
  1851. XmlReflectionImporter imp = new XmlReflectionImporter ();
  1852. Type type = typeof (List<int>);
  1853. XmlSerializer.GenerateSerializer (
  1854. new Type [] {type},
  1855. new XmlTypeMapping [] {imp.ImportTypeMapping (type)});
  1856. }
  1857. #endif
  1858. [Test]
  1859. public void Nullable ()
  1860. {
  1861. XmlSerializer ser = new XmlSerializer (typeof (int?));
  1862. int? nullableType = 5;
  1863. sw = new StringWriter ();
  1864. xtw = new XmlTextWriter (sw);
  1865. ser.Serialize (xtw, nullableType);
  1866. xtw.Close ();
  1867. string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><int>5</int>";
  1868. Assert.AreEqual (Infoset (expected), WriterText);
  1869. int? i = (int?) ser.Deserialize (new StringReader (sw.ToString ()));
  1870. Assert.AreEqual (5, i);
  1871. }
  1872. [Test]
  1873. public void NullableEnums ()
  1874. {
  1875. WithNulls w = new WithNulls ();
  1876. XmlSerializer ser = new XmlSerializer (typeof(WithNulls));
  1877. StringWriter tw = new StringWriter ();
  1878. ser.Serialize (tw, w);
  1879. string expected = "<?xml version='1.0' encoding='utf-16'?>" +
  1880. "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  1881. "<nint xsi:nil='true' />" +
  1882. "<nenum xsi:nil='true' />" +
  1883. "<ndate xsi:nil='true' />" +
  1884. "</WithNulls>";
  1885. Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
  1886. StringReader sr = new StringReader (tw.ToString ());
  1887. w = (WithNulls) ser.Deserialize (sr);
  1888. Assert.IsFalse (w.nint.HasValue);
  1889. Assert.IsFalse (w.nenum.HasValue);
  1890. Assert.IsFalse (w.ndate.HasValue);
  1891. DateTime t = new DateTime (2008,4,1);
  1892. w.nint = 4;
  1893. w.ndate = t;
  1894. w.nenum = TestEnumWithNulls.bb;
  1895. tw = new StringWriter ();
  1896. ser.Serialize (tw, w);
  1897. expected = "<?xml version='1.0' encoding='utf-16'?>" +
  1898. "<WithNulls xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  1899. "<nint>4</nint>" +
  1900. "<nenum>bb</nenum>" +
  1901. "<ndate>2008-04-01T00:00:00</ndate>" +
  1902. "</WithNulls>";
  1903. Assert.AreEqual (Infoset (expected), Infoset (tw.ToString ()));
  1904. sr = new StringReader (tw.ToString ());
  1905. w = (WithNulls) ser.Deserialize (sr);
  1906. Assert.IsTrue (w.nint.HasValue);
  1907. Assert.IsTrue (w.nenum.HasValue);
  1908. Assert.IsTrue (w.ndate.HasValue);
  1909. Assert.AreEqual (4, w.nint.Value);
  1910. Assert.AreEqual (TestEnumWithNulls.bb, w.nenum.Value);
  1911. Assert.AreEqual (t, w.ndate.Value);
  1912. }
  1913. [Test]
  1914. public void SerializeBase64Binary()
  1915. {
  1916. XmlSerializer ser = new XmlSerializer (typeof (Base64Binary));
  1917. sw = new StringWriter ();
  1918. XmlTextWriter xtw = new XmlTextWriter (sw);
  1919. ser.Serialize (xtw, new Base64Binary ());
  1920. xtw.Close ();
  1921. 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"" />";
  1922. Assert.AreEqual (Infoset (expected), WriterText);
  1923. Base64Binary h = (Base64Binary) ser.Deserialize (new StringReader (sw.ToString ()));
  1924. Assert.AreEqual (new byte [] {1, 2, 3}, h.Data);
  1925. }
  1926. [Test] // bug #79989, #79990
  1927. public void SerializeHexBinary ()
  1928. {
  1929. XmlSerializer ser = new XmlSerializer (typeof (HexBinary));
  1930. sw = new StringWriter ();
  1931. XmlTextWriter xtw = new XmlTextWriter (sw);
  1932. ser.Serialize (xtw, new HexBinary ());
  1933. xtw.Close ();
  1934. 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"" />";
  1935. Assert.AreEqual (Infoset (expected), WriterText);
  1936. HexBinary h = (HexBinary) ser.Deserialize (new StringReader (sw.ToString ()));
  1937. Assert.AreEqual (new byte[] { 1, 2, 3 }, h.Data);
  1938. }
  1939. [Test]
  1940. [ExpectedException (typeof (InvalidOperationException))]
  1941. public void XmlArrayAttributeOnInt ()
  1942. {
  1943. new XmlSerializer (typeof (XmlArrayOnInt));
  1944. }
  1945. [Test]
  1946. public void XmlArrayAttributeUnqualifiedWithNamespace ()
  1947. {
  1948. new XmlSerializer (typeof (XmlArrayUnqualifiedWithNamespace));
  1949. }
  1950. [Test]
  1951. public void XmlArrayItemAttributeUnqualifiedWithNamespace ()
  1952. {
  1953. new XmlSerializer (typeof (XmlArrayItemUnqualifiedWithNamespace));
  1954. }
  1955. [Test] // bug #78042
  1956. public void XmlArrayAttributeOnArray ()
  1957. {
  1958. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArray));
  1959. sw = new StringWriter ();
  1960. XmlTextWriter xtw = new XmlTextWriter (sw);
  1961. ser.Serialize (xtw, new XmlArrayOnArray ());
  1962. xtw.Close ();
  1963. 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>";
  1964. Assert.AreEqual (Infoset (expected), WriterText);
  1965. }
  1966. [Test]
  1967. public void XmlArrayAttributeOnCollection ()
  1968. {
  1969. XmlSerializer ser = new XmlSerializer (typeof (XmlArrayOnArrayList));
  1970. XmlArrayOnArrayList inst = new XmlArrayOnArrayList ();
  1971. inst.Sane.Add ("abc");
  1972. inst.Sane.Add (1);
  1973. sw = new StringWriter ();
  1974. XmlTextWriter xtw = new XmlTextWriter (sw);
  1975. ser.Serialize (xtw, inst);
  1976. xtw.Close ();
  1977. 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>";
  1978. Assert.AreEqual (Infoset (expected), WriterText);
  1979. }
  1980. [Test] // bug #338705
  1981. public void SerializeTimeSpan ()
  1982. {
  1983. // TimeSpan itself is not for duration. Hence it is just regarded as one of custom types.
  1984. XmlSerializer ser = new XmlSerializer (typeof (TimeSpan));
  1985. ser.Serialize (TextWriter.Null, TimeSpan.Zero);
  1986. }
  1987. [Test]
  1988. public void SerializeDurationToString ()
  1989. {
  1990. XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
  1991. ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
  1992. }
  1993. [Test]
  1994. [ExpectedException (typeof (InvalidOperationException))]
  1995. public void SerializeDurationToTimeSpan ()
  1996. {
  1997. XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
  1998. ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
  1999. }
  2000. [Test]
  2001. [ExpectedException (typeof (InvalidOperationException))]
  2002. public void SerializeInvalidDataType ()
  2003. {
  2004. XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
  2005. ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
  2006. }
  2007. [Test]
  2008. public void SerializeErrorneousIXmlSerializable ()
  2009. {
  2010. Serialize (new ErrorneousGetSchema ());
  2011. Assert.AreEqual ("<:ErrorneousGetSchema></>", Infoset (sw.ToString ()));
  2012. }
  2013. public void DateTimeRoundtrip ()
  2014. {
  2015. // bug #337729
  2016. XmlSerializer ser = new XmlSerializer (typeof (DateTime));
  2017. StringWriter sw = new StringWriter ();
  2018. ser.Serialize (sw, DateTime.UtcNow);
  2019. DateTime d = (DateTime) ser.Deserialize (new StringReader (sw.ToString ()));
  2020. Assert.AreEqual (DateTimeKind.Utc, d.Kind);
  2021. }
  2022. [Test]
  2023. public void SupportIXmlSerializableImplicitlyConvertible ()
  2024. {
  2025. XmlAttributes attrs = new XmlAttributes ();
  2026. XmlElementAttribute attr = new XmlElementAttribute ();
  2027. attr.ElementName = "XmlSerializable";
  2028. attr.Type = typeof (XmlSerializableImplicitConvertible.XmlSerializable);
  2029. attrs.XmlElements.Add (attr);
  2030. XmlAttributeOverrides attrOverrides = new
  2031. XmlAttributeOverrides ();
  2032. attrOverrides.Add (typeof (XmlSerializableImplicitConvertible), "B", attrs);
  2033. XmlSerializableImplicitConvertible x = new XmlSerializableImplicitConvertible ();
  2034. new XmlSerializer (typeof (XmlSerializableImplicitConvertible), attrOverrides).Serialize (TextWriter.Null, x);
  2035. }
  2036. [Test] // bug #566370
  2037. public void SerializeEnumWithCSharpKeyword ()
  2038. {
  2039. var ser = new XmlSerializer (typeof (DoxCompoundKind));
  2040. for (int i = 0; i < 100; i++) // test serialization code generator
  2041. ser.Serialize (Console.Out, DoxCompoundKind.@class);
  2042. }
  2043. public enum DoxCompoundKind
  2044. {
  2045. [XmlEnum("class")]
  2046. @class,
  2047. [XmlEnum("struct")]
  2048. @struct,
  2049. union,
  2050. [XmlEnum("interface")]
  2051. @interface,
  2052. protocol,
  2053. category,
  2054. exception,
  2055. file,
  2056. [XmlEnum("namespace")]
  2057. @namespace,
  2058. group,
  2059. page,
  2060. example,
  2061. dir
  2062. }
  2063. #region GenericsSeralizationTests
  2064. [Test]
  2065. public void TestSerializeGenSimpleClassString ()
  2066. {
  2067. GenSimpleClass<string> simple = new GenSimpleClass<string> ();
  2068. Serialize (simple);
  2069. Assert.AreEqual (Infoset ("<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  2070. simple.something = "hello";
  2071. Serialize (simple);
  2072. 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);
  2073. }
  2074. [Test]
  2075. public void TestSerializeGenSimpleClassBool ()
  2076. {
  2077. GenSimpleClass<bool> simple = new GenSimpleClass<bool> ();
  2078. Serialize (simple);
  2079. 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);
  2080. simple.something = true;
  2081. Serialize (simple);
  2082. 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);
  2083. }
  2084. [Test]
  2085. public void TestSerializeGenSimpleStructInt ()
  2086. {
  2087. GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
  2088. Serialize (simple);
  2089. 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);
  2090. simple.something = 123;
  2091. Serialize (simple);
  2092. 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);
  2093. }
  2094. [Test]
  2095. public void TestSerializeGenListClassString ()
  2096. {
  2097. GenListClass<string> genlist = new GenListClass<string> ();
  2098. Serialize (genlist);
  2099. 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);
  2100. genlist.somelist.Add ("Value1");
  2101. genlist.somelist.Add ("Value2");
  2102. Serialize (genlist);
  2103. 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);
  2104. }
  2105. [Test]
  2106. public void TestSerializeGenListClassFloat ()
  2107. {
  2108. GenListClass<float> genlist = new GenListClass<float> ();
  2109. Serialize (genlist);
  2110. 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);
  2111. genlist.somelist.Add (1);
  2112. genlist.somelist.Add (2.2F);
  2113. Serialize (genlist);
  2114. 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);
  2115. }
  2116. [Test]
  2117. public void TestSerializeGenListClassList ()
  2118. {
  2119. GenListClass<GenListClass<int>> genlist = new GenListClass<GenListClass<int>> ();
  2120. Serialize (genlist);
  2121. 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);
  2122. GenListClass<int> inlist1 = new GenListClass<int> ();
  2123. inlist1.somelist.Add (1);
  2124. inlist1.somelist.Add (2);
  2125. GenListClass<int> inlist2 = new GenListClass<int> ();
  2126. inlist2.somelist.Add (10);
  2127. inlist2.somelist.Add (20);
  2128. genlist.somelist.Add (inlist1);
  2129. genlist.somelist.Add (inlist2);
  2130. Serialize (genlist);
  2131. 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);
  2132. }
  2133. [Test]
  2134. public void TestSerializeGenListClassArray ()
  2135. {
  2136. GenListClass<GenArrayClass<char>> genlist = new GenListClass<GenArrayClass<char>> ();
  2137. Serialize (genlist);
  2138. 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);
  2139. GenArrayClass<char> genarr1 = new GenArrayClass<char> ();
  2140. genarr1.arr[0] = 'a';
  2141. genarr1.arr[1] = 'b';
  2142. genlist.somelist.Add (genarr1);
  2143. GenArrayClass<char> genarr2 = new GenArrayClass<char> ();
  2144. genarr2.arr[0] = 'd';
  2145. genarr2.arr[1] = 'e';
  2146. genarr2.arr[2] = 'f';
  2147. genlist.somelist.Add (genarr2);
  2148. Serialize (genlist);
  2149. 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);
  2150. }
  2151. [Test]
  2152. public void TestSerializeGenTwoClassCharDouble ()
  2153. {
  2154. GenTwoClass<char, double> gentwo = new GenTwoClass<char, double> ();
  2155. Serialize (gentwo);
  2156. 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);
  2157. gentwo.something1 = 'a';
  2158. gentwo.something2 = 2.2;
  2159. Serialize (gentwo);
  2160. 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);
  2161. }
  2162. [Test]
  2163. public void TestSerializeGenDerivedClassDecimalShort ()
  2164. {
  2165. GenDerivedClass<decimal, short> derived = new GenDerivedClass<decimal, short> ();
  2166. Serialize (derived);
  2167. 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);
  2168. derived.something1 = "Value1";
  2169. derived.something2 = 1;
  2170. derived.another1 = 1.1M;
  2171. derived.another2 = -22;
  2172. Serialize (derived);
  2173. 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);
  2174. }
  2175. [Test]
  2176. public void TestSerializeGenDerivedSecondClassByteUlong ()
  2177. {
  2178. GenDerived2Class<byte, ulong> derived2 = new GenDerived2Class<byte, ulong> ();
  2179. Serialize (derived2);
  2180. 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);
  2181. derived2.something1 = 1;
  2182. derived2.something2 = 222;
  2183. derived2.another1 = 111;
  2184. derived2.another2 = 222222;
  2185. Serialize (derived2);
  2186. 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);
  2187. }
  2188. [Test]
  2189. public void TestSerializeGenNestedClass ()
  2190. {
  2191. GenNestedClass<string, int>.InnerClass<bool> nested =
  2192. new GenNestedClass<string, int>.InnerClass<bool> ();
  2193. Serialize (nested);
  2194. 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);
  2195. nested.inner = 5;
  2196. nested.something = true;
  2197. Serialize (nested);
  2198. 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);
  2199. }
  2200. [Test]
  2201. public void TestSerializeGenListClassListNested ()
  2202. {
  2203. GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
  2204. new GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> ();
  2205. Serialize (genlist);
  2206. 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);
  2207. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist1 =
  2208. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2209. GenNestedClass<int, int>.InnerClass<string> inval1 = new GenNestedClass<int, int>.InnerClass<string> ();
  2210. inval1.inner = 1;
  2211. inval1.something = "ONE";
  2212. inlist1.somelist.Add (inval1);
  2213. GenNestedClass<int, int>.InnerClass<string> inval2 = new GenNestedClass<int, int>.InnerClass<string> ();
  2214. inval2.inner = 2;
  2215. inval2.something = "TWO";
  2216. inlist1.somelist.Add (inval2);
  2217. GenListClass<GenNestedClass<int, int>.InnerClass<string>> inlist2 =
  2218. new GenListClass<GenNestedClass<int, int>.InnerClass<string>> ();
  2219. GenNestedClass<int, int>.InnerClass<string> inval3 = new GenNestedClass<int, int>.InnerClass<string> ();
  2220. inval3.inner = 30;
  2221. inval3.something = "THIRTY";
  2222. inlist2.somelist.Add (inval3);
  2223. genlist.somelist.Add (inlist1);
  2224. genlist.somelist.Add (inlist2);
  2225. Serialize (genlist);
  2226. 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);
  2227. }
  2228. public enum Myenum { one, two, three, four, five, six };
  2229. [Test]
  2230. public void TestSerializeGenArrayClassEnum ()
  2231. {
  2232. GenArrayClass<Myenum> genarr = new GenArrayClass<Myenum> ();
  2233. Serialize (genarr);
  2234. 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);
  2235. genarr.arr[0] = Myenum.one;
  2236. genarr.arr[1] = Myenum.three;
  2237. genarr.arr[2] = Myenum.five;
  2238. Serialize (genarr);
  2239. 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);
  2240. }
  2241. [Test]
  2242. public void TestSerializeGenArrayStruct ()
  2243. {
  2244. GenArrayClass<GenSimpleStruct<uint>> genarr = new GenArrayClass<GenSimpleStruct<uint>> ();
  2245. Serialize (genarr);
  2246. 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);
  2247. GenSimpleStruct<uint> genstruct = new GenSimpleStruct<uint> ();
  2248. genstruct.something = 111;
  2249. genarr.arr[0] = genstruct;
  2250. genstruct.something = 222;
  2251. genarr.arr[1] = genstruct;
  2252. genstruct.something = 333;
  2253. genarr.arr[2] = genstruct;
  2254. Serialize (genarr);
  2255. 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);
  2256. }
  2257. [Test]
  2258. public void TestSerializeGenArrayList ()
  2259. {
  2260. GenArrayClass<GenListClass<string>> genarr = new GenArrayClass<GenListClass<string>> ();
  2261. Serialize (genarr);
  2262. 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);
  2263. GenListClass<string> genlist1 = new GenListClass<string> ();
  2264. genlist1.somelist.Add ("list1-val1");
  2265. genlist1.somelist.Add ("list1-val2");
  2266. genarr.arr[0] = genlist1;
  2267. GenListClass<string> genlist2 = new GenListClass<string> ();
  2268. genlist2.somelist.Add ("list2-val1");
  2269. genlist2.somelist.Add ("list2-val2");
  2270. genlist2.somelist.Add ("list2-val3");
  2271. genlist2.somelist.Add ("list2-val4");
  2272. genarr.arr[1] = genlist2;
  2273. GenListClass<string> genlist3 = new GenListClass<string> ();
  2274. genlist3.somelist.Add ("list3val");
  2275. genarr.arr[2] = genlist3;
  2276. Serialize (genarr);
  2277. 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);
  2278. }
  2279. [Test]
  2280. public void TestSerializeGenComplexStruct ()
  2281. {
  2282. GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
  2283. Serialize (complex);
  2284. 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);
  2285. complex.something = 123;
  2286. complex.simpleclass.something = 456;
  2287. complex.simplestruct.something = 789;
  2288. GenListClass<int> genlist = new GenListClass<int> ();
  2289. genlist.somelist.Add (100);
  2290. genlist.somelist.Add (200);
  2291. complex.listclass = genlist;
  2292. GenArrayClass<int> genarr = new GenArrayClass<int> ();
  2293. genarr.arr[0] = 11;
  2294. genarr.arr[1] = 22;
  2295. genarr.arr[2] = 33;
  2296. complex.arrayclass = genarr;
  2297. complex.twoclass.something1 = 10;
  2298. complex.twoclass.something2 = "Ten";
  2299. complex.derivedclass.another1 = 1;
  2300. complex.derivedclass.another2 = "one";
  2301. complex.derivedclass.something1 = "two";
  2302. complex.derivedclass.something2 = 2;
  2303. complex.derived2.another1 = 3;
  2304. complex.derived2.another2 = "three";
  2305. complex.derived2.something1 = 4;
  2306. complex.derived2.something2 = "four";
  2307. complex.nestedouter.outer = 5;
  2308. complex.nestedinner.inner = "six";
  2309. complex.nestedinner.something = 6;
  2310. Serialize (complex);
  2311. 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);
  2312. }
  2313. [Test] // bug #80759
  2314. public void HasNullableField ()
  2315. {
  2316. Bug80759 foo = new Bug80759 ();
  2317. foo.Test = "BAR";
  2318. foo.NullableInt = 10;
  2319. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
  2320. MemoryStream stream = new MemoryStream ();
  2321. serializer.Serialize (stream, foo);
  2322. stream.Position = 0;
  2323. foo = (Bug80759) serializer.Deserialize (stream);
  2324. }
  2325. [Test] // bug #80759, with fieldSpecified.
  2326. [ExpectedException (typeof (InvalidOperationException))]
  2327. [Category ("NotWorking")]
  2328. public void HasFieldSpecifiedButIrrelevant ()
  2329. {
  2330. Bug80759_2 foo = new Bug80759_2 ();
  2331. foo.Test = "BAR";
  2332. foo.NullableInt = 10;
  2333. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759_2));
  2334. MemoryStream stream = new MemoryStream ();
  2335. serializer.Serialize (stream, foo);
  2336. stream.Position = 0;
  2337. foo = (Bug80759_2) serializer.Deserialize (stream);
  2338. }
  2339. [Test]
  2340. public void HasNullableField2 ()
  2341. {
  2342. Bug80759 foo = new Bug80759 ();
  2343. foo.Test = "BAR";
  2344. foo.NullableInt = 10;
  2345. XmlSerializer serializer = new XmlSerializer (typeof (Bug80759));
  2346. MemoryStream stream = new MemoryStream ();
  2347. serializer.Serialize (stream, foo);
  2348. stream.Position = 0;
  2349. foo = (Bug80759) serializer.Deserialize (stream);
  2350. Assert.AreEqual ("BAR", foo.Test, "#1");
  2351. Assert.AreEqual (10, foo.NullableInt, "#2");
  2352. foo.NullableInt = null;
  2353. stream = new MemoryStream ();
  2354. serializer.Serialize (stream, foo);
  2355. stream.Position = 0;
  2356. foo = (Bug80759) serializer.Deserialize (stream);
  2357. Assert.AreEqual ("BAR", foo.Test, "#3");
  2358. Assert.IsNull (foo.NullableInt, "#4");
  2359. }
  2360. [Test]
  2361. public void SupportPrivateCtorOnly ()
  2362. {
  2363. XmlSerializer xs =
  2364. new XmlSerializer (typeof (PrivateCtorOnly));
  2365. StringWriter sw = new StringWriter ();
  2366. xs.Serialize (sw, PrivateCtorOnly.Instance);
  2367. xs.Deserialize (new StringReader (sw.ToString ()));
  2368. }
  2369. [Test]
  2370. public void XmlSchemaProviderQNameBecomesRootName ()
  2371. {
  2372. xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType));
  2373. Serialize (new XmlSchemaProviderQNameBecomesRootNameType ());
  2374. Assert.AreEqual (Infoset ("<foo />"), WriterText);
  2375. xs.Deserialize (new StringReader ("<foo/>"));
  2376. }
  2377. [Test]
  2378. public void XmlSchemaProviderQNameBecomesRootName2 ()
  2379. {
  2380. string xml = "<XmlSchemaProviderQNameBecomesRootNameType2 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo><foo /></Foo></XmlSchemaProviderQNameBecomesRootNameType2>";
  2381. xs = new XmlSerializer (typeof (XmlSchemaProviderQNameBecomesRootNameType2));
  2382. Serialize (new XmlSchemaProviderQNameBecomesRootNameType2 ());
  2383. Assert.AreEqual (Infoset (xml), WriterText);
  2384. xs.Deserialize (new StringReader (xml));
  2385. }
  2386. [Test]
  2387. public void XmlAnyElementForObjects () // bug #553032
  2388. {
  2389. new XmlSerializer (typeof (XmlAnyElementForObjectsType));
  2390. }
  2391. [Test]
  2392. [ExpectedException (typeof (InvalidOperationException))]
  2393. public void XmlAnyElementForObjects2 () // bug #553032-2
  2394. {
  2395. new XmlSerializer (typeof (XmlAnyElementForObjectsType)).Serialize (TextWriter.Null, new XmlAnyElementForObjectsType ());
  2396. }
  2397. public class Bug2893 {
  2398. public Bug2893 ()
  2399. {
  2400. Contents = new XmlDataDocument();
  2401. }
  2402. [XmlAnyElement("Contents")]
  2403. public XmlNode Contents;
  2404. }
  2405. // Bug Xamarin #2893
  2406. [Test]
  2407. public void XmlAnyElementForXmlNode ()
  2408. {
  2409. var obj = new Bug2893 ();
  2410. XmlSerializer mySerializer = new XmlSerializer(typeof(Bug2893));
  2411. XmlWriterSettings settings = new XmlWriterSettings();
  2412. var xsn = new XmlSerializerNamespaces();
  2413. xsn.Add(string.Empty, string.Empty);
  2414. byte[] buffer = new byte[2048];
  2415. var ms = new MemoryStream(buffer);
  2416. using (var xw = XmlWriter.Create(ms, settings))
  2417. {
  2418. mySerializer.Serialize(xw, obj, xsn);
  2419. xw.Flush();
  2420. }
  2421. mySerializer.Serialize(ms, obj);
  2422. }
  2423. [Test]
  2424. public void XmlRootOverridesSchemaProviderQName ()
  2425. {
  2426. var obj = new XmlRootOverridesSchemaProviderQNameType ();
  2427. XmlSerializer xs = new XmlSerializer (obj.GetType ());
  2428. var sw = new StringWriter ();
  2429. using (XmlWriter xw = XmlWriter.Create (sw))
  2430. xs.Serialize (xw, obj);
  2431. Assert.IsTrue (sw.ToString ().IndexOf ("foo") > 0, "#1");
  2432. }
  2433. public class AnotherArrayListType
  2434. {
  2435. [XmlAttribute]
  2436. public string one = "aaa";
  2437. [XmlAttribute]
  2438. public string another = "bbb";
  2439. }
  2440. public class DerivedArrayListType : AnotherArrayListType
  2441. {
  2442. }
  2443. public class ClassWithArrayList
  2444. {
  2445. [XmlElement (Type = typeof(int), ElementName = "int_elem")]
  2446. [XmlElement (Type = typeof(string), ElementName = "string_elem")]
  2447. [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
  2448. [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
  2449. public ArrayList list;
  2450. }
  2451. public class ClassWithArray
  2452. {
  2453. [XmlElement (Type = typeof(int), ElementName = "int_elem")]
  2454. [XmlElement (Type = typeof(string), ElementName = "string_elem")]
  2455. [XmlElement (Type = typeof(AnotherArrayListType), ElementName = "another_elem")]
  2456. [XmlElement (Type = typeof(DerivedArrayListType), ElementName = "derived_elem")]
  2457. public object[] list;
  2458. }
  2459. [Test]
  2460. public void MultipleXmlElementAttributesOnArrayList()
  2461. {
  2462. var test = new ClassWithArrayList();
  2463. test.list = new ArrayList();
  2464. test.list.Add(3);
  2465. test.list.Add("apepe");
  2466. test.list.Add(new AnotherArrayListType());
  2467. test.list.Add(new DerivedArrayListType());
  2468. Serialize(test);
  2469. 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'></></>";
  2470. Assert.AreEqual(WriterText, expected_text, WriterText);
  2471. }
  2472. [Test]
  2473. public void MultipleXmlElementAttributesOnArray()
  2474. {
  2475. var test = new ClassWithArray();
  2476. test.list = new object[] { 3, "apepe", new AnotherArrayListType(), new DerivedArrayListType() };
  2477. Serialize(test);
  2478. 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'></></>";
  2479. Assert.AreEqual(WriterText, expected_text, WriterText);
  2480. }
  2481. #endregion //GenericsSeralizationTests
  2482. #region XmlInclude on abstract class tests (Bug #18558)
  2483. [Test]
  2484. public void TestSerializeIntermediateType ()
  2485. {
  2486. string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlIntermediateType intermediate=\"false\"/></ContainerTypeForTest>";
  2487. var obj = new ContainerTypeForTest();
  2488. obj.MemberToUseInclude = new IntermediateTypeForTest ();
  2489. Serialize (obj);
  2490. Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
  2491. }
  2492. [Test]
  2493. public void TestSerializeSecondType ()
  2494. {
  2495. string expectedXml = "<ContainerTypeForTest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><XmlSecondType intermediate=\"false\"/></ContainerTypeForTest>";
  2496. var obj = new ContainerTypeForTest();
  2497. obj.MemberToUseInclude = new SecondDerivedTypeForTest ();
  2498. Serialize (obj);
  2499. Assert.AreEqual (Infoset (expectedXml), WriterText, "Serialized Output : " + WriterText);
  2500. }
  2501. #endregion
  2502. public class XmlArrayOnInt
  2503. {
  2504. [XmlArray]
  2505. public int Bogus;
  2506. }
  2507. public class XmlArrayUnqualifiedWithNamespace
  2508. {
  2509. [XmlArray (Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2510. public ArrayList Sane = new ArrayList ();
  2511. }
  2512. public class XmlArrayItemUnqualifiedWithNamespace
  2513. {
  2514. [XmlArrayItem ("foo", Namespace = "", Form = XmlSchemaForm.Unqualified)]
  2515. public ArrayList Sane = new ArrayList ();
  2516. }
  2517. [XmlRoot (Namespace = "urn:foo")]
  2518. public class XmlArrayOnArrayList
  2519. {
  2520. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2521. public ArrayList Sane = new ArrayList ();
  2522. }
  2523. [XmlRoot (Namespace = "urn:foo")]
  2524. public class XmlArrayOnArray
  2525. {
  2526. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2527. public string[] Sane = new string[] { "foo", "bar" };
  2528. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2529. public ArrayItemInXmlArray[] Mids =
  2530. new ArrayItemInXmlArray[] { new ArrayItemInXmlArray () };
  2531. }
  2532. [XmlType (Namespace = "urn:gyabo")]
  2533. public class ArrayItemInXmlArray
  2534. {
  2535. [XmlArray (Form = XmlSchemaForm.Unqualified)]
  2536. public string[] Whee = new string[] { "foo", "bar" };
  2537. }
  2538. [XmlRoot ("Base64Binary")]
  2539. public class Base64Binary
  2540. {
  2541. [XmlAttribute (DataType = "base64Binary")]
  2542. public byte [] Data = new byte [] {1, 2, 3};
  2543. }
  2544. [XmlRoot ("HexBinary")]
  2545. public class HexBinary
  2546. {
  2547. [XmlAttribute (DataType = "hexBinary")]
  2548. public byte[] Data = new byte[] { 1, 2, 3 };
  2549. }
  2550. [XmlRoot ("PrivateCtorOnly")]
  2551. public class PrivateCtorOnly
  2552. {
  2553. public static PrivateCtorOnly Instance = new PrivateCtorOnly ();
  2554. private PrivateCtorOnly ()
  2555. {
  2556. }
  2557. }
  2558. public class CDataTextNodesType
  2559. {
  2560. public CDataTextNodesInternal foo;
  2561. }
  2562. public class CDataTextNodesInternal
  2563. {
  2564. [XmlText]
  2565. public string Value;
  2566. }
  2567. public class InvalidTypeContainer
  2568. {
  2569. [XmlElement (DataType = "invalid")]
  2570. public string InvalidTypeItem = "aaa";
  2571. }
  2572. public class TimeSpanContainer1
  2573. {
  2574. [XmlElement (DataType = "duration")]
  2575. public string StringDuration = "aaa";
  2576. }
  2577. public class TimeSpanContainer2
  2578. {
  2579. [XmlElement (DataType = "duration")]
  2580. public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
  2581. }
  2582. public class Bug80759
  2583. {
  2584. public string Test;
  2585. public int? NullableInt;
  2586. }
  2587. public class Bug80759_2
  2588. {
  2589. public string Test;
  2590. public int? NullableInt;
  2591. [XmlIgnore]
  2592. public bool NullableIntSpecified {
  2593. get { return NullableInt.HasValue; }
  2594. }
  2595. }
  2596. [XmlSchemaProvider ("GetXsdType")]
  2597. public class XmlSchemaProviderQNameBecomesRootNameType : IXmlSerializable
  2598. {
  2599. public XmlSchema GetSchema ()
  2600. {
  2601. return null;
  2602. }
  2603. public void ReadXml (XmlReader reader)
  2604. {
  2605. reader.Skip ();
  2606. }
  2607. public void WriteXml (XmlWriter writer)
  2608. {
  2609. }
  2610. public static XmlQualifiedName GetXsdType (XmlSchemaSet xss)
  2611. {
  2612. if (xss.Count == 0) {
  2613. XmlSchema xs = new XmlSchema ();
  2614. XmlSchemaComplexType ct = new XmlSchemaComplexType ();
  2615. ct.Name = "foo";
  2616. xs.Items.Add (ct);
  2617. xss.Add (xs);
  2618. }
  2619. return new XmlQualifiedName ("foo");
  2620. }
  2621. }
  2622. public class XmlSchemaProviderQNameBecomesRootNameType2
  2623. {
  2624. [XmlArrayItem (typeof (XmlSchemaProviderQNameBecomesRootNameType))]
  2625. public object [] Foo = new object [] {new XmlSchemaProviderQNameBecomesRootNameType ()};
  2626. }
  2627. public class XmlAnyElementForObjectsType
  2628. {
  2629. [XmlAnyElement]
  2630. public object [] arr = new object [] {3,4,5};
  2631. }
  2632. [XmlRoot ("foo")]
  2633. [XmlSchemaProvider ("GetSchema")]
  2634. public class XmlRootOverridesSchemaProviderQNameType : IXmlSerializable
  2635. {
  2636. public static XmlQualifiedName GetSchema (XmlSchemaSet xss)
  2637. {
  2638. var xs = new XmlSchema ();
  2639. var xse = new XmlSchemaComplexType () { Name = "bar" };
  2640. xs.Items.Add (xse);
  2641. xss.Add (xs);
  2642. return new XmlQualifiedName ("bar");
  2643. }
  2644. XmlSchema IXmlSerializable.GetSchema ()
  2645. {
  2646. return null;
  2647. }
  2648. void IXmlSerializable.ReadXml (XmlReader reader)
  2649. {
  2650. }
  2651. void IXmlSerializable.WriteXml (XmlWriter writer)
  2652. {
  2653. }
  2654. }
  2655. void CDataTextNodes_BadNode (object s, XmlNodeEventArgs e)
  2656. {
  2657. Assert.Fail ();
  2658. }
  2659. // Helper methods
  2660. public static string Infoset (string sx)
  2661. {
  2662. XmlDocument doc = new XmlDocument ();
  2663. doc.LoadXml (sx);
  2664. StringBuilder sb = new StringBuilder ();
  2665. GetInfoset (doc.DocumentElement, sb);
  2666. return sb.ToString ();
  2667. }
  2668. public static string Infoset (XmlNode nod)
  2669. {
  2670. StringBuilder sb = new StringBuilder ();
  2671. GetInfoset (nod, sb);
  2672. return sb.ToString ();
  2673. }
  2674. static void GetInfoset (XmlNode nod, StringBuilder sb)
  2675. {
  2676. switch (nod.NodeType) {
  2677. case XmlNodeType.Attribute:
  2678. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  2679. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  2680. break;
  2681. case XmlNodeType.Element:
  2682. XmlElement elem = (XmlElement) nod;
  2683. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  2684. ArrayList ats = new ArrayList ();
  2685. foreach (XmlAttribute at in elem.Attributes)
  2686. ats.Add (at.LocalName + " " + at.NamespaceURI);
  2687. ats.Sort ();
  2688. foreach (string name in ats) {
  2689. string[] nn = name.Split (' ');
  2690. GetInfoset (elem.Attributes[nn[0], nn[1]], sb);
  2691. }
  2692. sb.Append (">");
  2693. foreach (XmlNode cn in elem.ChildNodes)
  2694. GetInfoset (cn, sb);
  2695. sb.Append ("</>");
  2696. break;
  2697. default:
  2698. sb.Append (nod.OuterXml);
  2699. break;
  2700. }
  2701. }
  2702. static XmlTypeMapping CreateSoapMapping (Type type)
  2703. {
  2704. SoapReflectionImporter importer = new SoapReflectionImporter ();
  2705. return importer.ImportTypeMapping (type);
  2706. }
  2707. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao)
  2708. {
  2709. SoapReflectionImporter importer = new SoapReflectionImporter (ao);
  2710. return importer.ImportTypeMapping (type);
  2711. }
  2712. static XmlTypeMapping CreateSoapMapping (Type type, SoapAttributeOverrides ao, string defaultNamespace)
  2713. {
  2714. SoapReflectionImporter importer = new SoapReflectionImporter (ao, defaultNamespace);
  2715. return importer.ImportTypeMapping (type);
  2716. }
  2717. [XmlSchemaProvider (null, IsAny = true)]
  2718. public class AnySchemaProviderClass : IXmlSerializable {
  2719. public string Text;
  2720. void IXmlSerializable.WriteXml (XmlWriter writer)
  2721. {
  2722. writer.WriteElementString ("text", Text);
  2723. }
  2724. void IXmlSerializable.ReadXml (XmlReader reader)
  2725. {
  2726. Text = reader.ReadElementString ("text");
  2727. }
  2728. XmlSchema IXmlSerializable.GetSchema ()
  2729. {
  2730. return null;
  2731. }
  2732. }
  2733. [Test]
  2734. public void SerializeAnySchemaProvider ()
  2735. {
  2736. string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
  2737. Environment.NewLine + "<text>test</text>";
  2738. var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
  2739. var obj = new AnySchemaProviderClass {
  2740. Text = "test",
  2741. };
  2742. using (var t = new StringWriter ()) {
  2743. ser.Serialize (t, obj);
  2744. Assert.AreEqual (expected, t.ToString ());
  2745. }
  2746. }
  2747. [Test]
  2748. public void DeserializeAnySchemaProvider ()
  2749. {
  2750. string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
  2751. Environment.NewLine + "<text>test</text>";
  2752. var ser = new XmlSerializer (typeof (AnySchemaProviderClass));
  2753. using (var t = new StringReader (expected)) {
  2754. var obj = (AnySchemaProviderClass) ser.Deserialize (t);
  2755. Assert.AreEqual ("test", obj.Text);
  2756. }
  2757. }
  2758. public class SubNoParameterlessConstructor : NoParameterlessConstructor
  2759. {
  2760. public SubNoParameterlessConstructor ()
  2761. : base ("")
  2762. {
  2763. }
  2764. }
  2765. public class NoParameterlessConstructor
  2766. {
  2767. [XmlElement ("Text")]
  2768. public string Text;
  2769. public NoParameterlessConstructor (string parameter)
  2770. {
  2771. }
  2772. }
  2773. [Test]
  2774. public void BaseClassWithoutParameterlessConstructor ()
  2775. {
  2776. var ser = new XmlSerializer (typeof (SubNoParameterlessConstructor));
  2777. var obj = new SubNoParameterlessConstructor {
  2778. Text = "test",
  2779. };
  2780. using (var w = new StringWriter ()) {
  2781. ser.Serialize (w, obj);
  2782. using (var r = new StringReader ( w.ToString ())) {
  2783. var desObj = (SubNoParameterlessConstructor) ser.Deserialize (r);
  2784. Assert.AreEqual (obj.Text, desObj.Text);
  2785. }
  2786. }
  2787. }
  2788. public class ClassWithXmlAnyElement
  2789. {
  2790. [XmlAnyElement ("Contents")]
  2791. public XmlNode Contents;
  2792. }
  2793. [Test] // bug #3211
  2794. public void TestClassWithXmlAnyElement ()
  2795. {
  2796. var d = new XmlDocument ();
  2797. var e = d.CreateElement ("Contents");
  2798. e.AppendChild (d.CreateElement ("SomeElement"));
  2799. var c = new ClassWithXmlAnyElement {
  2800. Contents = e,
  2801. };
  2802. var ser = new XmlSerializer (typeof (ClassWithXmlAnyElement));
  2803. using (var sw = new StringWriter ())
  2804. ser.Serialize (sw, c);
  2805. }
  2806. [Test]
  2807. public void ClassWithImplicitlyConvertibleElement ()
  2808. {
  2809. var ser = new XmlSerializer (typeof (ObjectWithElementRequiringImplicitCast));
  2810. var obj = new ObjectWithElementRequiringImplicitCast ("test");
  2811. using (var w = new StringWriter ()) {
  2812. ser.Serialize (w, obj);
  2813. using (var r = new StringReader ( w.ToString ())) {
  2814. var desObj = (ObjectWithElementRequiringImplicitCast) ser.Deserialize (r);
  2815. Assert.AreEqual (obj.Object.Text, desObj.Object.Text);
  2816. }
  2817. }
  2818. }
  2819. public class ClassWithOptionalMethods
  2820. {
  2821. private readonly bool shouldSerializeX;
  2822. private readonly bool xSpecified;
  2823. [XmlAttribute]
  2824. public int X { get; set; }
  2825. public bool ShouldSerializeX () { return shouldSerializeX; }
  2826. public bool XSpecified
  2827. {
  2828. get { return xSpecified; }
  2829. }
  2830. public ClassWithOptionalMethods ()
  2831. {
  2832. }
  2833. public ClassWithOptionalMethods (int x, bool shouldSerializeX, bool xSpecified)
  2834. {
  2835. this.X = x;
  2836. this.shouldSerializeX = shouldSerializeX;
  2837. this.xSpecified = xSpecified;
  2838. }
  2839. }
  2840. [Test]
  2841. public void OptionalMethods ()
  2842. {
  2843. var ser = new XmlSerializer (typeof (ClassWithOptionalMethods));
  2844. var expectedValueWithoutX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
  2845. "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />");
  2846. var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
  2847. "<ClassWithOptionalMethods xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
  2848. using (var t = new StringWriter ()) {
  2849. var obj = new ClassWithOptionalMethods (11, false, false);
  2850. ser.Serialize (t, obj);
  2851. Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
  2852. }
  2853. using (var t = new StringWriter ()) {
  2854. var obj = new ClassWithOptionalMethods (11, true, false);
  2855. ser.Serialize (t, obj);
  2856. Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
  2857. }
  2858. using (var t = new StringWriter ()) {
  2859. var obj = new ClassWithOptionalMethods (11, false, true);
  2860. ser.Serialize (t, obj);
  2861. Assert.AreEqual (expectedValueWithoutX, Infoset (t.ToString ()));
  2862. }
  2863. using (var t = new StringWriter ()) {
  2864. var obj = new ClassWithOptionalMethods (11, true, true);
  2865. ser.Serialize (t, obj);
  2866. Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
  2867. }
  2868. }
  2869. public class ClassWithShouldSerializeGeneric
  2870. {
  2871. [XmlAttribute]
  2872. public int X { get; set; }
  2873. public bool ShouldSerializeX<T> () { return false; }
  2874. }
  2875. [Test]
  2876. [Category("NotWorking")]
  2877. public void ShouldSerializeGeneric ()
  2878. {
  2879. var ser = new XmlSerializer (typeof (ClassWithShouldSerializeGeneric));
  2880. var expectedValueWithX = Infoset ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + Environment.NewLine +
  2881. "<ClassWithShouldSerializeGeneric xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" X=\"11\" />");
  2882. using (var t = new StringWriter ()) {
  2883. var obj = new ClassWithShouldSerializeGeneric { X = 11 };
  2884. ser.Serialize (t, obj);
  2885. Assert.AreEqual (expectedValueWithX, Infoset (t.ToString ()));
  2886. }
  2887. }
  2888. [Test]
  2889. public void NullableArrayItems ()
  2890. {
  2891. var ser = new XmlSerializer (typeof (ObjectWithNullableArrayItems));
  2892. var obj = new ObjectWithNullableArrayItems ();
  2893. obj.Elements = new List <SimpleClass> ();
  2894. obj.Elements.Add (new SimpleClass { something = "Hello" });
  2895. obj.Elements.Add (null);
  2896. obj.Elements.Add (new SimpleClass { something = "World" });
  2897. using (var w = new StringWriter ()) {
  2898. ser.Serialize (w, obj);
  2899. using (var r = new StringReader ( w.ToString ())) {
  2900. var desObj = (ObjectWithNullableArrayItems) ser.Deserialize (r);
  2901. Assert.IsNull (desObj.Elements [1]);
  2902. }
  2903. }
  2904. }
  2905. [Test]
  2906. public void NonNullableArrayItems ()
  2907. {
  2908. var ser = new XmlSerializer (typeof (ObjectWithNonNullableArrayItems));
  2909. var obj = new ObjectWithNonNullableArrayItems ();
  2910. obj.Elements = new List <SimpleClass> ();
  2911. obj.Elements.Add (new SimpleClass { something = "Hello" });
  2912. obj.Elements.Add (null);
  2913. obj.Elements.Add (new SimpleClass { something = "World" });
  2914. using (var w = new StringWriter ()) {
  2915. ser.Serialize (w, obj);
  2916. using (var r = new StringReader ( w.ToString ())) {
  2917. var desObj = (ObjectWithNonNullableArrayItems) ser.Deserialize (r);
  2918. Assert.IsNotNull (desObj.Elements [1]);
  2919. }
  2920. }
  2921. }
  2922. [Test]
  2923. public void NotSpecifiedNullableArrayItems ()
  2924. {
  2925. var ser = new XmlSerializer (typeof (ObjectWithNotSpecifiedNullableArrayItems));
  2926. var obj = new ObjectWithNotSpecifiedNullableArrayItems ();
  2927. obj.Elements = new List <SimpleClass> ();
  2928. obj.Elements.Add (new SimpleClass { something = "Hello" });
  2929. obj.Elements.Add (null);
  2930. obj.Elements.Add (new SimpleClass { something = "World" });
  2931. using (var w = new StringWriter ()) {
  2932. ser.Serialize (w, obj);
  2933. using (var r = new StringReader ( w.ToString ())) {
  2934. var desObj = (ObjectWithNotSpecifiedNullableArrayItems) ser.Deserialize (r);
  2935. Assert.IsNull (desObj.Elements [1]);
  2936. }
  2937. }
  2938. }
  2939. private static void TestClassWithDefaultTextNotNullAux (string value, string expected)
  2940. {
  2941. var obj = new ClassWithDefaultTextNotNull (value);
  2942. var ser = new XmlSerializer (typeof (ClassWithDefaultTextNotNull));
  2943. using (var mstream = new MemoryStream ())
  2944. using (var writer = new XmlTextWriter (mstream, Encoding.ASCII)) {
  2945. ser.Serialize (writer, obj);
  2946. mstream.Seek (0, SeekOrigin.Begin);
  2947. using (var reader = new XmlTextReader (mstream)) {
  2948. var result = (ClassWithDefaultTextNotNull) ser.Deserialize (reader);
  2949. Assert.AreEqual (expected, result.Value);
  2950. }
  2951. }
  2952. }
  2953. [Test]
  2954. public void TestClassWithDefaultTextNotNull ()
  2955. {
  2956. TestClassWithDefaultTextNotNullAux ("my_text", "my_text");
  2957. TestClassWithDefaultTextNotNullAux ("", ClassWithDefaultTextNotNull.DefaultValue);
  2958. TestClassWithDefaultTextNotNullAux (null, ClassWithDefaultTextNotNull.DefaultValue);
  2959. }
  2960. }
  2961. // Test generated serialization code.
  2962. public class XmlSerializerGeneratorTests : XmlSerializerTests {
  2963. private FieldInfo backgroundGeneration;
  2964. private FieldInfo generationThreshold;
  2965. private FieldInfo generatorFallback;
  2966. private bool backgroundGenerationOld;
  2967. private int generationThresholdOld;
  2968. private bool generatorFallbackOld;
  2969. [SetUp]
  2970. public void SetUp ()
  2971. {
  2972. // Make sure XmlSerializer static constructor is called
  2973. XmlSerializer.FromTypes (new Type [] {});
  2974. const BindingFlags binding = BindingFlags.Static | BindingFlags.NonPublic;
  2975. backgroundGeneration = typeof (XmlSerializer).GetField ("backgroundGeneration", binding);
  2976. generationThreshold = typeof (XmlSerializer).GetField ("generationThreshold", binding);
  2977. generatorFallback = typeof (XmlSerializer).GetField ("generatorFallback", binding);
  2978. if (backgroundGeneration == null)
  2979. Assert.Ignore ("Unable to access field backgroundGeneration");
  2980. if (generationThreshold == null)
  2981. Assert.Ignore ("Unable to access field generationThreshold");
  2982. if (generatorFallback == null)
  2983. Assert.Ignore ("Unable to access field generatorFallback");
  2984. backgroundGenerationOld = (bool) backgroundGeneration.GetValue (null);
  2985. generationThresholdOld = (int) generationThreshold.GetValue (null);
  2986. generatorFallbackOld = (bool) generatorFallback.GetValue (null);
  2987. backgroundGeneration.SetValue (null, false);
  2988. generationThreshold.SetValue (null, 0);
  2989. generatorFallback.SetValue (null, false);
  2990. }
  2991. [TearDown]
  2992. public void TearDown ()
  2993. {
  2994. if (backgroundGeneration == null || generationThreshold == null || generatorFallback == null)
  2995. return;
  2996. backgroundGeneration.SetValue (null, backgroundGenerationOld);
  2997. generationThreshold.SetValue (null, generationThresholdOld);
  2998. generatorFallback.SetValue (null, generatorFallbackOld);
  2999. }
  3000. }
  3001. #region XmlInclude on abstract class test classes
  3002. [XmlType]
  3003. public class ContainerTypeForTest
  3004. {
  3005. [XmlElement ("XmlSecondType", typeof (SecondDerivedTypeForTest))]
  3006. [XmlElement ("XmlIntermediateType", typeof (IntermediateTypeForTest))]
  3007. [XmlElement ("XmlFirstType", typeof (FirstDerivedTypeForTest))]
  3008. public AbstractTypeForTest MemberToUseInclude { get; set; }
  3009. }
  3010. [XmlInclude (typeof (SecondDerivedTypeForTest))]
  3011. [XmlInclude (typeof (IntermediateTypeForTest))]
  3012. [XmlInclude (typeof (FirstDerivedTypeForTest))]
  3013. public abstract class AbstractTypeForTest
  3014. {
  3015. }
  3016. public class IntermediateTypeForTest : AbstractTypeForTest
  3017. {
  3018. [XmlAttribute (AttributeName = "intermediate")]
  3019. public bool IntermediateMember { get; set; }
  3020. }
  3021. public class FirstDerivedTypeForTest : AbstractTypeForTest
  3022. {
  3023. public string FirstMember { get; set; }
  3024. }
  3025. public class SecondDerivedTypeForTest : IntermediateTypeForTest
  3026. {
  3027. public string SecondMember { get; set; }
  3028. }
  3029. #endregion
  3030. }