XmlSerializerTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. //
  2. // System.Xml.XmlSerializerTests
  3. //
  4. // Author:
  5. // Erik LeBel <[email protected]>
  6. //
  7. // (C) 2003 Erik LeBel
  8. //
  9. //
  10. // NOTES:
  11. // Where possible, these tests avoid testing the order of
  12. // an object's members serialization. Mono and .NET do not
  13. // reflect members in the same order.
  14. //
  15. // Only serializations tests so far, no deserialization.
  16. //
  17. // FIXME
  18. // test XmlArrayAttribute
  19. // test XmlArrayItemAttribute
  20. // test serialization of decimal type
  21. // test serialization of Guid type
  22. // test XmlNode serialization with and without modifying attributes.
  23. // test deserialization
  24. // FIXMEs found in this file
  25. using System;
  26. using System.Collections;
  27. using System.Globalization;
  28. using System.IO;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Xml.Schema;
  32. using System.Xml.Serialization;
  33. using NUnit.Framework;
  34. using MonoTests.System.Xml.TestClasses;
  35. namespace MonoTests.System.XmlSerialization
  36. {
  37. [TestFixture]
  38. public class XmlSerializerTests : Assertion
  39. {
  40. StringWriter sw;
  41. XmlTextWriter xtw;
  42. XmlSerializer xs;
  43. private void SetUpWriter()
  44. {
  45. sw = new StringWriter ();
  46. xtw = new XmlTextWriter (sw);
  47. xtw.QuoteChar = '\'';
  48. xtw.Formatting = Formatting.None;
  49. }
  50. private string WriterText
  51. {
  52. get
  53. {
  54. string val = sw.GetStringBuilder().ToString();
  55. int offset = val.IndexOf('>') + 1;
  56. val = val.Substring(offset);
  57. return Infoset(val);
  58. }
  59. }
  60. private void Serialize(object o)
  61. {
  62. SetUpWriter();
  63. xs = new XmlSerializer(o.GetType());
  64. xs.Serialize(xtw, o);
  65. }
  66. private void Serialize(object o, Type type)
  67. {
  68. SetUpWriter();
  69. xs = new XmlSerializer(type);
  70. xs.Serialize(xtw, o);
  71. }
  72. private void Serialize(object o, XmlSerializerNamespaces ns)
  73. {
  74. SetUpWriter();
  75. xs = new XmlSerializer(o.GetType());
  76. xs.Serialize(xtw, o, ns);
  77. }
  78. private void Serialize(object o, XmlAttributeOverrides ao)
  79. {
  80. SetUpWriter();
  81. xs = new XmlSerializer(o.GetType(), ao);
  82. xs.Serialize(xtw, o);
  83. }
  84. private void Serialize(object o, XmlRootAttribute root)
  85. {
  86. SetUpWriter();
  87. xs = new XmlSerializer(o.GetType(), root);
  88. xs.Serialize(xtw, o);
  89. }
  90. // test constructors
  91. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  92. [Test]
  93. public void TestConstructor()
  94. {
  95. XmlSerializer ser = new XmlSerializer (null, "");
  96. }
  97. #else
  98. #endif
  99. // test basic types ////////////////////////////////////////////////////////
  100. [Test]
  101. public void TestSerializeInt()
  102. {
  103. Serialize(10);
  104. AssertEquals(Infoset("<int>10</int>"), WriterText);
  105. }
  106. [Test]
  107. public void TestSerializeBool()
  108. {
  109. Serialize(true);
  110. AssertEquals(Infoset("<boolean>true</boolean>"), WriterText);
  111. Serialize(false);
  112. AssertEquals(Infoset("<boolean>false</boolean>"), WriterText);
  113. }
  114. [Test]
  115. public void TestSerializeString()
  116. {
  117. Serialize("hello");
  118. AssertEquals(Infoset("<string>hello</string>"), WriterText);
  119. }
  120. [Test]
  121. public void TestSerializeEmptyString()
  122. {
  123. Serialize(String.Empty);
  124. AssertEquals(Infoset("<string />"), WriterText);
  125. }
  126. [Test]
  127. public void TestSerializeNullObject()
  128. {
  129. Serialize(null, typeof(object));
  130. AssertEquals(Infoset("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  131. }
  132. [Test]
  133. [Ignore ("The generated XML is not exact but it is equivalent")]
  134. public void TestSerializeNullString()
  135. {
  136. Serialize(null, typeof(string));
  137. Console.WriteLine (WriterText);
  138. AssertEquals (Infoset("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  139. }
  140. [Test]
  141. public void TestSerializeIntArray()
  142. {
  143. Serialize(new int[] {1, 2, 3, 4});
  144. AssertEquals (Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);
  145. }
  146. [Test]
  147. public void TestSerializeEmptyArray()
  148. {
  149. Serialize(new int[] {});
  150. AssertEquals(Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  151. }
  152. [Test]
  153. public void TestSerializeChar()
  154. {
  155. Serialize('A');
  156. AssertEquals(Infoset("<char>65</char>"), WriterText);
  157. Serialize('\0');
  158. AssertEquals(Infoset("<char>0</char>"), WriterText);
  159. Serialize('\n');
  160. AssertEquals(Infoset("<char>10</char>"), WriterText);
  161. Serialize('\uFF01');
  162. AssertEquals(Infoset("<char>65281</char>"), WriterText);
  163. }
  164. [Test]
  165. public void TestSerializeFloat()
  166. {
  167. Serialize(10.78);
  168. AssertEquals(Infoset("<double>10.78</double>"), WriterText);
  169. Serialize(-1e8);
  170. AssertEquals(Infoset("<double>-100000000</double>"), WriterText);
  171. // FIXME test INF and other boundary conditions that may exist with floats
  172. }
  173. [Test]
  174. public void TestSerializeEnumeration()
  175. {
  176. Serialize(SimpleEnumeration.FIRST);
  177. AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  178. Serialize(SimpleEnumeration.SECOND);
  179. AssertEquals(Infoset("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);
  180. }
  181. [Test]
  182. public void TestSerializeEnumDefaultValue() {
  183. Serialize(new EnumDefaultValue());
  184. AssertEquals(Infoset("<EnumDefaultValue />"), WriterText);
  185. Serialize(new EnumDefaultValueNF());
  186. AssertEquals(Infoset("<EnumDefaultValueNF>0</EnumDefaultValueNF>"), WriterText);
  187. Serialize(new SimpleEnumeration());
  188. AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  189. }
  190. [Test]
  191. public void TestSerializeQualifiedName()
  192. {
  193. Serialize(new XmlQualifiedName("me", "home.urn"));
  194. AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  195. }
  196. [Test]
  197. public void TestSerializeBytes()
  198. {
  199. Serialize((byte)0xAB);
  200. AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);
  201. Serialize((byte)15);
  202. AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);
  203. }
  204. [Test]
  205. public void TestSerializeByteArrays()
  206. {
  207. Serialize(new byte[] {});
  208. AssertEquals(Infoset("<base64Binary />"), WriterText);
  209. Serialize(new byte[] {0xAB, 0xCD});
  210. AssertEquals(Infoset("<base64Binary>q80=</base64Binary>"), WriterText);
  211. }
  212. [Test]
  213. public void TestSerializeDateTime()
  214. {
  215. DateTime d = new DateTime();
  216. Serialize(d);
  217. TimeZone tz = TimeZone.CurrentTimeZone;
  218. TimeSpan off = tz.GetUtcOffset (d);
  219. string sp = string.Format ("{0}{1:00}:{2:00}", off.Ticks >= 0 ? "+" : "", off.Hours, off.Minutes);
  220. AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  221. }
  222. /*
  223. FIXME
  224. - decimal
  225. - Guid
  226. - XmlNode objects
  227. [Test]
  228. public void TestSerialize()
  229. {
  230. Serialize();
  231. AssertEquals(WriterText, "");
  232. }
  233. */
  234. // test basic class serialization /////////////////////////////////////
  235. [Test]
  236. public void TestSerializeSimpleClass()
  237. {
  238. SimpleClass simple = new SimpleClass();
  239. Serialize(simple);
  240. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  241. simple.something = "hello";
  242. Serialize(simple);
  243. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  244. }
  245. [Test]
  246. public void TestSerializeStringCollection()
  247. {
  248. StringCollection strings = new StringCollection();
  249. Serialize(strings);
  250. AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  251. strings.Add("hello");
  252. strings.Add("goodbye");
  253. Serialize(strings);
  254. AssertEquals(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);
  255. }
  256. [Test]
  257. public void TestSerializePlainContainer()
  258. {
  259. StringCollectionContainer container = new StringCollectionContainer();
  260. Serialize(container);
  261. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  262. container.Messages.Add("hello");
  263. container.Messages.Add("goodbye");
  264. Serialize(container);
  265. AssertEquals(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);
  266. }
  267. [Test]
  268. public void TestSerializeArrayContainer()
  269. {
  270. ArrayContainer container = new ArrayContainer();
  271. Serialize(container);
  272. AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  273. container.items = new object[] {10, 20};
  274. Serialize(container);
  275. AssertEquals(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);
  276. container.items = new object[] {10, "hello"};
  277. Serialize(container);
  278. AssertEquals(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);
  279. }
  280. [Test]
  281. public void TestSerializeClassArrayContainer()
  282. {
  283. ClassArrayContainer container = new ClassArrayContainer();
  284. Serialize(container);
  285. AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  286. SimpleClass simple1 = new SimpleClass();
  287. simple1.something = "hello";
  288. SimpleClass simple2 = new SimpleClass();
  289. simple2.something = "hello";
  290. container.items = new SimpleClass[2];
  291. container.items[0] = simple1;
  292. container.items[1] = simple2;
  293. Serialize(container);
  294. AssertEquals(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);
  295. }
  296. // test basic attributes ///////////////////////////////////////////////
  297. [Test]
  298. public void TestSerializeSimpleClassWithXmlAttributes()
  299. {
  300. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  301. Serialize(simple);
  302. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  303. simple.something = "hello";
  304. Serialize(simple);
  305. AssertEquals (Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  306. }
  307. // test overrides ///////////////////////////////////////////////////////
  308. [Test]
  309. public void TestSerializeSimpleClassWithOverrides()
  310. {
  311. // Also tests XmlIgnore
  312. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  313. XmlAttributes attr = new XmlAttributes();
  314. attr.XmlIgnore = true;
  315. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  316. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  317. simple.something = "hello";
  318. Serialize(simple, overrides);
  319. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  320. }
  321. [Test]
  322. public void TestSerializeSchema ()
  323. {
  324. XmlSchema schema = new XmlSchema ();
  325. schema.Items.Add (new XmlSchemaAttribute ());
  326. schema.Items.Add (new XmlSchemaAttributeGroup ());
  327. schema.Items.Add (new XmlSchemaComplexType ());
  328. schema.Items.Add (new XmlSchemaNotation ());
  329. schema.Items.Add (new XmlSchemaSimpleType ());
  330. schema.Items.Add (new XmlSchemaGroup ());
  331. schema.Items.Add (new XmlSchemaElement ());
  332. StringWriter sw = new StringWriter ();
  333. XmlTextWriter xtw = new XmlTextWriter (sw);
  334. xtw.QuoteChar = '\'';
  335. xtw.Formatting = Formatting.Indented;
  336. XmlSerializer xs = new XmlSerializer (schema.GetType ());
  337. xs.Serialize (xtw, schema);
  338. AssertEquals (string.Format(CultureInfo.InvariantCulture,
  339. "<?xml version='1.0' encoding='utf-16'?>{0}" +
  340. "<xsd:schema xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>{0}" +
  341. " <xsd:attribute />{0}" +
  342. " <xsd:attributeGroup />{0}" +
  343. " <xsd:complexType />{0}" +
  344. " <xsd:notation />{0}" +
  345. " <xsd:simpleType />{0}" +
  346. " <xsd:group />{0}" +
  347. " <xsd:element />{0}" +
  348. "</xsd:schema>", Environment.NewLine), sw.ToString ());
  349. }
  350. // test xmlText //////////////////////////////////////////////////////////
  351. [Test]
  352. public void TestSerializeXmlTextAttribute()
  353. {
  354. SimpleClass simple = new SimpleClass();
  355. simple.something = "hello";
  356. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  357. XmlAttributes attr = new XmlAttributes();
  358. overrides.Add(typeof(SimpleClass), "something", attr);
  359. attr.XmlText = new XmlTextAttribute();
  360. Serialize(simple, overrides);
  361. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  362. attr.XmlText = new XmlTextAttribute(typeof(string));
  363. Serialize(simple, overrides);
  364. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  365. try
  366. {
  367. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  368. Serialize(simple, overrides);
  369. Fail("XmlText.Type does not match the type it serializes: this should have failed");
  370. }
  371. catch (Exception)
  372. {
  373. }
  374. try
  375. {
  376. attr.XmlText = new XmlTextAttribute();
  377. attr.XmlText.DataType = "sometype";
  378. Serialize(simple, overrides);
  379. Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  380. }
  381. catch (Exception)
  382. {
  383. }
  384. }
  385. // test xmlRoot //////////////////////////////////////////////////////////
  386. [Test]
  387. public void TestSerializeXmlRootAttribute()
  388. {
  389. // constructor override & element name
  390. XmlRootAttribute root = new XmlRootAttribute();
  391. root.ElementName = "renamed";
  392. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  393. Serialize(simpleWithAttributes, root);
  394. AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  395. SimpleClass simple = null;
  396. root.IsNullable = false;
  397. try
  398. {
  399. Serialize(simple, root);
  400. Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  401. }
  402. catch (Exception)
  403. {
  404. }
  405. root.IsNullable = true;
  406. try
  407. {
  408. Serialize(simple, root);
  409. Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  410. }
  411. catch (Exception)
  412. {
  413. }
  414. simple = new SimpleClass();
  415. root.ElementName = null;
  416. root.Namespace = "some.urn";
  417. Serialize(simple, root);
  418. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  419. }
  420. [Test]
  421. public void TestSerializeXmlRootAttributeOnMember()
  422. {
  423. // nested root
  424. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  425. XmlAttributes childAttr = new XmlAttributes();
  426. childAttr.XmlRoot = new XmlRootAttribute("simple");
  427. overrides.Add(typeof(SimpleClass), childAttr);
  428. XmlAttributes attr = new XmlAttributes();
  429. attr.XmlRoot = new XmlRootAttribute("simple");
  430. overrides.Add(typeof(ClassArrayContainer), attr);
  431. ClassArrayContainer container = new ClassArrayContainer();
  432. container.items = new SimpleClass[1];
  433. container.items[0] = new SimpleClass();
  434. Serialize(container, overrides);
  435. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"),WriterText);
  436. // FIXME test data type
  437. }
  438. // test XmlAttribute /////////////////////////////////////////////////////
  439. [Test]
  440. public void TestSerializeXmlAttributeAttribute()
  441. {
  442. // null
  443. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  444. XmlAttributes attr = new XmlAttributes();
  445. attr.XmlAttribute = new XmlAttributeAttribute();
  446. overrides.Add(typeof(SimpleClass), "something", attr);
  447. SimpleClass simple = new SimpleClass();;
  448. Serialize(simple, overrides);
  449. AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  450. // regular
  451. simple.something = "hello";
  452. Serialize(simple, overrides);
  453. AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);
  454. // AttributeName
  455. attr.XmlAttribute.AttributeName = "somethingelse";
  456. Serialize(simple, overrides);
  457. AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);
  458. // Type
  459. // FIXME this should work, shouldnt it?
  460. // attr.XmlAttribute.Type = typeof(string);
  461. // Serialize(simple, overrides);
  462. // Assert(WriterText.EndsWith(" something='hello' />"));
  463. // Namespace
  464. attr.XmlAttribute.Namespace = "some:urn";
  465. Serialize(simple, overrides);
  466. AssertEquals ("#4", 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);
  467. // FIXME DataType
  468. // FIXME XmlSchemaForm Form
  469. // FIXME write XmlQualifiedName as attribute
  470. }
  471. // test XmlElement ///////////////////////////////////////////////////////
  472. [Test]
  473. public void TestSerializeXmlElementAttribute()
  474. {
  475. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  476. XmlAttributes attr = new XmlAttributes();
  477. XmlElementAttribute element = new XmlElementAttribute();
  478. attr.XmlElements.Add(element);
  479. overrides.Add(typeof(SimpleClass), "something", attr);
  480. // null
  481. SimpleClass simple = new SimpleClass();;
  482. Serialize(simple, overrides);
  483. AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  484. // not null
  485. simple.something = "hello";
  486. Serialize(simple, overrides);
  487. AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  488. //ElementName
  489. element.ElementName = "saying";
  490. Serialize(simple, overrides);
  491. AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);
  492. //IsNullable
  493. element.IsNullable = false;
  494. simple.something = null;
  495. Serialize(simple, overrides);
  496. AssertEquals("#4", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  497. element.IsNullable = true;
  498. simple.something = null;
  499. Serialize(simple, overrides);
  500. AssertEquals ("#5", 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);
  501. //Namespace
  502. element.ElementName = null;
  503. element.IsNullable = false;
  504. element.Namespace = "some:urn";
  505. simple.something = "hello";
  506. Serialize(simple, overrides);
  507. AssertEquals ("#6", 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);
  508. //FIXME DataType
  509. //FIXME Form
  510. //FIXME Type
  511. }
  512. // test XmlElementAttribute with arrays and collections //////////////////
  513. [Test]
  514. public void TestSerializeCollectionWithXmlElementAttribute()
  515. {
  516. // the rule is:
  517. // if no type is specified or the specified type
  518. // matches the contents of the collection,
  519. // serialize each element in an element named after the member.
  520. // if the type does not match, or matches the collection itself,
  521. // create a base wrapping element for the member, and then
  522. // wrap each collection item in its own wrapping element based on type.
  523. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  524. XmlAttributes attr = new XmlAttributes();
  525. XmlElementAttribute element = new XmlElementAttribute();
  526. attr.XmlElements.Add(element);
  527. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  528. // empty collection & no type info in XmlElementAttribute
  529. StringCollectionContainer container = new StringCollectionContainer();
  530. Serialize(container, overrides);
  531. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  532. // non-empty collection & no type info in XmlElementAttribute
  533. container.Messages.Add("hello");
  534. Serialize(container, overrides);
  535. AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  536. // non-empty collection & only type info in XmlElementAttribute
  537. element.Type = typeof(StringCollection);
  538. Serialize(container, overrides);
  539. AssertEquals (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);
  540. // non-empty collection & only type info in XmlElementAttribute
  541. element.Type = typeof(string);
  542. Serialize(container, overrides);
  543. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
  544. // two elements
  545. container.Messages.Add("goodbye");
  546. element.Type = null;
  547. Serialize(container, overrides);
  548. AssertEquals(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);
  549. }
  550. // test DefaultValue /////////////////////////////////////////////////////
  551. [Test]
  552. public void TestSerializeDefaultValueAttribute()
  553. {
  554. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  555. XmlAttributes attr = new XmlAttributes();
  556. string defaultValueInstance = "nothing";
  557. attr.XmlDefaultValue = defaultValueInstance;
  558. overrides.Add(typeof(SimpleClass), "something", attr);
  559. // use the default
  560. SimpleClass simple = new SimpleClass();
  561. Serialize(simple, overrides);
  562. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  563. // same value as default
  564. simple.something = defaultValueInstance;
  565. Serialize(simple, overrides);
  566. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  567. // some other value
  568. simple.something = "hello";
  569. Serialize(simple, overrides);
  570. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
  571. }
  572. // test XmlEnum //////////////////////////////////////////////////////////
  573. [Test]
  574. public void TestSerializeXmlEnumAttribute()
  575. {
  576. // technically this has an XmlIgnore attribute,
  577. // but it is not being serialized as a member.
  578. Serialize(XmlSchemaForm.None);
  579. AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  580. Serialize(XmlSchemaForm.Qualified);
  581. AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
  582. Serialize(XmlSchemaForm.Unqualified);
  583. AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
  584. }
  585. [Test]
  586. public void TestSerializeXmlNodeArray ()
  587. {
  588. XmlDocument doc = new XmlDocument ();
  589. Serialize (new XmlNode [] { doc.CreateAttribute("at"), doc.CreateElement("elem1"), doc.CreateElement("elem2") }, typeof(object));
  590. AssertEquals(Infoset("<anyType at=\"\"><elem1/><elem2/></anyType>"), WriterText);
  591. }
  592. [Test]
  593. public void TestSerializeXmlElement ()
  594. {
  595. XmlDocument doc = new XmlDocument ();
  596. Serialize (doc.CreateElement("elem"), typeof(XmlElement));
  597. AssertEquals(Infoset("<elem/>"), WriterText);
  598. }
  599. [Test]
  600. public void TestSerializeXmlElementSubclass ()
  601. {
  602. XmlDocument doc = new XmlDocument ();
  603. Serialize (new MyElem (doc), typeof(XmlElement));
  604. AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
  605. Serialize (new MyElem (doc), typeof(MyElem));
  606. AssertEquals(Infoset("<myelem aa=\"1\"/>"), WriterText);
  607. }
  608. [Test]
  609. public void TestSerializeXmlCDataSection ()
  610. {
  611. XmlDocument doc = new XmlDocument ();
  612. CDataContainer c = new CDataContainer ();
  613. c.cdata = doc.CreateCDataSection("data section contents");
  614. Serialize (c);
  615. AssertEquals(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);
  616. }
  617. [Test]
  618. public void TestSerializeXmlNode ()
  619. {
  620. XmlDocument doc = new XmlDocument ();
  621. NodeContainer c = new NodeContainer ();
  622. c.node = doc.CreateTextNode("text");
  623. Serialize (c);
  624. AssertEquals(Infoset("<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>"), WriterText);
  625. }
  626. [Test]
  627. public void TestSerializeChoice ()
  628. {
  629. Choices ch = new Choices ();
  630. ch.MyChoice = "choice text";
  631. ch.ItemType = ItemChoiceType.ChoiceZero;
  632. Serialize (ch);
  633. AssertEquals(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);
  634. ch.ItemType = ItemChoiceType.StrangeOne;
  635. Serialize (ch);
  636. AssertEquals(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);
  637. ch.ItemType = ItemChoiceType.ChoiceTwo;
  638. Serialize (ch);
  639. AssertEquals(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);
  640. }
  641. [Test]
  642. public void TestSerializeNamesWithSpaces ()
  643. {
  644. TestSpace ts = new TestSpace();
  645. ts.elem = 4;
  646. ts.attr = 5;
  647. Serialize (ts);
  648. AssertEquals(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);
  649. }
  650. [Test]
  651. public void TestSerializeReadOnlyProps ()
  652. {
  653. ReadOnlyProperties ts = new ReadOnlyProperties();
  654. Serialize (ts);
  655. AssertEquals(Infoset("<ReadOnlyProperties xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  656. }
  657. [Test]
  658. public void TestSerializeIList()
  659. {
  660. clsPerson k = new clsPerson();
  661. k.EmailAccounts = new ArrayList();
  662. k.EmailAccounts.Add("a");
  663. k.EmailAccounts.Add("b");
  664. Serialize (k);
  665. AssertEquals (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);
  666. }
  667. [Test]
  668. public void TestSerializeArrayEnc ()
  669. {
  670. SoapReflectionImporter imp = new SoapReflectionImporter ();
  671. XmlTypeMapping map = imp.ImportTypeMapping (typeof(ArrayClass));
  672. XmlSerializer ser = new XmlSerializer (map);
  673. StringWriter sw = new StringWriter ();
  674. XmlTextWriter tw = new XmlTextWriter (sw);
  675. tw.WriteStartElement ("aa");
  676. ser.Serialize (tw, new ArrayClass ());
  677. tw.WriteEndElement ();
  678. }
  679. [Test]
  680. public void TestIncludeType()
  681. {
  682. // Test for bug #76049
  683. XmlReflectionImporter imp = new XmlReflectionImporter ();
  684. XmlTypeMapping map = imp.ImportTypeMapping (typeof(object));
  685. imp.IncludeType (typeof(TestSpace));
  686. XmlSerializer ser = new XmlSerializer (map);
  687. ser.Serialize (new StringWriter (), new TestSpace ());
  688. }
  689. [Test]
  690. public void TestSerializeChoiceArray()
  691. {
  692. CompositeValueType v = new CompositeValueType ();
  693. v.Init ();
  694. Serialize (v);
  695. AssertEquals (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);
  696. }
  697. [Test]
  698. public void TestArrayAttributeWithDataType ()
  699. {
  700. Serialize (new ArrayAttributeWithType ());
  701. string res = "<ArrayAttributeWithType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
  702. res += "at='a b' bin1='AQI= AQI=' bin2='AQI=' />";
  703. AssertEquals (Infoset(res), WriterText);
  704. }
  705. [Test]
  706. [ExpectedException (typeof(InvalidOperationException))]
  707. public void TestArrayAttributeWithWrongDataType ()
  708. {
  709. Serialize (new ArrayAttributeWithWrongType ());
  710. }
  711. // Helper methods
  712. public static string Infoset (string sx)
  713. {
  714. XmlDocument doc = new XmlDocument ();
  715. doc.LoadXml (sx);
  716. StringBuilder sb = new StringBuilder ();
  717. GetInfoset (doc.DocumentElement, sb);
  718. return sb.ToString ();
  719. }
  720. public static string Infoset (XmlNode nod)
  721. {
  722. StringBuilder sb = new StringBuilder ();
  723. GetInfoset (nod, sb);
  724. return sb.ToString ();
  725. }
  726. static void GetInfoset (XmlNode nod, StringBuilder sb)
  727. {
  728. switch (nod.NodeType)
  729. {
  730. case XmlNodeType.Attribute:
  731. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  732. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  733. break;
  734. case XmlNodeType.Element:
  735. XmlElement elem = (XmlElement) nod;
  736. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  737. ArrayList ats = new ArrayList ();
  738. foreach (XmlAttribute at in elem.Attributes)
  739. ats.Add (at.LocalName + " " + at.NamespaceURI);
  740. ats.Sort ();
  741. foreach (string name in ats)
  742. {
  743. string[] nn = name.Split (' ');
  744. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  745. }
  746. sb.Append (">");
  747. foreach (XmlNode cn in elem.ChildNodes)
  748. GetInfoset (cn, sb);
  749. sb.Append ("</>");
  750. break;
  751. default:
  752. sb.Append (nod.OuterXml);
  753. break;
  754. }
  755. }
  756. }
  757. }