XmlTextWriterTests.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. //
  2. // System.Xml.XmlTextWriterTests
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Text;
  15. using System.Xml;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Xml
  18. {
  19. [TestFixture]
  20. public class XmlTextWriterTests
  21. {
  22. StringWriter sw;
  23. XmlTextWriter xtw;
  24. [SetUp]
  25. public void GetReady ()
  26. {
  27. sw = new StringWriter ();
  28. CreateXmlTextWriter ();
  29. }
  30. private void CreateXmlTextWriter ()
  31. {
  32. xtw = new XmlTextWriter (sw);
  33. xtw.QuoteChar = '\'';
  34. }
  35. private string StringWriterText
  36. {
  37. get { return sw.GetStringBuilder ().ToString (); }
  38. }
  39. [Test]
  40. public void AttributeNamespacesNonNamespaceAttributeBefore ()
  41. {
  42. xtw.WriteStartElement ("foo");
  43. xtw.WriteAttributeString("bar", "baz");
  44. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  45. Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
  46. }
  47. [Test]
  48. public void AttributeNamespacesNonNamespaceAttributeAfter ()
  49. {
  50. xtw.WriteStartElement ("foo");
  51. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  52. xtw.WriteAttributeString("bar", "baz");
  53. Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
  54. }
  55. [Test]
  56. public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
  57. {
  58. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  59. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText);
  60. }
  61. [Test]
  62. public void WriteAttributeString_XmlNs_Valid ()
  63. {
  64. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  65. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#1");
  66. sw.GetStringBuilder ().Length = 0;
  67. CreateXmlTextWriter ();
  68. xtw.WriteAttributeString ("xmlns", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  69. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#2");
  70. sw.GetStringBuilder ().Length = 0;
  71. CreateXmlTextWriter ();
  72. xtw.WriteAttributeString (null, "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  73. Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#3");
  74. sw.GetStringBuilder ().Length = 0;
  75. CreateXmlTextWriter ();
  76. xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  77. Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#4");
  78. sw.GetStringBuilder ().Length = 0;
  79. CreateXmlTextWriter ();
  80. xtw.WriteStartElement ("person");
  81. xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  82. xtw.WriteEndElement ();
  83. Assert.AreEqual ("<person xmlns:test='http://abc.def' />", StringWriterText, "#5");
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentException))]
  87. public void WriteAttributeString_XmlNs_Invalid1 ()
  88. {
  89. // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
  90. xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void WriteAttributeString_XmlNs_Invalid2 ()
  95. {
  96. // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
  97. xtw.WriteAttributeString (null, "xmlns", "http://somenamespace.com", "http://abc.def");
  98. }
  99. [Test]
  100. public void XmlSpace_Valid () // bug #77084
  101. {
  102. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  103. Assert.AreEqual ("xml:space='preserve'", StringWriterText, "#1");
  104. sw.GetStringBuilder ().Length = 0;
  105. CreateXmlTextWriter ();
  106. xtw.WriteAttributeString ("xml", "space", "whatever", "default");
  107. Assert.AreEqual ("xml:space='default'", StringWriterText, "#2");
  108. sw.GetStringBuilder ().Length = 0;
  109. CreateXmlTextWriter ();
  110. xtw.WriteStartElement ("person");
  111. xtw.WriteAttributeString ("xml", "space", "whatever", "default");
  112. xtw.WriteEndElement ();
  113. Assert.AreEqual ("<person xml:space='default' />", StringWriterText, "#3");
  114. }
  115. [Test]
  116. public void WriteAttributeString_XmlPrefix_Valid ()
  117. {
  118. xtw.WriteAttributeString ("xml", "something", "whatever", "default");
  119. Assert.AreEqual ("xml:something='default'", StringWriterText, "#1");
  120. sw.GetStringBuilder ().Length = 0;
  121. CreateXmlTextWriter ();
  122. xtw.WriteAttributeString ("xml", "else", null, "whatever");
  123. Assert.AreEqual ("xml:else='whatever'", StringWriterText, "#2");
  124. sw.GetStringBuilder ().Length = 0;
  125. CreateXmlTextWriter ();
  126. xtw.WriteStartElement ("person");
  127. xtw.WriteAttributeString ("xml", "something", "whatever", "default");
  128. xtw.WriteAttributeString ("xml", "else", null, "whatever");
  129. xtw.WriteEndElement ();
  130. Assert.AreEqual ("<person xml:something='default' xml:else='whatever' />",
  131. StringWriterText, "#3");
  132. }
  133. [Test]
  134. [ExpectedException (typeof (ArgumentException))]
  135. public void WriteAttributeString_XmlSpace_Invalid ()
  136. {
  137. // only preserve and default are valid values for xml:space
  138. xtw.WriteAttributeString ("xml", "space", null, "something");
  139. }
  140. [Test]
  141. public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
  142. {
  143. xtw.WriteAttributeString ("a", "http://somenamespace.com", "http://abc.def");
  144. Assert.AreEqual ("d0p1:a='http://abc.def'", StringWriterText, "#1");
  145. }
  146. [Test]
  147. [ExpectedException (typeof (ArgumentException))]
  148. public void AttributeNamespacesWithNullInNamespaceParam ()
  149. {
  150. // you cannot use prefix with an empty namespace
  151. xtw.WriteAttributeString ("a", "abc", null, "http://abc.def");
  152. }
  153. [Test]
  154. public void AttributeNamespacesWithTextInNamespaceParam ()
  155. {
  156. xtw.WriteAttributeString ("a", "abc", "http://somenamespace.com", "http://abc.def");
  157. Assert.AreEqual ("a:abc='http://abc.def'", StringWriterText, "#1");
  158. sw.GetStringBuilder ().Length = 0;
  159. CreateXmlTextWriter ();
  160. xtw.WriteAttributeString ("", "abc", "http://somenamespace.com", "http://abc.def");
  161. Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#2");
  162. sw.GetStringBuilder ().Length = 0;
  163. CreateXmlTextWriter ();
  164. xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
  165. Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#3");
  166. }
  167. [Test]
  168. [Category ("NotWorking")] // bug #77086, #77087 and #77088
  169. public void AutoCreatePrefixes ()
  170. {
  171. xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
  172. xtw.WriteAttributeString (null, "def", "http://somenamespace.com", "http://def.ghi");
  173. xtw.WriteAttributeString (null, "ghi", "http://othernamespace.com", "http://ghi.jkl");
  174. #if NET_2_0
  175. Assert.AreEqual ("d0p1:abc='http://abc.def' d0p1:def='http://def.ghi'" +
  176. " d0p2:ghi='http://ghi.jkl'", StringWriterText, "#1");
  177. #else
  178. // on 1.x a new prefix is always created when level is 0 ?
  179. Assert.AreEqual ("d0p1:abc='http://abc.def' d0p2:def='http://def.ghi'" +
  180. " d0p3:ghi='http://ghi.jkl'", StringWriterText, "#1");
  181. #endif
  182. sw.GetStringBuilder ().Length = 0;
  183. CreateXmlTextWriter ();
  184. xtw.WriteStartElement ("person");
  185. xtw.WriteAttributeString (null, "name", "http://somenamespace.com", "Gates");
  186. xtw.WriteAttributeString (null, "initials", "http://othernamespace.com", "BG");
  187. xtw.WriteAttributeString (null, "firstName", "http://somenamespace.com", "Bill");
  188. xtw.WriteStartElement ("address");
  189. xtw.WriteAttributeString (null, "street", "http://somenamespace.com", "Campus");
  190. xtw.WriteAttributeString (null, "number", "http://othernamespace.com", "1");
  191. xtw.WriteAttributeString (null, "zip", "http://newnamespace.com", "3000");
  192. xtw.WriteAttributeString (null, "box", "http://othernamespace.com", "a");
  193. xtw.WriteEndElement ();
  194. xtw.WriteEndElement ();
  195. Assert.AreEqual (
  196. "<person" +
  197. " d1p1:name='Gates'" +
  198. " d1p2:initials='BG'" +
  199. " d1p1:firstName='Bill'" +
  200. " xmlns:d1p2='http://othernamespace.com'" +
  201. " xmlns:d1p1='http://somenamespace.com'>" +
  202. "<address" +
  203. " d1p1:street='Campus'" +
  204. " d1p2:number='1'" +
  205. " d2p1:zip='3000'" +
  206. " d1p2:box='a'" +
  207. " xmlns:d2p1='http://newnamespace.com' />" +
  208. "</person>", StringWriterText, "#2");
  209. }
  210. [Test]
  211. [Category ("NotDotNet")]
  212. public void AttributeNamespacesXmlnsXmlns ()
  213. {
  214. xtw.WriteStartElement ("foo");
  215. try {
  216. xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  217. // This should not be allowed, even though MS.NET doesn't treat as an error.
  218. // See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
  219. Assert.Fail ("any prefix which name starts from \"xml\" must not be allowed.");
  220. } catch (ArgumentException) {}
  221. xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
  222. }
  223. [Test]
  224. public void WriteAttributeString_EmptyLocalName ()
  225. {
  226. xtw.WriteAttributeString ("", "something");
  227. Assert.AreEqual ("='something'", StringWriterText, "#1");
  228. sw.GetStringBuilder ().Length = 0;
  229. CreateXmlTextWriter ();
  230. xtw.WriteAttributeString ("", "", "something");
  231. Assert.AreEqual ("='something'", StringWriterText, "#2");
  232. sw.GetStringBuilder ().Length = 0;
  233. CreateXmlTextWriter ();
  234. xtw.WriteAttributeString ("", "http://somenamespace.com", "something");
  235. Assert.AreEqual ("d0p1:='something'", StringWriterText, "#3");
  236. sw.GetStringBuilder ().Length = 0;
  237. CreateXmlTextWriter ();
  238. xtw.WriteAttributeString ("x", "", "http://somenamespace.com", "something");
  239. Assert.AreEqual ("x:='something'", StringWriterText, "#4");
  240. sw.GetStringBuilder ().Length = 0;
  241. CreateXmlTextWriter ();
  242. xtw.WriteAttributeString (null, "something");
  243. Assert.AreEqual ("='something'", StringWriterText, "#5");
  244. sw.GetStringBuilder ().Length = 0;
  245. CreateXmlTextWriter ();
  246. xtw.WriteAttributeString (null, "", "something");
  247. Assert.AreEqual ("='something'", StringWriterText, "#6");
  248. sw.GetStringBuilder ().Length = 0;
  249. CreateXmlTextWriter ();
  250. xtw.WriteAttributeString (null, "http://somenamespace.com", "something");
  251. Assert.AreEqual ("d0p1:='something'", StringWriterText, "#7");
  252. sw.GetStringBuilder ().Length = 0;
  253. CreateXmlTextWriter ();
  254. xtw.WriteAttributeString ("x", null, "http://somenamespace.com", "something");
  255. Assert.AreEqual ("x:='something'", StringWriterText, "#8");
  256. }
  257. [Test]
  258. public void WriteStartAttribute_EmptyLocalName ()
  259. {
  260. xtw.WriteStartAttribute ("", "");
  261. Assert.AreEqual ("='", StringWriterText, "#1");
  262. sw.GetStringBuilder ().Length = 0;
  263. CreateXmlTextWriter ();
  264. xtw.WriteStartAttribute ("", "", "");
  265. Assert.AreEqual ("='", StringWriterText, "#2");
  266. sw.GetStringBuilder ().Length = 0;
  267. CreateXmlTextWriter ();
  268. xtw.WriteStartAttribute ("", "", "http://somenamespace.com");
  269. Assert.AreEqual ("d0p1:='", StringWriterText, "#3");
  270. sw.GetStringBuilder ().Length = 0;
  271. CreateXmlTextWriter ();
  272. xtw.WriteStartAttribute ("x", "", "http://somenamespace.com");
  273. Assert.AreEqual ("x:='", StringWriterText, "#4");
  274. sw.GetStringBuilder ().Length = 0;
  275. CreateXmlTextWriter ();
  276. xtw.WriteStartAttribute ("", null);
  277. Assert.AreEqual ("='", StringWriterText, "#5");
  278. sw.GetStringBuilder ().Length = 0;
  279. CreateXmlTextWriter ();
  280. xtw.WriteStartAttribute ("", null, "");
  281. Assert.AreEqual ("='", StringWriterText, "#6");
  282. sw.GetStringBuilder ().Length = 0;
  283. CreateXmlTextWriter ();
  284. xtw.WriteStartAttribute ("", null, "http://somenamespace.com");
  285. Assert.AreEqual ("d0p1:='", StringWriterText, "#7");
  286. sw.GetStringBuilder ().Length = 0;
  287. CreateXmlTextWriter ();
  288. xtw.WriteStartAttribute ("x", null, "http://somenamespace.com");
  289. Assert.AreEqual ("x:='", StringWriterText, "#8");
  290. }
  291. [Test]
  292. public void AttributeWriteAttributeString ()
  293. {
  294. xtw.WriteStartElement ("foo");
  295. xtw.WriteAttributeString ("foo", "bar");
  296. Assert.AreEqual ("<foo foo='bar'", StringWriterText);
  297. xtw.WriteAttributeString ("bar", "");
  298. Assert.AreEqual ("<foo foo='bar' bar=''", StringWriterText);
  299. xtw.WriteAttributeString ("baz", null);
  300. Assert.AreEqual ("<foo foo='bar' bar='' baz=''", StringWriterText);
  301. xtw.WriteAttributeString ("hoge", "a\nb");
  302. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b'", StringWriterText);
  303. xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
  304. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b' fuga=' a\t&#xD;&#xA;b\t'", StringWriterText);
  305. }
  306. [Test]
  307. [ExpectedException (typeof (InvalidOperationException))]
  308. public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
  309. {
  310. xtw.WriteStartElement ("foo");
  311. xtw.WriteString ("bar");
  312. xtw.WriteAttributeString ("baz", "quux");
  313. }
  314. [Test]
  315. public void AttributeWriteAttributeStringWithoutParentElement ()
  316. {
  317. xtw.WriteAttributeString ("foo", "bar");
  318. Assert.AreEqual ("foo='bar'", StringWriterText);
  319. xtw.WriteAttributeString ("baz", "quux");
  320. Assert.AreEqual ("foo='bar' baz='quux'", StringWriterText);
  321. }
  322. [Test]
  323. public void WriteStartElement_EmptyLocalName ()
  324. {
  325. xtw.WriteStartElement ("", "");
  326. Assert.AreEqual ("<", StringWriterText, "#1");
  327. sw.GetStringBuilder ().Length = 0;
  328. CreateXmlTextWriter ();
  329. xtw.WriteStartElement ("", "", "");
  330. Assert.AreEqual ("<", StringWriterText, "#2");
  331. sw.GetStringBuilder ().Length = 0;
  332. CreateXmlTextWriter ();
  333. xtw.WriteStartElement ("", "", "http://somenamespace.com");
  334. Assert.AreEqual ("<", StringWriterText, "#3");
  335. sw.GetStringBuilder ().Length = 0;
  336. CreateXmlTextWriter ();
  337. xtw.WriteStartElement ("x", "", "http://somenamespace.com");
  338. Assert.AreEqual ("<x:", StringWriterText, "#4");
  339. sw.GetStringBuilder ().Length = 0;
  340. CreateXmlTextWriter ();
  341. xtw.WriteStartElement ("", null);
  342. Assert.AreEqual ("<", StringWriterText, "#5");
  343. sw.GetStringBuilder ().Length = 0;
  344. CreateXmlTextWriter ();
  345. xtw.WriteStartElement ("", null, "");
  346. Assert.AreEqual ("<", StringWriterText, "#6");
  347. sw.GetStringBuilder ().Length = 0;
  348. CreateXmlTextWriter ();
  349. xtw.WriteStartElement ("", null, "http://somenamespace.com");
  350. Assert.AreEqual ("<", StringWriterText, "#7");
  351. sw.GetStringBuilder ().Length = 0;
  352. CreateXmlTextWriter ();
  353. xtw.WriteStartElement ("x", null, "http://somenamespace.com");
  354. Assert.AreEqual ("<x:", StringWriterText, "#8");
  355. }
  356. [Test]
  357. public void WriteElementString_EmptyLocalName ()
  358. {
  359. xtw.WriteElementString ("", "");
  360. Assert.AreEqual ("< />", StringWriterText, "#1");
  361. sw.GetStringBuilder ().Length = 0;
  362. CreateXmlTextWriter ();
  363. xtw.WriteElementString ("", "", "");
  364. Assert.AreEqual ("< />", StringWriterText, "#2");
  365. sw.GetStringBuilder ().Length = 0;
  366. CreateXmlTextWriter ();
  367. xtw.WriteElementString ("", "http://somenamespace.com", "whatever");
  368. Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#3");
  369. sw.GetStringBuilder ().Length = 0;
  370. CreateXmlTextWriter ();
  371. xtw.WriteElementString ("", "http://somenamespace.com", "");
  372. Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#4");
  373. sw.GetStringBuilder ().Length = 0;
  374. CreateXmlTextWriter ();
  375. xtw.WriteElementString (null, null);
  376. Assert.AreEqual ("< />", StringWriterText, "#5");
  377. sw.GetStringBuilder ().Length = 0;
  378. CreateXmlTextWriter ();
  379. xtw.WriteElementString (null, null, null);
  380. Assert.AreEqual ("< />", StringWriterText, "#6");
  381. sw.GetStringBuilder ().Length = 0;
  382. CreateXmlTextWriter ();
  383. xtw.WriteElementString (null, "http://somenamespace.com", "whatever");
  384. Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#7");
  385. sw.GetStringBuilder ().Length = 0;
  386. CreateXmlTextWriter ();
  387. xtw.WriteElementString (null, "http://somenamespace.com", null);
  388. Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#8");
  389. }
  390. [Test]
  391. #if ONLY_1_1
  392. [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
  393. #endif
  394. public void WriteStartElement_Prefix_EmptyNamespace ()
  395. {
  396. xtw.WriteStartElement ("x", "whatever", "");
  397. Assert.AreEqual ("<whatever", StringWriterText, "#1");
  398. xtw.WriteEndElement ();
  399. Assert.AreEqual ("<whatever />", StringWriterText, "#2");
  400. }
  401. [Test]
  402. [ExpectedException (typeof (ArgumentException))]
  403. public void WriteStartElement_Prefix_NullNamespace ()
  404. {
  405. xtw.WriteStartElement ("x", "whatever", null);
  406. }
  407. [Test]
  408. [Category ("NotWorking")]
  409. public void WriteStartElement_XmlPrefix ()
  410. {
  411. // uncomment the next code block once bug #77094 has been fixed.
  412. /*
  413. xtw.WriteStartElement ("xml", "something", "http://www.w3.org/XML/1998/namespace");
  414. Assert.AreEqual ("<xml:something", StringWriterText, "#1");
  415. sw.GetStringBuilder ().Length = 0;
  416. CreateXmlTextWriter ();
  417. xtw.WriteStartElement ("XmL", null, "http://www.w3.org/XML/1998/namespace");
  418. Assert.AreEqual ("<XmL:", StringWriterText, "#2");
  419. sw.GetStringBuilder ().Length = 0;
  420. CreateXmlTextWriter ();
  421. xtw.WriteStartElement ("xmlsomething", "name", "http://www.w3.org/XML/1998/namespace");
  422. Assert.AreEqual ("<xmlsomething:name", StringWriterText, "#3");
  423. sw.GetStringBuilder ().Length = 0;
  424. CreateXmlTextWriter ();
  425. */
  426. }
  427. [Test]
  428. [ExpectedException (typeof (ArgumentException))]
  429. public void WriteStartElement_XmlPrefix_Invalid1 ()
  430. {
  431. xtw.WriteStartElement ("xml", null, "http://somenamespace.com");
  432. }
  433. [Test]
  434. [Category ("NotWorking")] // bug #77094
  435. [ExpectedException (typeof (ArgumentException))]
  436. public void WriteStartElement_XmlPrefix_Invalid2 ()
  437. {
  438. xtw.WriteStartElement ("XmL", null, "http://somenamespace.com");
  439. }
  440. [Test]
  441. public void WriteStartElement_XmlPrefix_Invalid3 ()
  442. {
  443. // from XML 1.0 (third edition) specification:
  444. //
  445. // [Definition: A Name is a token beginning with a letter or one of a
  446. // few punctuation characters, and continuing with letters, digits,
  447. // hyphens, underscores, colons, or full stops, together known as name
  448. // characters.] Names beginning with the string "xml", or with any string
  449. // which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for
  450. // standardization in this or future versions of this specification.
  451. //
  452. // from the Namespaces in XML 1.0 specification:
  453. //
  454. // Prefixes beginning with the three-letter sequence x, m, l, in any case
  455. // combination, are reserved for use by XML and XML-related specifications.
  456. //
  457. // should this prefix then not be considered invalid ?
  458. //
  459. // both Mono and MS.NET 1.x/2.0 accept it though
  460. xtw.WriteStartElement ("xmlsomething", null, "http://somenamespace.com");
  461. Assert.AreEqual ("<xmlsomething:", StringWriterText, "#1");
  462. sw.GetStringBuilder ().Length = 0;
  463. CreateXmlTextWriter ();
  464. xtw.WriteStartElement ("XmLsomething", null, "http://somenamespace.com");
  465. Assert.AreEqual ("<XmLsomething:", StringWriterText, "#2");
  466. }
  467. [Test]
  468. public void CDataValid ()
  469. {
  470. xtw.WriteCData ("foo");
  471. Assert.AreEqual ("<![CDATA[foo]]>", StringWriterText, "WriteCData had incorrect output.");
  472. }
  473. [Test]
  474. public void CDataNull ()
  475. {
  476. xtw.WriteCData (null);
  477. Assert.AreEqual ("<![CDATA[]]>", StringWriterText, "WriteCData had incorrect output.");
  478. }
  479. [Test]
  480. [ExpectedException (typeof (ArgumentException))]
  481. public void CDataInvalid ()
  482. {
  483. xtw.WriteCData("foo]]>bar");
  484. }
  485. [Test]
  486. public void CloseOpenElements ()
  487. {
  488. xtw.WriteStartElement("foo");
  489. xtw.WriteStartElement("bar");
  490. xtw.WriteStartElement("baz");
  491. xtw.Close();
  492. Assert.AreEqual ("<foo><bar><baz /></bar></foo>", StringWriterText,
  493. "Close didn't write out end elements properly.");
  494. }
  495. [Test]
  496. public void CloseWriteAfter ()
  497. {
  498. xtw.WriteElementString ("foo", "bar");
  499. xtw.Close ();
  500. // WriteEndElement and WriteStartDocument aren't tested here because
  501. // they will always throw different exceptions besides 'The Writer is closed.'
  502. // and there are already tests for those exceptions.
  503. try {
  504. xtw.WriteCData ("foo");
  505. Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  506. } catch (InvalidOperationException) {
  507. // Don't rely on English message assertion.
  508. // It is enough to check an exception occurs.
  509. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  510. }
  511. try {
  512. xtw.WriteComment ("foo");
  513. Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  514. } catch (InvalidOperationException) {
  515. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  516. }
  517. try {
  518. xtw.WriteProcessingInstruction ("foo", "bar");
  519. Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  520. } catch (InvalidOperationException) {
  521. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  522. }
  523. try {
  524. xtw.WriteStartElement ("foo", "bar", "baz");
  525. Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  526. } catch (InvalidOperationException) {
  527. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  528. }
  529. try {
  530. xtw.WriteAttributeString ("foo", "bar");
  531. Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  532. } catch (InvalidOperationException) {
  533. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  534. }
  535. try {
  536. xtw.WriteString ("foo");
  537. Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  538. } catch (InvalidOperationException) {
  539. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  540. }
  541. }
  542. [Test]
  543. public void CommentValid ()
  544. {
  545. xtw.WriteComment ("foo");
  546. Assert.AreEqual ("<!--foo-->", StringWriterText, "WriteComment had incorrect output.");
  547. }
  548. [Test]
  549. public void CommentInvalid ()
  550. {
  551. try {
  552. xtw.WriteComment("foo-");
  553. Assert.Fail("Should have thrown an ArgumentException.");
  554. } catch (ArgumentException) { }
  555. try {
  556. xtw.WriteComment("foo-->bar");
  557. Assert.Fail("Should have thrown an ArgumentException.");
  558. } catch (ArgumentException) { }
  559. }
  560. [Test]
  561. public void ConstructorsAndBaseStream ()
  562. {
  563. Assert.IsTrue (Object.ReferenceEquals (null, this.xtw.BaseStream), "BaseStream property returned wrong value.");
  564. MemoryStream ms;
  565. StreamReader sr;
  566. XmlTextWriter xtw;
  567. ms = new MemoryStream ();
  568. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  569. xtw.WriteStartDocument ();
  570. xtw.Flush ();
  571. ms.Seek (0, SeekOrigin.Begin);
  572. sr = new StreamReader (ms, Encoding.Unicode);
  573. string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  574. string actualXmlDeclaration = sr.ReadToEnd();
  575. Assert.AreEqual (expectedXmlDeclaration, actualXmlDeclaration);
  576. Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
  577. ms = new MemoryStream ();
  578. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  579. xtw.WriteStartDocument (true);
  580. xtw.Flush ();
  581. ms.Seek (0, SeekOrigin.Begin);
  582. sr = new StreamReader (ms, Encoding.Unicode);
  583. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  584. ms = new MemoryStream ();
  585. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  586. xtw.WriteStartDocument ();
  587. xtw.Flush ();
  588. ms.Seek (0, SeekOrigin.Begin);
  589. sr = new StreamReader (ms, Encoding.UTF8);
  590. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  591. ms = new MemoryStream ();
  592. xtw = new XmlTextWriter (ms, null);
  593. xtw.WriteStartDocument ();
  594. xtw.Flush ();
  595. ms.Seek (0, SeekOrigin.Begin);
  596. sr = new StreamReader (ms, Encoding.UTF8);
  597. Assert.AreEqual ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  598. ms = new MemoryStream ();
  599. xtw = new XmlTextWriter (ms, null);
  600. xtw.WriteStartDocument (true);
  601. xtw.Flush ();
  602. ms.Seek (0, SeekOrigin.Begin);
  603. sr = new StreamReader (ms, Encoding.UTF8);
  604. Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  605. Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
  606. }
  607. [Test]
  608. public void DocumentStart ()
  609. {
  610. xtw.WriteStartDocument ();
  611. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", StringWriterText,
  612. "XmlDeclaration is incorrect.");
  613. try {
  614. xtw.WriteStartDocument ();
  615. Assert.Fail("Should have thrown an InvalidOperationException.");
  616. } catch (InvalidOperationException) {
  617. // Don't rely on English message assertion.
  618. // It is enough to check an exception occurs.
  619. // Assert.AreEqual ("WriteStartDocument should be the first call.", e.Message, "Exception message is incorrect.");
  620. }
  621. xtw = new XmlTextWriter (sw = new StringWriter ());
  622. xtw.QuoteChar = '\'';
  623. xtw.WriteStartDocument (true);
  624. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
  625. xtw = new XmlTextWriter (sw = new StringWriter ());
  626. xtw.QuoteChar = '\'';
  627. xtw.WriteStartDocument (false);
  628. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
  629. }
  630. [Test]
  631. public void ElementAndAttributeSameXmlns ()
  632. {
  633. xtw.WriteStartElement ("ped", "foo", "urn:foo");
  634. xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
  635. xtw.WriteEndElement ();
  636. Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
  637. }
  638. [Test]
  639. [Category ("NotDotNet")]
  640. public void ElementXmlnsNeedEscape ()
  641. {
  642. xtw.WriteStartElement ("test", "foo", "'");
  643. xtw.WriteEndElement ();
  644. // MS.NET output is : xmlns:test='''
  645. Assert.AreEqual ("<test:foo xmlns:test='&apos;' />", StringWriterText);
  646. }
  647. [Test]
  648. public void ElementEmpty ()
  649. {
  650. xtw.WriteStartElement ("foo");
  651. xtw.WriteEndElement ();
  652. Assert.AreEqual ("<foo />", StringWriterText, "Incorrect output.");
  653. }
  654. [Test]
  655. public void ElementWriteElementString ()
  656. {
  657. xtw.WriteElementString ("foo", "bar");
  658. Assert.AreEqual ("<foo>bar</foo>", StringWriterText, "WriteElementString has incorrect output.");
  659. xtw.WriteElementString ("baz", "");
  660. Assert.AreEqual ("<foo>bar</foo><baz />", StringWriterText);
  661. xtw.WriteElementString ("quux", null);
  662. Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText);
  663. xtw.WriteElementString ("", "quuux");
  664. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  665. xtw.WriteElementString (null, "quuuux");
  666. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  667. }
  668. [Test]
  669. public void FormattingTest ()
  670. {
  671. xtw.Formatting = Formatting.Indented;
  672. xtw.WriteStartDocument ();
  673. xtw.WriteStartElement ("foo");
  674. xtw.WriteElementString ("bar", "");
  675. xtw.Close ();
  676. Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
  677. }
  678. [Test]
  679. public void FormattingInvalidXmlForFun ()
  680. {
  681. xtw.Formatting = Formatting.Indented;
  682. xtw.IndentChar = 'x';
  683. xtw.WriteStartDocument ();
  684. xtw.WriteStartElement ("foo");
  685. xtw.WriteStartElement ("bar");
  686. xtw.WriteElementString ("baz", "");
  687. xtw.Close ();
  688. Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
  689. }
  690. [Test]
  691. public void FormattingFromRemarks ()
  692. {
  693. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  694. xtw.Formatting = Formatting.Indented;
  695. xtw.WriteStartElement ("ol");
  696. xtw.WriteStartElement ("li");
  697. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  698. xtw.WriteElementString ("b", "E");
  699. xtw.WriteElementString ("i", "lephant");
  700. xtw.WriteString (" walks slowly.");
  701. xtw.WriteEndElement ();
  702. xtw.WriteEndElement ();
  703. Assert.AreEqual (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
  704. }
  705. [Test]
  706. public void LookupPrefix ()
  707. {
  708. xtw.WriteStartElement ("root");
  709. xtw.WriteStartElement ("one");
  710. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  711. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  712. Assert.AreEqual ("foo", xtw.LookupPrefix ("http://abc.def"));
  713. Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  714. xtw.WriteEndElement ();
  715. xtw.WriteStartElement ("two");
  716. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  717. xtw.WriteString("quux");
  718. Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  719. Assert.IsNull (xtw.LookupPrefix ("http://abc.def"));
  720. Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"));
  721. Assert.IsNull (xtw.LookupPrefix ("http://bogus"));
  722. }
  723. [Test]
  724. public void NamespacesAttributesPassingInNamespaces ()
  725. {
  726. xtw.Namespaces = false;
  727. xtw.WriteStartElement ("foo");
  728. // These shouldn't throw any exceptions since they don't pass in
  729. // a namespace.
  730. xtw.WriteAttributeString ("bar", "baz");
  731. xtw.WriteAttributeString ("", "a", "", "b");
  732. xtw.WriteAttributeString (null, "c", "", "d");
  733. xtw.WriteAttributeString ("", "e", null, "f");
  734. xtw.WriteAttributeString (null, "g", null, "h");
  735. Assert.AreEqual ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  736. }
  737. [Test]
  738. public void NamespacesElementsPassingInNamespaces ()
  739. {
  740. xtw.Namespaces = false;
  741. // These shouldn't throw any exceptions since they don't pass in
  742. // a namespace.
  743. xtw.WriteElementString ("foo", "bar");
  744. xtw.WriteStartElement ("baz");
  745. xtw.WriteStartElement ("quux", "");
  746. xtw.WriteStartElement ("quuux", null);
  747. xtw.WriteStartElement (null, "a", null);
  748. xtw.WriteStartElement (null, "b", "");
  749. xtw.WriteStartElement ("", "c", null);
  750. xtw.WriteStartElement ("", "d", "");
  751. Assert.AreEqual ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  752. }
  753. [Test]
  754. [ExpectedException (typeof (ArgumentException))]
  755. public void NamespacesElementsPassingInNamespacesInvalid1 ()
  756. {
  757. // These should throw ArgumentException because they pass in a
  758. // namespace when Namespaces = false.
  759. xtw.Namespaces = false;
  760. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  761. }
  762. [Test]
  763. [ExpectedException (typeof (ArgumentException))]
  764. public void NamespacesElementsPassingInNamespacesInvalid2 ()
  765. {
  766. xtw.Namespaces = false;
  767. xtw.WriteStartElement ("foo", "http://netsack.com/");
  768. }
  769. [Test]
  770. [ExpectedException (typeof (ArgumentException))]
  771. public void NamespacesElementsPassingInNamespacesInvalid3 ()
  772. {
  773. xtw.Namespaces = false;
  774. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  775. }
  776. [Test]
  777. [ExpectedException (typeof (ArgumentException))]
  778. public void NamespacesElementsPassingInNamespacesInvalid4 ()
  779. {
  780. xtw.Namespaces = false;
  781. xtw.WriteStartElement ("foo", "bar", null);
  782. }
  783. [Test]
  784. [ExpectedException (typeof (ArgumentException))]
  785. public void NamespacesElementsPassingInNamespacesInvalid5 ()
  786. {
  787. xtw.Namespaces = false;
  788. xtw.WriteStartElement ("foo", "bar", "");
  789. }
  790. [Test]
  791. [ExpectedException (typeof (ArgumentException))]
  792. public void NamespacesElementsPassingInNamespacesInvalid6 ()
  793. {
  794. xtw.Namespaces = false;
  795. xtw.WriteStartElement ("foo", "", "");
  796. }
  797. [Test]
  798. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  799. {
  800. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  801. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  802. xtw.WriteElementString("baz", String.Empty, String.Empty);
  803. xtw.WriteEndElement();
  804. xtw.WriteEndElement();
  805. Assert.AreEqual ("<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
  806. StringWriterText, "XmlTextWriter is incorrectly outputting namespaces.");
  807. }
  808. [Test]
  809. public void NamespacesPrefix ()
  810. {
  811. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  812. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  813. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  814. xtw.WriteEndElement ();
  815. xtw.WriteEndElement ();
  816. Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
  817. StringWriterText, "XmlTextWriter is incorrectly outputting prefixes.");
  818. }
  819. [Test]
  820. #if ONLY_1_1
  821. [Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
  822. #endif
  823. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  824. {
  825. xtw.WriteStartElement ("foo", "bar", "");
  826. }
  827. [Test]
  828. [ExpectedException (typeof (ArgumentException))]
  829. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  830. {
  831. xtw.WriteStartElement ("foo", "bar", null);
  832. }
  833. [Test]
  834. public void NamespacesSettingWhenWriteStateNotStart ()
  835. {
  836. xtw.WriteStartElement ("foo");
  837. try {
  838. xtw.Namespaces = false;
  839. Assert.Fail ("Expected an InvalidOperationException.");
  840. } catch (InvalidOperationException) {}
  841. Assert.IsTrue (xtw.Namespaces);
  842. }
  843. [Test]
  844. public void ProcessingInstructionValid ()
  845. {
  846. xtw.WriteProcessingInstruction("foo", "bar");
  847. Assert.AreEqual ("<?foo bar?>", StringWriterText, "WriteProcessingInstruction had incorrect output.");
  848. }
  849. [Test]
  850. [ExpectedException (typeof (ArgumentException))]
  851. public void ProcessingInstructionInvalid1 ()
  852. {
  853. xtw.WriteProcessingInstruction("fo?>o", "bar");
  854. }
  855. [Test]
  856. [ExpectedException (typeof (ArgumentException))]
  857. public void ProcessingInstructionInvalid2 ()
  858. {
  859. xtw.WriteProcessingInstruction("foo", "ba?>r");
  860. }
  861. [Test]
  862. [ExpectedException (typeof (ArgumentException))]
  863. public void ProcessingInstructionInvalid3 ()
  864. {
  865. xtw.WriteProcessingInstruction("", "bar");
  866. }
  867. [Test]
  868. [ExpectedException (typeof (ArgumentException))]
  869. public void ProcessingInstructionInvalid4 ()
  870. {
  871. xtw.WriteProcessingInstruction(null, "bar");
  872. }
  873. [Test]
  874. public void QuoteCharDoubleQuote ()
  875. {
  876. xtw.QuoteChar = '"';
  877. // version, encoding, standalone
  878. xtw.WriteStartDocument (true);
  879. // namespace declaration
  880. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  881. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  882. }
  883. [Test]
  884. [ExpectedException (typeof (ArgumentException))]
  885. public void QuoteCharInvalid ()
  886. {
  887. xtw.QuoteChar = 'x';
  888. }
  889. [Test]
  890. public void WriteBase64 ()
  891. {
  892. UTF8Encoding encoding = new UTF8Encoding();
  893. byte[] fooBar = encoding.GetBytes("foobar");
  894. xtw.WriteBase64 (fooBar, 0, 6);
  895. Assert.AreEqual ("Zm9vYmFy", StringWriterText);
  896. try {
  897. xtw.WriteBase64 (fooBar, 3, 6);
  898. Assert.Fail ("Expected an Argument Exception to be thrown.");
  899. } catch (ArgumentException) {}
  900. try {
  901. xtw.WriteBase64 (fooBar, -1, 6);
  902. Assert.Fail ("Expected an Argument Exception to be thrown.");
  903. } catch (ArgumentOutOfRangeException) {}
  904. try {
  905. xtw.WriteBase64 (fooBar, 3, -1);
  906. Assert.Fail ("Expected an Argument Exception to be thrown.");
  907. } catch (ArgumentOutOfRangeException) {}
  908. try {
  909. xtw.WriteBase64 (null, 0, 6);
  910. Assert.Fail ("Expected an Argument Exception to be thrown.");
  911. } catch (ArgumentNullException) {}
  912. }
  913. [Test]
  914. public void WriteBinHex ()
  915. {
  916. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  917. xtw.WriteBinHex (bytes, 0, 11);
  918. Assert.AreEqual ("040E22365E7286C2FF0005", StringWriterText);
  919. }
  920. [Test]
  921. public void WriteCharEntity ()
  922. {
  923. xtw.WriteCharEntity ('a');
  924. Assert.AreEqual ("&#x61;", StringWriterText);
  925. xtw.WriteCharEntity ('A');
  926. Assert.AreEqual ("&#x61;&#x41;", StringWriterText);
  927. xtw.WriteCharEntity ('1');
  928. Assert.AreEqual ("&#x61;&#x41;&#x31;", StringWriterText);
  929. xtw.WriteCharEntity ('K');
  930. Assert.AreEqual ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  931. try {
  932. xtw.WriteCharEntity ((char)0xd800);
  933. } catch (ArgumentException) {}
  934. }
  935. [Test]
  936. [ExpectedException (typeof (InvalidOperationException))]
  937. public void WriteEndAttribute ()
  938. {
  939. xtw.WriteEndAttribute ();
  940. }
  941. [Test]
  942. public void WriteEndDocument ()
  943. {
  944. try {
  945. xtw.WriteEndDocument ();
  946. Assert.Fail ("Expected an Exception.");
  947. // in .NET 2.0 it is InvalidOperationException.
  948. // in .NET 1,1 it is ArgumentException.
  949. } catch (Exception) {}
  950. }
  951. [Test]
  952. public void WriteEndDocument2 ()
  953. {
  954. xtw.WriteStartDocument ();
  955. try
  956. {
  957. xtw.WriteEndDocument ();
  958. Assert.Fail ("Expected an Exception.");
  959. // in .NET 2.0 it is InvalidOperationException.
  960. // in .NET 1,1 it is ArgumentException.
  961. } catch (Exception) {}
  962. }
  963. [Test]
  964. public void WriteEndDocument3 ()
  965. {
  966. xtw.WriteStartDocument ();
  967. xtw.WriteStartElement ("foo");
  968. xtw.WriteStartAttribute ("bar", null);
  969. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  970. xtw.WriteEndDocument ();
  971. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  972. Assert.AreEqual (WriteState.Start, xtw.WriteState);
  973. }
  974. [Test]
  975. [ExpectedException (typeof (InvalidOperationException))]
  976. public void WriteEndElement ()
  977. {
  978. // no matching StartElement
  979. xtw.WriteEndElement ();
  980. }
  981. [Test]
  982. public void WriteEndElement2 ()
  983. {
  984. xtw.WriteStartElement ("foo");
  985. xtw.WriteEndElement ();
  986. Assert.AreEqual ("<foo />", StringWriterText);
  987. xtw.WriteStartElement ("bar");
  988. xtw.WriteStartAttribute ("baz", null);
  989. xtw.WriteEndElement ();
  990. Assert.AreEqual ("<foo /><bar baz='' />", StringWriterText);
  991. }
  992. [Test]
  993. public void FullEndElement ()
  994. {
  995. xtw.WriteStartElement ("foo");
  996. xtw.WriteFullEndElement ();
  997. Assert.AreEqual ("<foo></foo>", StringWriterText);
  998. xtw.WriteStartElement ("bar");
  999. xtw.WriteAttributeString ("foo", "bar");
  1000. xtw.WriteFullEndElement ();
  1001. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  1002. xtw.WriteStartElement ("baz");
  1003. xtw.WriteStartAttribute ("bar", null);
  1004. xtw.WriteFullEndElement ();
  1005. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  1006. }
  1007. [Test]
  1008. public void WriteQualifiedName ()
  1009. {
  1010. xtw.WriteStartElement (null, "test", null);
  1011. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  1012. xtw.WriteQualifiedName ("bob", "http://localhost/");
  1013. xtw.WriteEndElement ();
  1014. Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  1015. }
  1016. [Test]
  1017. public void WriteQualifiedNameNonDeclaredAttribute ()
  1018. {
  1019. xtw.WriteStartElement ("foo");
  1020. xtw.WriteStartAttribute ("a", "");
  1021. xtw.WriteQualifiedName ("attr", "urn:a");
  1022. xtw.WriteWhitespace (" ");
  1023. xtw.WriteQualifiedName ("attr", "urn:b");
  1024. xtw.WriteEndAttribute ();
  1025. xtw.WriteEndElement ();
  1026. string xml = sw.ToString ();
  1027. Assert.IsTrue (xml.IndexOf ("<foo ") >= 0, "foo");
  1028. Assert.IsTrue (xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0, "qnames");
  1029. Assert.IsTrue (xml.IndexOf (" xmlns:d1p1='urn:a'") > 0, "xmlns:a");
  1030. Assert.IsTrue (xml.IndexOf (" xmlns:d1p2='urn:b'") > 0, "xmlns:b");
  1031. }
  1032. [Test]
  1033. [ExpectedException (typeof (ArgumentException))]
  1034. public void WriteQualifiedNameNonDeclaredContent ()
  1035. {
  1036. xtw.WriteStartElement ("foo");
  1037. xtw.WriteQualifiedName ("abc", "urn:abc");
  1038. }
  1039. [Test]
  1040. [ExpectedException (typeof (ArgumentException))]
  1041. public void WriteQualifiedNameNonNCName ()
  1042. {
  1043. xtw.WriteStartElement ("foo");
  1044. xtw.WriteAttributeString ("xmlns", "urn:default");
  1045. xtw.WriteStartElement ("child");
  1046. xtw.WriteStartAttribute ("a", "");
  1047. xtw.WriteQualifiedName ("x:def", "urn:def");
  1048. }
  1049. [Test]
  1050. public void WriteRaw ()
  1051. {
  1052. xtw.WriteRaw("&<>\"'");
  1053. Assert.AreEqual ("&<>\"'", StringWriterText);
  1054. xtw.WriteRaw(null);
  1055. Assert.AreEqual ("&<>\"'", StringWriterText);
  1056. xtw.WriteRaw("");
  1057. Assert.AreEqual ("&<>\"'", StringWriterText);
  1058. }
  1059. [Test]
  1060. public void WriteRawInvalidInAttribute ()
  1061. {
  1062. xtw.WriteStartElement ("foo");
  1063. xtw.WriteStartAttribute ("bar", null);
  1064. xtw.WriteRaw ("&<>\"'");
  1065. xtw.WriteEndAttribute ();
  1066. xtw.WriteEndElement ();
  1067. Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
  1068. }
  1069. [Test]
  1070. public void WriteStateTest ()
  1071. {
  1072. Assert.AreEqual (WriteState.Start, xtw.WriteState);
  1073. xtw.WriteStartDocument ();
  1074. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1075. xtw.WriteStartElement ("root");
  1076. Assert.AreEqual (WriteState.Element, xtw.WriteState);
  1077. xtw.WriteElementString ("foo", "bar");
  1078. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1079. xtw.Close ();
  1080. Assert.AreEqual (WriteState.Closed, xtw.WriteState);
  1081. }
  1082. [Test]
  1083. public void WriteString ()
  1084. {
  1085. xtw.WriteStartDocument ();
  1086. try {
  1087. xtw.WriteString("foo");
  1088. } catch (InvalidOperationException) {}
  1089. }
  1090. [Test]
  1091. public void WriteString2 ()
  1092. {
  1093. xtw.WriteStartDocument ();
  1094. // Testing attribute values
  1095. xtw.WriteStartElement ("foo");
  1096. xtw.WriteAttributeString ("bar", "&<>");
  1097. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  1098. }
  1099. [Test]
  1100. public void WriteAttributeStringSingleQuoteChar()
  1101. {
  1102. // When QuoteChar is single quote then replaces single quotes within attributes
  1103. // but not double quotes.
  1104. xtw.WriteStartElement ("foo");
  1105. xtw.WriteAttributeString ("bar", "\"baz\"");
  1106. xtw.WriteAttributeString ("quux", "'baz'");
  1107. Assert.AreEqual ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  1108. }
  1109. [Test]
  1110. public void WriteAttributeStringDoubleQuoteChar()
  1111. {
  1112. // When QuoteChar is double quote then replaces double quotes within attributes
  1113. // but not single quotes.
  1114. xtw.QuoteChar = '"';
  1115. xtw.WriteStartElement ("foo");
  1116. xtw.WriteAttributeString ("bar", "\"baz\"");
  1117. xtw.WriteAttributeString ("quux", "'baz'");
  1118. Assert.AreEqual ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  1119. }
  1120. [Test]
  1121. public void WriteStringWithEntities()
  1122. {
  1123. // Testing element values
  1124. xtw.QuoteChar = '\'';
  1125. xtw.WriteElementString ("foo", "&<>\"'");
  1126. Assert.AreEqual ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  1127. }
  1128. [Test]
  1129. public void XmlLang ()
  1130. {
  1131. Assert.IsNull (xtw.XmlLang);
  1132. xtw.WriteStartElement ("foo");
  1133. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  1134. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1135. Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
  1136. xtw.WriteAttributeString ("boo", "yah");
  1137. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1138. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  1139. xtw.WriteElementString("bar", "baz");
  1140. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1141. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  1142. xtw.WriteString("baz");
  1143. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1144. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  1145. xtw.WriteStartElement ("quux");
  1146. xtw.WriteStartAttribute ("xml", "lang", null);
  1147. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1148. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1149. xtw.WriteString("langbar");
  1150. // Commented out there: it is implementation-dependent.
  1151. // and incompatible between .NET 1.0 and 1.1
  1152. // Assert.AreEqual ("langfoo", xtw.XmlLang);
  1153. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1154. xtw.WriteEndAttribute ();
  1155. // Commented out there: it is implementation-dependent.
  1156. // and incompatible between .NET 1.0 and 1.1
  1157. // Assert.AreEqual ("langbar", xtw.XmlLang);
  1158. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  1159. // check if xml:lang repeats output even if same as current scope.
  1160. xtw.WriteStartElement ("joe");
  1161. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  1162. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  1163. xtw.WriteElementString ("quuux", "squonk");
  1164. Assert.AreEqual ("langbar", xtw.XmlLang);
  1165. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  1166. xtw.WriteEndElement ();
  1167. xtw.WriteEndElement ();
  1168. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1169. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", StringWriterText);
  1170. xtw.WriteEndElement ();
  1171. Assert.IsNull (xtw.XmlLang);
  1172. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", StringWriterText);
  1173. xtw.Close ();
  1174. Assert.IsNull (xtw.XmlLang);
  1175. }
  1176. // TODO: test operational aspects
  1177. [Test]
  1178. public void XmlSpaceTest ()
  1179. {
  1180. xtw.WriteStartElement ("foo");
  1181. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1182. xtw.WriteStartElement ("bar");
  1183. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1184. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1185. Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText);
  1186. xtw.WriteStartElement ("baz");
  1187. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1188. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1189. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  1190. xtw.WriteStartElement ("quux");
  1191. xtw.WriteStartAttribute ("xml", "space", null);
  1192. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1193. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  1194. // Commented out there: it is implementation-dependent
  1195. // and incompatible between .NET 1.0 and 1.1
  1196. xtw.WriteString ("default");
  1197. // Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1198. // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  1199. xtw.WriteEndAttribute ();
  1200. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
  1201. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  1202. xtw.WriteEndElement ();
  1203. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1204. xtw.WriteEndElement ();
  1205. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1206. xtw.WriteEndElement ();
  1207. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1208. xtw.WriteStartElement ("quux");
  1209. }
  1210. [Test]
  1211. [ExpectedException (typeof (ArgumentException))]
  1212. public void XmlSpaceTestInvalidValue1 ()
  1213. {
  1214. xtw.WriteStartElement ("foo");
  1215. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1216. }
  1217. [Test]
  1218. [ExpectedException (typeof (ArgumentException))]
  1219. public void XmlSpaceTestInvalidValue2 ()
  1220. {
  1221. xtw.WriteStartElement ("foo");
  1222. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  1223. }
  1224. [Test]
  1225. [ExpectedException (typeof (ArgumentException))]
  1226. public void XmlSpaceTestInvalidValue3 ()
  1227. {
  1228. xtw.WriteStartElement ("foo");
  1229. xtw.WriteAttributeString ("xml", "space", null, "Default");
  1230. }
  1231. [Test]
  1232. [ExpectedException (typeof (ArgumentException))]
  1233. public void XmlSpaceTestInvalidValue4 ()
  1234. {
  1235. xtw.WriteStartElement ("foo");
  1236. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1237. }
  1238. [Test]
  1239. [ExpectedException (typeof (ArgumentException))]
  1240. public void WriteWhitespaceNonWhitespace ()
  1241. {
  1242. xtw.WriteWhitespace ("x");
  1243. }
  1244. [Test]
  1245. [ExpectedException (typeof (ArgumentException))]
  1246. public void WriteWhitespace_Null ()
  1247. {
  1248. xtw.WriteWhitespace ((string) null);
  1249. }
  1250. [Test]
  1251. [ExpectedException (typeof (ArgumentException))]
  1252. public void WriteWhitespace_Empty ()
  1253. {
  1254. xtw.WriteWhitespace (string.Empty);
  1255. }
  1256. [Test]
  1257. [ExpectedException (typeof (ArgumentException))]
  1258. public void WriteNmToken_Null ()
  1259. {
  1260. xtw.WriteNmToken ((string) null);
  1261. }
  1262. [Test]
  1263. [ExpectedException (typeof (ArgumentException))]
  1264. public void WriteNmToken_Empty ()
  1265. {
  1266. xtw.WriteNmToken (string.Empty);
  1267. }
  1268. [Test]
  1269. [Category ("NotWorking")] // on mono, an XmlException is thrown instead
  1270. [ExpectedException (typeof (ArgumentException))]
  1271. public void WriteNmToken_InvalidChars ()
  1272. {
  1273. xtw.WriteNmToken ("\uFFFF");
  1274. }
  1275. [Test]
  1276. public void WriteNmToken ()
  1277. {
  1278. xtw.WriteNmToken ("some:name");
  1279. Assert.AreEqual ("some:name", StringWriterText);
  1280. }
  1281. [Test]
  1282. public void XmlSpaceRaw ()
  1283. {
  1284. xtw.WriteStartElement ("foo");
  1285. xtw.WriteStartAttribute ("xml", "space", null);
  1286. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1287. Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1288. xtw.WriteString ("default");
  1289. // Commented out there: it is implementation-dependent
  1290. // and incompatible between .NET 1.0 and 1.1
  1291. // Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1292. // Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1293. xtw.WriteEndAttribute ();
  1294. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
  1295. Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
  1296. }
  1297. [Test]
  1298. public void WriteAttributes ()
  1299. {
  1300. XmlDocument doc = new XmlDocument();
  1301. StringWriter sw = new StringWriter();
  1302. XmlWriter wr = new XmlTextWriter(sw);
  1303. StringBuilder sb = sw.GetStringBuilder();
  1304. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1305. XmlTextReader xtr = new XmlTextReader("<?xml version='1.0' encoding='utf-8' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  1306. xtr.Read(); // read XMLDecl
  1307. wr.WriteAttributes(xtr, false);
  1308. // This method don't always have to take this double-quoted style...
  1309. Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
  1310. "#WriteAttributes.XmlDecl.1");
  1311. sb.Remove(0, sb.Length); // init
  1312. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1313. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  1314. xtr.Read(); // read XMLDecl
  1315. Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
  1316. sw = new StringWriter ();
  1317. wr = new XmlTextWriter (sw);
  1318. // This block raises an error on MS.NET 1.0.
  1319. wr.WriteAttributes(xtr, false);
  1320. // This method don't always have to take this double-quoted style...
  1321. Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
  1322. "#WriteAttributes.XmlDecl.2");
  1323. sw = new StringWriter ();
  1324. wr = new XmlTextWriter (sw);
  1325. sb.Remove(0, sb.Length); // init
  1326. xtr.Read(); // read root
  1327. Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
  1328. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  1329. wr.WriteAttributes(xtr, false);
  1330. wr.WriteEndElement();
  1331. wr.Close();
  1332. // This method don't always have to take this double-quoted style...
  1333. Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
  1334. "#WriteAttributes.Element");
  1335. xtr.Close ();
  1336. }
  1337. [Test]
  1338. public void WriteWhitespace ()
  1339. {
  1340. xtw.WriteStartElement ("a");
  1341. xtw.WriteWhitespace ("\n\t");
  1342. xtw.WriteStartElement ("b");
  1343. xtw.WriteWhitespace ("\n\t");
  1344. xtw.WriteEndElement ();
  1345. xtw.WriteWhitespace ("\n");
  1346. xtw.WriteEndElement ();
  1347. xtw.WriteWhitespace ("\n");
  1348. xtw.Flush ();
  1349. Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  1350. }
  1351. [Test]
  1352. public void FlushDoesntCloseTag ()
  1353. {
  1354. xtw.WriteStartElement ("foo");
  1355. xtw.WriteAttributeString ("bar", "baz");
  1356. xtw.Flush ();
  1357. Assert.AreEqual ("<foo bar='baz'", StringWriterText);
  1358. }
  1359. [Test]
  1360. public void WriteWhitespaceClosesTag ()
  1361. {
  1362. xtw.WriteStartElement ("foo");
  1363. xtw.WriteAttributeString ("bar", "baz");
  1364. xtw.WriteWhitespace (" ");
  1365. Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
  1366. }
  1367. [Test]
  1368. public void DontOutputMultipleXmlns ()
  1369. {
  1370. XmlDocument doc = new XmlDocument();
  1371. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  1372. XmlDocument doc2 = new XmlDocument();
  1373. doc2.LoadXml(doc.InnerXml);
  1374. Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  1375. doc2.OuterXml);
  1376. }
  1377. [Test]
  1378. public void DontOutputNonDeclaredXmlns ()
  1379. {
  1380. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  1381. XmlDocument doc = new XmlDocument();
  1382. doc.LoadXml(xml);
  1383. XmlDocument doc2 = new XmlDocument();
  1384. doc2.LoadXml(doc.InnerXml);
  1385. Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
  1386. }
  1387. [Test]
  1388. public void DontOutputRemovalDefaultNSDeclaration ()
  1389. {
  1390. xtw.WriteStartDocument ();
  1391. xtw.WriteStartElement ("foo");
  1392. xtw.WriteAttributeString ("xmlns", "probe");
  1393. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
  1394. xtw.WriteStartElement ("b");
  1395. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
  1396. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1397. xtw.WriteEndElement (); // b2
  1398. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1399. xtw.WriteEndElement (); // b2
  1400. xtw.WriteEndElement (); // b
  1401. xtw.WriteEndElement (); // foo
  1402. xtw.WriteEndDocument ();
  1403. xtw.Close ();
  1404. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText);
  1405. }
  1406. [Test]
  1407. public void DontOutputRemovalDefaultNSDeclaration2 ()
  1408. {
  1409. xtw.WriteStartDocument ();
  1410. // IMPORTANT DIFFERENCE!! ns = "", not null
  1411. xtw.WriteStartElement ("foo", "");
  1412. xtw.WriteAttributeString ("xmlns", "probe");
  1413. Assert.IsNull (xtw.LookupPrefix ("probe"));
  1414. xtw.WriteStartElement ("b");
  1415. Assert.IsNull (xtw.LookupPrefix ("probe"));
  1416. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1417. xtw.WriteEndElement (); // b2
  1418. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1419. xtw.WriteEndElement (); // b2
  1420. xtw.WriteEndElement (); // b
  1421. xtw.WriteEndElement (); // foo
  1422. xtw.WriteEndDocument ();
  1423. xtw.Close ();
  1424. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText);
  1425. }
  1426. [Test]
  1427. public void DoOutputRemovalDefaultNSDeclaration ()
  1428. {
  1429. xtw.WriteStartElement ("docelem", "a-namespace");
  1430. XmlDocument doc = new XmlDocument ();
  1431. doc.CreateElement ("hola").WriteTo (xtw);
  1432. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  1433. xtw.WriteEndElement ();
  1434. xtw.Close ();
  1435. Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  1436. }
  1437. [Test]
  1438. public void WriteAttributeTakePrecedenceOnXmlns ()
  1439. {
  1440. xtw.WriteStartElement ("root", "urn:foo");
  1441. xtw.WriteAttributeString ("xmlns", "urn:bar");
  1442. xtw.WriteEndElement ();
  1443. xtw.Close ();
  1444. Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
  1445. }
  1446. [Test]
  1447. [ExpectedException (typeof (ArgumentException))]
  1448. public void LookupPrefixNull ()
  1449. {
  1450. xtw.LookupPrefix (null);
  1451. }
  1452. [Test]
  1453. [ExpectedException (typeof (ArgumentException))]
  1454. public void LookupPrefixEmpty ()
  1455. {
  1456. xtw.LookupPrefix (String.Empty);
  1457. }
  1458. [Test]
  1459. public void LookupPrefixIgnoresXmlnsAttribute ()
  1460. {
  1461. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1462. xtw.WriteStartElement ("root");
  1463. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1464. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1465. // Surprisingly to say, it is ignored!!
  1466. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1467. xtw.WriteStartElement ("hoge");
  1468. // (still after flushing previous start element.)
  1469. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1470. xtw.WriteStartElement ("fuga", "urn:foo");
  1471. // Is this testing on the correct way? Yes, here it is.
  1472. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1473. }
  1474. [Test]
  1475. public void WriteInvalidNames ()
  1476. {
  1477. xtw.WriteStartElement ("foo<>");
  1478. xtw.WriteAttributeString ("ho<>ge", "value");
  1479. }
  1480. [Test]
  1481. [ExpectedException (typeof (ArgumentException))]
  1482. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1483. {
  1484. xtw.WriteStartAttribute ("some", "foo", null);
  1485. }
  1486. [Test]
  1487. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1488. {
  1489. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1490. }
  1491. [Test]
  1492. [ExpectedException (typeof (ArgumentException))]
  1493. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1494. {
  1495. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1496. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1497. xtw.WriteEndAttribute ();
  1498. }
  1499. [Test]
  1500. [ExpectedException (typeof (ArgumentException))]
  1501. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1502. {
  1503. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1504. }
  1505. [Test]
  1506. [ExpectedException (typeof (ArgumentException))]
  1507. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1508. {
  1509. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1510. }
  1511. [Test]
  1512. public void WriteRawProceedToProlog ()
  1513. {
  1514. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1515. xtw.WriteRaw ("");
  1516. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1517. }
  1518. [Test]
  1519. public void Indent ()
  1520. {
  1521. XmlDocument doc = new XmlDocument ();
  1522. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1523. StringWriter sw = new StringWriter ();
  1524. sw.NewLine = "_";
  1525. XmlTextWriter xtw = new XmlTextWriter (sw);
  1526. xtw.Formatting = Formatting.Indented;
  1527. doc.WriteContentTo (xtw);
  1528. Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1529. }
  1530. [Test]
  1531. public void Indent2 ()
  1532. {
  1533. StringWriter sw = new StringWriter ();
  1534. XmlTextWriter xtw = new XmlTextWriter (sw);
  1535. xtw.Formatting = Formatting.Indented;
  1536. // sadly, this silly usage of this method is actually
  1537. // used in WriteNode() in MS.NET.
  1538. xtw.WriteProcessingInstruction ("xml",
  1539. "version=\"1.0\"");
  1540. xtw.WriteComment ("sample XML fragment");
  1541. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  1542. @"<?xml version=""1.0""?>{0}" +
  1543. "<!--sample XML fragment-->", Environment.NewLine),
  1544. sw.ToString ());
  1545. }
  1546. [Test]
  1547. public void CloseTwice ()
  1548. {
  1549. StringWriter sw = new StringWriter ();
  1550. XmlTextWriter writer = new XmlTextWriter (sw);
  1551. writer.Close ();
  1552. // should not result in an exception
  1553. writer.Close ();
  1554. }
  1555. [Test]
  1556. public void WriteRawWriteString ()
  1557. {
  1558. // WriteRaw () -> WriteString ().
  1559. xtw.WriteRaw ("");
  1560. xtw.WriteString ("foo");
  1561. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1562. }
  1563. [Test]
  1564. public void LookupOverridenPrefix ()
  1565. {
  1566. xtw.WriteStartElement ("out");
  1567. xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1568. xtw.WriteStartElement ("baz", "foo", "abc");
  1569. Assert.IsNull (xtw.LookupPrefix ("xyz"));
  1570. }
  1571. [Test]
  1572. public void DuplicatingNamespaceMappingInAttributes ()
  1573. {
  1574. xtw.WriteStartElement ("out");
  1575. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1576. xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1577. xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1578. xtw.WriteStartElement ("out");
  1579. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1580. xtw.WriteStartElement ("out");
  1581. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1582. xtw.WriteEndElement ();
  1583. xtw.WriteEndElement ();
  1584. xtw.WriteEndElement ();
  1585. string xml = sw.ToString ();
  1586. Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo");
  1587. Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar");
  1588. Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz");
  1589. Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2");
  1590. Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1");
  1591. Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p");
  1592. Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining");
  1593. }
  1594. [Test]
  1595. public void WriteXmlSpaceIgnoresNS ()
  1596. {
  1597. xtw.WriteStartElement ("root");
  1598. xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1599. xtw.WriteEndElement ();
  1600. Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
  1601. }
  1602. [Test] // bug #75546
  1603. public void WriteEmptyNSQNameInAttribute ()
  1604. {
  1605. XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
  1606. xtw.WriteStartElement ("foo", "urn:goo");
  1607. xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1608. xtw.WriteStartAttribute ("foo", "");
  1609. xtw.WriteQualifiedName ("n1", "urn:bar");
  1610. xtw.WriteEndAttribute ();
  1611. xtw.WriteStartAttribute ("foo", "");
  1612. xtw.WriteQualifiedName ("n2", "");
  1613. xtw.WriteEndAttribute ();
  1614. }
  1615. [Test]
  1616. public void WriteDocType ()
  1617. {
  1618. // we have the following test matrix:
  1619. //
  1620. // | name | publicid | systemid | subset|
  1621. // |------------------------------------|
  1622. // | X | X | X | X | #01
  1623. // | X | E | X | X | #02
  1624. // | X | X | E | X | #03
  1625. // | X | X | X | E | #04
  1626. // | X | E | E | X | #05
  1627. // | X | X | E | E | #06
  1628. // | X | E | X | E | #07
  1629. // | X | E | E | E | #08
  1630. // | X | N | X | X | #09
  1631. // | X | X | N | X | #10
  1632. // | X | X | X | N | #11
  1633. // | X | N | N | X | #12
  1634. // | X | X | N | N | #13
  1635. // | X | N | X | N | #14
  1636. // | X | N | N | N | #15
  1637. //
  1638. // Legend:
  1639. // -------
  1640. // X = Has value
  1641. // E = Zero-length string
  1642. // N = Null
  1643. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1644. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1645. Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
  1646. " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
  1647. sw.ToString (), "#01");
  1648. sw.GetStringBuilder ().Length = 0;
  1649. xtw = new XmlTextWriter (sw);
  1650. xtw.WriteDocType ("test", string.Empty,
  1651. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1652. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1653. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1654. sw.ToString (), "#02");
  1655. sw.GetStringBuilder ().Length = 0;
  1656. xtw = new XmlTextWriter (sw);
  1657. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1658. string.Empty, "sub");
  1659. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1660. " \"\"[sub]>",
  1661. sw.ToString (), "#03");
  1662. sw.GetStringBuilder ().Length = 0;
  1663. xtw = new XmlTextWriter (sw);
  1664. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1665. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1666. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1667. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1668. sw.ToString (), "#04");
  1669. sw.GetStringBuilder ().Length = 0;
  1670. xtw = new XmlTextWriter (sw);
  1671. xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
  1672. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
  1673. sw.ToString (), "#05");
  1674. sw.GetStringBuilder ().Length = 0;
  1675. xtw = new XmlTextWriter (sw);
  1676. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1677. string.Empty, string.Empty);
  1678. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1679. " \"\"[]>",
  1680. sw.ToString (), "#06");
  1681. sw.GetStringBuilder ().Length = 0;
  1682. xtw = new XmlTextWriter (sw);
  1683. xtw.WriteDocType ("test", string.Empty,
  1684. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1685. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1686. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1687. sw.ToString (), "#07");
  1688. sw.GetStringBuilder ().Length = 0;
  1689. xtw = new XmlTextWriter (sw);
  1690. xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
  1691. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
  1692. sw.ToString (), "#08");
  1693. sw.GetStringBuilder ().Length = 0;
  1694. xtw = new XmlTextWriter (sw);
  1695. xtw.WriteDocType ("test", (string) null,
  1696. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1697. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1698. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1699. sw.ToString (), "#09");
  1700. sw.GetStringBuilder ().Length = 0;
  1701. xtw = new XmlTextWriter (sw);
  1702. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1703. (string) null, "sub");
  1704. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1705. " \"\"[sub]>",
  1706. sw.ToString (), "#10");
  1707. sw.GetStringBuilder ().Length = 0;
  1708. xtw = new XmlTextWriter (sw);
  1709. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1710. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1711. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1712. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1713. sw.ToString (), "#11");
  1714. sw.GetStringBuilder ().Length = 0;
  1715. xtw = new XmlTextWriter (sw);
  1716. xtw.WriteDocType ("test", (string) null, (string) null, "sub");
  1717. Assert.AreEqual ("<!DOCTYPE test[sub]>",
  1718. sw.ToString (), "#12");
  1719. sw.GetStringBuilder ().Length = 0;
  1720. xtw = new XmlTextWriter (sw);
  1721. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1722. (string) null, (string) null);
  1723. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1724. " \"\">",
  1725. sw.ToString (), "#13");
  1726. sw.GetStringBuilder ().Length = 0;
  1727. xtw = new XmlTextWriter (sw);
  1728. xtw.WriteDocType ("test", (string) null,
  1729. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1730. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1731. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1732. sw.ToString (), "#14");
  1733. sw.GetStringBuilder ().Length = 0;
  1734. xtw = new XmlTextWriter (sw);
  1735. xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
  1736. Assert.AreEqual ("<!DOCTYPE test>",
  1737. sw.ToString (), "#15");
  1738. }
  1739. [Test]
  1740. [ExpectedException (typeof (ArgumentException))]
  1741. public void WriteDocType_EmptyName ()
  1742. {
  1743. xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1744. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1745. }
  1746. [Test]
  1747. [ExpectedException (typeof (ArgumentException))]
  1748. public void WriteDocType_NullName ()
  1749. {
  1750. xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1751. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1752. }
  1753. [Test] // bug #76095
  1754. public void SurrogatePairsInWriteString ()
  1755. {
  1756. MemoryStream ms = new MemoryStream ();
  1757. XmlWriter writer = new XmlTextWriter(ms, null);
  1758. writer.WriteElementString("a", "\ud800\udf39");
  1759. writer.Close();
  1760. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1761. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1762. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1763. }
  1764. #if NET_2_0
  1765. [Test]
  1766. [ExpectedException (typeof (InvalidOperationException))]
  1767. public void RejectWritingAtErrorState ()
  1768. {
  1769. try {
  1770. xtw.WriteEndElement ();
  1771. } catch (Exception) {
  1772. }
  1773. xtw.WriteStartElement ("foo");
  1774. }
  1775. #endif
  1776. }
  1777. }