XmlDocumentNavigator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. //
  2. // System.Xml.XmlDocumentNavigator
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond
  9. // (C) 2003 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.XPath;
  36. namespace System.Xml
  37. {
  38. internal class XmlDocumentNavigator : XPathNavigator, IHasXmlNode
  39. {
  40. #region Constructors
  41. internal XmlDocumentNavigator (XmlNode node)
  42. : this (node, null)
  43. {
  44. nsNodeXml = document.CreateAttribute ("xmlns", "xml", Xmlns);
  45. nsNodeXml.Value = XmlnsXML;
  46. if (node.NodeType == XmlNodeType.Attribute && node.NamespaceURI == XmlNamespaceManager.XmlnsXmlns) {
  47. nsNode = (XmlAttribute) node;
  48. node = nsNode.OwnerElement;
  49. }
  50. }
  51. private XmlDocumentNavigator (XmlNode node, XmlAttribute nsNodeXml)
  52. {
  53. this.node = node;
  54. this.document = node.NodeType == XmlNodeType.Document ?
  55. node as XmlDocument : node.OwnerDocument;
  56. this.nsNodeXml = nsNodeXml;
  57. }
  58. #endregion
  59. #region Fields
  60. private const string Xmlns = "http://www.w3.org/2000/xmlns/";
  61. private const string XmlnsXML = "http://www.w3.org/XML/1998/namespace";
  62. private XmlAttribute nsNodeXml;
  63. private XmlNode node;
  64. private XmlDocument document;
  65. // Current namespace node (ancestor's attribute of current node).
  66. private XmlAttribute nsNode;
  67. private ArrayList iteratedNsNames;
  68. #endregion
  69. #region Properties
  70. public override string BaseURI {
  71. get {
  72. return node.BaseURI;
  73. }
  74. }
  75. public override bool HasAttributes {
  76. get {
  77. if (NsNode != null)
  78. return false;
  79. XmlElement el = node as XmlElement;
  80. if (el == null || !el.HasAttributes)
  81. return false;
  82. for (int i = 0; i < node.Attributes.Count; i++)
  83. if (node.Attributes [i].NamespaceURI != Xmlns)
  84. return true;
  85. return false;
  86. }
  87. }
  88. public override bool HasChildren {
  89. get {
  90. if (NsNode != null)
  91. return false;
  92. XPathNodeType nodeType = NodeType;
  93. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  94. return canHaveChildren && GetFirstChild (node) != null;
  95. }
  96. }
  97. public override bool IsEmptyElement {
  98. get {
  99. if (NsNode != null)
  100. return false;
  101. return node.NodeType == XmlNodeType.Element
  102. && ((XmlElement) node).IsEmpty;
  103. }
  104. }
  105. public XmlAttribute NsNode {
  106. get { return nsNode; }
  107. set {
  108. if (value == null)
  109. iteratedNsNames = null;
  110. else
  111. {
  112. if (iteratedNsNames == null)
  113. iteratedNsNames = new ArrayList();
  114. else
  115. {
  116. if (iteratedNsNames.IsReadOnly)
  117. iteratedNsNames = new ArrayList(iteratedNsNames);
  118. }
  119. iteratedNsNames.Add (value.Name);
  120. }
  121. nsNode = value;
  122. }
  123. }
  124. public override string LocalName {
  125. get {
  126. XmlAttribute nsNode = NsNode;
  127. if (nsNode != null) {
  128. if (nsNode == nsNodeXml)
  129. return "xml";
  130. else
  131. return (nsNode.Name == "xmlns") ? String.Empty : nsNode.LocalName;
  132. }
  133. XPathNodeType nodeType = NodeType;
  134. bool canHaveName =
  135. nodeType == XPathNodeType.Element ||
  136. nodeType == XPathNodeType.Attribute ||
  137. nodeType == XPathNodeType.ProcessingInstruction ||
  138. nodeType == XPathNodeType.Namespace;
  139. return canHaveName ? node.LocalName : String.Empty;
  140. }
  141. }
  142. public override string Name {
  143. get {
  144. if (NsNode != null)
  145. return LocalName;
  146. XPathNodeType nodeType = NodeType;
  147. bool canHaveName =
  148. nodeType == XPathNodeType.Element ||
  149. nodeType == XPathNodeType.Attribute ||
  150. nodeType == XPathNodeType.ProcessingInstruction ||
  151. nodeType == XPathNodeType.Namespace;
  152. return canHaveName ? node.Name : String.Empty;
  153. }
  154. }
  155. public override string NamespaceURI {
  156. get { return (NsNode != null) ? String.Empty : node.NamespaceURI; }
  157. }
  158. public override XmlNameTable NameTable {
  159. get {
  160. return document.NameTable;
  161. }
  162. }
  163. public override XPathNodeType NodeType {
  164. get {
  165. if (NsNode != null)
  166. return XPathNodeType.Namespace;
  167. XmlNode n = node;
  168. bool sw = false;
  169. do {
  170. switch (n.NodeType) {
  171. case XmlNodeType.SignificantWhitespace:
  172. sw = true;
  173. n = GetNextSibling (n);
  174. break;
  175. case XmlNodeType.Whitespace:
  176. n = GetNextSibling (n);
  177. break;
  178. case XmlNodeType.Text:
  179. case XmlNodeType.CDATA:
  180. return XPathNodeType.Text;
  181. default:
  182. n = null;
  183. break;
  184. }
  185. } while (n != null);
  186. return sw ?
  187. XPathNodeType.SignificantWhitespace :
  188. node.XPathNodeType;
  189. }
  190. }
  191. public override string Prefix {
  192. get { return (NsNode != null) ? String.Empty : node.Prefix; }
  193. }
  194. #if NET_2_0
  195. public override IXmlSchemaInfo SchemaInfo {
  196. get { return NsNode != null ? null : node.SchemaInfo; }
  197. }
  198. public override object UnderlyingObject {
  199. get { return node; }
  200. }
  201. #endif
  202. public override string Value {
  203. get {
  204. switch (NodeType) {
  205. case XPathNodeType.Attribute:
  206. case XPathNodeType.Comment:
  207. case XPathNodeType.ProcessingInstruction:
  208. return node.Value;
  209. case XPathNodeType.Text:
  210. case XPathNodeType.Whitespace:
  211. case XPathNodeType.SignificantWhitespace:
  212. string value = node.Value;
  213. for (XmlNode n = GetNextSibling (node); n != null; n = GetNextSibling (n)) {
  214. switch (n.XPathNodeType) {
  215. case XPathNodeType.Text:
  216. case XPathNodeType.Whitespace:
  217. case XPathNodeType.SignificantWhitespace:
  218. value += n.Value;
  219. continue;
  220. }
  221. break;
  222. }
  223. return value;
  224. case XPathNodeType.Element:
  225. case XPathNodeType.Root:
  226. return node.InnerText;
  227. case XPathNodeType.Namespace:
  228. return NsNode == nsNodeXml ? XmlnsXML : NsNode.Value;
  229. }
  230. return String.Empty;
  231. }
  232. }
  233. public override string XmlLang {
  234. get {
  235. return node.XmlLang;
  236. }
  237. }
  238. #endregion
  239. #region Methods
  240. private bool CheckNsNameAppearance (string name, string ns)
  241. {
  242. if (iteratedNsNames != null && iteratedNsNames.Contains (name))
  243. return true;
  244. // default namespace erasure - just add name and never return this node
  245. if (ns == String.Empty) {
  246. if (iteratedNsNames == null)
  247. iteratedNsNames = new ArrayList();
  248. else
  249. {
  250. if (iteratedNsNames.IsReadOnly)
  251. iteratedNsNames = new ArrayList(iteratedNsNames);
  252. }
  253. iteratedNsNames.Add ("xmlns");
  254. return true;
  255. }
  256. return false;
  257. }
  258. public override XPathNavigator Clone ()
  259. {
  260. XmlDocumentNavigator clone = new XmlDocumentNavigator (node, nsNodeXml);
  261. clone.nsNode = nsNode;
  262. clone.iteratedNsNames = (iteratedNsNames == null || iteratedNsNames.IsReadOnly) ? iteratedNsNames : ArrayList.ReadOnly(iteratedNsNames);
  263. return clone;
  264. }
  265. public override string GetAttribute (string localName, string namespaceURI)
  266. {
  267. if (HasAttributes) {
  268. XmlElement el = Node as XmlElement;
  269. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  270. }
  271. return String.Empty;
  272. }
  273. public override string GetNamespace (string name)
  274. {
  275. // MSDN says "String.Empty if a matching namespace
  276. // node is not found or if the navigator is not
  277. // positioned on an element node", but in fact it
  278. // returns actual namespace for the other nodes.
  279. return Node.GetNamespaceOfPrefix (name);
  280. }
  281. public override bool IsDescendant (XPathNavigator other)
  282. {
  283. if (NsNode != null)
  284. return false;
  285. XmlDocumentNavigator o = other as XmlDocumentNavigator;
  286. if (o == null)
  287. return false;
  288. XmlNode n =
  289. o.node.NodeType == XmlNodeType.Attribute ?
  290. ((XmlAttribute) o.node).OwnerElement :
  291. o.node.ParentNode;
  292. for (;n != null; n = n.ParentNode)
  293. if (n == node)
  294. return true;
  295. return false;
  296. }
  297. public override bool IsSamePosition (XPathNavigator other)
  298. {
  299. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  300. if (otherDocumentNavigator != null)
  301. return node == otherDocumentNavigator.node
  302. && NsNode == otherDocumentNavigator.NsNode;
  303. return false;
  304. }
  305. public override bool MoveTo (XPathNavigator other)
  306. {
  307. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  308. if (otherDocumentNavigator != null) {
  309. if (document == otherDocumentNavigator.document) {
  310. node = otherDocumentNavigator.node;
  311. NsNode = otherDocumentNavigator.NsNode;
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. public override bool MoveToAttribute (string localName, string namespaceURI)
  318. {
  319. if (HasAttributes) {
  320. XmlAttribute attr = node.Attributes [localName, namespaceURI];
  321. if (attr != null) {
  322. node = attr;
  323. NsNode = null;
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. #if NET_2_0
  330. #else
  331. public override bool MoveToFirst ()
  332. {
  333. return MoveToFirstImpl ();
  334. }
  335. #endif
  336. public override bool MoveToFirstAttribute ()
  337. {
  338. if (NodeType == XPathNodeType.Element) {
  339. XmlElement el = node as XmlElement;
  340. if (!el.HasAttributes)
  341. return false;
  342. for (int i = 0; i < node.Attributes.Count; i++) {
  343. XmlAttribute attr = node.Attributes [i];
  344. if (attr.NamespaceURI != Xmlns) {
  345. node = attr;
  346. NsNode = null;
  347. return true;
  348. }
  349. }
  350. }
  351. return false;
  352. }
  353. public override bool MoveToFirstChild ()
  354. {
  355. if (HasChildren) {
  356. XmlNode n = GetFirstChild (node);
  357. if (n == null)
  358. return false;
  359. node = n;
  360. return true;
  361. }
  362. return false;
  363. }
  364. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  365. {
  366. if (NodeType != XPathNodeType.Element)
  367. return false;
  368. XmlElement el = node as XmlElement;
  369. do {
  370. if (el.HasAttributes) {
  371. for (int i = 0; i < el.Attributes.Count; i++) {
  372. XmlAttribute attr = el.Attributes [i];
  373. if (attr.NamespaceURI == Xmlns) {
  374. if (CheckNsNameAppearance (attr.Name, attr.Value))
  375. continue;
  376. NsNode = attr;
  377. return true;
  378. }
  379. }
  380. }
  381. if (namespaceScope == XPathNamespaceScope.Local)
  382. return false;
  383. el = GetParentNode (el) as XmlElement;
  384. } while (el != null);
  385. if (namespaceScope == XPathNamespaceScope.All) {
  386. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  387. return false;
  388. NsNode = nsNodeXml;
  389. return true;
  390. }
  391. else
  392. return false;
  393. }
  394. public override bool MoveToId (string id)
  395. {
  396. XmlElement eltNew = document.GetElementById (id);
  397. if (eltNew == null)
  398. return false;
  399. node = eltNew;
  400. return true;
  401. }
  402. public override bool MoveToNamespace (string name)
  403. {
  404. if (name == "xml") {
  405. NsNode = nsNodeXml;
  406. return true;
  407. }
  408. if (NodeType != XPathNodeType.Element)
  409. return false;
  410. XmlElement el = node as XmlElement;
  411. do {
  412. if (el.HasAttributes) {
  413. for (int i = 0; i < el.Attributes.Count; i++) {
  414. XmlAttribute attr = el.Attributes [i];
  415. if (attr.NamespaceURI == Xmlns && attr.Name == name) {
  416. NsNode = attr;
  417. return true;
  418. }
  419. }
  420. }
  421. el = GetParentNode (node) as XmlElement;
  422. } while (el != null);
  423. return false;
  424. }
  425. public override bool MoveToNext ()
  426. {
  427. if (NsNode != null)
  428. return false;
  429. XmlNode n = node;
  430. if (NodeType == XPathNodeType.Text) {
  431. do {
  432. n = GetNextSibling (n);
  433. if (n == null)
  434. return false;
  435. switch (n.NodeType) {
  436. case XmlNodeType.CDATA:
  437. case XmlNodeType.SignificantWhitespace:
  438. case XmlNodeType.Text:
  439. case XmlNodeType.Whitespace:
  440. continue;
  441. default:
  442. break;
  443. }
  444. break;
  445. } while (true);
  446. }
  447. else
  448. n = GetNextSibling (n);
  449. if (n == null)
  450. return false;
  451. node = n;
  452. return true;
  453. }
  454. public override bool MoveToNextAttribute ()
  455. {
  456. if (node == null)
  457. return false;
  458. if (NodeType != XPathNodeType.Attribute)
  459. return false;
  460. // Find current attribute.
  461. int pos = 0;
  462. XmlElement owner = ((XmlAttribute) node).OwnerElement;
  463. if (owner == null)
  464. return false;
  465. int count = owner.Attributes.Count;
  466. for(; pos < count; pos++)
  467. if (owner.Attributes [pos] == node)
  468. break;
  469. if (pos == count)
  470. return false; // Where is current attribute? Maybe removed.
  471. // Find next attribute.
  472. for(pos++; pos < count; pos++) {
  473. if (owner.Attributes [pos].NamespaceURI != Xmlns) {
  474. node = owner.Attributes [pos];
  475. NsNode = null;
  476. return true;
  477. }
  478. }
  479. return false;
  480. }
  481. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  482. {
  483. if (NsNode == nsNodeXml)
  484. // Current namespace is "xml", so there should be no more namespace nodes.
  485. return false;
  486. if (NsNode == null)
  487. return false;
  488. // Get current attribute's position.
  489. int pos = 0;
  490. XmlElement owner = ((XmlAttribute) NsNode).OwnerElement;
  491. if (owner == null)
  492. return false;
  493. int count = owner.Attributes.Count;
  494. for(; pos < count; pos++)
  495. if (owner.Attributes [pos] == NsNode)
  496. break;
  497. if (pos == count)
  498. return false; // Where is current attribute? Maybe removed.
  499. // Find next namespace from the same element as current ns node.
  500. for(pos++; pos < count; pos++) {
  501. if (owner.Attributes [pos].NamespaceURI == Xmlns) {
  502. XmlAttribute a = owner.Attributes [pos];
  503. if (CheckNsNameAppearance (a.Name, a.Value))
  504. continue;
  505. NsNode = a;
  506. return true;
  507. }
  508. }
  509. // If not found more, then find from ancestors.
  510. // But if scope is Local, then it returns false here.
  511. if (namespaceScope == XPathNamespaceScope.Local)
  512. return false;
  513. owner = GetParentNode (owner) as XmlElement;
  514. while (owner != null) {
  515. if (owner.HasAttributes) {
  516. for (int i = 0; i < owner.Attributes.Count; i++) {
  517. XmlAttribute attr = owner.Attributes [i];
  518. if (attr.NamespaceURI == Xmlns) {
  519. if (CheckNsNameAppearance (attr.Name, attr.Value))
  520. continue;
  521. NsNode = attr;
  522. return true;
  523. }
  524. }
  525. }
  526. owner = GetParentNode (owner) as XmlElement;
  527. }
  528. if (namespaceScope == XPathNamespaceScope.All) {
  529. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  530. return false;
  531. NsNode = nsNodeXml;
  532. return true;
  533. }
  534. return false;
  535. }
  536. public override bool MoveToParent ()
  537. {
  538. if (NsNode != null) {
  539. NsNode = null;
  540. return true;
  541. }
  542. else if (node.NodeType == XmlNodeType.Attribute) {
  543. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  544. if (ownerElement != null) {
  545. node = ownerElement;
  546. NsNode = null;
  547. return true;
  548. }
  549. else
  550. return false;
  551. }
  552. XmlNode n = GetParentNode (node);
  553. if (n == null)
  554. return false;
  555. node = n;
  556. NsNode = null;
  557. return true;
  558. }
  559. public override bool MoveToPrevious ()
  560. {
  561. if (NsNode != null)
  562. return false;
  563. XmlNode p = GetPreviousSibling (node);
  564. if (p == null)
  565. return false;
  566. node = p;
  567. return true;
  568. }
  569. public override void MoveToRoot ()
  570. {
  571. XmlAttribute attr = node as XmlAttribute;
  572. XmlNode tmp = attr != null ? attr.OwnerElement : node;
  573. for (XmlNode tmp2 = GetParentNode (tmp); tmp2 != null; tmp2 = GetParentNode (tmp2))
  574. tmp = tmp2;
  575. node = tmp;
  576. NsNode = null;
  577. }
  578. private XmlNode Node { get { return NsNode != null ? NsNode : node; } }
  579. XmlNode IHasXmlNode.GetNode ()
  580. {
  581. return Node;
  582. }
  583. private XmlNode GetFirstChild (XmlNode n)
  584. {
  585. if (n.FirstChild == null)
  586. return null;
  587. switch (n.FirstChild.NodeType) {
  588. case XmlNodeType.XmlDeclaration:
  589. case XmlNodeType.DocumentType:
  590. return GetNextSibling (n.FirstChild);
  591. case XmlNodeType.EntityReference:
  592. foreach (XmlNode c in n.ChildNodes) {
  593. if (c.NodeType == XmlNodeType.EntityReference) {
  594. XmlNode ec = GetFirstChild (c);
  595. if (ec != null)
  596. return ec;
  597. }
  598. return c;
  599. }
  600. return null;
  601. default:
  602. return n.FirstChild;
  603. }
  604. }
  605. private XmlNode GetLastChild (XmlNode n)
  606. {
  607. if (n.LastChild == null)
  608. return null;
  609. switch (n.LastChild.NodeType) {
  610. case XmlNodeType.XmlDeclaration:
  611. case XmlNodeType.DocumentType:
  612. return GetPreviousSibling (n.LastChild);
  613. case XmlNodeType.EntityReference:
  614. for (XmlNode c = n.LastChild; c != null; c = c.PreviousSibling) {
  615. if (c.NodeType == XmlNodeType.EntityReference) {
  616. XmlNode ec = GetLastChild (c);
  617. if (ec != null)
  618. return ec;
  619. }
  620. return c;
  621. }
  622. return null;
  623. default:
  624. return n.LastChild;
  625. }
  626. }
  627. private XmlNode GetPreviousSibling (XmlNode n)
  628. {
  629. XmlNode p = n.PreviousSibling;
  630. if (p != null) {
  631. switch (p.NodeType) {
  632. case XmlNodeType.EntityReference:
  633. XmlNode c = GetLastChild (p);
  634. if (c != null)
  635. return c;
  636. else // empty entity reference etc.
  637. return GetPreviousSibling (p);
  638. case XmlNodeType.XmlDeclaration:
  639. case XmlNodeType.DocumentType:
  640. return GetPreviousSibling (p);
  641. default:
  642. return p;
  643. }
  644. } else {
  645. if (n.ParentNode == null || n.ParentNode.NodeType != XmlNodeType.EntityReference)
  646. return null;
  647. return GetPreviousSibling (n.ParentNode);
  648. }
  649. }
  650. private XmlNode GetNextSibling (XmlNode n)
  651. {
  652. XmlNode nx = n.NextSibling;
  653. if (nx != null) {
  654. switch (nx.NodeType) {
  655. case XmlNodeType.EntityReference:
  656. XmlNode c = GetFirstChild (nx);
  657. if (c != null)
  658. return c;
  659. else // empty entity reference etc.
  660. return GetNextSibling (nx);
  661. case XmlNodeType.XmlDeclaration:
  662. case XmlNodeType.DocumentType:
  663. return GetNextSibling (nx);
  664. default:
  665. return n.NextSibling;
  666. }
  667. } else {
  668. if (n.ParentNode == null || n.ParentNode.NodeType != XmlNodeType.EntityReference)
  669. return null;
  670. return GetNextSibling (n.ParentNode);
  671. }
  672. }
  673. private XmlNode GetParentNode (XmlNode n)
  674. {
  675. if (n.ParentNode == null)
  676. return null;
  677. for (XmlNode p = n.ParentNode; p != null; p = p.ParentNode)
  678. if (p.NodeType != XmlNodeType.EntityReference)
  679. return p;
  680. return null;
  681. }
  682. #endregion
  683. }
  684. }