XmlTextWriterTests.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  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. public void WriteQualifiedNameNonNamespacedName ()
  1037. {
  1038. xtw.WriteStartElement ("root");
  1039. xtw.WriteQualifiedName ("foo", "");
  1040. xtw.WriteEndElement ();
  1041. Assert.AreEqual ("<root>foo</root>", StringWriterText);
  1042. }
  1043. [Test]
  1044. [ExpectedException (typeof (ArgumentException))]
  1045. public void WriteQualifiedNameNonDeclaredContent ()
  1046. {
  1047. xtw.WriteStartElement ("foo");
  1048. xtw.WriteQualifiedName ("abc", "urn:abc");
  1049. }
  1050. [Test]
  1051. [ExpectedException (typeof (ArgumentException))]
  1052. public void WriteQualifiedNameNonNCName ()
  1053. {
  1054. xtw.WriteStartElement ("foo");
  1055. xtw.WriteAttributeString ("xmlns", "urn:default");
  1056. xtw.WriteStartElement ("child");
  1057. xtw.WriteStartAttribute ("a", "");
  1058. xtw.WriteQualifiedName ("x:def", "urn:def");
  1059. }
  1060. [Test]
  1061. public void WriteRaw ()
  1062. {
  1063. xtw.WriteRaw("&<>\"'");
  1064. Assert.AreEqual ("&<>\"'", StringWriterText);
  1065. xtw.WriteRaw(null);
  1066. Assert.AreEqual ("&<>\"'", StringWriterText);
  1067. xtw.WriteRaw("");
  1068. Assert.AreEqual ("&<>\"'", StringWriterText);
  1069. // bug #77623
  1070. xtw.WriteRaw ("{0}{1}");
  1071. }
  1072. [Test]
  1073. public void WriteRawInvalidInAttribute ()
  1074. {
  1075. xtw.WriteStartElement ("foo");
  1076. xtw.WriteStartAttribute ("bar", null);
  1077. xtw.WriteRaw ("&<>\"'");
  1078. xtw.WriteEndAttribute ();
  1079. xtw.WriteEndElement ();
  1080. Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
  1081. }
  1082. [Test]
  1083. public void WriteStateTest ()
  1084. {
  1085. Assert.AreEqual (WriteState.Start, xtw.WriteState);
  1086. xtw.WriteStartDocument ();
  1087. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1088. xtw.WriteStartElement ("root");
  1089. Assert.AreEqual (WriteState.Element, xtw.WriteState);
  1090. xtw.WriteElementString ("foo", "bar");
  1091. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1092. xtw.Close ();
  1093. Assert.AreEqual (WriteState.Closed, xtw.WriteState);
  1094. }
  1095. [Test]
  1096. public void WriteString ()
  1097. {
  1098. xtw.WriteStartDocument ();
  1099. try {
  1100. xtw.WriteString("foo");
  1101. Assert.Fail ("should raise an error.");
  1102. } catch (InvalidOperationException) {}
  1103. }
  1104. [Test]
  1105. public void WriteString2 ()
  1106. {
  1107. xtw.WriteStartDocument ();
  1108. // Testing attribute values
  1109. xtw.WriteStartElement ("foo");
  1110. xtw.WriteAttributeString ("bar", "&<>");
  1111. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  1112. }
  1113. [Test]
  1114. public void WriteAttributeStringSingleQuoteChar()
  1115. {
  1116. // When QuoteChar is single quote then replaces single quotes within attributes
  1117. // but not double quotes.
  1118. xtw.WriteStartElement ("foo");
  1119. xtw.WriteAttributeString ("bar", "\"baz\"");
  1120. xtw.WriteAttributeString ("quux", "'baz'");
  1121. Assert.AreEqual ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  1122. }
  1123. [Test]
  1124. public void WriteAttributeStringDoubleQuoteChar()
  1125. {
  1126. // When QuoteChar is double quote then replaces double quotes within attributes
  1127. // but not single quotes.
  1128. xtw.QuoteChar = '"';
  1129. xtw.WriteStartElement ("foo");
  1130. xtw.WriteAttributeString ("bar", "\"baz\"");
  1131. xtw.WriteAttributeString ("quux", "'baz'");
  1132. Assert.AreEqual ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  1133. }
  1134. [Test]
  1135. public void WriteStringWithEntities()
  1136. {
  1137. // Testing element values
  1138. xtw.QuoteChar = '\'';
  1139. xtw.WriteElementString ("foo", "&<>\"'");
  1140. Assert.AreEqual ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  1141. }
  1142. [Test]
  1143. public void XmlLang ()
  1144. {
  1145. Assert.IsNull (xtw.XmlLang);
  1146. xtw.WriteStartElement ("foo");
  1147. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  1148. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1149. Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
  1150. xtw.WriteAttributeString ("boo", "yah");
  1151. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1152. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  1153. xtw.WriteElementString("bar", "baz");
  1154. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1155. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  1156. xtw.WriteString("baz");
  1157. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1158. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  1159. xtw.WriteStartElement ("quux");
  1160. xtw.WriteStartAttribute ("xml", "lang", null);
  1161. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1162. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1163. xtw.WriteString("langbar");
  1164. // Commented out there: it is implementation-dependent.
  1165. // and incompatible between .NET 1.0 and 1.1
  1166. // Assert.AreEqual ("langfoo", xtw.XmlLang);
  1167. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1168. xtw.WriteEndAttribute ();
  1169. // Commented out there: it is implementation-dependent.
  1170. // and incompatible between .NET 1.0 and 1.1
  1171. // Assert.AreEqual ("langbar", xtw.XmlLang);
  1172. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  1173. // check if xml:lang repeats output even if same as current scope.
  1174. xtw.WriteStartElement ("joe");
  1175. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  1176. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  1177. xtw.WriteElementString ("quuux", "squonk");
  1178. Assert.AreEqual ("langbar", xtw.XmlLang);
  1179. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  1180. xtw.WriteEndElement ();
  1181. xtw.WriteEndElement ();
  1182. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1183. 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);
  1184. xtw.WriteEndElement ();
  1185. Assert.IsNull (xtw.XmlLang);
  1186. 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);
  1187. xtw.Close ();
  1188. Assert.IsNull (xtw.XmlLang);
  1189. }
  1190. // TODO: test operational aspects
  1191. [Test]
  1192. public void XmlSpaceTest ()
  1193. {
  1194. xtw.WriteStartElement ("foo");
  1195. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#1");
  1196. xtw.WriteStartElement ("bar");
  1197. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1198. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#2");
  1199. Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText, "#3");
  1200. xtw.WriteStartElement ("baz");
  1201. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1202. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#4");
  1203. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText, "#5");
  1204. xtw.WriteStartElement ("quux");
  1205. xtw.WriteStartAttribute ("xml", "space", null);
  1206. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#6");
  1207. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText, "#7");
  1208. // Commented out there: it is implementation-dependent
  1209. // and incompatible between .NET 1.0 and 1.1
  1210. xtw.WriteString ("default");
  1211. // Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1212. // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  1213. xtw.WriteEndAttribute ();
  1214. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace, "#8");
  1215. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText, "#9");
  1216. xtw.WriteEndElement ();
  1217. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#10");
  1218. xtw.WriteEndElement ();
  1219. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#11");
  1220. xtw.WriteEndElement ();
  1221. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#12");
  1222. xtw.WriteStartElement ("quux");
  1223. }
  1224. [Test]
  1225. [ExpectedException (typeof (ArgumentException))]
  1226. public void XmlSpaceTestInvalidValue1 ()
  1227. {
  1228. xtw.WriteStartElement ("foo");
  1229. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1230. }
  1231. [Test]
  1232. [ExpectedException (typeof (ArgumentException))]
  1233. public void XmlSpaceTestInvalidValue2 ()
  1234. {
  1235. xtw.WriteStartElement ("foo");
  1236. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  1237. }
  1238. [Test]
  1239. [ExpectedException (typeof (ArgumentException))]
  1240. public void XmlSpaceTestInvalidValue3 ()
  1241. {
  1242. xtw.WriteStartElement ("foo");
  1243. xtw.WriteAttributeString ("xml", "space", null, "Default");
  1244. }
  1245. [Test]
  1246. [ExpectedException (typeof (ArgumentException))]
  1247. public void XmlSpaceTestInvalidValue4 ()
  1248. {
  1249. xtw.WriteStartElement ("foo");
  1250. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1251. }
  1252. [Test]
  1253. [ExpectedException (typeof (ArgumentException))]
  1254. public void WriteWhitespaceNonWhitespace ()
  1255. {
  1256. xtw.WriteWhitespace ("x");
  1257. }
  1258. [Test]
  1259. [ExpectedException (typeof (ArgumentException))]
  1260. public void WriteWhitespace_Null ()
  1261. {
  1262. xtw.WriteWhitespace ((string) null);
  1263. }
  1264. [Test]
  1265. [ExpectedException (typeof (ArgumentException))]
  1266. public void WriteWhitespace_Empty ()
  1267. {
  1268. xtw.WriteWhitespace (string.Empty);
  1269. }
  1270. [Test]
  1271. [ExpectedException (typeof (ArgumentException))]
  1272. public void WriteNmToken_Null ()
  1273. {
  1274. xtw.WriteNmToken ((string) null);
  1275. }
  1276. [Test]
  1277. [ExpectedException (typeof (ArgumentException))]
  1278. public void WriteNmToken_Empty ()
  1279. {
  1280. xtw.WriteNmToken (string.Empty);
  1281. }
  1282. [Test]
  1283. [ExpectedException (typeof (ArgumentException))]
  1284. public void WriteNmToken_InvalidChars ()
  1285. {
  1286. xtw.WriteNmToken ("\uFFFF");
  1287. }
  1288. [Test]
  1289. public void WriteNmToken ()
  1290. {
  1291. xtw.WriteNmToken ("some:name");
  1292. Assert.AreEqual ("some:name", StringWriterText);
  1293. }
  1294. [Test]
  1295. public void XmlSpaceRaw ()
  1296. {
  1297. xtw.WriteStartElement ("foo");
  1298. xtw.WriteStartAttribute ("xml", "space", null);
  1299. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1300. Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1301. xtw.WriteString ("default");
  1302. // Commented out there: it is implementation-dependent
  1303. // and incompatible between .NET 1.0 and 1.1
  1304. // Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1305. // Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1306. xtw.WriteEndAttribute ();
  1307. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
  1308. Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
  1309. }
  1310. [Test]
  1311. public void WriteAttributes ()
  1312. {
  1313. XmlDocument doc = new XmlDocument();
  1314. StringWriter sw = new StringWriter();
  1315. XmlWriter wr = new XmlTextWriter(sw);
  1316. StringBuilder sb = sw.GetStringBuilder();
  1317. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1318. 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);
  1319. xtr.Read(); // read XMLDecl
  1320. wr.WriteAttributes(xtr, false);
  1321. // This method don't always have to take this double-quoted style...
  1322. Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
  1323. "#WriteAttributes.XmlDecl.1");
  1324. sb.Remove(0, sb.Length); // init
  1325. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1326. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  1327. xtr.Read(); // read XMLDecl
  1328. Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
  1329. sw = new StringWriter ();
  1330. wr = new XmlTextWriter (sw);
  1331. // This block raises an error on MS.NET 1.0.
  1332. wr.WriteAttributes(xtr, false);
  1333. // This method don't always have to take this double-quoted style...
  1334. Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
  1335. "#WriteAttributes.XmlDecl.2");
  1336. sw = new StringWriter ();
  1337. wr = new XmlTextWriter (sw);
  1338. sb.Remove(0, sb.Length); // init
  1339. xtr.Read(); // read root
  1340. Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
  1341. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  1342. wr.WriteAttributes(xtr, false);
  1343. wr.WriteEndElement();
  1344. wr.Close();
  1345. // This method don't always have to take this double-quoted style...
  1346. Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
  1347. "#WriteAttributes.Element");
  1348. xtr.Close ();
  1349. }
  1350. [Test]
  1351. public void WriteWhitespace ()
  1352. {
  1353. xtw.WriteStartElement ("a");
  1354. xtw.WriteWhitespace ("\n\t");
  1355. xtw.WriteStartElement ("b");
  1356. xtw.WriteWhitespace ("\n\t");
  1357. xtw.WriteEndElement ();
  1358. xtw.WriteWhitespace ("\n");
  1359. xtw.WriteEndElement ();
  1360. xtw.WriteWhitespace ("\n");
  1361. xtw.Flush ();
  1362. Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  1363. }
  1364. [Test]
  1365. public void FlushDoesntCloseTag ()
  1366. {
  1367. xtw.WriteStartElement ("foo");
  1368. xtw.WriteAttributeString ("bar", "baz");
  1369. xtw.Flush ();
  1370. Assert.AreEqual ("<foo bar='baz'", StringWriterText);
  1371. }
  1372. [Test]
  1373. public void WriteWhitespaceClosesTag ()
  1374. {
  1375. xtw.WriteStartElement ("foo");
  1376. xtw.WriteAttributeString ("bar", "baz");
  1377. xtw.WriteWhitespace (" ");
  1378. Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
  1379. }
  1380. [Test]
  1381. public void DontOutputMultipleXmlns ()
  1382. {
  1383. XmlDocument doc = new XmlDocument();
  1384. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  1385. XmlDocument doc2 = new XmlDocument();
  1386. doc2.LoadXml(doc.InnerXml);
  1387. Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  1388. doc2.OuterXml);
  1389. }
  1390. [Test]
  1391. public void DontOutputNonDeclaredXmlns ()
  1392. {
  1393. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  1394. XmlDocument doc = new XmlDocument();
  1395. doc.LoadXml(xml);
  1396. XmlDocument doc2 = new XmlDocument();
  1397. doc2.LoadXml(doc.InnerXml);
  1398. Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
  1399. }
  1400. [Test]
  1401. public void DontOutputRemovalDefaultNSDeclaration ()
  1402. {
  1403. xtw.WriteStartDocument ();
  1404. xtw.WriteStartElement ("foo");
  1405. xtw.WriteAttributeString ("xmlns", "probe");
  1406. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#1");
  1407. xtw.WriteStartElement ("b");
  1408. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#2");
  1409. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1410. xtw.WriteEndElement (); // b2
  1411. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1412. xtw.WriteEndElement (); // b2
  1413. xtw.WriteEndElement (); // b
  1414. xtw.WriteEndElement (); // foo
  1415. xtw.WriteEndDocument ();
  1416. xtw.Close ();
  1417. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText, "#3");
  1418. }
  1419. [Test]
  1420. public void DontOutputRemovalDefaultNSDeclaration2 ()
  1421. {
  1422. xtw.WriteStartDocument ();
  1423. // IMPORTANT DIFFERENCE!! ns = "", not null
  1424. xtw.WriteStartElement ("foo", "");
  1425. xtw.WriteAttributeString ("xmlns", "probe");
  1426. Assert.IsNull (xtw.LookupPrefix ("probe"), "#1");
  1427. xtw.WriteStartElement ("b");
  1428. Assert.IsNull (xtw.LookupPrefix ("probe"), "#2");
  1429. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1430. xtw.WriteEndElement (); // b2
  1431. xtw.WriteStartElement (null, "b2", ""); // *Don't* output xmlns=""
  1432. xtw.WriteEndElement (); // b2
  1433. xtw.WriteEndElement (); // b
  1434. xtw.WriteEndElement (); // foo
  1435. xtw.WriteEndDocument ();
  1436. xtw.Close ();
  1437. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText, "#3");
  1438. }
  1439. [Test]
  1440. public void DoOutputRemovalDefaultNSDeclaration ()
  1441. {
  1442. xtw.WriteStartElement ("docelem", "a-namespace");
  1443. XmlDocument doc = new XmlDocument ();
  1444. doc.CreateElement ("hola").WriteTo (xtw);
  1445. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  1446. xtw.WriteEndElement ();
  1447. xtw.Close ();
  1448. Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  1449. }
  1450. [Test]
  1451. public void WriteAttributeTakePrecedenceOnXmlns ()
  1452. {
  1453. xtw.WriteStartElement ("root", "urn:foo");
  1454. xtw.WriteAttributeString ("xmlns", "urn:bar");
  1455. xtw.WriteEndElement ();
  1456. xtw.Close ();
  1457. Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
  1458. }
  1459. [Test]
  1460. [ExpectedException (typeof (ArgumentException))]
  1461. public void LookupPrefixNull ()
  1462. {
  1463. xtw.LookupPrefix (null);
  1464. }
  1465. [Test]
  1466. [ExpectedException (typeof (ArgumentException))]
  1467. public void LookupPrefixEmpty ()
  1468. {
  1469. xtw.LookupPrefix (String.Empty);
  1470. }
  1471. [Test]
  1472. public void LookupPrefixIgnoresXmlnsAttribute ()
  1473. {
  1474. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1475. xtw.WriteStartElement ("root");
  1476. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1477. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1478. // Surprisingly to say, it is ignored!!
  1479. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1480. xtw.WriteStartElement ("hoge");
  1481. // (still after flushing previous start element.)
  1482. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1483. xtw.WriteStartElement ("fuga", "urn:foo");
  1484. // Is this testing on the correct way? Yes, here it is.
  1485. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1486. }
  1487. [Test]
  1488. public void WriteInvalidNames ()
  1489. {
  1490. xtw.WriteStartElement ("foo<>");
  1491. xtw.WriteAttributeString ("ho<>ge", "value");
  1492. }
  1493. [Test]
  1494. [ExpectedException (typeof (ArgumentException))]
  1495. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1496. {
  1497. xtw.WriteStartAttribute ("some", "foo", null);
  1498. }
  1499. [Test]
  1500. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1501. {
  1502. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1503. }
  1504. [Test]
  1505. [ExpectedException (typeof (ArgumentException))]
  1506. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1507. {
  1508. // This test checks if the specified namespace URI is
  1509. // incorrectly empty or not. Compare it with
  1510. // AttributeWriteStartAttributeXmlnsNullNS().
  1511. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1512. xtw.WriteEndAttribute ();
  1513. }
  1514. [Test]
  1515. [ExpectedException (typeof (ArgumentException))]
  1516. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1517. {
  1518. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1519. }
  1520. [Test]
  1521. [ExpectedException (typeof (ArgumentException))]
  1522. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1523. {
  1524. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1525. }
  1526. [Test]
  1527. public void WriteRawProceedToProlog ()
  1528. {
  1529. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1530. xtw.WriteRaw ("");
  1531. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1532. }
  1533. [Test]
  1534. public void Indent ()
  1535. {
  1536. XmlDocument doc = new XmlDocument ();
  1537. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1538. StringWriter sw = new StringWriter ();
  1539. sw.NewLine = "_";
  1540. XmlTextWriter xtw = new XmlTextWriter (sw);
  1541. xtw.Formatting = Formatting.Indented;
  1542. doc.WriteContentTo (xtw);
  1543. Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1544. }
  1545. [Test]
  1546. public void Indent2 ()
  1547. {
  1548. StringWriter sw = new StringWriter ();
  1549. XmlTextWriter xtw = new XmlTextWriter (sw);
  1550. xtw.Formatting = Formatting.Indented;
  1551. // sadly, this silly usage of this method is actually
  1552. // used in WriteNode() in MS.NET.
  1553. xtw.WriteProcessingInstruction ("xml",
  1554. "version=\"1.0\"");
  1555. xtw.WriteComment ("sample XML fragment");
  1556. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  1557. @"<?xml version=""1.0""?>{0}" +
  1558. "<!--sample XML fragment-->", Environment.NewLine),
  1559. sw.ToString ());
  1560. }
  1561. [Test]
  1562. public void Indent3 ()
  1563. {
  1564. XmlDocument doc = new XmlDocument ();
  1565. doc.PreserveWhitespace = true;
  1566. string s;
  1567. doc.LoadXml ("<root><element></element><!-- comment indented --><element>sample <!-- comment non-indented --></element></root>");
  1568. s = GetIndentedOutput (doc.DocumentElement);
  1569. Assert.AreEqual (s, String.Format ("<root>{0} <element>{0} </element>{0} <!-- comment indented -->{0} <element>sample <!-- comment non-indented --></element>{0}</root>", "\n"), "#1");
  1570. doc.LoadXml ("<root> \n<mid> \n<mid> \n<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value'/>\n<a>test</a> \n</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>");
  1571. s = GetIndentedOutput (doc.DocumentElement);
  1572. Assert.AreEqual (s, String.Format ("<root> {0}<mid> {0}<mid> {0}<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value' />{0}<a>test</a> {0}</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>", "\n"), "#2");
  1573. doc.LoadXml ("<!-- after /MemberType and after /returnValue --><root><MemberType>blah</MemberType>\n <returnValue><returnType>System.String</returnType></returnValue>\n <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks/></Docs></root>");
  1574. s = GetIndentedOutput (doc.DocumentElement);
  1575. Assert.AreEqual (s, String.Format ("<root>{0} <MemberType>blah</MemberType>{0} <returnValue><returnType>System.String</returnType></returnValue>{0} <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks /></Docs></root>", "\n"), "#3");
  1576. }
  1577. string GetIndentedOutput (XmlNode n)
  1578. {
  1579. StringWriter sw = new StringWriter ();
  1580. sw.NewLine = "\n";
  1581. XmlTextWriter xtw = new XmlTextWriter (sw);
  1582. xtw.QuoteChar = '\'';
  1583. xtw.Formatting = Formatting.Indented;
  1584. n.WriteTo (xtw);
  1585. return sw.ToString ();
  1586. }
  1587. [Test]
  1588. public void CloseTwice ()
  1589. {
  1590. StringWriter sw = new StringWriter ();
  1591. XmlTextWriter writer = new XmlTextWriter (sw);
  1592. writer.Close ();
  1593. // should not result in an exception
  1594. writer.Close ();
  1595. }
  1596. [Test]
  1597. public void WriteRawWriteString ()
  1598. {
  1599. // WriteRaw () -> WriteString ().
  1600. xtw.WriteRaw ("");
  1601. xtw.WriteString ("foo");
  1602. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1603. }
  1604. [Test]
  1605. public void LookupOverridenPrefix ()
  1606. {
  1607. xtw.WriteStartElement ("out");
  1608. xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1609. xtw.WriteStartElement ("baz", "foo", "abc");
  1610. Assert.IsNull (xtw.LookupPrefix ("xyz"));
  1611. }
  1612. [Test]
  1613. public void DuplicatingNamespaceMappingInAttributes ()
  1614. {
  1615. xtw.WriteStartElement ("out");
  1616. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1617. xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1618. xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1619. xtw.WriteStartElement ("out");
  1620. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1621. xtw.WriteStartElement ("out");
  1622. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1623. xtw.WriteEndElement ();
  1624. xtw.WriteEndElement ();
  1625. xtw.WriteEndElement ();
  1626. string xml = sw.ToString ();
  1627. Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo" + ". output is " + xml);
  1628. Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar" + ". output is " + xml);
  1629. Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz" + ". output is " + xml);
  1630. Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2" + ". output is " + xml);
  1631. Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1" + ". output is " + xml);
  1632. Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p" + ". output is " + xml);
  1633. Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining" + ". output is " + xml);
  1634. }
  1635. [Test]
  1636. public void WriteXmlSpaceIgnoresNS ()
  1637. {
  1638. xtw.WriteStartElement ("root");
  1639. xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1640. xtw.WriteEndElement ();
  1641. Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
  1642. }
  1643. [Test] // bug #75546
  1644. public void WriteEmptyNSQNameInAttribute ()
  1645. {
  1646. XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
  1647. xtw.WriteStartElement ("foo", "urn:goo");
  1648. xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1649. xtw.WriteStartAttribute ("foo", "");
  1650. xtw.WriteQualifiedName ("n1", "urn:bar");
  1651. xtw.WriteEndAttribute ();
  1652. xtw.WriteStartAttribute ("foo", "");
  1653. xtw.WriteQualifiedName ("n2", "");
  1654. xtw.WriteEndAttribute ();
  1655. }
  1656. [Test]
  1657. [ExpectedException (typeof (ArgumentException))]
  1658. // cannot bind any prefix to "http://www.w3.org/2000/xmlns/".
  1659. public void WriteQualifiedNameXmlnsError ()
  1660. {
  1661. xtw.WriteStartElement ("foo");
  1662. xtw.WriteQualifiedName ("", "http://www.w3.org/2000/xmlns/");
  1663. }
  1664. [Test]
  1665. public void WriteDocType ()
  1666. {
  1667. // we have the following test matrix:
  1668. //
  1669. // | name | publicid | systemid | subset|
  1670. // |------------------------------------|
  1671. // | X | X | X | X | #01
  1672. // | X | E | X | X | #02
  1673. // | X | X | E | X | #03
  1674. // | X | X | X | E | #04
  1675. // | X | E | E | X | #05
  1676. // | X | X | E | E | #06
  1677. // | X | E | X | E | #07
  1678. // | X | E | E | E | #08
  1679. // | X | N | X | X | #09
  1680. // | X | X | N | X | #10
  1681. // | X | X | X | N | #11
  1682. // | X | N | N | X | #12
  1683. // | X | X | N | N | #13
  1684. // | X | N | X | N | #14
  1685. // | X | N | N | N | #15
  1686. //
  1687. // Legend:
  1688. // -------
  1689. // X = Has value
  1690. // E = Zero-length string
  1691. // N = Null
  1692. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1693. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1694. Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
  1695. " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
  1696. sw.ToString (), "#01");
  1697. sw.GetStringBuilder ().Length = 0;
  1698. xtw = new XmlTextWriter (sw);
  1699. xtw.WriteDocType ("test", string.Empty,
  1700. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1701. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1702. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1703. sw.ToString (), "#02");
  1704. sw.GetStringBuilder ().Length = 0;
  1705. xtw = new XmlTextWriter (sw);
  1706. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1707. string.Empty, "sub");
  1708. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1709. " \"\"[sub]>",
  1710. sw.ToString (), "#03");
  1711. sw.GetStringBuilder ().Length = 0;
  1712. xtw = new XmlTextWriter (sw);
  1713. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1714. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1715. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1716. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1717. sw.ToString (), "#04");
  1718. sw.GetStringBuilder ().Length = 0;
  1719. xtw = new XmlTextWriter (sw);
  1720. xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
  1721. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
  1722. sw.ToString (), "#05");
  1723. sw.GetStringBuilder ().Length = 0;
  1724. xtw = new XmlTextWriter (sw);
  1725. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1726. string.Empty, string.Empty);
  1727. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1728. " \"\"[]>",
  1729. sw.ToString (), "#06");
  1730. sw.GetStringBuilder ().Length = 0;
  1731. xtw = new XmlTextWriter (sw);
  1732. xtw.WriteDocType ("test", string.Empty,
  1733. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1734. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1735. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1736. sw.ToString (), "#07");
  1737. sw.GetStringBuilder ().Length = 0;
  1738. xtw = new XmlTextWriter (sw);
  1739. xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
  1740. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
  1741. sw.ToString (), "#08");
  1742. sw.GetStringBuilder ().Length = 0;
  1743. xtw = new XmlTextWriter (sw);
  1744. xtw.WriteDocType ("test", (string) null,
  1745. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1746. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1747. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1748. sw.ToString (), "#09");
  1749. sw.GetStringBuilder ().Length = 0;
  1750. xtw = new XmlTextWriter (sw);
  1751. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1752. (string) null, "sub");
  1753. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1754. " \"\"[sub]>",
  1755. sw.ToString (), "#10");
  1756. sw.GetStringBuilder ().Length = 0;
  1757. xtw = new XmlTextWriter (sw);
  1758. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1759. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1760. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1761. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1762. sw.ToString (), "#11");
  1763. sw.GetStringBuilder ().Length = 0;
  1764. xtw = new XmlTextWriter (sw);
  1765. xtw.WriteDocType ("test", (string) null, (string) null, "sub");
  1766. Assert.AreEqual ("<!DOCTYPE test[sub]>",
  1767. sw.ToString (), "#12");
  1768. sw.GetStringBuilder ().Length = 0;
  1769. xtw = new XmlTextWriter (sw);
  1770. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1771. (string) null, (string) null);
  1772. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1773. " \"\">",
  1774. sw.ToString (), "#13");
  1775. sw.GetStringBuilder ().Length = 0;
  1776. xtw = new XmlTextWriter (sw);
  1777. xtw.WriteDocType ("test", (string) null,
  1778. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1779. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1780. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1781. sw.ToString (), "#14");
  1782. sw.GetStringBuilder ().Length = 0;
  1783. xtw = new XmlTextWriter (sw);
  1784. xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
  1785. Assert.AreEqual ("<!DOCTYPE test>",
  1786. sw.ToString (), "#15");
  1787. }
  1788. [Test]
  1789. [ExpectedException (typeof (ArgumentException))]
  1790. public void WriteDocType_EmptyName ()
  1791. {
  1792. xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1793. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1794. }
  1795. [Test]
  1796. [ExpectedException (typeof (ArgumentException))]
  1797. public void WriteDocType_NullName ()
  1798. {
  1799. xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1800. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1801. }
  1802. [Test] // bug #76095
  1803. public void SurrogatePairsInWriteString ()
  1804. {
  1805. MemoryStream ms = new MemoryStream ();
  1806. XmlWriter writer = new XmlTextWriter(ms, null);
  1807. writer.WriteElementString("a", "\ud800\udf39");
  1808. writer.Close();
  1809. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1810. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1811. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1812. }
  1813. [Test]
  1814. public void InvalidCharIsWrittenAsSillyReferences ()
  1815. {
  1816. // I can't say how MS XmlTextWriter is silly.
  1817. // The expected output is *not* well-formed XML.
  1818. // Everyone have to make sure that he or she does
  1819. // not write invalid characters directly so that
  1820. // the output XML string can be fed by other XML
  1821. // processors.
  1822. // The funny thing is that XmlTextWriter spends
  1823. // significant performance on checking invalid
  1824. // characters, but results in nothing.
  1825. xtw.WriteElementString ("a", "\x0");
  1826. NUnit.Framework.Assert.AreEqual ("<a>&#x0;</a>",
  1827. StringWriterText);
  1828. }
  1829. [Test] // see also bug #77082
  1830. public void WriteDocTypeIndent ()
  1831. {
  1832. string expected = String.Format (@"<?xml version='1.0'?>{0}<!DOCTYPE root PUBLIC '' 'urn:foo'[]>{0}<root />", Environment.NewLine);
  1833. xtw.Formatting = Formatting.Indented;
  1834. xtw.WriteProcessingInstruction ("xml", "version='1.0'");
  1835. xtw.WriteDocType ("root", "", "urn:foo", "");
  1836. xtw.WriteStartElement ("root");
  1837. xtw.WriteEndElement ();
  1838. xtw.Close ();
  1839. Assert.AreEqual (expected, StringWriterText);
  1840. }
  1841. [Test]
  1842. [ExpectedException (typeof (InvalidOperationException))]
  1843. public void WriteDocTypeTwice ()
  1844. {
  1845. xtw.WriteDocType ("root", "", "urn:foo", "");
  1846. xtw.WriteDocType ("root", "", "urn:foo", "");
  1847. }
  1848. [Test]
  1849. [ExpectedException (typeof (InvalidOperationException))]
  1850. public void XmlDeclAfterDocType ()
  1851. {
  1852. xtw.WriteDocType ("root", "", "urn:foo", "");
  1853. xtw.WriteStartDocument ();
  1854. }
  1855. [Test]
  1856. [ExpectedException (typeof (InvalidOperationException))]
  1857. public void XmlDeclAfterWhitespace ()
  1858. {
  1859. xtw.WriteWhitespace (" ");
  1860. xtw.WriteStartDocument ();
  1861. }
  1862. [Test]
  1863. [ExpectedException (typeof (InvalidOperationException))]
  1864. public void XmlDeclAfterPI ()
  1865. {
  1866. xtw.WriteProcessingInstruction ("pi", "");
  1867. xtw.WriteStartDocument ();
  1868. }
  1869. [Test]
  1870. public void WriteRawEmptyCloseStartTag ()
  1871. {
  1872. xtw.WriteStartElement ("stream", "stream","http://etherx.jabber.org/streams");
  1873. xtw.WriteAttributeString ("version", "1.0");
  1874. xtw.WriteAttributeString ("to", "[email protected]");
  1875. xtw.WriteAttributeString ("from", "server");
  1876. xtw.WriteAttributeString ("xmlns", "jabber:client");
  1877. xtw.WriteRaw ("");// Ensure that the tag is closed
  1878. xtw.Flush ();
  1879. Assert.AreEqual ("<stream:stream version='1.0' to='[email protected]' from='server' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>", StringWriterText);
  1880. }
  1881. [Test] // bug #78148
  1882. public void UpdateFormattingOnTheFly ()
  1883. {
  1884. XmlTextWriter w = new XmlTextWriter (TextWriter.Null);
  1885. w.WriteStartElement ("test");
  1886. w.Formatting = Formatting.Indented;
  1887. }
  1888. [Test] // bug #78598
  1889. public void WriteGlobalAttributeInDefaultNS ()
  1890. {
  1891. StringWriter sw = new StringWriter ();
  1892. XmlTextWriter tw = new XmlTextWriter (sw);
  1893. string ns = "http://schemas.xmlsoap.org/soap/envelope/";
  1894. tw.WriteStartElement ("Envelope");
  1895. tw.WriteAttributeString ("xmlns", ns);
  1896. int start = sw.ToString ().Length;
  1897. tw.WriteStartElement ("UserInfo");
  1898. tw.WriteStartAttribute ("actor", ns);
  1899. tw.WriteEndAttribute ();
  1900. tw.WriteEndElement ();
  1901. tw.WriteEndElement ();
  1902. Assert.IsTrue (sw.ToString ().IndexOf (ns, start) > 0);
  1903. }
  1904. [Test]
  1905. public void WriteCommentPIAndIndent ()
  1906. {
  1907. StringWriter sw = new StringWriter ();
  1908. XmlTextWriter w = new XmlTextWriter (sw);
  1909. w.Formatting = Formatting.Indented;
  1910. w.WriteStartElement ("foo");
  1911. w.WriteComment ("test");
  1912. w.WriteProcessingInstruction ("PI", "");
  1913. w.WriteStartElement ("child");
  1914. w.WriteEndElement ();
  1915. w.WriteComment ("test");
  1916. w.WriteString ("STRING");
  1917. w.WriteEndElement ();
  1918. Assert.AreEqual (String.Format (@"<foo>{0} <!--test-->{0} <?PI ?>{0} <child />{0} <!--test-->STRING</foo>", Environment.NewLine), sw.ToString ());
  1919. }
  1920. [Test]
  1921. public void WriteBinHexAttribute () // for bug #79019
  1922. {
  1923. XmlWriter writer = new XmlTextWriter (TextWriter.Null);
  1924. writer.WriteStartElement ("test");
  1925. byte [] buffer1 = new byte [] {200, 155};
  1926. writer.WriteStartAttribute ("key", "");
  1927. writer.WriteBinHex (buffer1, 0, buffer1.Length);
  1928. writer.WriteEndAttribute ();
  1929. writer.WriteEndElement ();
  1930. }
  1931. [Test]
  1932. public void LookupNamespace ()
  1933. {
  1934. StringWriter sw = new StringWriter ();
  1935. XmlTextWriter xw = new XmlTextWriter (sw);
  1936. xw.Formatting = Formatting.Indented;
  1937. string q1 = "urn:test";
  1938. string q1prefix_first= "q1";
  1939. // Ensure we get a different reference for the string "q1"
  1940. string q1prefix_second = ("q1" + "a").Substring(0,2);
  1941. xw.WriteStartElement("document");
  1942. xw.WriteStartElement("item");
  1943. xw.WriteStartElement (q1prefix_first, "addMedia", q1);
  1944. xw.WriteEndElement();
  1945. xw.WriteEndElement();
  1946. xw.WriteStartElement("item");
  1947. xw.WriteStartElement (q1prefix_second, "addMedia", q1);
  1948. xw.WriteEndElement();
  1949. xw.WriteEndElement();
  1950. xw.WriteEndElement();
  1951. string xml = sw.ToString ();
  1952. int first = xml.IndexOf ("xmlns");
  1953. Assert.IsTrue (xml.IndexOf ("xmlns", first + 5) > 0);
  1954. }
  1955. [Test]
  1956. public void WriteAttributePrefixedNullNamespace ()
  1957. {
  1958. StringWriter sw = new StringWriter ();
  1959. XmlWriter xw = new XmlTextWriter (sw);
  1960. xw.WriteStartElement ("root");
  1961. xw.WriteAttributeString ("xmlns", "abc", null, "uri:abcnamespace");
  1962. xw.WriteAttributeString ("abc", "def", null, "value");
  1963. xw.WriteEndElement ();
  1964. Assert.AreEqual ("<root xmlns:abc=\"uri:abcnamespace\" abc:def=\"value\" />", sw.ToString ());
  1965. }
  1966. [Test]
  1967. public void WriteElementPrefixedNullNamespace ()
  1968. {
  1969. StringWriter sw = new StringWriter ();
  1970. XmlWriter xw = new XmlTextWriter (sw);
  1971. xw.WriteStartElement ("root");
  1972. xw.WriteAttributeString ("xmlns", "abc", null, "uri:abcnamespace");
  1973. xw.WriteStartElement ("abc", "def", null);
  1974. xw.WriteEndElement ();
  1975. xw.WriteEndElement ();
  1976. Assert.AreEqual ("<root xmlns:abc=\"uri:abcnamespace\"><abc:def /></root>", sw.ToString ());
  1977. }
  1978. #if NET_2_0
  1979. [Test]
  1980. [ExpectedException (typeof (InvalidOperationException))]
  1981. public void RejectWritingAtErrorState ()
  1982. {
  1983. try {
  1984. xtw.WriteEndElement ();
  1985. } catch (Exception) {
  1986. }
  1987. xtw.WriteStartElement ("foo");
  1988. }
  1989. #endif
  1990. }
  1991. }