XmlDocumentTests.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // System.Xml.XmlDocumentTests
  2. //
  3. // Authors:
  4. // Jason Diamond <[email protected]>
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond, Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. using System.IO;
  15. using System.Text;
  16. using NUnit.Framework;
  17. #if NET_2_0
  18. using InvalidNodeTypeArgException = System.ArgumentException;
  19. #else // it makes less sense
  20. using InvalidNodeTypeArgException = System.ArgumentOutOfRangeException;
  21. #endif
  22. namespace MonoTests.System.Xml
  23. {
  24. [TestFixture]
  25. public class XmlDocumentTests
  26. {
  27. private XmlDocument document;
  28. private ArrayList eventStrings = new ArrayList();
  29. // These Event* methods support the TestEventNode* Tests in this file.
  30. // Most of them are event handlers for the XmlNodeChangedEventHandler
  31. // delegate.
  32. private void EventStringAdd(string eventName, XmlNodeChangedEventArgs e)
  33. {
  34. string oldParent = (e.OldParent != null) ? e.OldParent.Name : "<none>";
  35. string newParent = (e.NewParent != null) ? e.NewParent.Name : "<none>";
  36. eventStrings.Add (String.Format ("{0}, {1}, {2}, {3}, {4}", eventName, e.Action.ToString (), e.Node.OuterXml, oldParent, newParent));
  37. }
  38. private void EventNodeChanged(Object sender, XmlNodeChangedEventArgs e)
  39. {
  40. EventStringAdd ("NodeChanged", e);
  41. }
  42. private void EventNodeChanging (Object sender, XmlNodeChangedEventArgs e)
  43. {
  44. EventStringAdd ("NodeChanging", e);
  45. }
  46. private void EventNodeChangingException (Object sender, XmlNodeChangedEventArgs e)
  47. {
  48. throw new Exception ("don't change the value.");
  49. }
  50. private void EventNodeInserted(Object sender, XmlNodeChangedEventArgs e)
  51. {
  52. EventStringAdd ("NodeInserted", e);
  53. }
  54. private void EventNodeInserting(Object sender, XmlNodeChangedEventArgs e)
  55. {
  56. EventStringAdd ("NodeInserting", e);
  57. }
  58. private void EventNodeInsertingException(Object sender, XmlNodeChangedEventArgs e)
  59. {
  60. throw new Exception ("don't insert the element.");
  61. }
  62. private void EventNodeRemoved(Object sender, XmlNodeChangedEventArgs e)
  63. {
  64. EventStringAdd ("NodeRemoved", e);
  65. }
  66. private void EventNodeRemoving(Object sender, XmlNodeChangedEventArgs e)
  67. {
  68. EventStringAdd ("NodeRemoving", e);
  69. }
  70. private void EventNodeRemovingException(Object sender, XmlNodeChangedEventArgs e)
  71. {
  72. throw new Exception ("don't remove the element.");
  73. }
  74. [SetUp]
  75. public void GetReady ()
  76. {
  77. document = new XmlDocument ();
  78. document.PreserveWhitespace = true;
  79. }
  80. [Test]
  81. public void CreateNodeNodeTypeNameEmptyParams ()
  82. {
  83. try {
  84. document.CreateNode (null, null, null);
  85. Assert.Fail ("Expected an ArgumentException to be thrown.");
  86. } catch (ArgumentException) {}
  87. try {
  88. document.CreateNode ("attribute", null, null);
  89. Assert.Fail ("Expected a NullReferenceException to be thrown.");
  90. } catch (NullReferenceException) {}
  91. try {
  92. document.CreateNode ("attribute", "", null);
  93. Assert.Fail ("Expected an ArgumentException to be thrown.");
  94. } catch (ArgumentException) {}
  95. try {
  96. document.CreateNode ("element", null, null);
  97. Assert.Fail ("Expected a NullReferenceException to be thrown.");
  98. } catch (NullReferenceException) {}
  99. try {
  100. document.CreateNode ("element", "", null);
  101. Assert.Fail ("Expected an ArgumentException to be thrown.");
  102. } catch (ArgumentException) {}
  103. try {
  104. document.CreateNode ("entityreference", null, null);
  105. Assert.Fail ("Expected a NullReferenceException to be thrown.");
  106. } catch (NullReferenceException) {}
  107. }
  108. [Test]
  109. public void CreateNodeInvalidXmlNodeType ()
  110. {
  111. XmlNode node;
  112. try {
  113. node = document.CreateNode (XmlNodeType.EndElement, null, null);
  114. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  115. } catch (InvalidNodeTypeArgException) {}
  116. try {
  117. node = document.CreateNode (XmlNodeType.EndEntity, null, null);
  118. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  119. } catch (InvalidNodeTypeArgException) {}
  120. try {
  121. node = document.CreateNode (XmlNodeType.Entity, null, null);
  122. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  123. } catch (InvalidNodeTypeArgException) {}
  124. try {
  125. node = document.CreateNode (XmlNodeType.None, null, null);
  126. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  127. } catch (InvalidNodeTypeArgException) {}
  128. try {
  129. node = document.CreateNode (XmlNodeType.Notation, null, null);
  130. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  131. } catch (InvalidNodeTypeArgException) {}
  132. // TODO: undocumented allowable type.
  133. node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
  134. Assert.AreEqual (XmlNodeType.XmlDeclaration, node.NodeType);
  135. }
  136. [Test]
  137. public void CreateNodeWhichParamIsUsed ()
  138. {
  139. XmlNode node;
  140. // No constructor params for Document, DocumentFragment.
  141. node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
  142. Assert.AreEqual (String.Empty, ((XmlCDataSection)node).Value);
  143. node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
  144. Assert.AreEqual (String.Empty, ((XmlComment)node).Value);
  145. node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
  146. Assert.IsNull (((XmlDocumentType)node).Value);
  147. // TODO: add this back in to test when it's implemented.
  148. // node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
  149. // Assert.IsNull (((XmlEntityReference)node).Value);
  150. // TODO: add this back in to test when it's implemented.
  151. // node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
  152. // Assert.AreEqual (String.Empty, ((XmlProcessingInstruction)node).Value);
  153. node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
  154. Assert.AreEqual (String.Empty, ((XmlSignificantWhitespace)node).Value);
  155. node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
  156. Assert.AreEqual (String.Empty, ((XmlText)node).Value);
  157. node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
  158. Assert.AreEqual (String.Empty, ((XmlWhitespace)node).Value);
  159. node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
  160. Assert.AreEqual ("version=\"1.0\"", ((XmlDeclaration)node).Value);
  161. }
  162. [Test]
  163. #if NET_2_0
  164. [Category ("NotDotNet")] // enbug in 2.0
  165. #endif
  166. public void CreateNodeNodeTypeName ()
  167. {
  168. XmlNode node;
  169. try {
  170. node = document.CreateNode ("foo", null, null);
  171. Assert.Fail ("Expected an ArgumentException to be thrown.");
  172. } catch (ArgumentException) {}
  173. // .NET 2.0 fails here.
  174. node = document.CreateNode("attribute", "foo", null);
  175. Assert.AreEqual (XmlNodeType.Attribute, node.NodeType);
  176. node = document.CreateNode("cdatasection", null, null);
  177. Assert.AreEqual (XmlNodeType.CDATA, node.NodeType);
  178. node = document.CreateNode("comment", null, null);
  179. Assert.AreEqual (XmlNodeType.Comment, node.NodeType);
  180. node = document.CreateNode("document", null, null);
  181. Assert.AreEqual (XmlNodeType.Document, node.NodeType);
  182. // TODO: test which constructor this ended up calling,
  183. // i.e. reuse underlying NameTable or not?
  184. node = document.CreateNode("documentfragment", null, null);
  185. Assert.AreEqual (XmlNodeType.DocumentFragment, node.NodeType);
  186. node = document.CreateNode("documenttype", null, null);
  187. Assert.AreEqual (XmlNodeType.DocumentType, node.NodeType);
  188. node = document.CreateNode("element", "foo", null);
  189. Assert.AreEqual (XmlNodeType.Element, node.NodeType);
  190. // TODO: add this back in to test when it's implemented.
  191. // ---> It is implemented, but it is LAMESPEC that allows null entity reference name.
  192. // node = document.CreateNode("entityreference", "foo", null);
  193. // Assert.AreEqual (XmlNodeType.EntityReference, node.NodeType);
  194. // LAMESPEC: null PI name is silly.
  195. // node = document.CreateNode("processinginstruction", null, null);
  196. // Assert.AreEqual (XmlNodeType.ProcessingInstruction, node.NodeType);
  197. node = document.CreateNode("significantwhitespace", null, null);
  198. Assert.AreEqual (XmlNodeType.SignificantWhitespace, node.NodeType);
  199. node = document.CreateNode("text", null, null);
  200. Assert.AreEqual (XmlNodeType.Text, node.NodeType);
  201. node = document.CreateNode("whitespace", null, null);
  202. Assert.AreEqual (XmlNodeType.Whitespace, node.NodeType);
  203. }
  204. [Test]
  205. public void DocumentElement ()
  206. {
  207. Assert.IsNull (document.DocumentElement);
  208. XmlElement element = document.CreateElement ("foo", "bar", "http://foo/");
  209. Assert.IsNotNull (element);
  210. Assert.AreEqual ("foo", element.Prefix);
  211. Assert.AreEqual ("bar", element.LocalName);
  212. Assert.AreEqual ("http://foo/", element.NamespaceURI);
  213. Assert.AreEqual ("foo:bar", element.Name);
  214. Assert.AreSame (element, document.AppendChild (element));
  215. Assert.AreSame (element, document.DocumentElement);
  216. }
  217. [Test]
  218. public void DocumentEmpty()
  219. {
  220. Assert.AreEqual ("", document.OuterXml, "Incorrect output for empty document.");
  221. }
  222. [Test]
  223. public void EventNodeChanged()
  224. {
  225. XmlElement element;
  226. XmlComment comment;
  227. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  228. // Node that is part of the document.
  229. document.AppendChild (document.CreateElement ("foo"));
  230. comment = document.CreateComment ("bar");
  231. document.DocumentElement.AppendChild (comment);
  232. Assert.AreEqual ("<!--bar-->", document.DocumentElement.InnerXml);
  233. comment.Value = "baz";
  234. Assert.IsTrue (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  235. Assert.AreEqual ("<!--baz-->", document.DocumentElement.InnerXml);
  236. // Node that isn't part of the document but created by the document.
  237. element = document.CreateElement ("foo");
  238. comment = document.CreateComment ("bar");
  239. element.AppendChild (comment);
  240. Assert.AreEqual ("<!--bar-->", element.InnerXml);
  241. comment.Value = "baz";
  242. Assert.IsTrue (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  243. Assert.AreEqual ("<!--baz-->", element.InnerXml);
  244. /*
  245. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  246. // Node that is part of the document.
  247. element = document.CreateElement ("foo");
  248. element.InnerText = "bar";
  249. document.AppendChild(element);
  250. element.InnerText = "baz";
  251. Assert.IsTrue (eventStrings.Contains("NodeChanged, Change, baz, foo, foo"));
  252. // Node that isn't part of the document but created by the document.
  253. element = document.CreateElement("qux");
  254. element.InnerText = "quux";
  255. element.InnerText = "quuux";
  256. Assert.IsTrue (eventStrings.Contains("NodeChanged, Change, quuux, qux, qux"));
  257. */
  258. }
  259. [Test]
  260. public void EventNodeChanging()
  261. {
  262. XmlElement element;
  263. XmlComment comment;
  264. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChanging);
  265. // Node that is part of the document.
  266. document.AppendChild (document.CreateElement ("foo"));
  267. comment = document.CreateComment ("bar");
  268. document.DocumentElement.AppendChild (comment);
  269. Assert.AreEqual ("<!--bar-->", document.DocumentElement.InnerXml);
  270. comment.Value = "baz";
  271. Assert.IsTrue (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  272. Assert.AreEqual ("<!--baz-->", document.DocumentElement.InnerXml);
  273. // Node that isn't part of the document but created by the document.
  274. element = document.CreateElement ("foo");
  275. comment = document.CreateComment ("bar");
  276. element.AppendChild (comment);
  277. Assert.AreEqual ("<!--bar-->", element.InnerXml);
  278. comment.Value = "baz";
  279. Assert.IsTrue (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  280. Assert.AreEqual ("<!--baz-->", element.InnerXml);
  281. // If an exception is thrown the Document returns to original state.
  282. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  283. element = document.CreateElement("foo");
  284. comment = document.CreateComment ("bar");
  285. element.AppendChild (comment);
  286. Assert.AreEqual ("<!--bar-->", element.InnerXml);
  287. try
  288. {
  289. comment.Value = "baz";
  290. Assert.Fail ("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  291. } catch (Exception) {}
  292. Assert.AreEqual ("<!--bar-->", element.InnerXml);
  293. // Yes it's a bit anal but this tests whether the node changing event exception fires before the
  294. // ArgumentOutOfRangeException. Turns out it does so that means our implementation needs to raise
  295. // the node changing event before doing any work.
  296. try
  297. {
  298. comment.ReplaceData(-1, 0, "qux");
  299. Assert.Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  300. }
  301. catch (Exception) {}
  302. /*
  303. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  304. // Node that is part of the document.
  305. element = document.CreateElement ("foo");
  306. element.InnerText = "bar";
  307. document.AppendChild(element);
  308. element.InnerText = "baz";
  309. Assert.IsTrue (eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  310. // Node that isn't part of the document but created by the document.
  311. element = document.CreateElement("foo");
  312. element.InnerText = "bar";
  313. element.InnerText = "baz";
  314. Assert.IsTrue (eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  315. // If an exception is thrown the Document returns to original state.
  316. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  317. element = document.CreateElement("foo");
  318. element.InnerText = "bar";
  319. try {
  320. element.InnerText = "baz";
  321. Assert.Fail ("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  322. } catch (Exception) {}
  323. Assert.AreEqual ("bar", element.InnerText);
  324. */
  325. }
  326. [Test]
  327. public void EventNodeInserted()
  328. {
  329. XmlElement element;
  330. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  331. // Inserted 'foo' element to the document.
  332. element = document.CreateElement ("foo");
  333. document.AppendChild (element);
  334. Assert.IsTrue (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, #document"));
  335. // Append child on node in document
  336. element = document.CreateElement ("foo");
  337. document.DocumentElement.AppendChild (element);
  338. Assert.IsTrue (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, foo"));
  339. // Append child on node not in document but created by document
  340. element = document.CreateElement ("bar");
  341. element.AppendChild(document.CreateElement ("bar"));
  342. Assert.IsTrue (eventStrings.Contains("NodeInserted, Insert, <bar />, <none>, bar"));
  343. }
  344. [Test]
  345. public void EventNodeInserting()
  346. {
  347. XmlElement element;
  348. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  349. // Inserting 'foo' element to the document.
  350. element = document.CreateElement ("foo");
  351. document.AppendChild (element);
  352. Assert.IsTrue (eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, #document"));
  353. // Append child on node in document
  354. element = document.CreateElement ("foo");
  355. document.DocumentElement.AppendChild (element);
  356. Assert.IsTrue (eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, foo"));
  357. // Append child on node not in document but created by document
  358. element = document.CreateElement ("bar");
  359. Assert.AreEqual (0, element.ChildNodes.Count);
  360. element.AppendChild (document.CreateElement ("bar"));
  361. Assert.IsTrue (eventStrings.Contains ("NodeInserting, Insert, <bar />, <none>, bar"));
  362. Assert.AreEqual (1, element.ChildNodes.Count);
  363. // If an exception is thrown the Document returns to original state.
  364. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInsertingException);
  365. Assert.AreEqual (1, element.ChildNodes.Count);
  366. try
  367. {
  368. element.AppendChild (document.CreateElement("baz"));
  369. Assert.Fail ("Expected an exception to be thrown by the NodeInserting event handler method EventNodeInsertingException().");
  370. }
  371. catch (Exception) {}
  372. Assert.AreEqual (1, element.ChildNodes.Count);
  373. }
  374. [Test]
  375. public void EventNodeRemoved()
  376. {
  377. XmlElement element;
  378. XmlElement element2;
  379. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  380. // Removed 'bar' element from 'foo' outside document.
  381. element = document.CreateElement ("foo");
  382. element2 = document.CreateElement ("bar");
  383. element.AppendChild (element2);
  384. Assert.AreEqual (1, element.ChildNodes.Count);
  385. element.RemoveChild (element2);
  386. Assert.IsTrue (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  387. Assert.AreEqual (0, element.ChildNodes.Count);
  388. /*
  389. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  390. // RemoveAll.
  391. element = document.CreateElement ("foo");
  392. element2 = document.CreateElement ("bar");
  393. element.AppendChild(element2);
  394. Assert.AreEqual (1, element.ChildNodes.Count);
  395. element.RemoveAll();
  396. Assert.IsTrue (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  397. Assert.AreEqual (0, element.ChildNodes.Count);
  398. */
  399. // Removed 'bar' element from 'foo' inside document.
  400. element = document.CreateElement ("foo");
  401. document.AppendChild (element);
  402. element = document.CreateElement ("bar");
  403. document.DocumentElement.AppendChild (element);
  404. Assert.AreEqual (1, document.DocumentElement.ChildNodes.Count);
  405. document.DocumentElement.RemoveChild (element);
  406. Assert.IsTrue (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  407. Assert.AreEqual (0, document.DocumentElement.ChildNodes.Count);
  408. }
  409. [Test]
  410. public void EventNodeRemoving()
  411. {
  412. XmlElement element;
  413. XmlElement element2;
  414. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  415. // Removing 'bar' element from 'foo' outside document.
  416. element = document.CreateElement ("foo");
  417. element2 = document.CreateElement ("bar");
  418. element.AppendChild (element2);
  419. Assert.AreEqual (1, element.ChildNodes.Count);
  420. element.RemoveChild (element2);
  421. Assert.IsTrue (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  422. Assert.AreEqual (0, element.ChildNodes.Count);
  423. /*
  424. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  425. // RemoveAll.
  426. element = document.CreateElement ("foo");
  427. element2 = document.CreateElement ("bar");
  428. element.AppendChild(element2);
  429. Assert.AreEqual (1, element.ChildNodes.Count);
  430. element.RemoveAll();
  431. Assert.IsTrue (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  432. Assert.AreEqual (0, element.ChildNodes.Count);
  433. */
  434. // Removing 'bar' element from 'foo' inside document.
  435. element = document.CreateElement ("foo");
  436. document.AppendChild (element);
  437. element = document.CreateElement ("bar");
  438. document.DocumentElement.AppendChild (element);
  439. Assert.AreEqual (1, document.DocumentElement.ChildNodes.Count);
  440. document.DocumentElement.RemoveChild (element);
  441. Assert.IsTrue (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  442. Assert.AreEqual (0, document.DocumentElement.ChildNodes.Count);
  443. // If an exception is thrown the Document returns to original state.
  444. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemovingException);
  445. element.AppendChild (element2);
  446. Assert.AreEqual (1, element.ChildNodes.Count);
  447. try
  448. {
  449. element.RemoveChild(element2);
  450. Assert.Fail ("Expected an exception to be thrown by the NodeRemoving event handler method EventNodeRemovingException().");
  451. }
  452. catch (Exception) {}
  453. Assert.AreEqual (1, element.ChildNodes.Count);
  454. }
  455. [Test]
  456. public void GetElementsByTagNameNoNameSpace ()
  457. {
  458. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  459. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  460. <author>Tom Clancy</author><price>6.95</price></book><book>
  461. <title>Bourne Identity</title><author>Robert Ludlum</author>
  462. <price>9.95</price></book><Fluffer><Nutter><book>
  463. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  464. <price>9.95</price></book></Nutter></Fluffer></library>";
  465. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  466. document = new XmlDocument ();
  467. document.Load (memoryStream);
  468. XmlNodeList bookList = document.GetElementsByTagName ("book");
  469. Assert.AreEqual (4, bookList.Count, "GetElementsByTagName (string) returned incorrect count.");
  470. }
  471. [Test]
  472. public void GetElementsByTagNameUsingNameSpace ()
  473. {
  474. StringBuilder xml = new StringBuilder ();
  475. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  476. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  477. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  478. xml.Append ("<North:author>John Doe</North:author> " );
  479. xml.Append ("<North:price>34.95</North:price></North:book> " );
  480. xml.Append ("<South:book type=\"fiction\"> " );
  481. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  482. xml.Append ("<South:author>Tom Clancy</South:author> " );
  483. xml.Append ("<South:price>6.95</South:price></South:book> " );
  484. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  485. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  486. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  487. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  488. document = new XmlDocument ();
  489. document.Load (memoryStream);
  490. XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.goo.com");
  491. Assert.AreEqual (2, bookList.Count, "GetElementsByTagName (string, uri) returned incorrect count.");
  492. }
  493. [Test]
  494. public void GetElementsByTagNameNs2 ()
  495. {
  496. document.LoadXml (@"<root>
  497. <x:a xmlns:x='urn:foo' id='a'>
  498. <y:a xmlns:y='urn:foo' id='b'/>
  499. <x:a id='c' />
  500. <z id='d' />
  501. text node
  502. <?a processing instruction ?>
  503. <x:w id='e'/>
  504. </x:a>
  505. </root>");
  506. // id='b' has different prefix. Should not caught by (name),
  507. // while should caught by (name, ns).
  508. XmlNodeList nl = document.GetElementsByTagName ("x:a");
  509. Assert.AreEqual (2, nl.Count);
  510. Assert.AreEqual ("a", nl [0].Attributes ["id"].Value);
  511. Assert.AreEqual ("c", nl [1].Attributes ["id"].Value);
  512. nl = document.GetElementsByTagName ("a", "urn:foo");
  513. Assert.AreEqual (3, nl.Count);
  514. Assert.AreEqual ("a", nl [0].Attributes ["id"].Value);
  515. Assert.AreEqual ("b", nl [1].Attributes ["id"].Value);
  516. Assert.AreEqual ("c", nl [2].Attributes ["id"].Value);
  517. // name wildcard
  518. nl = document.GetElementsByTagName ("*");
  519. Assert.AreEqual (6, nl.Count);
  520. Assert.AreEqual ("root", nl [0].Name);
  521. Assert.AreEqual ("a", nl [1].Attributes ["id"].Value);
  522. Assert.AreEqual ("b", nl [2].Attributes ["id"].Value);
  523. Assert.AreEqual ("c", nl [3].Attributes ["id"].Value);
  524. Assert.AreEqual ("d", nl [4].Attributes ["id"].Value);
  525. Assert.AreEqual ("e", nl [5].Attributes ["id"].Value);
  526. // wildcard - local and ns
  527. nl = document.GetElementsByTagName ("*", "*");
  528. Assert.AreEqual (6, nl.Count);
  529. Assert.AreEqual ("root", nl [0].Name);
  530. Assert.AreEqual ("a", nl [1].Attributes ["id"].Value);
  531. Assert.AreEqual ("b", nl [2].Attributes ["id"].Value);
  532. Assert.AreEqual ("c", nl [3].Attributes ["id"].Value);
  533. Assert.AreEqual ("d", nl [4].Attributes ["id"].Value);
  534. Assert.AreEqual ("e", nl [5].Attributes ["id"].Value);
  535. // namespace wildcard - namespace
  536. nl = document.GetElementsByTagName ("*", "urn:foo");
  537. Assert.AreEqual (4, nl.Count);
  538. Assert.AreEqual ("a", nl [0].Attributes ["id"].Value);
  539. Assert.AreEqual ("b", nl [1].Attributes ["id"].Value);
  540. Assert.AreEqual ("c", nl [2].Attributes ["id"].Value);
  541. Assert.AreEqual ("e", nl [3].Attributes ["id"].Value);
  542. // namespace wildcard - local only. I dare say, such usage is not XML-ish!
  543. nl = document.GetElementsByTagName ("a", "*");
  544. Assert.AreEqual (3, nl.Count);
  545. Assert.AreEqual ("a", nl [0].Attributes ["id"].Value);
  546. Assert.AreEqual ("b", nl [1].Attributes ["id"].Value);
  547. Assert.AreEqual ("c", nl [2].Attributes ["id"].Value);
  548. }
  549. [Test]
  550. public void Implementation ()
  551. {
  552. Assert.IsNotNull (new XmlDocument ().Implementation);
  553. }
  554. [Test]
  555. public void InnerAndOuterXml ()
  556. {
  557. Assert.AreEqual (String.Empty, document.InnerXml);
  558. Assert.AreEqual (document.InnerXml, document.OuterXml);
  559. XmlDeclaration declaration = document.CreateXmlDeclaration ("1.0", null, null);
  560. document.AppendChild (declaration);
  561. Assert.AreEqual ("<?xml version=\"1.0\"?>", document.InnerXml);
  562. Assert.AreEqual (document.InnerXml, document.OuterXml);
  563. XmlElement element = document.CreateElement ("foo");
  564. document.AppendChild (element);
  565. Assert.AreEqual ("<?xml version=\"1.0\"?><foo />", document.InnerXml);
  566. Assert.AreEqual (document.InnerXml, document.OuterXml);
  567. XmlComment comment = document.CreateComment ("bar");
  568. document.DocumentElement.AppendChild (comment);
  569. Assert.AreEqual ("<?xml version=\"1.0\"?><foo><!--bar--></foo>", document.InnerXml);
  570. Assert.AreEqual (document.InnerXml, document.OuterXml);
  571. XmlText text = document.CreateTextNode ("baz");
  572. document.DocumentElement.AppendChild (text);
  573. Assert.AreEqual ("<?xml version=\"1.0\"?><foo><!--bar-->baz</foo>", document.InnerXml);
  574. Assert.AreEqual (document.InnerXml, document.OuterXml);
  575. element = document.CreateElement ("quux");
  576. element.SetAttribute ("quuux", "squonk");
  577. document.DocumentElement.AppendChild (element);
  578. Assert.AreEqual ("<?xml version=\"1.0\"?><foo><!--bar-->baz<quux quuux=\"squonk\" /></foo>", document.InnerXml);
  579. Assert.AreEqual (document.InnerXml, document.OuterXml);
  580. }
  581. [Test]
  582. public void LoadWithSystemIOStream ()
  583. {
  584. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  585. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  586. <author>Tom Clancy</author><price>6.95</price></book><book>
  587. <title>Bourne Identity</title><author>Robert Ludlum</author>
  588. <price>9.95</price></book><Fluffer><Nutter><book>
  589. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  590. <price>9.95</price></book></Nutter></Fluffer></library>";
  591. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  592. document = new XmlDocument ();
  593. document.Load (memoryStream);
  594. Assert.AreEqual (true, document.HasChildNodes, "Not Loaded From IOStream");
  595. }
  596. [Test]
  597. public void LoadXmlReaderNamespacesFalse ()
  598. {
  599. XmlTextReader xtr = new XmlTextReader (
  600. "<root xmlns='urn:foo' />", XmlNodeType.Document, null);
  601. xtr.Namespaces = false;
  602. document.Load (xtr); // Don't complain about xmlns attribute with its namespaceURI == String.Empty.
  603. }
  604. [Test]
  605. public void LoadXmlCDATA ()
  606. {
  607. document.LoadXml ("<foo><![CDATA[bar]]></foo>");
  608. Assert.IsTrue (document.DocumentElement.FirstChild.NodeType == XmlNodeType.CDATA);
  609. Assert.AreEqual ("bar", document.DocumentElement.FirstChild.Value);
  610. }
  611. [Test]
  612. public void LoadXMLComment()
  613. {
  614. // XmlTextReader needs to throw this exception
  615. // try {
  616. // document.LoadXml("<!--foo-->");
  617. // Assert.Fail ("XmlException should have been thrown.");
  618. // }
  619. // catch (XmlException e) {
  620. // Assert.AreEqual ("The root element is missing.", e.Message, "Exception message doesn't match.");
  621. // }
  622. document.LoadXml ("<foo><!--Comment--></foo>");
  623. Assert.IsTrue (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Comment);
  624. Assert.AreEqual ("Comment", document.DocumentElement.FirstChild.Value);
  625. document.LoadXml (@"<foo><!--bar--></foo>");
  626. Assert.AreEqual ("bar", ((XmlComment)document.FirstChild.FirstChild).Data, "Incorrect target.");
  627. }
  628. [Test]
  629. public void LoadXmlElementSingle ()
  630. {
  631. Assert.IsNull (document.DocumentElement);
  632. document.LoadXml ("<foo/>");
  633. Assert.IsNotNull (document.DocumentElement);
  634. Assert.AreSame (document.FirstChild, document.DocumentElement);
  635. Assert.AreEqual (String.Empty, document.DocumentElement.Prefix);
  636. Assert.AreEqual ("foo", document.DocumentElement.LocalName);
  637. Assert.AreEqual (String.Empty, document.DocumentElement.NamespaceURI);
  638. Assert.AreEqual ("foo", document.DocumentElement.Name);
  639. }
  640. [Test]
  641. public void LoadXmlElementWithAttributes ()
  642. {
  643. Assert.IsNull (document.DocumentElement);
  644. document.LoadXml ("<foo bar='baz' quux='quuux' hoge='hello &amp; world' />");
  645. XmlElement documentElement = document.DocumentElement;
  646. Assert.AreEqual ("baz", documentElement.GetAttribute ("bar"));
  647. Assert.AreEqual ("quuux", documentElement.GetAttribute ("quux"));
  648. Assert.AreEqual ("hello & world", documentElement.GetAttribute ("hoge"));
  649. Assert.AreEqual ("hello & world", documentElement.Attributes ["hoge"].Value);
  650. Assert.AreEqual (1, documentElement.GetAttributeNode ("hoge").ChildNodes.Count);
  651. }
  652. [Test]
  653. public void LoadXmlElementWithChildElement ()
  654. {
  655. document.LoadXml ("<foo><bar/></foo>");
  656. Assert.IsTrue (document.ChildNodes.Count == 1);
  657. Assert.IsTrue (document.FirstChild.ChildNodes.Count == 1);
  658. Assert.AreEqual ("foo", document.DocumentElement.LocalName);
  659. Assert.AreEqual ("bar", document.DocumentElement.FirstChild.LocalName);
  660. }
  661. [Test]
  662. public void LoadXmlElementWithTextNode ()
  663. {
  664. document.LoadXml ("<foo>bar</foo>");
  665. Assert.IsTrue (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Text);
  666. Assert.AreEqual ("bar", document.DocumentElement.FirstChild.Value);
  667. }
  668. [Test]
  669. public void LoadXmlExceptionClearsDocument ()
  670. {
  671. document.LoadXml ("<foo/>");
  672. Assert.IsTrue (document.FirstChild != null);
  673. try {
  674. document.LoadXml ("<123/>");
  675. Assert.Fail ("An XmlException should have been thrown.");
  676. } catch (XmlException) {}
  677. Assert.IsTrue (document.FirstChild == null);
  678. }
  679. [Test]
  680. public void LoadXmlProcessingInstruction ()
  681. {
  682. document.LoadXml (@"<?foo bar='baaz' quux='quuux'?><quuuux></quuuux>");
  683. Assert.AreEqual ("foo", ((XmlProcessingInstruction)document.FirstChild).Target, "Incorrect target.");
  684. Assert.AreEqual ("bar='baaz' quux='quuux'", ((XmlProcessingInstruction)document.FirstChild).Data, "Incorrect data.");
  685. }
  686. [Test]
  687. public void OuterXml ()
  688. {
  689. string xml;
  690. xml = "<root><![CDATA[foo]]></root>";
  691. document.LoadXml (xml);
  692. Assert.AreEqual (xml, document.OuterXml, "XmlDocument with cdata OuterXml is incorrect.");
  693. xml = "<root><!--foo--></root>";
  694. document.LoadXml (xml);
  695. Assert.AreEqual (xml, document.OuterXml, "XmlDocument with comment OuterXml is incorrect.");
  696. xml = "<root><?foo bar?></root>";
  697. document.LoadXml (xml);
  698. Assert.AreEqual (xml, document.OuterXml, "XmlDocument with processing instruction OuterXml is incorrect.");
  699. }
  700. [Test]
  701. public void ParentNodes ()
  702. {
  703. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  704. XmlNode node = document.FirstChild.FirstChild.FirstChild;
  705. Assert.AreEqual ("baz", node.LocalName, "Wrong child found.");
  706. Assert.AreEqual ("bar", node.ParentNode.LocalName, "Wrong parent.");
  707. Assert.AreEqual ("foo", node.ParentNode.ParentNode.LocalName, "Wrong parent.");
  708. Assert.AreEqual ("#document", node.ParentNode.ParentNode.ParentNode.LocalName, "Wrong parent.");
  709. Assert.IsNull (node.ParentNode.ParentNode.ParentNode.ParentNode, "Expected parent to be null.");
  710. }
  711. [Test]
  712. public void RemovedElementNextSibling ()
  713. {
  714. XmlNode node;
  715. XmlNode nextSibling;
  716. document.LoadXml ("<foo><child1/><child2/></foo>");
  717. node = document.DocumentElement.FirstChild;
  718. document.DocumentElement.RemoveChild (node);
  719. nextSibling = node.NextSibling;
  720. Assert.IsNull (nextSibling, "Expected removed node's next sibling to be null.");
  721. }
  722. // ImportNode
  723. [Test]
  724. public void ImportNode ()
  725. {
  726. XmlNode n;
  727. string xlinkURI = "http://www.w3.org/1999/XLink";
  728. string xml1 = "<?xml version='1.0' encoding='utf-8' ?><foo xmlns:xlink='" + xlinkURI + "'><bar a1='v1' xlink:href='#foo'><baz><![CDATA[cdata section.\n\titem 1\n\titem 2\n]]>From here, simple text node.</baz></bar></foo>";
  729. document.LoadXml(xml1);
  730. XmlDocument newDoc = new XmlDocument();
  731. newDoc.LoadXml("<hoge><fuga /></hoge>");
  732. XmlElement bar = document.DocumentElement.FirstChild as XmlElement;
  733. // Attribute
  734. n = newDoc.ImportNode(bar.GetAttributeNode("href", xlinkURI), true);
  735. Assert.AreEqual ("href", n.LocalName, "#ImportNode.Attr.NS.LocalName");
  736. Assert.AreEqual (xlinkURI, n.NamespaceURI, "#ImportNode.Attr.NS.NSURI");
  737. Assert.AreEqual ("#foo", n.Value, "#ImportNode.Attr.NS.Value");
  738. // CDATA
  739. n = newDoc.ImportNode(bar.FirstChild.FirstChild, true);
  740. Assert.AreEqual ("cdata section.\n\titem 1\n\titem 2\n", n.Value, "#ImportNode.CDATA");
  741. // Element
  742. XmlElement e = newDoc.ImportNode(bar, true) as XmlElement;
  743. Assert.AreEqual ("bar", e.Name, "#ImportNode.Element.Name");
  744. Assert.AreEqual ("#foo", e.GetAttribute("href", xlinkURI), "#ImportNode.Element.Attr");
  745. Assert.AreEqual ("baz", e.FirstChild.Name, "#ImportNode.Element.deep");
  746. // Entity Reference:
  747. // [2002/10/14] CreateEntityReference was not implemented.
  748. // document.LoadXml("<!DOCTYPE test PUBLIC 'dummy' [<!ENTITY FOOENT 'foo'>]><root>&FOOENT;</root>");
  749. // n = newDoc.ImportNode(document.DocumentElement.FirstChild);
  750. // Assert.AreEqual ("FOOENT", n.Name, "#ImportNode.EntityReference");
  751. // Assert.AreEqual ("foo_", n.Value, "#ImportNode.EntityReference");
  752. // Processing Instruction
  753. document.LoadXml("<foo><?xml-stylesheet href='foo.xsl' ?></foo>");
  754. XmlProcessingInstruction pi = (XmlProcessingInstruction)newDoc.ImportNode(document.DocumentElement.FirstChild, false);
  755. Assert.AreEqual ("xml-stylesheet", pi.Name, "#ImportNode.ProcessingInstruction.Name");
  756. Assert.AreEqual ("href='foo.xsl'", pi.Data.Trim(), "#ImportNode.ProcessingInstruction.Data");
  757. // Text
  758. document.LoadXml(xml1);
  759. n = newDoc.ImportNode((XmlText)bar.FirstChild.ChildNodes[1], true);
  760. Assert.AreEqual ("From here, simple text node.", n.Value, "#ImportNode.Text");
  761. // XmlDeclaration
  762. document.LoadXml(xml1);
  763. XmlDeclaration decl = (XmlDeclaration)newDoc.ImportNode(document.FirstChild, false);
  764. Assert.AreEqual (XmlNodeType.XmlDeclaration, decl.NodeType, "#ImportNode.XmlDeclaration.Type");
  765. Assert.AreEqual ("utf-8", decl.Encoding, "#ImportNode.XmlDeclaration.Encoding");
  766. }
  767. [Test]
  768. public void NameTable()
  769. {
  770. XmlDocument doc = new XmlDocument();
  771. Assert.IsNotNull (doc.NameTable);
  772. }
  773. [Test]
  774. public void SingleEmptyRootDocument()
  775. {
  776. XmlDocument doc = new XmlDocument();
  777. doc.LoadXml("<root />");
  778. Assert.IsNotNull (doc.DocumentElement);
  779. }
  780. [Test]
  781. public void DocumentWithDoctypeDecl ()
  782. {
  783. XmlDocument doc = new XmlDocument ();
  784. // In fact it is invalid, but it doesn't fail with MS.NET 1.0.
  785. doc.LoadXml ("<!DOCTYPE test><root />");
  786. Assert.IsNotNull (doc.DocumentType);
  787. #if NetworkEnabled
  788. try
  789. {
  790. doc.LoadXml ("<!DOCTYPE test SYSTEM 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  791. } catch (XmlException) {
  792. Assert.Fail ("#DoctypeDecl.System");
  793. }
  794. try {
  795. doc.LoadXml ("<!DOCTYPE test PUBLIC '-//test' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  796. } catch (XmlException) {
  797. Assert.Fail ("#DoctypeDecl.Public");
  798. }
  799. #endif
  800. // Should this be commented out?
  801. doc.LoadXml ("<!DOCTYPE test [<!ELEMENT foo EMPTY>]><test><foo/></test>");
  802. }
  803. [Test]
  804. public void CloneNode ()
  805. {
  806. XmlDocument doc = new XmlDocument ();
  807. doc.LoadXml ("<foo><bar /><baz hoge='fuga'>TEST Text</baz></foo>");
  808. XmlDocument doc2 = (XmlDocument)doc.CloneNode (false);
  809. Assert.AreEqual (0, doc2.ChildNodes.Count, "ShallowCopy");
  810. doc2 = (XmlDocument)doc.CloneNode (true);
  811. Assert.AreEqual ("foo", doc2.DocumentElement.Name, "DeepCopy");
  812. }
  813. [Test]
  814. public void OuterXmlWithDefaultXmlns ()
  815. {
  816. XmlDocument doc = new XmlDocument ();
  817. doc.LoadXml ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username></username></query></iq>");
  818. Assert.AreEqual ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username></username></query></iq>", doc.OuterXml);
  819. }
  820. [Test]
  821. public void PreserveWhitespace ()
  822. {
  823. string input =
  824. "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!-- --> <foo/>";
  825. XmlDocument dom = new XmlDocument ();
  826. XmlTextReader reader = new XmlTextReader (new StringReader (input));
  827. dom.Load (reader);
  828. Assert.AreEqual (XmlNodeType.Element, dom.FirstChild.NextSibling.NextSibling.NodeType);
  829. }
  830. [Test]
  831. public void PreserveWhitespace2 ()
  832. {
  833. XmlDocument doc = new XmlDocument ();
  834. Assert.IsTrue (!doc.PreserveWhitespace);
  835. doc.PreserveWhitespace = true;
  836. XmlDocument d2 = doc.Clone () as XmlDocument;
  837. Assert.IsTrue (!d2.PreserveWhitespace); // i.e. not cloned
  838. d2.AppendChild (d2.CreateElement ("root"));
  839. d2.DocumentElement.AppendChild (d2.CreateWhitespace (" "));
  840. StringWriter sw = new StringWriter ();
  841. d2.WriteTo (new XmlTextWriter (sw));
  842. Assert.AreEqual ("<root> </root>", sw.ToString ());
  843. }
  844. [Test]
  845. public void CreateAttribute ()
  846. {
  847. XmlDocument dom = new XmlDocument ();
  848. // Check that null prefix and namespace are allowed and
  849. // equivalent to ""
  850. XmlAttribute attr = dom.CreateAttribute (null, "FOO", null);
  851. Assert.AreEqual (attr.Prefix, "");
  852. Assert.AreEqual (attr.NamespaceURI, "");
  853. }
  854. [Test]
  855. public void DocumentTypeNodes ()
  856. {
  857. string entities = "<!ENTITY foo 'foo-ent'>";
  858. string dtd = "<!DOCTYPE root [<!ELEMENT root (#PCDATA)*> " + entities + "]>";
  859. string xml = dtd + "<root>&foo;</root>";
  860. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
  861. document.Load (xvr);
  862. Assert.IsNotNull (document.DocumentType);
  863. Assert.AreEqual (1, document.DocumentType.Entities.Count);
  864. XmlEntity foo = document.DocumentType.Entities.GetNamedItem ("foo") as XmlEntity;
  865. Assert.IsNotNull (foo);
  866. Assert.IsNotNull (document.DocumentType.Entities.GetNamedItem ("foo", ""));
  867. Assert.AreEqual ("foo", foo.Name);
  868. Assert.IsNull (foo.Value);
  869. Assert.AreEqual ("foo-ent", foo.InnerText);
  870. }
  871. [Test]
  872. public void DTDEntityAttributeHandling ()
  873. {
  874. string dtd = "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
  875. string xml = dtd + "<root>&foo;</root>";
  876. XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document,null);
  877. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  878. xvr.ValidationType = ValidationType.None;
  879. document.Load (xvr);
  880. // Don't include default attributes here.
  881. Assert.AreEqual (xml, document.OuterXml);
  882. Assert.AreEqual ("hoge-def", document.DocumentElement.GetAttribute ("hoge"));
  883. }
  884. // [Test] Comment out in the meantime.
  885. // public void LoadExternalUri ()
  886. // {
  887. // // set any URL of well-formed XML.
  888. // document.Load ("http://www.go-mono.com/index.rss");
  889. // }
  890. // [Test] comment out in the meantime.
  891. // public void LoadDocumentWithIgnoreSection ()
  892. // {
  893. // // set any URL of well-formed XML.
  894. // document.Load ("xmlfiles/test.xml");
  895. // }
  896. [Test]
  897. [ExpectedException (typeof (XmlException))]
  898. public void LoadThrowsUndeclaredEntity ()
  899. {
  900. string ent1 = "<!ENTITY ent 'entity string'>";
  901. string ent2 = "<!ENTITY ent2 '<foo/><foo/>'>]>";
  902. string dtd = "<!DOCTYPE root[<!ELEMENT root (#PCDATA|foo)*>" + ent1 + ent2;
  903. string xml = dtd + "<root>&ent3;&ent2;</root>";
  904. XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
  905. document.Load (xtr);
  906. xtr.Close ();
  907. }
  908. [Test]
  909. public void CreateEntityReferencesWithoutDTD ()
  910. {
  911. document.RemoveAll ();
  912. document.AppendChild (document.CreateElement ("root"));
  913. document.DocumentElement.AppendChild (document.CreateEntityReference ("foo"));
  914. }
  915. [Test]
  916. public void LoadEntityReference ()
  917. {
  918. string xml = "<!DOCTYPE root [<!ELEMENT root (#PCDATA)*><!ENTITY ent 'val'>]><root attr='a &ent; string'>&ent;</root>";
  919. XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
  920. XmlDocument doc = new XmlDocument ();
  921. doc.Load (xtr);
  922. Assert.AreEqual (XmlNodeType.EntityReference, doc.DocumentElement.FirstChild.NodeType, "#text node");
  923. Assert.AreEqual (XmlNodeType.EntityReference, doc.DocumentElement.Attributes [0].ChildNodes [1].NodeType, "#attribute");
  924. }
  925. [Test]
  926. public void ReadNodeEmptyContent ()
  927. {
  928. XmlTextReader xr = new XmlTextReader ("", XmlNodeType.Element, null);
  929. xr.Read ();
  930. Console.WriteLine (xr.NodeType);
  931. XmlNode n = document.ReadNode (xr);
  932. Assert.IsNull (n);
  933. }
  934. [Test]
  935. public void ReadNodeWhitespace ()
  936. {
  937. XmlTextReader xr = new XmlTextReader (" ", XmlNodeType.Element, null);
  938. xr.Read ();
  939. Console.WriteLine (xr.NodeType);
  940. document.PreserveWhitespace = false; // Note this line.
  941. XmlNode n = document.ReadNode (xr);
  942. Assert.IsNotNull (n);
  943. Assert.AreEqual (XmlNodeType.Whitespace, n.NodeType);
  944. }
  945. [Test]
  946. public void SavePreserveWhitespace ()
  947. {
  948. string xml = "<root> <element>text\n</element></root>";
  949. XmlDocument doc = new XmlDocument ();
  950. doc.PreserveWhitespace = true;
  951. doc.LoadXml (xml);
  952. StringWriter sw = new StringWriter ();
  953. doc.Save (sw);
  954. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + xml, sw.ToString ());
  955. doc.PreserveWhitespace = false;
  956. sw = new StringWriter ();
  957. doc.Save (sw);
  958. string NEL = Environment.NewLine;
  959. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>"
  960. + NEL + "<root> <element>text"
  961. + "\n</element></root>",
  962. sw.ToString ());
  963. }
  964. [Test]
  965. public void ReadNodeEntityReferenceFillsChildren ()
  966. {
  967. string dtd = "<!DOCTYPE root [<!ELEMENT root (#PCDATA)*><!ENTITY ent 'val'>]>";
  968. string xml = dtd + "<root attr='a &ent; string'>&ent;</root>";
  969. XmlValidatingReader reader = new XmlValidatingReader (
  970. xml, XmlNodeType.Document, null);
  971. reader.EntityHandling = EntityHandling.ExpandCharEntities;
  972. reader.ValidationType = ValidationType.None;
  973. //skip the doctype delcaration
  974. reader.Read ();
  975. reader.Read ();
  976. XmlDocument doc = new XmlDocument ();
  977. doc.Load (reader);
  978. Assert.AreEqual (1,
  979. doc.DocumentElement.FirstChild.ChildNodes.Count);
  980. }
  981. [Test]
  982. public void LoadTreatsFixedAttributesAsIfItExisted ()
  983. {
  984. string xml = @"<!DOCTYPE foo [<!ELEMENT foo EMPTY><!ATTLIST foo xmlns CDATA #FIXED 'urn:foo'>]><foo />";
  985. XmlDocument doc = new XmlDocument ();
  986. doc.Load (new StringReader (xml));
  987. Assert.AreEqual ("urn:foo", doc.DocumentElement.NamespaceURI);
  988. }
  989. [Test]
  990. public void Bug79468 () // XmlNameEntryCache bug
  991. {
  992. string xml = "<?xml version='1.0' encoding='UTF-8'?>"
  993. + "<ns0:DebtAmountRequest xmlns:ns0='http://whatever'>"
  994. + " <Signature xmlns='http://www.w3.org/2000/09/xmldsig#' />"
  995. + "</ns0:DebtAmountRequest>";
  996. XmlDocument doc = new XmlDocument ();
  997. doc.LoadXml (xml);
  998. XmlNodeList nodeList = doc.GetElementsByTagName ("Signature");
  999. }
  1000. class MyXmlDocument : XmlDocument
  1001. {
  1002. public override XmlAttribute CreateAttribute (string p, string l, string n)
  1003. {
  1004. return base.CreateAttribute (p, "hijacked", n);
  1005. }
  1006. }
  1007. [Test]
  1008. public void UseOverridenCreateAttribute ()
  1009. {
  1010. XmlDocument doc = new MyXmlDocument ();
  1011. doc.LoadXml ("<root a='sane' />");
  1012. Assert.IsNotNull (doc.DocumentElement.GetAttributeNode ("hijacked"));
  1013. Assert.IsNull (doc.DocumentElement.GetAttributeNode ("a"));
  1014. }
  1015. [Test]
  1016. public void LoadFromMiddleOfDocument ()
  1017. {
  1018. // bug #598953
  1019. string xml = @"<?xml version='1.0' encoding='utf-8' ?>
  1020. <Racal>
  1021. <Ports>
  1022. <ConsolePort value='9998' />
  1023. </Ports>
  1024. </Racal>";
  1025. var r = new XmlTextReader (new StringReader (xml));
  1026. r.WhitespaceHandling = WhitespaceHandling.All;
  1027. r.MoveToContent ();
  1028. r.Read ();
  1029. var doc = new XmlDocument ();
  1030. doc.Load (r);
  1031. Assert.AreEqual (XmlNodeType.EndElement, r.NodeType, "#1");
  1032. }
  1033. }
  1034. }