XmlDocumentTests.cs 37 KB

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