XmlSerializerTests.cs 119 KB

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