XmlTextWriterTests.cs 68 KB

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