XPathNavigatorReaderTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. //
  2. // MonoTests.System.Xml.XPathNavigatorReaderTests
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. using System;
  10. using System.Xml;
  11. using System.Xml.XPath;
  12. using NUnit.Framework;
  13. using MonoTests.System.Xml; // XmlAssert
  14. namespace MonoTests.System.Xml.XPath
  15. {
  16. [TestFixture]
  17. public class XPathNavigatorReaderTests
  18. {
  19. XmlDocument document;
  20. XPathNavigator nav;
  21. XPathDocument xpathDocument;
  22. [SetUp]
  23. public void GetReady ()
  24. {
  25. document = new XmlDocument ();
  26. }
  27. private XPathNavigator GetXmlDocumentNavigator (string xml)
  28. {
  29. document.LoadXml (xml);
  30. return document.CreateNavigator ();
  31. }
  32. private XPathNavigator GetXPathDocumentNavigator (XmlNode node)
  33. {
  34. XmlNodeReader xr = new XmlNodeReader (node);
  35. xpathDocument = new XPathDocument (xr);
  36. return xpathDocument.CreateNavigator ();
  37. }
  38. [Test]
  39. public void ReadSubtree1 ()
  40. {
  41. string xml = "<root/>";
  42. nav = GetXmlDocumentNavigator (xml);
  43. ReadSubtree1 (nav, "#1.");
  44. nav.MoveToRoot ();
  45. nav.MoveToFirstChild ();
  46. ReadSubtree1 (nav, "#2.");
  47. nav = GetXPathDocumentNavigator (document);
  48. ReadSubtree1 (nav, "#3.");
  49. nav.MoveToRoot ();
  50. nav.MoveToFirstChild ();
  51. ReadSubtree1 (nav, "#4.");
  52. }
  53. void ReadSubtree1 (XPathNavigator nav, string label)
  54. {
  55. XmlReader r = nav.ReadSubtree ();
  56. XmlAssert.AssertNode (label + "#1", r,
  57. // NodeType, Depth, IsEmptyElement
  58. XmlNodeType.None, 0, false,
  59. // Name, Prefix, LocalName, NamespaceURI
  60. String.Empty, String.Empty, String.Empty, String.Empty,
  61. // Value, HasValue, AttributeCount, HasAttributes
  62. String.Empty, false, 0, false);
  63. Assert.IsTrue (r.Read (), label + "#2");
  64. XmlAssert.AssertNode (label + "#3", r,
  65. // NodeType, Depth, IsEmptyElement
  66. XmlNodeType.Element, 0, true,
  67. // Name, Prefix, LocalName, NamespaceURI
  68. "root", String.Empty, "root", String.Empty,
  69. // Value, HasValue, AttributeCount, HasAttributes
  70. String.Empty, false, 0, false);
  71. Assert.IsFalse (r.Read (), label + "#4");
  72. }
  73. [Test]
  74. public void ReadSubtree2 ()
  75. {
  76. string xml = "<root></root>";
  77. nav = GetXmlDocumentNavigator (xml);
  78. ReadSubtree2 (nav, "#1.");
  79. nav.MoveToRoot ();
  80. nav.MoveToFirstChild ();
  81. ReadSubtree2 (nav, "#2.");
  82. nav = GetXPathDocumentNavigator (document);
  83. ReadSubtree2 (nav, "#3.");
  84. nav.MoveToRoot ();
  85. nav.MoveToFirstChild ();
  86. ReadSubtree2 (nav, "#4.");
  87. }
  88. void ReadSubtree2 (XPathNavigator nav, string label)
  89. {
  90. XmlReader r = nav.ReadSubtree ();
  91. XmlAssert.AssertNode (label + "#1", r,
  92. // NodeType, Depth, IsEmptyElement
  93. XmlNodeType.None, 0, false,
  94. // Name, Prefix, LocalName, NamespaceURI
  95. String.Empty, String.Empty, String.Empty, String.Empty,
  96. // Value, HasValue, AttributeCount, HasAttributes
  97. String.Empty, false, 0, false);
  98. Assert.IsTrue (r.Read (), label + "#2");
  99. XmlAssert.AssertNode (label + "#3", r,
  100. // NodeType, Depth, IsEmptyElement
  101. XmlNodeType.Element, 0, false,
  102. // Name, Prefix, LocalName, NamespaceURI
  103. "root", String.Empty, "root", String.Empty,
  104. // Value, HasValue, AttributeCount, HasAttributes
  105. String.Empty, false, 0, false);
  106. Assert.IsTrue (r.Read (), label + "#4");
  107. XmlAssert.AssertNode (label + "#5", r,
  108. // NodeType, Depth, IsEmptyElement
  109. XmlNodeType.EndElement, 0, false,
  110. // Name, Prefix, LocalName, NamespaceURI
  111. "root", String.Empty, "root", String.Empty,
  112. // Value, HasValue, AttributeCount, HasAttributes
  113. String.Empty, false, 0, false);
  114. Assert.IsFalse (r.Read (), label + "#6");
  115. }
  116. [Test]
  117. public void ReadSubtree3 ()
  118. {
  119. string xml = "<root attr='value'/>";
  120. nav = GetXmlDocumentNavigator (xml);
  121. ReadSubtree3 (nav, "#1.");
  122. nav.MoveToRoot ();
  123. nav.MoveToFirstChild ();
  124. ReadSubtree3 (nav, "#2.");
  125. nav = GetXPathDocumentNavigator (document);
  126. ReadSubtree3 (nav, "#3.");
  127. nav.MoveToRoot ();
  128. nav.MoveToFirstChild ();
  129. ReadSubtree3 (nav, "#4.");
  130. }
  131. void ReadSubtree3 (XPathNavigator nav, string label)
  132. {
  133. XmlReader r = nav.ReadSubtree ();
  134. XmlAssert.AssertNode (label + "#1", r,
  135. // NodeType, Depth, IsEmptyElement
  136. XmlNodeType.None, 0, false,
  137. // Name, Prefix, LocalName, NamespaceURI
  138. String.Empty, String.Empty, String.Empty, String.Empty,
  139. // Value, HasValue, AttributeCount, HasAttributes
  140. String.Empty, false, 0, false);
  141. Assert.IsTrue (r.Read (), label + "#2");
  142. XmlAssert.AssertNode (label + "#3", r,
  143. // NodeType, Depth, IsEmptyElement
  144. XmlNodeType.Element, 0, true,
  145. // Name, Prefix, LocalName, NamespaceURI
  146. "root", String.Empty, "root", String.Empty,
  147. // Value, HasValue, AttributeCount, HasAttributes
  148. String.Empty, false, 1, true);
  149. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#4");
  150. XmlAssert.AssertNode (label + "#5", r,
  151. // NodeType, Depth, IsEmptyElement
  152. XmlNodeType.Attribute, 1, false,
  153. // Name, Prefix, LocalName, NamespaceURI
  154. "attr", String.Empty, "attr", String.Empty,
  155. // Value, HasValue, AttributeCount, HasAttributes
  156. "value", true, 1, true);
  157. Assert.IsTrue (r.ReadAttributeValue (), label + "#6");
  158. XmlAssert.AssertNode (label + "#7", r,
  159. // NodeType, Depth, IsEmptyElement
  160. XmlNodeType.Text, 2, false,
  161. // Name, Prefix, LocalName, NamespaceURI
  162. String.Empty, String.Empty, String.Empty, String.Empty,
  163. // Value, HasValue, AttributeCount, HasAttributes
  164. "value", true, 1, true);
  165. Assert.IsFalse (r.ReadAttributeValue (), label + "#8");
  166. Assert.IsFalse (r.MoveToNextAttribute (), label + "#9");
  167. Assert.IsTrue (r.MoveToElement (), label + "#10");
  168. Assert.IsFalse (r.Read (), label + "#11");
  169. }
  170. [Test]
  171. public void DocElem_OpenClose_Attribute ()
  172. {
  173. string xml = "<root attr='value'></root>";
  174. nav = GetXmlDocumentNavigator (xml);
  175. DocElem_OpenClose_Attribute (nav, "#1.");
  176. nav.MoveToRoot ();
  177. nav.MoveToFirstChild ();
  178. DocElem_OpenClose_Attribute (nav, "#2.");
  179. nav = GetXPathDocumentNavigator (document);
  180. DocElem_OpenClose_Attribute (nav, "#3.");
  181. nav.MoveToRoot ();
  182. nav.MoveToFirstChild ();
  183. DocElem_OpenClose_Attribute (nav, "#4.");
  184. }
  185. void DocElem_OpenClose_Attribute (XPathNavigator nav, string label)
  186. {
  187. XmlReader r = nav.ReadSubtree ();
  188. XmlAssert.AssertNode (label + "#1", r,
  189. // NodeType, Depth, IsEmptyElement
  190. XmlNodeType.None, 0, false,
  191. // Name, Prefix, LocalName, NamespaceURI
  192. String.Empty, String.Empty, String.Empty, String.Empty,
  193. // Value, HasValue, AttributeCount, HasAttributes
  194. String.Empty, false, 0, false);
  195. Assert.IsTrue (r.Read (), label + "#2");
  196. XmlAssert.AssertNode (label + "#3", r,
  197. // NodeType, Depth, IsEmptyElement
  198. XmlNodeType.Element, 0, false,
  199. // Name, Prefix, LocalName, NamespaceURI
  200. "root", String.Empty, "root", String.Empty,
  201. // Value, HasValue, AttributeCount, HasAttributes
  202. String.Empty, false, 1, true);
  203. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#4");
  204. XmlAssert.AssertNode (label + "#5", r,
  205. // NodeType, Depth, IsEmptyElement
  206. XmlNodeType.Attribute, 1, false,
  207. // Name, Prefix, LocalName, NamespaceURI
  208. "attr", String.Empty, "attr", String.Empty,
  209. // Value, HasValue, AttributeCount, HasAttributes
  210. "value", true, 1, true);
  211. Assert.IsTrue (r.ReadAttributeValue (), label + "#6");
  212. XmlAssert.AssertNode (label + "#7", r,
  213. // NodeType, Depth, IsEmptyElement
  214. XmlNodeType.Text, 2, false,
  215. // Name, Prefix, LocalName, NamespaceURI
  216. String.Empty, String.Empty, String.Empty, String.Empty,
  217. // Value, HasValue, AttributeCount, HasAttributes
  218. "value", true, 1, true);
  219. Assert.IsFalse (r.ReadAttributeValue (), label + "#8");
  220. Assert.IsFalse (r.MoveToNextAttribute (), label + "#9");
  221. Assert.IsTrue (r.MoveToElement (), label + "#10");
  222. Assert.IsTrue (r.Read (), label + "#11");
  223. XmlAssert.AssertNode (label + "#12", r,
  224. // NodeType, Depth, IsEmptyElement
  225. XmlNodeType.EndElement, 0, false,
  226. // Name, Prefix, LocalName, NamespaceURI
  227. "root", String.Empty, "root", String.Empty,
  228. // Value, HasValue, AttributeCount, HasAttributes
  229. String.Empty, false, 0, false);
  230. Assert.IsFalse (r.Read (), label + "#13");
  231. }
  232. [Test]
  233. public void FromChildElement ()
  234. {
  235. string xml = "<root><foo attr='value'>test</foo><bar/></root>";
  236. nav = GetXmlDocumentNavigator (xml);
  237. nav.MoveToFirstChild ();
  238. nav.MoveToFirstChild (); // foo
  239. FromChildElement (nav, "#1.");
  240. nav = GetXPathDocumentNavigator (document);
  241. nav.MoveToFirstChild ();
  242. nav.MoveToFirstChild (); // foo
  243. FromChildElement (nav, "#2.");
  244. }
  245. void FromChildElement (XPathNavigator nav, string label)
  246. {
  247. XmlReader r = nav.ReadSubtree ();
  248. XmlAssert.AssertNode (label + "#1", r,
  249. // NodeType, Depth, IsEmptyElement
  250. XmlNodeType.None, 0, false,
  251. // Name, Prefix, LocalName, NamespaceURI
  252. String.Empty, String.Empty, String.Empty, String.Empty,
  253. // Value, HasValue, AttributeCount, HasAttributes
  254. String.Empty, false, 0, false);
  255. Assert.IsTrue (r.Read (), label + "#2");
  256. XmlAssert.AssertNode (label + "#3", r,
  257. // NodeType, Depth, IsEmptyElement
  258. XmlNodeType.Element, 0, false,
  259. // Name, Prefix, LocalName, NamespaceURI
  260. "foo", String.Empty, "foo", String.Empty,
  261. // Value, HasValue, AttributeCount, HasAttributes
  262. String.Empty, false, 1, true);
  263. Assert.IsTrue (r.Read (), label + "#4");
  264. XmlAssert.AssertNode (label + "#5", r,
  265. // NodeType, Depth, IsEmptyElement
  266. XmlNodeType.Text, 1, false,
  267. // Name, Prefix, LocalName, NamespaceURI
  268. String.Empty, String.Empty, String.Empty, String.Empty,
  269. // Value, HasValue, AttributeCount, HasAttributes
  270. "test", true, 0, false);
  271. Assert.IsTrue (r.Read (), label + "#6");
  272. XmlAssert.AssertNode (label + "#7", r,
  273. // NodeType, Depth, IsEmptyElement
  274. XmlNodeType.EndElement, 0, false,
  275. // Name, Prefix, LocalName, NamespaceURI
  276. "foo", String.Empty, "foo", String.Empty,
  277. // Value, HasValue, AttributeCount, HasAttributes
  278. String.Empty, false, 0, false);
  279. // end at </foo>, without moving toward <bar>.
  280. Assert.IsFalse (r.Read (), label + "#8");
  281. }
  282. [Test]
  283. [Category ("NotDotNet")] // MS bug
  284. [Ignore ("Bug in Microsoft reference source")]
  285. public void AttributesAndNamespaces ()
  286. {
  287. string xml = "<root attr='value' x:a2='v2' xmlns:x='urn:foo' xmlns='urn:default'></root>";
  288. nav = GetXmlDocumentNavigator (xml);
  289. AttributesAndNamespaces (nav, "#1.");
  290. nav.MoveToRoot ();
  291. nav.MoveToFirstChild ();
  292. AttributesAndNamespaces (nav, "#2.");
  293. nav = GetXPathDocumentNavigator (document);
  294. AttributesAndNamespaces (nav, "#3.");
  295. nav.MoveToRoot ();
  296. nav.MoveToFirstChild ();
  297. AttributesAndNamespaces (nav, "#4.");
  298. }
  299. void AttributesAndNamespaces (XPathNavigator nav, string label)
  300. {
  301. XmlReader r = nav.ReadSubtree ();
  302. XmlAssert.AssertNode (label + "#1", r,
  303. // NodeType, Depth, IsEmptyElement
  304. XmlNodeType.None, 0, false,
  305. // Name, Prefix, LocalName, NamespaceURI
  306. String.Empty, String.Empty, String.Empty, String.Empty,
  307. // Value, HasValue, AttributeCount, HasAttributes
  308. String.Empty, false, 0, false);
  309. Assert.IsTrue (r.Read (), label + "#2");
  310. XmlAssert.AssertNode (label + "#3", r,
  311. // NodeType, Depth, IsEmptyElement
  312. XmlNodeType.Element, 0, false,
  313. // Name, Prefix, LocalName, NamespaceURI
  314. "root", String.Empty, "root", "urn:default",
  315. // Value, HasValue, AttributeCount, HasAttributes
  316. String.Empty, false, 4, true);
  317. // Namespaces
  318. Assert.IsTrue (r.MoveToAttribute ("xmlns:x"), label + "#4");
  319. XmlAssert.AssertNode (label + "#5", r,
  320. // NodeType, Depth, IsEmptyElement
  321. XmlNodeType.Attribute, 1, false,
  322. // Name, Prefix, LocalName, NamespaceURI
  323. "xmlns:x", "xmlns", "x",
  324. "http://www.w3.org/2000/xmlns/",
  325. // Value, HasValue, AttributeCount, HasAttributes
  326. "urn:foo", true, 4, true);
  327. Assert.IsTrue (r.ReadAttributeValue (), label + "#6");
  328. ///* MS.NET has a bug here
  329. XmlAssert.AssertNode (label + "#7", r,
  330. // NodeType, Depth, IsEmptyElement
  331. XmlNodeType.Text, 2, false,
  332. // Name, Prefix, LocalName, NamespaceURI
  333. String.Empty, String.Empty, String.Empty, String.Empty,
  334. // Value, HasValue, AttributeCount, HasAttributes
  335. "urn:foo", true, 4, true);
  336. //*/
  337. Assert.IsFalse (r.ReadAttributeValue (), label + "#8");
  338. Assert.IsTrue (r.MoveToAttribute ("xmlns"), label + "#9");
  339. XmlAssert.AssertNode (label + "#10", r,
  340. // NodeType, Depth, IsEmptyElement
  341. XmlNodeType.Attribute, 1, false,
  342. // Name, Prefix, LocalName, NamespaceURI
  343. "xmlns", String.Empty, "xmlns",
  344. "http://www.w3.org/2000/xmlns/",
  345. // Value, HasValue, AttributeCount, HasAttributes
  346. "urn:default", true, 4, true);
  347. Assert.IsTrue (r.ReadAttributeValue (), label + "#11");
  348. ///* MS.NET has a bug here
  349. XmlAssert.AssertNode (label + "#12", r,
  350. // NodeType, Depth, IsEmptyElement
  351. XmlNodeType.Text, 2, false,
  352. // Name, Prefix, LocalName, NamespaceURI
  353. String.Empty, String.Empty, String.Empty, String.Empty,
  354. // Value, HasValue, AttributeCount, HasAttributes
  355. "urn:default", true, 4, true);
  356. //*/
  357. Assert.IsFalse (r.ReadAttributeValue (), label + "#13");
  358. // Attributes
  359. Assert.IsTrue (r.MoveToAttribute ("attr"), label + "#14");
  360. XmlAssert.AssertNode (label + "#15", r,
  361. // NodeType, Depth, IsEmptyElement
  362. XmlNodeType.Attribute, 1, false,
  363. // Name, Prefix, LocalName, NamespaceURI
  364. "attr", String.Empty, "attr", String.Empty,
  365. // Value, HasValue, AttributeCount, HasAttributes
  366. "value", true, 4, true);
  367. Assert.IsTrue (r.ReadAttributeValue (), label + "#16");
  368. XmlAssert.AssertNode (label + "#17", r,
  369. // NodeType, Depth, IsEmptyElement
  370. XmlNodeType.Text, 2, false,
  371. // Name, Prefix, LocalName, NamespaceURI
  372. String.Empty, String.Empty, String.Empty, String.Empty,
  373. // Value, HasValue, AttributeCount, HasAttributes
  374. "value", true, 4, true);
  375. Assert.IsFalse (r.ReadAttributeValue (), label + "#18");
  376. Assert.IsTrue (r.MoveToAttribute ("x:a2"), label + "#19");
  377. XmlAssert.AssertNode (label + "#20", r,
  378. // NodeType, Depth, IsEmptyElement
  379. XmlNodeType.Attribute, 1, false,
  380. // Name, Prefix, LocalName, NamespaceURI
  381. "x:a2", "x", "a2", "urn:foo",
  382. // Value, HasValue, AttributeCount, HasAttributes
  383. "v2", true, 4, true);
  384. Assert.IsTrue (r.ReadAttributeValue (), label + "#21");
  385. XmlAssert.AssertNode (label + "#22", r,
  386. // NodeType, Depth, IsEmptyElement
  387. XmlNodeType.Text, 2, false,
  388. // Name, Prefix, LocalName, NamespaceURI
  389. String.Empty, String.Empty, String.Empty, String.Empty,
  390. // Value, HasValue, AttributeCount, HasAttributes
  391. "v2", true, 4, true);
  392. Assert.IsTrue (r.MoveToElement (), label + "#24");
  393. Assert.IsTrue (r.Read (), label + "#25");
  394. XmlAssert.AssertNode (label + "#26", r,
  395. // NodeType, Depth, IsEmptyElement
  396. XmlNodeType.EndElement, 0, false,
  397. // Name, Prefix, LocalName, NamespaceURI
  398. "root", String.Empty, "root", "urn:default",
  399. // Value, HasValue, AttributeCount, HasAttributes
  400. String.Empty, false, 0, false);
  401. Assert.IsFalse (r.Read (), label + "#27");
  402. }
  403. [Test]
  404. public void MixedContentAndDepth ()
  405. {
  406. string xml = @"<one> <two>Some data.<three>more</three> done.</two> </one>";
  407. nav = GetXmlDocumentNavigator (xml);
  408. MixedContentAndDepth (nav, "#1.");
  409. nav.MoveToRoot ();
  410. nav.MoveToFirstChild ();
  411. MixedContentAndDepth (nav, "#2.");
  412. nav = GetXPathDocumentNavigator (document);
  413. MixedContentAndDepth (nav, "#3.");
  414. nav.MoveToRoot ();
  415. nav.MoveToFirstChild ();
  416. MixedContentAndDepth (nav, "#4.");
  417. }
  418. void MixedContentAndDepth (XPathNavigator nav, string label)
  419. {
  420. XmlReader r = nav.ReadSubtree ();
  421. r.Read ();
  422. XmlAssert.AssertNode (label + "#1", r,
  423. // NodeType, Depth, IsEmptyElement
  424. XmlNodeType.Element, 0, false,
  425. // Name, Prefix, LocalName, NamespaceURI
  426. "one", String.Empty, "one", String.Empty,
  427. // Value, HasValue, AttributeCount, HasAttributes
  428. String.Empty, false, 0, false);
  429. r.Read ();
  430. XmlAssert.AssertNode (label + "#2", r,
  431. // NodeType, Depth, IsEmptyElement
  432. XmlNodeType.Element, 1, false,
  433. // Name, Prefix, LocalName, NamespaceURI
  434. "two", String.Empty, "two", String.Empty,
  435. // Value, HasValue, AttributeCount, HasAttributes
  436. String.Empty, false, 0, false);
  437. r.Read ();
  438. XmlAssert.AssertNode (label + "#3", r,
  439. // NodeType, Depth, IsEmptyElement
  440. XmlNodeType.Text, 2, false,
  441. // Name, Prefix, LocalName, NamespaceURI
  442. String.Empty, String.Empty, String.Empty, String.Empty,
  443. // Value, HasValue, AttributeCount, HasAttributes
  444. "Some data.", true, 0, false);
  445. r.Read ();
  446. XmlAssert.AssertNode (label + "#4", r,
  447. // NodeType, Depth, IsEmptyElement
  448. XmlNodeType.Element, 2, false,
  449. // Name, Prefix, LocalName, NamespaceURI
  450. "three", String.Empty, "three", String.Empty,
  451. // Value, HasValue, AttributeCount, HasAttributes
  452. String.Empty, false, 0, false);
  453. r.Read ();
  454. XmlAssert.AssertNode (label + "#5", r,
  455. // NodeType, Depth, IsEmptyElement
  456. XmlNodeType.Text, 3, false,
  457. // Name, Prefix, LocalName, NamespaceURI
  458. String.Empty, String.Empty, String.Empty, String.Empty,
  459. // Value, HasValue, AttributeCount, HasAttributes
  460. "more", true, 0, false);
  461. r.Read ();
  462. XmlAssert.AssertNode (label + "#6", r,
  463. // NodeType, Depth, IsEmptyElement
  464. XmlNodeType.EndElement, 2, false,
  465. // Name, Prefix, LocalName, NamespaceURI
  466. "three", String.Empty, "three", String.Empty,
  467. // Value, HasValue, AttributeCount, HasAttributes
  468. String.Empty, false, 0, false);
  469. r.Read ();
  470. XmlAssert.AssertNode (label + "#7", r,
  471. // NodeType, Depth, IsEmptyElement
  472. XmlNodeType.Text, 2, false,
  473. // Name, Prefix, LocalName, NamespaceURI
  474. String.Empty, String.Empty, String.Empty, String.Empty,
  475. // Value, HasValue, AttributeCount, HasAttributes
  476. " done.", true, 0, false);
  477. r.Read ();
  478. XmlAssert.AssertNode (label + "#8", r,
  479. // NodeType, Depth, IsEmptyElement
  480. XmlNodeType.EndElement, 1, false,
  481. // Name, Prefix, LocalName, NamespaceURI
  482. "two", String.Empty, "two", String.Empty,
  483. // Value, HasValue, AttributeCount, HasAttributes
  484. String.Empty, false, 0, false);
  485. r.Read ();
  486. XmlAssert.AssertNode (label + "#9", r,
  487. // NodeType, Depth, IsEmptyElement
  488. XmlNodeType.EndElement, 0, false,
  489. // Name, Prefix, LocalName, NamespaceURI
  490. "one", String.Empty, "one", String.Empty,
  491. // Value, HasValue, AttributeCount, HasAttributes
  492. String.Empty, false, 0, false);
  493. Assert.IsFalse (r.Read (), label + "#10");
  494. }
  495. [Test]
  496. public void MoveToFirstAttributeFromAttribute ()
  497. {
  498. string xml = @"<one xmlns:foo='urn:foo' a='v' />";
  499. nav = GetXmlDocumentNavigator (xml);
  500. MoveToFirstAttributeFromAttribute (nav, "#1.");
  501. nav.MoveToRoot ();
  502. nav.MoveToFirstChild ();
  503. MoveToFirstAttributeFromAttribute (nav, "#2.");
  504. nav = GetXPathDocumentNavigator (document);
  505. MoveToFirstAttributeFromAttribute (nav, "#3.");
  506. nav.MoveToRoot ();
  507. nav.MoveToFirstChild ();
  508. MoveToFirstAttributeFromAttribute (nav, "#4.");
  509. }
  510. void MoveToFirstAttributeFromAttribute (XPathNavigator nav, string label)
  511. {
  512. XmlReader r = nav.ReadSubtree ();
  513. r.MoveToContent ();
  514. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#1");
  515. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#2");
  516. r.ReadAttributeValue ();
  517. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#3");
  518. Assert.IsTrue (r.MoveToNextAttribute (), label + "#4");
  519. Assert.IsTrue (r.MoveToFirstAttribute (), label + "#5");
  520. }
  521. [Test]
  522. [ExpectedException (typeof (InvalidOperationException))]
  523. public void ReadSubtreeAttribute ()
  524. {
  525. string xml = "<root a='b' />";
  526. nav = GetXmlDocumentNavigator (xml);
  527. nav.MoveToFirstChild ();
  528. nav.MoveToFirstAttribute ();
  529. nav.ReadSubtree ();
  530. }
  531. [Test]
  532. [ExpectedException (typeof (InvalidOperationException))]
  533. public void ReadSubtreeNamespace ()
  534. {
  535. string xml = "<root xmlns='urn:foo' />";
  536. nav = GetXmlDocumentNavigator (xml);
  537. nav.MoveToFirstChild ();
  538. nav.MoveToFirstNamespace ();
  539. nav.ReadSubtree ();
  540. }
  541. [Test]
  542. [ExpectedException (typeof (InvalidOperationException))]
  543. public void ReadSubtreePI ()
  544. {
  545. string xml = "<?pi ?><root xmlns='urn:foo' />";
  546. nav = GetXmlDocumentNavigator (xml);
  547. nav.MoveToFirstChild ();
  548. nav.ReadSubtree ();
  549. }
  550. [Test]
  551. [ExpectedException (typeof (InvalidOperationException))]
  552. public void ReadSubtreeComment ()
  553. {
  554. string xml = "<!-- comment --><root xmlns='urn:foo' />";
  555. nav = GetXmlDocumentNavigator (xml);
  556. nav.MoveToFirstChild ();
  557. nav.ReadSubtree ();
  558. }
  559. [Test]
  560. public void ReadSubtreeAttributesByIndex ()
  561. {
  562. XmlWriter xw;
  563. XmlDocument doc = new XmlDocument ();
  564. doc.LoadXml ("<u:Timestamp u:Id='ID1' xmlns:u='urn:foo'></u:Timestamp>");
  565. XmlReader r = doc.CreateNavigator ().ReadSubtree ();
  566. r.Read ();
  567. r.MoveToAttribute (0);
  568. if (r.LocalName != "Id")
  569. r.MoveToAttribute (1);
  570. if (r.LocalName != "Id")
  571. Assert.Fail ("Should move to the attribute.");
  572. }
  573. }
  574. }