XmlTextWriterTests.cs 70 KB

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