XmlDocumentTests.cs 40 KB

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