XmlDocumentTests.cs 38 KB

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