XmlTextWriterTests.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  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 ("any prefix which name starts from \"xml\" must not be allowed.");
  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);
  654. xtw.WriteElementString ("quux", null);
  655. Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText);
  656. xtw.WriteElementString ("", "quuux");
  657. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  658. xtw.WriteElementString (null, "quuuux");
  659. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  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"));
  706. Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  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"));
  712. Assert.IsNull (xtw.LookupPrefix ("http://abc.def"));
  713. Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"));
  714. Assert.IsNull (xtw.LookupPrefix ("http://bogus"));
  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);
  1175. xtw.WriteStartElement ("bar");
  1176. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1177. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1178. Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText);
  1179. xtw.WriteStartElement ("baz");
  1180. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1181. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1182. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  1183. xtw.WriteStartElement ("quux");
  1184. xtw.WriteStartAttribute ("xml", "space", null);
  1185. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1186. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  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);
  1194. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  1195. xtw.WriteEndElement ();
  1196. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1197. xtw.WriteEndElement ();
  1198. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1199. xtw.WriteEndElement ();
  1200. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  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"));
  1386. xtw.WriteStartElement ("b");
  1387. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
  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);
  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"));
  1406. xtw.WriteStartElement ("b");
  1407. Assert.IsNull (xtw.LookupPrefix ("probe"));
  1408. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1409. xtw.WriteEndElement (); // b2
  1410. xtw.WriteStartElement (null, "b2", ""); // *Do* 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);
  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. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1488. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1489. xtw.WriteEndAttribute ();
  1490. }
  1491. [Test]
  1492. [ExpectedException (typeof (ArgumentException))]
  1493. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1494. {
  1495. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1496. }
  1497. [Test]
  1498. [ExpectedException (typeof (ArgumentException))]
  1499. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1500. {
  1501. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1502. }
  1503. [Test]
  1504. public void WriteRawProceedToProlog ()
  1505. {
  1506. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1507. xtw.WriteRaw ("");
  1508. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1509. }
  1510. [Test]
  1511. public void Indent ()
  1512. {
  1513. XmlDocument doc = new XmlDocument ();
  1514. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1515. StringWriter sw = new StringWriter ();
  1516. sw.NewLine = "_";
  1517. XmlTextWriter xtw = new XmlTextWriter (sw);
  1518. xtw.Formatting = Formatting.Indented;
  1519. doc.WriteContentTo (xtw);
  1520. Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1521. }
  1522. [Test]
  1523. public void Indent2 ()
  1524. {
  1525. StringWriter sw = new StringWriter ();
  1526. XmlTextWriter xtw = new XmlTextWriter (sw);
  1527. xtw.Formatting = Formatting.Indented;
  1528. // sadly, this silly usage of this method is actually
  1529. // used in WriteNode() in MS.NET.
  1530. xtw.WriteProcessingInstruction ("xml",
  1531. "version=\"1.0\"");
  1532. xtw.WriteComment ("sample XML fragment");
  1533. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  1534. @"<?xml version=""1.0""?>{0}" +
  1535. "<!--sample XML fragment-->", Environment.NewLine),
  1536. sw.ToString ());
  1537. }
  1538. [Test]
  1539. public void CloseTwice ()
  1540. {
  1541. StringWriter sw = new StringWriter ();
  1542. XmlTextWriter writer = new XmlTextWriter (sw);
  1543. writer.Close ();
  1544. // should not result in an exception
  1545. writer.Close ();
  1546. }
  1547. [Test]
  1548. public void WriteRawWriteString ()
  1549. {
  1550. // WriteRaw () -> WriteString ().
  1551. xtw.WriteRaw ("");
  1552. xtw.WriteString ("foo");
  1553. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1554. }
  1555. [Test]
  1556. public void LookupOverridenPrefix ()
  1557. {
  1558. xtw.WriteStartElement ("out");
  1559. xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1560. xtw.WriteStartElement ("baz", "foo", "abc");
  1561. Assert.IsNull (xtw.LookupPrefix ("xyz"));
  1562. }
  1563. [Test]
  1564. public void DuplicatingNamespaceMappingInAttributes ()
  1565. {
  1566. xtw.WriteStartElement ("out");
  1567. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1568. xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1569. xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1570. xtw.WriteStartElement ("out");
  1571. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1572. xtw.WriteStartElement ("out");
  1573. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1574. xtw.WriteEndElement ();
  1575. xtw.WriteEndElement ();
  1576. xtw.WriteEndElement ();
  1577. string xml = sw.ToString ();
  1578. Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo");
  1579. Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar");
  1580. Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz");
  1581. Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2");
  1582. Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1");
  1583. Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p");
  1584. Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining");
  1585. }
  1586. [Test]
  1587. public void WriteXmlSpaceIgnoresNS ()
  1588. {
  1589. xtw.WriteStartElement ("root");
  1590. xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1591. xtw.WriteEndElement ();
  1592. Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
  1593. }
  1594. [Test] // bug #75546
  1595. public void WriteEmptyNSQNameInAttribute ()
  1596. {
  1597. XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
  1598. xtw.WriteStartElement ("foo", "urn:goo");
  1599. xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1600. xtw.WriteStartAttribute ("foo", "");
  1601. xtw.WriteQualifiedName ("n1", "urn:bar");
  1602. xtw.WriteEndAttribute ();
  1603. xtw.WriteStartAttribute ("foo", "");
  1604. xtw.WriteQualifiedName ("n2", "");
  1605. xtw.WriteEndAttribute ();
  1606. }
  1607. [Test]
  1608. public void WriteDocType ()
  1609. {
  1610. // we have the following test matrix:
  1611. //
  1612. // | name | publicid | systemid | subset|
  1613. // |------------------------------------|
  1614. // | X | X | X | X | #01
  1615. // | X | E | X | X | #02
  1616. // | X | X | E | X | #03
  1617. // | X | X | X | E | #04
  1618. // | X | E | E | X | #05
  1619. // | X | X | E | E | #06
  1620. // | X | E | X | E | #07
  1621. // | X | E | E | E | #08
  1622. // | X | N | X | X | #09
  1623. // | X | X | N | X | #10
  1624. // | X | X | X | N | #11
  1625. // | X | N | N | X | #12
  1626. // | X | X | N | N | #13
  1627. // | X | N | X | N | #14
  1628. // | X | N | N | N | #15
  1629. //
  1630. // Legend:
  1631. // -------
  1632. // X = Has value
  1633. // E = Zero-length string
  1634. // N = Null
  1635. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1636. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1637. Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
  1638. " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
  1639. sw.ToString (), "#01");
  1640. sw.GetStringBuilder ().Length = 0;
  1641. xtw = new XmlTextWriter (sw);
  1642. xtw.WriteDocType ("test", string.Empty,
  1643. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1644. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1645. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1646. sw.ToString (), "#02");
  1647. sw.GetStringBuilder ().Length = 0;
  1648. xtw = new XmlTextWriter (sw);
  1649. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1650. string.Empty, "sub");
  1651. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1652. " \"\"[sub]>",
  1653. sw.ToString (), "#03");
  1654. sw.GetStringBuilder ().Length = 0;
  1655. xtw = new XmlTextWriter (sw);
  1656. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1657. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1658. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1659. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1660. sw.ToString (), "#04");
  1661. sw.GetStringBuilder ().Length = 0;
  1662. xtw = new XmlTextWriter (sw);
  1663. xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
  1664. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
  1665. sw.ToString (), "#05");
  1666. sw.GetStringBuilder ().Length = 0;
  1667. xtw = new XmlTextWriter (sw);
  1668. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1669. string.Empty, string.Empty);
  1670. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1671. " \"\"[]>",
  1672. sw.ToString (), "#06");
  1673. sw.GetStringBuilder ().Length = 0;
  1674. xtw = new XmlTextWriter (sw);
  1675. xtw.WriteDocType ("test", string.Empty,
  1676. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1677. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1678. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1679. sw.ToString (), "#07");
  1680. sw.GetStringBuilder ().Length = 0;
  1681. xtw = new XmlTextWriter (sw);
  1682. xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
  1683. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
  1684. sw.ToString (), "#08");
  1685. sw.GetStringBuilder ().Length = 0;
  1686. xtw = new XmlTextWriter (sw);
  1687. xtw.WriteDocType ("test", (string) null,
  1688. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1689. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1690. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1691. sw.ToString (), "#09");
  1692. sw.GetStringBuilder ().Length = 0;
  1693. xtw = new XmlTextWriter (sw);
  1694. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1695. (string) null, "sub");
  1696. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1697. " \"\"[sub]>",
  1698. sw.ToString (), "#10");
  1699. sw.GetStringBuilder ().Length = 0;
  1700. xtw = new XmlTextWriter (sw);
  1701. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1702. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1703. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1704. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1705. sw.ToString (), "#11");
  1706. sw.GetStringBuilder ().Length = 0;
  1707. xtw = new XmlTextWriter (sw);
  1708. xtw.WriteDocType ("test", (string) null, (string) null, "sub");
  1709. Assert.AreEqual ("<!DOCTYPE test[sub]>",
  1710. sw.ToString (), "#12");
  1711. sw.GetStringBuilder ().Length = 0;
  1712. xtw = new XmlTextWriter (sw);
  1713. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1714. (string) null, (string) null);
  1715. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1716. " \"\">",
  1717. sw.ToString (), "#13");
  1718. sw.GetStringBuilder ().Length = 0;
  1719. xtw = new XmlTextWriter (sw);
  1720. xtw.WriteDocType ("test", (string) null,
  1721. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1722. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1723. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1724. sw.ToString (), "#14");
  1725. sw.GetStringBuilder ().Length = 0;
  1726. xtw = new XmlTextWriter (sw);
  1727. xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
  1728. Assert.AreEqual ("<!DOCTYPE test>",
  1729. sw.ToString (), "#15");
  1730. }
  1731. [Test]
  1732. [ExpectedException (typeof (ArgumentException))]
  1733. public void WriteDocType_EmptyName ()
  1734. {
  1735. xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1736. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1737. }
  1738. [Test]
  1739. [ExpectedException (typeof (ArgumentException))]
  1740. public void WriteDocType_NullName ()
  1741. {
  1742. xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1743. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1744. }
  1745. [Test] // bug #76095
  1746. public void SurrogatePairsInWriteString ()
  1747. {
  1748. MemoryStream ms = new MemoryStream ();
  1749. XmlWriter writer = new XmlTextWriter(ms, null);
  1750. writer.WriteElementString("a", "\ud800\udf39");
  1751. writer.Close();
  1752. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1753. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1754. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1755. }
  1756. [Test] // see also bug #77082
  1757. public void WriteDocTypeIndent ()
  1758. {
  1759. string expected = String.Format (@"<?xml version='1.0'?>{0}<!DOCTYPE root PUBLIC '' 'urn:foo'[]>{0}<root />", Environment.NewLine);
  1760. xtw.Formatting = Formatting.Indented;
  1761. xtw.WriteProcessingInstruction ("xml", "version='1.0'");
  1762. xtw.WriteDocType ("root", "", "urn:foo", "");
  1763. xtw.WriteStartElement ("root");
  1764. xtw.WriteEndElement ();
  1765. xtw.Close ();
  1766. Assert.AreEqual (expected, StringWriterText);
  1767. }
  1768. [Test]
  1769. [ExpectedException (typeof (InvalidOperationException))]
  1770. public void WriteDocTypeTwice ()
  1771. {
  1772. xtw.WriteDocType ("root", "", "urn:foo", "");
  1773. xtw.WriteDocType ("root", "", "urn:foo", "");
  1774. }
  1775. [Test]
  1776. [ExpectedException (typeof (InvalidOperationException))]
  1777. public void XmlDeclAfterDocType ()
  1778. {
  1779. xtw.WriteDocType ("root", "", "urn:foo", "");
  1780. xtw.WriteStartDocument ();
  1781. }
  1782. [Test]
  1783. [ExpectedException (typeof (InvalidOperationException))]
  1784. public void XmlDeclAfterWhitespace ()
  1785. {
  1786. xtw.WriteWhitespace (" ");
  1787. xtw.WriteStartDocument ();
  1788. }
  1789. [Test]
  1790. [ExpectedException (typeof (InvalidOperationException))]
  1791. public void XmlDeclAfterPI ()
  1792. {
  1793. xtw.WriteProcessingInstruction ("pi", "");
  1794. xtw.WriteStartDocument ();
  1795. }
  1796. #if NET_2_0
  1797. [Test]
  1798. [ExpectedException (typeof (InvalidOperationException))]
  1799. public void RejectWritingAtErrorState ()
  1800. {
  1801. try {
  1802. xtw.WriteEndElement ();
  1803. } catch (Exception) {
  1804. }
  1805. xtw.WriteStartElement ("foo");
  1806. }
  1807. #endif
  1808. }
  1809. }