XmlDocumentTests.cs 41 KB

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