XmlDocumentTests.cs 41 KB

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