XmlDocumentTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. //
  2. // System.Xml.XmlDocumentTests
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Kral Ferch <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond, Kral Ferch
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Xml;
  13. using System.IO;
  14. using System.Text;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. public class XmlDocumentTests : TestCase
  19. {
  20. public XmlDocumentTests () : base ("MonoTests.System.Xml.XmlDocumentTests testsuite") {}
  21. public XmlDocumentTests (string name) : base (name) {}
  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. protected override void SetUp ()
  70. {
  71. document = new XmlDocument ();
  72. document.PreserveWhitespace = true;
  73. }
  74. public void TestCreateNodeNodeTypeNameEmptyParams ()
  75. {
  76. XmlNode node;
  77. try {
  78. node = document.CreateNode (null, null, null);
  79. Fail ("Expected an ArgumentException to be thrown.");
  80. } catch (ArgumentException) {}
  81. try {
  82. node = document.CreateNode ("attribute", null, null);
  83. Fail ("Expected a NullReferenceException to be thrown.");
  84. } catch (NullReferenceException) {}
  85. try {
  86. node = document.CreateNode ("attribute", "", null);
  87. Fail ("Expected an ArgumentException to be thrown.");
  88. } catch (ArgumentException) {}
  89. try {
  90. node = document.CreateNode ("element", null, null);
  91. Fail ("Expected a NullReferenceException to be thrown.");
  92. } catch (NullReferenceException) {}
  93. try {
  94. node = document.CreateNode ("element", "", null);
  95. Fail ("Expected an ArgumentException to be thrown.");
  96. } catch (ArgumentException) {}
  97. try {
  98. node = document.CreateNode ("entityreference", null, null);
  99. Fail ("Expected a NullReferenceException to be thrown.");
  100. } catch (NullReferenceException) {}
  101. }
  102. public void TestCreateNodeInvalidXmlNodeType ()
  103. {
  104. XmlNode node;
  105. try {
  106. node = document.CreateNode (XmlNodeType.EndElement, null, null);
  107. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  108. } catch (ArgumentOutOfRangeException) {}
  109. try {
  110. node = document.CreateNode (XmlNodeType.EndEntity, null, null);
  111. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  112. } catch (ArgumentOutOfRangeException) {}
  113. try {
  114. node = document.CreateNode (XmlNodeType.Entity, null, null);
  115. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  116. } catch (ArgumentOutOfRangeException) {}
  117. try {
  118. node = document.CreateNode (XmlNodeType.None, null, null);
  119. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  120. } catch (ArgumentOutOfRangeException) {}
  121. try {
  122. node = document.CreateNode (XmlNodeType.Notation, null, null);
  123. Fail ("Expected an ArgumentOutOfRangeException to be thrown.");
  124. } catch (ArgumentOutOfRangeException) {}
  125. // TODO: undocumented allowable type.
  126. node = document.CreateNode (XmlNodeType.XmlDeclaration, null, null);
  127. AssertEquals (XmlNodeType.XmlDeclaration, node.NodeType);
  128. }
  129. public void TestCreateNodeWhichParamIsUsed ()
  130. {
  131. XmlNode node;
  132. // No constructor params for Document, DocumentFragment.
  133. node = document.CreateNode (XmlNodeType.CDATA, "a", "b", "c");
  134. AssertEquals (String.Empty, ((XmlCDataSection)node).Value);
  135. node = document.CreateNode (XmlNodeType.Comment, "a", "b", "c");
  136. AssertEquals (String.Empty, ((XmlComment)node).Value);
  137. node = document.CreateNode (XmlNodeType.DocumentType, "a", "b", "c");
  138. AssertNull (((XmlDocumentType)node).Value);
  139. // TODO: add this back in to test when it's implemented.
  140. // node = document.CreateNode (XmlNodeType.EntityReference, "a", "b", "c");
  141. // AssertNull (((XmlEntityReference)node).Value);
  142. node = document.CreateNode (XmlNodeType.ProcessingInstruction, "a", "b", "c");
  143. AssertEquals (String.Empty, ((XmlProcessingInstruction)node).Value);
  144. node = document.CreateNode (XmlNodeType.SignificantWhitespace, "a", "b", "c");
  145. AssertEquals (String.Empty, ((XmlSignificantWhitespace)node).Value);
  146. node = document.CreateNode (XmlNodeType.Text, "a", "b", "c");
  147. AssertEquals (String.Empty, ((XmlText)node).Value);
  148. node = document.CreateNode (XmlNodeType.Whitespace, "a", "b", "c");
  149. AssertEquals (String.Empty, ((XmlWhitespace)node).Value);
  150. node = document.CreateNode (XmlNodeType.XmlDeclaration, "a", "b", "c");
  151. AssertEquals ("version=\"1.0\"", ((XmlDeclaration)node).Value);
  152. }
  153. public void TestCreateNodeNodeTypeName ()
  154. {
  155. XmlNode node;
  156. try {
  157. node = document.CreateNode ("foo", null, null);
  158. Fail ("Expected an ArgumentException to be thrown.");
  159. } catch (ArgumentException) {}
  160. node = document.CreateNode("attribute", "foo", null);
  161. AssertEquals (XmlNodeType.Attribute, node.NodeType);
  162. node = document.CreateNode("cdatasection", null, null);
  163. AssertEquals (XmlNodeType.CDATA, node.NodeType);
  164. node = document.CreateNode("comment", null, null);
  165. AssertEquals (XmlNodeType.Comment, node.NodeType);
  166. node = document.CreateNode("document", null, null);
  167. AssertEquals (XmlNodeType.Document, node.NodeType);
  168. // TODO: test which constructor this ended up calling,
  169. // i.e. reuse underlying NameTable or not?
  170. // TODO: add this back in to test when it's implemented.
  171. // node = document.CreateNode("documentfragment", null, null);
  172. // AssertEquals (XmlNodeType.DocumentFragment, node.NodeType);
  173. node = document.CreateNode("documenttype", null, null);
  174. AssertEquals (XmlNodeType.DocumentType, node.NodeType);
  175. node = document.CreateNode("element", "foo", null);
  176. AssertEquals (XmlNodeType.Element, node.NodeType);
  177. // TODO: add this back in to test when it's implemented.
  178. // node = document.CreateNode("entityreference", "foo", null);
  179. // AssertEquals (XmlNodeType.EntityReference, node.NodeType);
  180. node = document.CreateNode("processinginstruction", null, null);
  181. AssertEquals (XmlNodeType.ProcessingInstruction, node.NodeType);
  182. node = document.CreateNode("significantwhitespace", null, null);
  183. AssertEquals (XmlNodeType.SignificantWhitespace, node.NodeType);
  184. node = document.CreateNode("text", null, null);
  185. AssertEquals (XmlNodeType.Text, node.NodeType);
  186. node = document.CreateNode("whitespace", null, null);
  187. AssertEquals (XmlNodeType.Whitespace, node.NodeType);
  188. }
  189. public void TestDocumentElement ()
  190. {
  191. AssertNull (document.DocumentElement);
  192. XmlElement element = document.CreateElement ("foo", "bar", "http://foo/");
  193. AssertNotNull (element);
  194. AssertEquals ("foo", element.Prefix);
  195. AssertEquals ("bar", element.LocalName);
  196. AssertEquals ("http://foo/", element.NamespaceURI);
  197. AssertEquals ("foo:bar", element.Name);
  198. AssertSame (element, document.AppendChild (element));
  199. AssertSame (element, document.DocumentElement);
  200. }
  201. public void TestDocumentEmpty()
  202. {
  203. AssertEquals ("Incorrect output for empty document.", "", document.OuterXml);
  204. }
  205. public void TestEventNodeChanged()
  206. {
  207. XmlElement element;
  208. XmlComment comment;
  209. document.NodeChanged += new XmlNodeChangedEventHandler (this.EventNodeChanged);
  210. // Node that is part of the document.
  211. document.AppendChild (document.CreateElement ("foo"));
  212. comment = document.CreateComment ("bar");
  213. document.DocumentElement.AppendChild (comment);
  214. AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
  215. comment.Value = "baz";
  216. Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  217. AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
  218. // Node that isn't part of the document but created by the document.
  219. element = document.CreateElement ("foo");
  220. comment = document.CreateComment ("bar");
  221. element.AppendChild (comment);
  222. AssertEquals ("<!--bar-->", element.InnerXml);
  223. comment.Value = "baz";
  224. Assert (eventStrings.Contains ("NodeChanged, Change, <!--baz-->, foo, foo"));
  225. AssertEquals ("<!--baz-->", element.InnerXml);
  226. /*
  227. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  228. // Node that is part of the document.
  229. element = document.CreateElement ("foo");
  230. element.InnerText = "bar";
  231. document.AppendChild(element);
  232. element.InnerText = "baz";
  233. Assert(eventStrings.Contains("NodeChanged, Change, baz, foo, foo"));
  234. // Node that isn't part of the document but created by the document.
  235. element = document.CreateElement("qux");
  236. element.InnerText = "quux";
  237. element.InnerText = "quuux";
  238. Assert(eventStrings.Contains("NodeChanged, Change, quuux, qux, qux"));
  239. */
  240. }
  241. public void TestEventNodeChanging()
  242. {
  243. XmlElement element;
  244. XmlComment comment;
  245. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChanging);
  246. // Node that is part of the document.
  247. document.AppendChild (document.CreateElement ("foo"));
  248. comment = document.CreateComment ("bar");
  249. document.DocumentElement.AppendChild (comment);
  250. AssertEquals ("<!--bar-->", document.DocumentElement.InnerXml);
  251. comment.Value = "baz";
  252. Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  253. AssertEquals ("<!--baz-->", document.DocumentElement.InnerXml);
  254. // Node that isn't part of the document but created by the document.
  255. element = document.CreateElement ("foo");
  256. comment = document.CreateComment ("bar");
  257. element.AppendChild (comment);
  258. AssertEquals ("<!--bar-->", element.InnerXml);
  259. comment.Value = "baz";
  260. Assert (eventStrings.Contains ("NodeChanging, Change, <!--bar-->, foo, foo"));
  261. AssertEquals ("<!--baz-->", element.InnerXml);
  262. // If an exception is thrown the Document returns to original state.
  263. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  264. element = document.CreateElement("foo");
  265. comment = document.CreateComment ("bar");
  266. element.AppendChild (comment);
  267. AssertEquals ("<!--bar-->", element.InnerXml);
  268. try
  269. {
  270. comment.Value = "baz";
  271. Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  272. } catch (Exception) {}
  273. AssertEquals ("<!--bar-->", element.InnerXml);
  274. // Yes it's a bit anal but this tests whether the node changing event exception fires before the
  275. // ArgumentOutOfRangeException. Turns out it does so that means our implementation needs to raise
  276. // the node changing event before doing any work.
  277. try
  278. {
  279. comment.ReplaceData(-1, 0, "qux");
  280. Fail("Expected an ArgumentOutOfRangeException to be thrown.");
  281. }
  282. catch (Exception) {}
  283. /*
  284. TODO: Insert this when XmlNode.InnerText() and XmlNode.InnerXml() have been implemented.
  285. // Node that is part of the document.
  286. element = document.CreateElement ("foo");
  287. element.InnerText = "bar";
  288. document.AppendChild(element);
  289. element.InnerText = "baz";
  290. Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  291. // Node that isn't part of the document but created by the document.
  292. element = document.CreateElement("foo");
  293. element.InnerText = "bar";
  294. element.InnerText = "baz";
  295. Assert(eventStrings.Contains("NodeChanging, Change, bar, foo, foo"));
  296. // If an exception is thrown the Document returns to original state.
  297. document.NodeChanging += new XmlNodeChangedEventHandler (this.EventNodeChangingException);
  298. element = document.CreateElement("foo");
  299. element.InnerText = "bar";
  300. try {
  301. element.InnerText = "baz";
  302. Fail("Expected an exception to be thrown by the NodeChanging event handler method EventNodeChangingException().");
  303. } catch (Exception) {}
  304. AssertEquals("bar", element.InnerText);
  305. */
  306. }
  307. public void TestEventNodeInserted()
  308. {
  309. XmlElement element;
  310. document.NodeInserted += new XmlNodeChangedEventHandler (this.EventNodeInserted);
  311. // Inserted 'foo' element to the document.
  312. element = document.CreateElement ("foo");
  313. document.AppendChild (element);
  314. Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, #document"));
  315. // Append child on node in document
  316. element = document.CreateElement ("foo");
  317. document.DocumentElement.AppendChild (element);
  318. Assert (eventStrings.Contains ("NodeInserted, Insert, <foo />, <none>, foo"));
  319. // Append child on node not in document but created by document
  320. element = document.CreateElement ("bar");
  321. element.AppendChild(document.CreateElement ("bar"));
  322. Assert(eventStrings.Contains("NodeInserted, Insert, <bar />, <none>, bar"));
  323. }
  324. public void TestEventNodeInserting()
  325. {
  326. XmlElement element;
  327. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInserting);
  328. // Inserting 'foo' element to the document.
  329. element = document.CreateElement ("foo");
  330. document.AppendChild (element);
  331. Assert (eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, #document"));
  332. // Append child on node in document
  333. element = document.CreateElement ("foo");
  334. document.DocumentElement.AppendChild (element);
  335. Assert(eventStrings.Contains ("NodeInserting, Insert, <foo />, <none>, foo"));
  336. // Append child on node not in document but created by document
  337. element = document.CreateElement ("bar");
  338. AssertEquals (0, element.ChildNodes.Count);
  339. element.AppendChild (document.CreateElement ("bar"));
  340. Assert (eventStrings.Contains ("NodeInserting, Insert, <bar />, <none>, bar"));
  341. AssertEquals (1, element.ChildNodes.Count);
  342. // If an exception is thrown the Document returns to original state.
  343. document.NodeInserting += new XmlNodeChangedEventHandler (this.EventNodeInsertingException);
  344. AssertEquals (1, element.ChildNodes.Count);
  345. try
  346. {
  347. element.AppendChild (document.CreateElement("baz"));
  348. Fail ("Expected an exception to be thrown by the NodeInserting event handler method EventNodeInsertingException().");
  349. }
  350. catch (Exception) {}
  351. AssertEquals (1, element.ChildNodes.Count);
  352. }
  353. public void TestEventNodeRemoved()
  354. {
  355. XmlElement element;
  356. XmlElement element2;
  357. document.NodeRemoved += new XmlNodeChangedEventHandler (this.EventNodeRemoved);
  358. // Removed 'bar' element from 'foo' outside document.
  359. element = document.CreateElement ("foo");
  360. element2 = document.CreateElement ("bar");
  361. element.AppendChild (element2);
  362. AssertEquals (1, element.ChildNodes.Count);
  363. element.RemoveChild (element2);
  364. Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  365. AssertEquals (0, element.ChildNodes.Count);
  366. /*
  367. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  368. // RemoveAll.
  369. element = document.CreateElement ("foo");
  370. element2 = document.CreateElement ("bar");
  371. element.AppendChild(element2);
  372. AssertEquals(1, element.ChildNodes.Count);
  373. element.RemoveAll();
  374. Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  375. AssertEquals(0, element.ChildNodes.Count);
  376. */
  377. // Removed 'bar' element from 'foo' inside document.
  378. element = document.CreateElement ("foo");
  379. document.AppendChild (element);
  380. element = document.CreateElement ("bar");
  381. document.DocumentElement.AppendChild (element);
  382. AssertEquals (1, document.DocumentElement.ChildNodes.Count);
  383. document.DocumentElement.RemoveChild (element);
  384. Assert (eventStrings.Contains ("NodeRemoved, Remove, <bar />, foo, <none>"));
  385. AssertEquals (0, document.DocumentElement.ChildNodes.Count);
  386. }
  387. public void TestEventNodeRemoving()
  388. {
  389. XmlElement element;
  390. XmlElement element2;
  391. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemoving);
  392. // Removing 'bar' element from 'foo' outside document.
  393. element = document.CreateElement ("foo");
  394. element2 = document.CreateElement ("bar");
  395. element.AppendChild (element2);
  396. AssertEquals (1, element.ChildNodes.Count);
  397. element.RemoveChild (element2);
  398. Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  399. AssertEquals (0, element.ChildNodes.Count);
  400. /*
  401. * TODO: put this test back in when AttributeCollection.RemoveAll() is implemented.
  402. // RemoveAll.
  403. element = document.CreateElement ("foo");
  404. element2 = document.CreateElement ("bar");
  405. element.AppendChild(element2);
  406. AssertEquals(1, element.ChildNodes.Count);
  407. element.RemoveAll();
  408. Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  409. AssertEquals(0, element.ChildNodes.Count);
  410. */
  411. // Removing 'bar' element from 'foo' inside document.
  412. element = document.CreateElement ("foo");
  413. document.AppendChild (element);
  414. element = document.CreateElement ("bar");
  415. document.DocumentElement.AppendChild (element);
  416. AssertEquals (1, document.DocumentElement.ChildNodes.Count);
  417. document.DocumentElement.RemoveChild (element);
  418. Assert (eventStrings.Contains ("NodeRemoving, Remove, <bar />, foo, <none>"));
  419. AssertEquals (0, document.DocumentElement.ChildNodes.Count);
  420. // If an exception is thrown the Document returns to original state.
  421. document.NodeRemoving += new XmlNodeChangedEventHandler (this.EventNodeRemovingException);
  422. element.AppendChild (element2);
  423. AssertEquals (1, element.ChildNodes.Count);
  424. try
  425. {
  426. element.RemoveChild(element2);
  427. Fail ("Expected an exception to be thrown by the NodeRemoving event handler method EventNodeRemovingException().");
  428. }
  429. catch (Exception) {}
  430. AssertEquals (1, element.ChildNodes.Count);
  431. }
  432. public void TestGetElementsByTagNameNoNameSpace ()
  433. {
  434. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  435. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  436. <author>Tom Clancy</author><price>6.95</price></book><book>
  437. <title>Bourne Identity</title><author>Robert Ludlum</author>
  438. <price>9.95</price></book><Fluffer><Nutter><book>
  439. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  440. <price>9.95</price></book></Nutter></Fluffer></library>";
  441. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  442. document = new XmlDocument ();
  443. document.Load (memoryStream);
  444. XmlNodeList bookList = document.GetElementsByTagName ("book");
  445. AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
  446. }
  447. public void TestGetElementsByTagNameUsingNameSpace ()
  448. {
  449. StringBuilder xml = new StringBuilder ();
  450. xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
  451. xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
  452. xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
  453. xml.Append ("<North:author>John Doe</North:author> " );
  454. xml.Append ("<North:price>34.95</North:price></North:book> " );
  455. xml.Append ("<South:book type=\"fiction\"> " );
  456. xml.Append ("<South:title>Bear and the Dragon</South:title> " );
  457. xml.Append ("<South:author>Tom Clancy</South:author> " );
  458. xml.Append ("<South:price>6.95</South:price></South:book> " );
  459. xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
  460. xml.Append ("<South:author>Robert Ludlum</South:author> " );
  461. xml.Append ("<South:price>9.95</South:price></South:book></library>");
  462. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
  463. document = new XmlDocument ();
  464. document.Load (memoryStream);
  465. XmlNodeList bookList = document.GetElementsByTagName ("book", "http://www.goo.com");
  466. AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 2, bookList.Count);
  467. }
  468. public void TestInnerAndOuterXml ()
  469. {
  470. AssertEquals (String.Empty, document.InnerXml);
  471. AssertEquals (document.InnerXml, document.OuterXml);
  472. XmlDeclaration declaration = document.CreateXmlDeclaration ("1.0", null, null);
  473. document.AppendChild (declaration);
  474. AssertEquals ("<?xml version=\"1.0\"?>", document.InnerXml);
  475. AssertEquals (document.InnerXml, document.OuterXml);
  476. XmlElement element = document.CreateElement ("foo");
  477. document.AppendChild (element);
  478. AssertEquals ("<?xml version=\"1.0\"?><foo />", document.InnerXml);
  479. AssertEquals (document.InnerXml, document.OuterXml);
  480. XmlComment comment = document.CreateComment ("bar");
  481. document.DocumentElement.AppendChild (comment);
  482. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar--></foo>", document.InnerXml);
  483. AssertEquals (document.InnerXml, document.OuterXml);
  484. XmlText text = document.CreateTextNode ("baz");
  485. document.DocumentElement.AppendChild (text);
  486. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz</foo>", document.InnerXml);
  487. AssertEquals (document.InnerXml, document.OuterXml);
  488. element = document.CreateElement ("quux");
  489. element.SetAttribute ("quuux", "squonk");
  490. document.DocumentElement.AppendChild (element);
  491. AssertEquals ("<?xml version=\"1.0\"?><foo><!--bar-->baz<quux quuux=\"squonk\" /></foo>", document.InnerXml);
  492. AssertEquals (document.InnerXml, document.OuterXml);
  493. }
  494. public void TestLoadWithSystemIOStream ()
  495. {
  496. string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
  497. <price>34.95</price></book><book><title>Bear and the Dragon</title>
  498. <author>Tom Clancy</author><price>6.95</price></book><book>
  499. <title>Bourne Identity</title><author>Robert Ludlum</author>
  500. <price>9.95</price></book><Fluffer><Nutter><book>
  501. <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
  502. <price>9.95</price></book></Nutter></Fluffer></library>";
  503. MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
  504. document = new XmlDocument ();
  505. document.Load (memoryStream);
  506. AssertEquals ("Not Loaded From IOStream", true, document.HasChildNodes);
  507. }
  508. public void TestLoadXmlCDATA ()
  509. {
  510. document.LoadXml ("<foo><![CDATA[bar]]></foo>");
  511. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.CDATA);
  512. AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  513. }
  514. public void TestLoadXMLComment()
  515. {
  516. // XmlTextReader needs to throw this exception
  517. // try {
  518. // document.LoadXml("<!--foo-->");
  519. // Fail("XmlException should have been thrown.");
  520. // }
  521. // catch (XmlException e) {
  522. // AssertEquals("Exception message doesn't match.", "The root element is missing.", e.Message);
  523. // }
  524. document.LoadXml ("<foo><!--Comment--></foo>");
  525. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Comment);
  526. AssertEquals ("Comment", document.DocumentElement.FirstChild.Value);
  527. document.LoadXml (@"<foo><!--bar--></foo>");
  528. AssertEquals ("Incorrect target.", "bar", ((XmlComment)document.FirstChild.FirstChild).Data);
  529. }
  530. public void TestLoadXmlElementSingle ()
  531. {
  532. AssertNull (document.DocumentElement);
  533. document.LoadXml ("<foo/>");
  534. AssertNotNull (document.DocumentElement);
  535. AssertSame (document.FirstChild, document.DocumentElement);
  536. AssertEquals (String.Empty, document.DocumentElement.Prefix);
  537. AssertEquals ("foo", document.DocumentElement.LocalName);
  538. AssertEquals (String.Empty, document.DocumentElement.NamespaceURI);
  539. AssertEquals ("foo", document.DocumentElement.Name);
  540. }
  541. public void TestLoadXmlElementWithAttributes ()
  542. {
  543. AssertNull (document.DocumentElement);
  544. document.LoadXml ("<foo bar='baz' quux='quuux'/>");
  545. XmlElement documentElement = document.DocumentElement;
  546. AssertEquals ("baz", documentElement.GetAttribute ("bar"));
  547. AssertEquals ("quuux", documentElement.GetAttribute ("quux"));
  548. }
  549. public void TestLoadXmlElementWithChildElement ()
  550. {
  551. document.LoadXml ("<foo><bar/></foo>");
  552. Assert (document.ChildNodes.Count == 1);
  553. Assert (document.FirstChild.ChildNodes.Count == 1);
  554. AssertEquals ("foo", document.DocumentElement.LocalName);
  555. AssertEquals ("bar", document.DocumentElement.FirstChild.LocalName);
  556. }
  557. public void TestLoadXmlElementWithTextNode ()
  558. {
  559. document.LoadXml ("<foo>bar</foo>");
  560. Assert (document.DocumentElement.FirstChild.NodeType == XmlNodeType.Text);
  561. AssertEquals ("bar", document.DocumentElement.FirstChild.Value);
  562. }
  563. public void TestLoadXmlExceptionClearsDocument ()
  564. {
  565. document.LoadXml ("<foo/>");
  566. Assert (document.FirstChild != null);
  567. try {
  568. document.LoadXml ("<123/>");
  569. Fail ("An XmlException should have been thrown.");
  570. } catch (XmlException) {}
  571. Assert (document.FirstChild == null);
  572. }
  573. public void TestLoadXmlProcessingInstruction ()
  574. {
  575. document.LoadXml (@"<?foo bar='baaz' quux='quuux'?><quuuux></quuuux>");
  576. AssertEquals ("Incorrect target.", "foo", ((XmlProcessingInstruction)document.FirstChild).Target);
  577. AssertEquals ("Incorrect data.", "bar='baaz' quux='quuux'", ((XmlProcessingInstruction)document.FirstChild).Data);
  578. }
  579. public void TestOuterXml ()
  580. {
  581. string xml;
  582. xml = "<root><![CDATA[foo]]></root>";
  583. document.LoadXml (xml);
  584. AssertEquals("XmlDocument with cdata OuterXml is incorrect.", xml, document.OuterXml);
  585. xml = "<root><!--foo--></root>";
  586. document.LoadXml (xml);
  587. AssertEquals("XmlDocument with comment OuterXml is incorrect.", xml, document.OuterXml);
  588. xml = "<root><?foo bar?></root>";
  589. document.LoadXml (xml);
  590. AssertEquals("XmlDocument with processing instruction OuterXml is incorrect.", xml, document.OuterXml);
  591. }
  592. public void TestParentNodes ()
  593. {
  594. document.LoadXml ("<foo><bar><baz/></bar></foo>");
  595. XmlNode node = document.FirstChild.FirstChild.FirstChild;
  596. AssertEquals ("Wrong child found.", "baz", node.LocalName);
  597. AssertEquals ("Wrong parent.", "bar", node.ParentNode.LocalName);
  598. AssertEquals ("Wrong parent.", "foo", node.ParentNode.ParentNode.LocalName);
  599. AssertEquals ("Wrong parent.", "#document", node.ParentNode.ParentNode.ParentNode.LocalName);
  600. AssertNull ("Expected parent to be null.", node.ParentNode.ParentNode.ParentNode.ParentNode);
  601. }
  602. public void TestRemovedElementNextSibling ()
  603. {
  604. XmlNode node;
  605. XmlNode nextSibling;
  606. document.LoadXml ("<foo><child1/><child2/></foo>");
  607. node = document.DocumentElement.FirstChild;
  608. document.DocumentElement.RemoveChild (node);
  609. nextSibling = node.NextSibling;
  610. AssertNull ("Expected removed node's next sibling to be null.", nextSibling);
  611. }
  612. // ImportNode
  613. public void TestImportNode ()
  614. {
  615. XmlNode n;
  616. string xlinkURI = "http://www.w3.org/1999/XLink";
  617. 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>";
  618. document.LoadXml(xml1);
  619. XmlDocument newDoc = new XmlDocument();
  620. newDoc.LoadXml("<hoge><fuga /></hoge>");
  621. XmlElement bar = document.DocumentElement.FirstChild as XmlElement;
  622. // Attribute
  623. n = newDoc.ImportNode(bar.GetAttributeNode("href", xlinkURI), true);
  624. AssertEquals("#ImportNode.Attr.NS.LocalName", "href", n.LocalName);
  625. AssertEquals("#ImportNode.Attr.NS.NSURI", xlinkURI, n.NamespaceURI);
  626. AssertEquals("#ImportNode.Attr.NS.Value", "#foo", n.Value);
  627. // CDATA
  628. n = newDoc.ImportNode(bar.FirstChild.FirstChild, true);
  629. AssertEquals("#ImportNode.CDATA", "cdata section.\n\titem 1\n\titem 2\n", n.Value);
  630. // Element
  631. XmlElement e = newDoc.ImportNode(bar, true) as XmlElement;
  632. AssertEquals("#ImportNode.Element.Name", "bar", e.Name);
  633. AssertEquals("#ImportNode.Element.Attr", "#foo", e.GetAttribute("href", xlinkURI));
  634. AssertEquals("#ImportNode.Element.deep", "baz", e.FirstChild.Name);
  635. // Entity Reference:
  636. // [2002/10/14] CreateEntityReference was not implemented.
  637. // document.LoadXml("<!DOCTYPE test PUBLIC 'dummy' [<!ENTITY FOOENT 'foo'>]><root>&FOOENT;</root>");
  638. // n = newDoc.ImportNode(document.DocumentElement.FirstChild);
  639. // AssertEquals("#ImportNode.EntityReference", "FOOENT", n.Name);
  640. // AssertEquals("#ImportNode.EntityReference", "foo_", n.Value);
  641. // Processing Instruction
  642. document.LoadXml("<foo><?xml-stylesheet href='foo.xsl' ?></foo>");
  643. XmlProcessingInstruction pi = (XmlProcessingInstruction)newDoc.ImportNode(document.DocumentElement.FirstChild, false);
  644. AssertEquals("#ImportNode.ProcessingInstruction.Name", "xml-stylesheet", pi.Name);
  645. AssertEquals("#ImportNode.ProcessingInstruction.Data", "href='foo.xsl'", pi.Data.Trim());
  646. // Text
  647. document.LoadXml(xml1);
  648. n = newDoc.ImportNode((XmlText)bar.FirstChild.ChildNodes[1], true);
  649. AssertEquals("#ImportNode.Text", "From here, simple text node.", n.Value);
  650. // XmlDeclaration
  651. document.LoadXml(xml1);
  652. XmlDeclaration decl = (XmlDeclaration)newDoc.ImportNode(document.FirstChild, false);
  653. AssertEquals("#ImportNode.XmlDeclaration.Type", XmlNodeType.XmlDeclaration, decl.NodeType);
  654. AssertEquals("#ImportNode.XmlDeclaration.Encoding", "utf-8", decl.Encoding);
  655. }
  656. public void TestNameTable()
  657. {
  658. XmlDocument doc = new XmlDocument();
  659. AssertNotNull(doc.NameTable);
  660. }
  661. public void TestSingleEmptyRootDocument()
  662. {
  663. XmlDocument doc = new XmlDocument();
  664. doc.LoadXml("<root />");
  665. AssertNotNull(doc.DocumentElement);
  666. }
  667. public void TestDocumentWithDoctypeDecl ()
  668. {
  669. XmlDocument doc = new XmlDocument ();
  670. try {
  671. doc.LoadXml ("<!DOCTYPE test><root />");
  672. } catch (XmlException) {
  673. Fail ("#DoctypeDecl.OnlyName");
  674. }
  675. try
  676. {
  677. doc.LoadXml ("<!DOCTYPE test SYSTEM 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  678. } catch (XmlException) {
  679. Fail("#DoctypeDecl.System");
  680. }
  681. try {
  682. doc.LoadXml ("<!DOCTYPE test PUBLIC '-//test' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><root />");
  683. } catch (XmlException) {
  684. Fail ("#DoctypeDecl.Public");
  685. }
  686. // Should this be commented out?
  687. // try {
  688. // doc.LoadXml ("<!DOCTYPE test [<!ELEMENT foo >]><root />");
  689. // } catch (XmlException) {
  690. // Fail("#DoctypeDecl.ElementDecl");
  691. // }
  692. }
  693. public void TestCloneNode ()
  694. {
  695. XmlDocument doc = new XmlDocument ();
  696. doc.LoadXml ("<foo><bar /><baz hoge='fuga'>TEST Text</baz></foo>");
  697. XmlDocument doc2 = (XmlDocument)doc.CloneNode (false);
  698. AssertEquals ("ShallowCopy", 0, doc2.ChildNodes.Count);
  699. doc2 = (XmlDocument)doc.CloneNode (true);
  700. AssertEquals ("DeepCopy", "foo", doc2.DocumentElement.Name);
  701. }
  702. public void TestOuterXmlWithDefaultXmlns ()
  703. {
  704. XmlDocument doc = new XmlDocument ();
  705. doc.LoadXml ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username></username></query></iq>");
  706. AssertEquals ("<iq type=\"get\" id=\"ATECLIENT_1\"><query xmlns=\"jabber:iq:auth\"><username /></query></iq>", doc.OuterXml);
  707. }
  708. public void TestPreserveWhitespace ()
  709. {
  710. string input =
  711. "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!-- --> <foo/>";
  712. XmlDocument dom = new XmlDocument ();
  713. XmlTextReader reader = new XmlTextReader (new StringReader (input));
  714. dom.Load (reader);
  715. AssertEquals (XmlNodeType.Element, dom.FirstChild.NextSibling.NextSibling.NodeType);
  716. }
  717. }
  718. }