XmlSerializerTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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.IO;
  28. using System.Text;
  29. using System.Xml;
  30. using System.Xml.Schema;
  31. using System.Xml.Serialization;
  32. using NUnit.Framework;
  33. using MonoTests.System.Xml.TestClasses;
  34. namespace MonoTests.System.XmlSerialization
  35. {
  36. [TestFixture]
  37. public class XmlSerializerTests : Assertion
  38. {
  39. StringWriter sw;
  40. XmlTextWriter xtw;
  41. XmlSerializer xs;
  42. private void SetUpWriter()
  43. {
  44. sw = new StringWriter ();
  45. xtw = new XmlTextWriter (sw);
  46. xtw.QuoteChar = '\'';
  47. xtw.Formatting = Formatting.None;
  48. }
  49. private string WriterText
  50. {
  51. get
  52. {
  53. string val = sw.GetStringBuilder().ToString();
  54. int offset = val.IndexOf('>') + 1;
  55. val = val.Substring(offset);
  56. return Infoset(val);
  57. }
  58. }
  59. private void Serialize(object o)
  60. {
  61. SetUpWriter();
  62. xs = new XmlSerializer(o.GetType());
  63. xs.Serialize(xtw, o);
  64. }
  65. private void Serialize(object o, Type type)
  66. {
  67. SetUpWriter();
  68. xs = new XmlSerializer(type);
  69. xs.Serialize(xtw, o);
  70. }
  71. private void Serialize(object o, XmlSerializerNamespaces ns)
  72. {
  73. SetUpWriter();
  74. xs = new XmlSerializer(o.GetType());
  75. xs.Serialize(xtw, o, ns);
  76. }
  77. private void Serialize(object o, XmlAttributeOverrides ao)
  78. {
  79. SetUpWriter();
  80. xs = new XmlSerializer(o.GetType(), ao);
  81. xs.Serialize(xtw, o);
  82. }
  83. private void Serialize(object o, XmlRootAttribute root)
  84. {
  85. SetUpWriter();
  86. xs = new XmlSerializer(o.GetType(), root);
  87. xs.Serialize(xtw, o);
  88. }
  89. // test constructors
  90. #if USE_VERSION_1_1 // It doesn't pass on MS.NET 1.1.
  91. [Test]
  92. public void TestConstructor()
  93. {
  94. XmlSerializer ser = new XmlSerializer (null, "");
  95. }
  96. #else
  97. #endif
  98. // test basic types ////////////////////////////////////////////////////////
  99. [Test]
  100. public void TestSerializeInt()
  101. {
  102. Serialize(10);
  103. AssertEquals(Infoset("<int>10</int>"), WriterText);
  104. }
  105. [Test]
  106. public void TestSerializeBool()
  107. {
  108. Serialize(true);
  109. AssertEquals(Infoset("<boolean>true</boolean>"), WriterText);
  110. Serialize(false);
  111. AssertEquals(Infoset("<boolean>false</boolean>"), WriterText);
  112. }
  113. [Test]
  114. public void TestSerializeString()
  115. {
  116. Serialize("hello");
  117. AssertEquals(Infoset("<string>hello</string>"), WriterText);
  118. }
  119. [Test]
  120. public void TestSerializeEmptyString()
  121. {
  122. Serialize(String.Empty);
  123. AssertEquals(Infoset("<string />"), WriterText);
  124. }
  125. [Test]
  126. public void TestSerializeNullObject()
  127. {
  128. Serialize(null, typeof(object));
  129. AssertEquals(Infoset("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  130. }
  131. [Test]
  132. [Ignore ("The generated XML is not exact but it is equivalent")]
  133. public void TestSerializeNullString()
  134. {
  135. Serialize(null, typeof(string));
  136. Console.WriteLine (WriterText);
  137. AssertEquals (Infoset("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);
  138. }
  139. [Test]
  140. public void TestSerializeIntArray()
  141. {
  142. Serialize(new int[] {1, 2, 3, 4});
  143. 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);
  144. }
  145. [Test]
  146. public void TestSerializeEmptyArray()
  147. {
  148. Serialize(new int[] {});
  149. AssertEquals(Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  150. }
  151. [Test]
  152. public void TestSerializeChar()
  153. {
  154. Serialize('A');
  155. AssertEquals(Infoset("<char>65</char>"), WriterText);
  156. Serialize('\0');
  157. AssertEquals(Infoset("<char>0</char>"), WriterText);
  158. Serialize('\n');
  159. AssertEquals(Infoset("<char>10</char>"), WriterText);
  160. Serialize('\uFF01');
  161. AssertEquals(Infoset("<char>65281</char>"), WriterText);
  162. }
  163. [Test]
  164. public void TestSerializeFloat()
  165. {
  166. Serialize(10.78);
  167. AssertEquals(Infoset("<double>10.78</double>"), WriterText);
  168. Serialize(-1e8);
  169. AssertEquals(Infoset("<double>-100000000</double>"), WriterText);
  170. // FIXME test INF and other boundary conditions that may exist with floats
  171. }
  172. [Test]
  173. public void TestSerializeEnumeration()
  174. {
  175. Serialize(SimpleEnumeration.FIRST);
  176. AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);
  177. Serialize(SimpleEnumeration.SECOND);
  178. AssertEquals(Infoset("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);
  179. }
  180. [Test]
  181. public void TestSerializeQualifiedName()
  182. {
  183. Serialize(new XmlQualifiedName("me", "home.urn"));
  184. AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);
  185. }
  186. [Test]
  187. public void TestSerializeBytes()
  188. {
  189. Serialize((byte)0xAB);
  190. AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);
  191. Serialize((byte)15);
  192. AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);
  193. }
  194. [Test]
  195. public void TestSerializeByteArrays()
  196. {
  197. Serialize(new byte[] {});
  198. AssertEquals(Infoset("<base64Binary />"), WriterText);
  199. Serialize(new byte[] {0xAB, 0xCD});
  200. AssertEquals(Infoset("<base64Binary>q80=</base64Binary>"), WriterText);
  201. }
  202. [Test]
  203. public void TestSerializeDateTime()
  204. {
  205. DateTime d = new DateTime();
  206. Serialize(d);
  207. TimeZone tz = TimeZone.CurrentTimeZone;
  208. TimeSpan off = tz.GetUtcOffset (d);
  209. string sp = string.Format ("{0:00}:{1:00}", off.TotalHours, off.TotalMinutes%60);
  210. if (off.Ticks > 0) sp = "+" + sp;
  211. else sp = "-" + sp;
  212. AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);
  213. }
  214. /*
  215. FIXME
  216. - decimal
  217. - Guid
  218. - XmlNode objects
  219. [Test]
  220. public void TestSerialize()
  221. {
  222. Serialize();
  223. AssertEquals(WriterText, "");
  224. }
  225. */
  226. // test basic class serialization /////////////////////////////////////
  227. [Test]
  228. public void TestSerializeSimpleClass()
  229. {
  230. SimpleClass simple = new SimpleClass();
  231. Serialize(simple);
  232. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  233. simple.something = "hello";
  234. Serialize(simple);
  235. 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);
  236. }
  237. [Test]
  238. public void TestSerializeStringCollection()
  239. {
  240. StringCollection strings = new StringCollection();
  241. Serialize(strings);
  242. AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  243. strings.Add("hello");
  244. strings.Add("goodbye");
  245. Serialize(strings);
  246. 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);
  247. }
  248. [Test]
  249. public void TestSerializePlainContainer()
  250. {
  251. StringCollectionContainer container = new StringCollectionContainer();
  252. Serialize(container);
  253. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);
  254. container.Messages.Add("hello");
  255. container.Messages.Add("goodbye");
  256. Serialize(container);
  257. 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);
  258. }
  259. [Test]
  260. public void TestSerializeArrayContainer()
  261. {
  262. ArrayContainer container = new ArrayContainer();
  263. Serialize(container);
  264. AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  265. container.items = new object[] {10, 20};
  266. Serialize(container);
  267. 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);
  268. container.items = new object[] {10, "hello"};
  269. Serialize(container);
  270. 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);
  271. }
  272. [Test]
  273. public void TestSerializeClassArrayContainer()
  274. {
  275. ClassArrayContainer container = new ClassArrayContainer();
  276. Serialize(container);
  277. AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  278. SimpleClass simple1 = new SimpleClass();
  279. simple1.something = "hello";
  280. SimpleClass simple2 = new SimpleClass();
  281. simple2.something = "hello";
  282. container.items = new SimpleClass[2];
  283. container.items[0] = simple1;
  284. container.items[1] = simple2;
  285. Serialize(container);
  286. 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);
  287. }
  288. // test basic attributes ///////////////////////////////////////////////
  289. [Test]
  290. public void TestSerializeSimpleClassWithXmlAttributes()
  291. {
  292. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  293. Serialize(simple);
  294. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  295. simple.something = "hello";
  296. Serialize(simple);
  297. AssertEquals (Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);
  298. }
  299. // test overrides ///////////////////////////////////////////////////////
  300. [Test]
  301. public void TestSerializeSimpleClassWithOverrides()
  302. {
  303. // Also tests XmlIgnore
  304. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  305. XmlAttributes attr = new XmlAttributes();
  306. attr.XmlIgnore = true;
  307. overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);
  308. SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();
  309. simple.something = "hello";
  310. Serialize(simple, overrides);
  311. AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  312. }
  313. // test xmlText //////////////////////////////////////////////////////////
  314. [Test]
  315. public void TestSerializeXmlTextAttribute()
  316. {
  317. SimpleClass simple = new SimpleClass();
  318. simple.something = "hello";
  319. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  320. XmlAttributes attr = new XmlAttributes();
  321. overrides.Add(typeof(SimpleClass), "something", attr);
  322. attr.XmlText = new XmlTextAttribute();
  323. Serialize(simple, overrides);
  324. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  325. attr.XmlText = new XmlTextAttribute(typeof(string));
  326. Serialize(simple, overrides);
  327. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);
  328. try
  329. {
  330. attr.XmlText = new XmlTextAttribute(typeof(byte[]));
  331. Serialize(simple, overrides);
  332. Fail("XmlText.Type does not match the type it serializes: this should have failed");
  333. }
  334. catch (Exception)
  335. {
  336. }
  337. try
  338. {
  339. attr.XmlText = new XmlTextAttribute();
  340. attr.XmlText.DataType = "sometype";
  341. Serialize(simple, overrides);
  342. Fail("XmlText.DataType does not match the type it serializes: this should have failed");
  343. }
  344. catch (Exception)
  345. {
  346. }
  347. }
  348. // test xmlRoot //////////////////////////////////////////////////////////
  349. [Test]
  350. public void TestSerializeXmlRootAttribute()
  351. {
  352. // constructor override & element name
  353. XmlRootAttribute root = new XmlRootAttribute();
  354. root.ElementName = "renamed";
  355. SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();
  356. Serialize(simpleWithAttributes, root);
  357. AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  358. SimpleClass simple = null;
  359. root.IsNullable = false;
  360. try
  361. {
  362. Serialize(simple, root);
  363. Fail("Cannot serialize null object if XmlRoot's IsNullable == false");
  364. }
  365. catch (Exception)
  366. {
  367. }
  368. root.IsNullable = true;
  369. try
  370. {
  371. Serialize(simple, root);
  372. Fail("Cannot serialize null object if XmlRoot's IsNullable == true");
  373. }
  374. catch (Exception)
  375. {
  376. }
  377. simple = new SimpleClass();
  378. root.ElementName = null;
  379. root.Namespace = "some.urn";
  380. Serialize(simple, root);
  381. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);
  382. }
  383. [Test]
  384. public void TestSerializeXmlRootAttributeOnMember()
  385. {
  386. // nested root
  387. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  388. XmlAttributes childAttr = new XmlAttributes();
  389. childAttr.XmlRoot = new XmlRootAttribute("simple");
  390. overrides.Add(typeof(SimpleClass), childAttr);
  391. XmlAttributes attr = new XmlAttributes();
  392. attr.XmlRoot = new XmlRootAttribute("simple");
  393. overrides.Add(typeof(ClassArrayContainer), attr);
  394. ClassArrayContainer container = new ClassArrayContainer();
  395. container.items = new SimpleClass[1];
  396. container.items[0] = new SimpleClass();
  397. Serialize(container, overrides);
  398. 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);
  399. // FIXME test data type
  400. }
  401. // test XmlAttribute /////////////////////////////////////////////////////
  402. [Test]
  403. public void TestSerializeXmlAttributeAttribute()
  404. {
  405. // null
  406. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  407. XmlAttributes attr = new XmlAttributes();
  408. attr.XmlAttribute = new XmlAttributeAttribute();
  409. overrides.Add(typeof(SimpleClass), "something", attr);
  410. SimpleClass simple = new SimpleClass();;
  411. Serialize(simple, overrides);
  412. AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  413. // regular
  414. simple.something = "hello";
  415. Serialize(simple, overrides);
  416. AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);
  417. // AttributeName
  418. attr.XmlAttribute.AttributeName = "somethingelse";
  419. Serialize(simple, overrides);
  420. AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);
  421. // Type
  422. // FIXME this should work, shouldnt it?
  423. // attr.XmlAttribute.Type = typeof(string);
  424. // Serialize(simple, overrides);
  425. // Assert(WriterText.EndsWith(" something='hello' />"));
  426. // Namespace
  427. attr.XmlAttribute.Namespace = "some:urn";
  428. Serialize(simple, overrides);
  429. 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);
  430. // FIXME DataType
  431. // FIXME XmlSchemaForm Form
  432. // FIXME write XmlQualifiedName as attribute
  433. }
  434. // test XmlElement ///////////////////////////////////////////////////////
  435. [Test]
  436. public void TestSerializeXmlElementAttribute()
  437. {
  438. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  439. XmlAttributes attr = new XmlAttributes();
  440. XmlElementAttribute element = new XmlElementAttribute();
  441. attr.XmlElements.Add(element);
  442. overrides.Add(typeof(SimpleClass), "something", attr);
  443. // null
  444. SimpleClass simple = new SimpleClass();;
  445. Serialize(simple, overrides);
  446. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  447. // not null
  448. simple.something = "hello";
  449. Serialize(simple, overrides);
  450. 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);
  451. //ElementName
  452. element.ElementName = "saying";
  453. Serialize(simple, overrides);
  454. AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);
  455. //IsNullable
  456. element.IsNullable = false;
  457. simple.something = null;
  458. Serialize(simple, overrides);
  459. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
  460. element.IsNullable = true;
  461. simple.something = null;
  462. Serialize(simple, overrides);
  463. AssertEquals (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);
  464. //Namespace
  465. element.ElementName = null;
  466. element.IsNullable = false;
  467. element.Namespace = "some:urn";
  468. simple.something = "hello";
  469. Serialize(simple, overrides);
  470. AssertEquals (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);
  471. //FIXME DataType
  472. //FIXME Form
  473. //FIXME Type
  474. }
  475. // test XmlElementAttribute with arrays and collections //////////////////
  476. [Test]
  477. public void TestSerializeCollectionWithXmlElementAttribute()
  478. {
  479. // the rule is:
  480. // if no type is specified or the specified type
  481. // matches the contents of the collection,
  482. // serialize each element in an element named after the member.
  483. // if the type does not match, or matches the collection itself,
  484. // create a base wrapping element for the member, and then
  485. // wrap each collection item in its own wrapping element based on type.
  486. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  487. XmlAttributes attr = new XmlAttributes();
  488. XmlElementAttribute element = new XmlElementAttribute();
  489. attr.XmlElements.Add(element);
  490. overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
  491. // empty collection & no type info in XmlElementAttribute
  492. StringCollectionContainer container = new StringCollectionContainer();
  493. Serialize(container, overrides);
  494. AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  495. // non-empty collection & no type info in XmlElementAttribute
  496. container.Messages.Add("hello");
  497. Serialize(container, overrides);
  498. 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);
  499. // non-empty collection & only type info in XmlElementAttribute
  500. element.Type = typeof(StringCollection);
  501. Serialize(container, overrides);
  502. 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);
  503. // non-empty collection & only type info in XmlElementAttribute
  504. element.Type = typeof(string);
  505. Serialize(container, overrides);
  506. 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);
  507. // two elements
  508. container.Messages.Add("goodbye");
  509. element.Type = null;
  510. Serialize(container, overrides);
  511. 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);
  512. }
  513. // test DefaultValue /////////////////////////////////////////////////////
  514. [Test]
  515. public void TestSerializeDefaultValueAttribute()
  516. {
  517. XmlAttributeOverrides overrides = new XmlAttributeOverrides();
  518. XmlAttributes attr = new XmlAttributes();
  519. string defaultValueInstance = "nothing";
  520. attr.XmlDefaultValue = defaultValueInstance;
  521. overrides.Add(typeof(SimpleClass), "something", attr);
  522. // use the default
  523. SimpleClass simple = new SimpleClass();
  524. Serialize(simple, overrides);
  525. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  526. // same value as default
  527. simple.something = defaultValueInstance;
  528. Serialize(simple, overrides);
  529. AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
  530. // some other value
  531. simple.something = "hello";
  532. Serialize(simple, overrides);
  533. 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);
  534. }
  535. // test XmlEnum //////////////////////////////////////////////////////////
  536. [Test]
  537. public void TestSerializeXmlEnumAttribute()
  538. {
  539. // technically this has an XmlIgnore attribute,
  540. // but it is not being serialized as a member.
  541. Serialize(XmlSchemaForm.None);
  542. AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);
  543. Serialize(XmlSchemaForm.Qualified);
  544. AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);
  545. Serialize(XmlSchemaForm.Unqualified);
  546. AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);
  547. }
  548. public static string Infoset (string sx)
  549. {
  550. XmlDocument doc = new XmlDocument ();
  551. doc.LoadXml (sx);
  552. StringBuilder sb = new StringBuilder ();
  553. GetInfoset (doc.DocumentElement, sb);
  554. return sb.ToString ();
  555. }
  556. public static string Infoset (XmlNode nod)
  557. {
  558. StringBuilder sb = new StringBuilder ();
  559. GetInfoset (nod, sb);
  560. return sb.ToString ();
  561. }
  562. static void GetInfoset (XmlNode nod, StringBuilder sb)
  563. {
  564. switch (nod.NodeType)
  565. {
  566. case XmlNodeType.Attribute:
  567. if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;
  568. sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");
  569. break;
  570. case XmlNodeType.Element:
  571. XmlElement elem = (XmlElement) nod;
  572. sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);
  573. ArrayList ats = new ArrayList ();
  574. foreach (XmlAttribute at in elem.Attributes)
  575. ats.Add (at.LocalName + " " + at.NamespaceURI);
  576. ats.Sort ();
  577. foreach (string name in ats)
  578. {
  579. string[] nn = name.Split (' ');
  580. GetInfoset (elem.Attributes[nn[0],nn[1]], sb);
  581. }
  582. sb.Append (">");
  583. foreach (XmlNode cn in elem.ChildNodes)
  584. GetInfoset (cn, sb);
  585. sb.Append ("</>");
  586. break;
  587. default:
  588. sb.Append (nod.OuterXml);
  589. break;
  590. }
  591. }
  592. }
  593. }