XmlSerializerTests.cs 21 KB

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