XmlTextWriterTests.cs 74 KB

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