XmlTextWriterTests.cs 59 KB

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