XmlDocumentNavigator.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. if (node.Attributes != null)
  80. for (int i = 0; i < node.Attributes.Count; i++)
  81. if (node.Attributes [i].NamespaceURI != Xmlns)
  82. return true;
  83. return false;
  84. }
  85. }
  86. public override bool HasChildren {
  87. get {
  88. if (NsNode != null)
  89. return false;
  90. XPathNodeType nodeType = NodeType;
  91. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  92. return canHaveChildren && GetFirstChild (node) != null;
  93. }
  94. }
  95. public override bool IsEmptyElement {
  96. get {
  97. if (NsNode != null)
  98. return false;
  99. return node.NodeType == XmlNodeType.Element
  100. && ((XmlElement) node).IsEmpty;
  101. }
  102. }
  103. public XmlAttribute NsNode {
  104. get { return nsNode; }
  105. set {
  106. if (value == null)
  107. iteratedNsNames = null;
  108. else
  109. {
  110. if (iteratedNsNames == null)
  111. iteratedNsNames = new ArrayList();
  112. else
  113. {
  114. if (iteratedNsNames.IsReadOnly)
  115. iteratedNsNames = new ArrayList(iteratedNsNames);
  116. }
  117. iteratedNsNames.Add (value.Name);
  118. }
  119. nsNode = value;
  120. }
  121. }
  122. public override string LocalName {
  123. get {
  124. XmlAttribute nsNode = NsNode;
  125. if (nsNode != null) {
  126. if (nsNode == nsNodeXml)
  127. return "xml";
  128. else
  129. return (nsNode.Name == "xmlns") ? String.Empty : nsNode.LocalName;
  130. }
  131. XPathNodeType nodeType = NodeType;
  132. bool canHaveName =
  133. nodeType == XPathNodeType.Element ||
  134. nodeType == XPathNodeType.Attribute ||
  135. nodeType == XPathNodeType.ProcessingInstruction ||
  136. nodeType == XPathNodeType.Namespace;
  137. return canHaveName ? node.LocalName : String.Empty;
  138. }
  139. }
  140. public override string Name {
  141. get {
  142. if (NsNode != null)
  143. return LocalName;
  144. XPathNodeType nodeType = NodeType;
  145. bool canHaveName =
  146. nodeType == XPathNodeType.Element ||
  147. nodeType == XPathNodeType.Attribute ||
  148. nodeType == XPathNodeType.ProcessingInstruction ||
  149. nodeType == XPathNodeType.Namespace;
  150. return canHaveName ? node.Name : String.Empty;
  151. }
  152. }
  153. public override string NamespaceURI {
  154. get { return (NsNode != null) ? String.Empty : node.NamespaceURI; }
  155. }
  156. public override XmlNameTable NameTable {
  157. get {
  158. return document.NameTable;
  159. }
  160. }
  161. public override XPathNodeType NodeType {
  162. get {
  163. if (NsNode != null)
  164. return XPathNodeType.Namespace;
  165. XmlNode n = node;
  166. bool sw = false;
  167. do {
  168. switch (n.NodeType) {
  169. case XmlNodeType.SignificantWhitespace:
  170. sw = true;
  171. n = GetNextSibling (n);
  172. break;
  173. case XmlNodeType.Whitespace:
  174. n = GetNextSibling (n);
  175. break;
  176. case XmlNodeType.Text:
  177. case XmlNodeType.CDATA:
  178. return XPathNodeType.Text;
  179. default:
  180. n = null;
  181. break;
  182. }
  183. } while (n != null);
  184. return sw ?
  185. XPathNodeType.SignificantWhitespace :
  186. node.XPathNodeType;
  187. }
  188. }
  189. public override string Prefix {
  190. get { return (NsNode != null) ? String.Empty : node.Prefix; }
  191. }
  192. #if NET_2_0
  193. public override IXmlSchemaInfo SchemaInfo {
  194. get { return NsNode != null ? null : node.SchemaInfo; }
  195. }
  196. public override object UnderlyingObject {
  197. get { return node; }
  198. }
  199. #endif
  200. public override string Value {
  201. get {
  202. switch (NodeType) {
  203. case XPathNodeType.Attribute:
  204. case XPathNodeType.Comment:
  205. case XPathNodeType.ProcessingInstruction:
  206. return node.Value;
  207. case XPathNodeType.Text:
  208. case XPathNodeType.Whitespace:
  209. case XPathNodeType.SignificantWhitespace:
  210. string value = node.Value;
  211. for (XmlNode n = GetNextSibling (node); n != null; n = GetNextSibling (n)) {
  212. switch (n.XPathNodeType) {
  213. case XPathNodeType.Text:
  214. case XPathNodeType.Whitespace:
  215. case XPathNodeType.SignificantWhitespace:
  216. value += n.Value;
  217. continue;
  218. }
  219. break;
  220. }
  221. return value;
  222. case XPathNodeType.Element:
  223. case XPathNodeType.Root:
  224. return node.InnerText;
  225. case XPathNodeType.Namespace:
  226. return NsNode == nsNodeXml ? XmlnsXML : NsNode.Value;
  227. }
  228. return String.Empty;
  229. }
  230. }
  231. public override string XmlLang {
  232. get {
  233. return node.XmlLang;
  234. }
  235. }
  236. #endregion
  237. #region Methods
  238. private bool CheckNsNameAppearance (string name, string ns)
  239. {
  240. if (iteratedNsNames != null && iteratedNsNames.Contains (name))
  241. return true;
  242. // default namespace erasure - just add name and never return this node
  243. if (ns == String.Empty) {
  244. if (iteratedNsNames == null)
  245. iteratedNsNames = new ArrayList();
  246. else
  247. {
  248. if (iteratedNsNames.IsReadOnly)
  249. iteratedNsNames = new ArrayList(iteratedNsNames);
  250. }
  251. iteratedNsNames.Add ("xmlns");
  252. return true;
  253. }
  254. return false;
  255. }
  256. public override XPathNavigator Clone ()
  257. {
  258. XmlDocumentNavigator clone = new XmlDocumentNavigator (node, nsNodeXml);
  259. clone.nsNode = nsNode;
  260. clone.iteratedNsNames = (iteratedNsNames == null || iteratedNsNames.IsReadOnly) ? iteratedNsNames : ArrayList.ReadOnly(iteratedNsNames);
  261. return clone;
  262. }
  263. public override string GetAttribute (string localName, string namespaceURI)
  264. {
  265. if (HasAttributes) {
  266. XmlElement el = Node as XmlElement;
  267. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  268. }
  269. return String.Empty;
  270. }
  271. public override string GetNamespace (string name)
  272. {
  273. // MSDN says "String.Empty if a matching namespace
  274. // node is not found or if the navigator is not
  275. // positioned on an element node", but in fact it
  276. // returns actual namespace for the other nodes.
  277. return Node.GetNamespaceOfPrefix (name);
  278. }
  279. public override bool IsDescendant (XPathNavigator other)
  280. {
  281. if (NsNode != null)
  282. return false;
  283. XmlDocumentNavigator o = other as XmlDocumentNavigator;
  284. if (o == null)
  285. return false;
  286. XmlNode n =
  287. o.node.NodeType == XmlNodeType.Attribute ?
  288. ((XmlAttribute) o.node).OwnerElement :
  289. o.node.ParentNode;
  290. for (;n != null; n = n.ParentNode)
  291. if (n == node)
  292. return true;
  293. return false;
  294. }
  295. public override bool IsSamePosition (XPathNavigator other)
  296. {
  297. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  298. if (otherDocumentNavigator != null)
  299. return node == otherDocumentNavigator.node
  300. && NsNode == otherDocumentNavigator.NsNode;
  301. return false;
  302. }
  303. public override bool MoveTo (XPathNavigator other)
  304. {
  305. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  306. if (otherDocumentNavigator != null) {
  307. if (document == otherDocumentNavigator.document) {
  308. node = otherDocumentNavigator.node;
  309. NsNode = otherDocumentNavigator.NsNode;
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. public override bool MoveToAttribute (string localName, string namespaceURI)
  316. {
  317. if (node.Attributes != null) {
  318. for (int i = 0; i < node.Attributes.Count; i++) {
  319. XmlAttribute attr = node.Attributes [i];
  320. if (attr.LocalName == localName
  321. && attr.NamespaceURI == namespaceURI) {
  322. node = attr;
  323. NsNode = null;
  324. return true;
  325. }
  326. }
  327. }
  328. return false;
  329. }
  330. #if NET_2_0
  331. #else
  332. public override bool MoveToFirst ()
  333. {
  334. return MoveToFirstImpl ();
  335. }
  336. #endif
  337. public override bool MoveToFirstAttribute ()
  338. {
  339. if (node.Attributes == null)
  340. return false;
  341. if (NodeType == XPathNodeType.Element) {
  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. if (node.Attributes != null) {
  370. do {
  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. if (namespaceScope == XPathNamespaceScope.Local)
  381. return false;
  382. el = GetParentNode (el) as XmlElement;
  383. } while (el != null);
  384. }
  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. if (node.Attributes != null) {
  412. do {
  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. el = GetParentNode (node) as XmlElement;
  421. } while (el != null);
  422. }
  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. for (int i = 0; i < owner.Attributes.Count; i++) {
  516. XmlAttribute attr = owner.Attributes [i];
  517. if (attr.NamespaceURI == Xmlns) {
  518. if (CheckNsNameAppearance (attr.Name, attr.Value))
  519. continue;
  520. NsNode = attr;
  521. return true;
  522. }
  523. }
  524. owner = GetParentNode (owner) as XmlElement;
  525. }
  526. if (namespaceScope == XPathNamespaceScope.All) {
  527. if (CheckNsNameAppearance (nsNodeXml.Name, nsNodeXml.Value))
  528. return false;
  529. NsNode = nsNodeXml;
  530. return true;
  531. }
  532. return false;
  533. }
  534. public override bool MoveToParent ()
  535. {
  536. if (NsNode != null) {
  537. NsNode = null;
  538. return true;
  539. }
  540. else if (node.NodeType == XmlNodeType.Attribute) {
  541. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  542. if (ownerElement != null) {
  543. node = ownerElement;
  544. NsNode = null;
  545. return true;
  546. }
  547. else
  548. return false;
  549. }
  550. XmlNode n = GetParentNode (node);
  551. if (n == null)
  552. return false;
  553. node = n;
  554. NsNode = null;
  555. return true;
  556. }
  557. public override bool MoveToPrevious ()
  558. {
  559. if (NsNode != null)
  560. return false;
  561. XmlNode p = GetPreviousSibling (node);
  562. if (p == null)
  563. return false;
  564. node = p;
  565. return true;
  566. }
  567. public override void MoveToRoot ()
  568. {
  569. XmlAttribute attr = node as XmlAttribute;
  570. XmlNode tmp = attr != null ? attr.OwnerElement : node;
  571. for (XmlNode tmp2 = GetParentNode (tmp); tmp2 != null; tmp2 = GetParentNode (tmp2))
  572. tmp = tmp2;
  573. node = tmp;
  574. NsNode = null;
  575. }
  576. private XmlNode Node { get { return NsNode != null ? NsNode : node; } }
  577. XmlNode IHasXmlNode.GetNode ()
  578. {
  579. return Node;
  580. }
  581. private XmlNode GetFirstChild (XmlNode n)
  582. {
  583. if (n.FirstChild == null)
  584. return null;
  585. switch (n.FirstChild.NodeType) {
  586. case XmlNodeType.XmlDeclaration:
  587. case XmlNodeType.DocumentType:
  588. return GetNextSibling (n.FirstChild);
  589. case XmlNodeType.EntityReference:
  590. foreach (XmlNode c in n.ChildNodes) {
  591. if (c.NodeType == XmlNodeType.EntityReference) {
  592. XmlNode ec = GetFirstChild (c);
  593. if (ec != null)
  594. return ec;
  595. }
  596. return c;
  597. }
  598. return null;
  599. default:
  600. return n.FirstChild;
  601. }
  602. }
  603. private XmlNode GetLastChild (XmlNode n)
  604. {
  605. if (n.LastChild == null)
  606. return null;
  607. switch (n.LastChild.NodeType) {
  608. case XmlNodeType.XmlDeclaration:
  609. case XmlNodeType.DocumentType:
  610. return GetPreviousSibling (n.LastChild);
  611. case XmlNodeType.EntityReference:
  612. for (XmlNode c = n.LastChild; c != null; c = c.PreviousSibling) {
  613. if (c.NodeType == XmlNodeType.EntityReference) {
  614. XmlNode ec = GetLastChild (c);
  615. if (ec != null)
  616. return ec;
  617. }
  618. return c;
  619. }
  620. return null;
  621. default:
  622. return n.LastChild;
  623. }
  624. }
  625. private XmlNode GetPreviousSibling (XmlNode n)
  626. {
  627. if (n.PreviousSibling != null) {
  628. switch (n.PreviousSibling.NodeType) {
  629. case XmlNodeType.EntityReference:
  630. XmlNode c = GetLastChild (n.PreviousSibling);
  631. if (c != null)
  632. return c;
  633. else // empty entity reference etc.
  634. return GetPreviousSibling (n.PreviousSibling);
  635. case XmlNodeType.XmlDeclaration:
  636. case XmlNodeType.DocumentType:
  637. return GetPreviousSibling (n.PreviousSibling);
  638. default:
  639. return n.PreviousSibling;
  640. }
  641. } else {
  642. if (n.ParentNode == null || n.ParentNode.NodeType != XmlNodeType.EntityReference)
  643. return null;
  644. return GetPreviousSibling (n.ParentNode);
  645. }
  646. }
  647. private XmlNode GetNextSibling (XmlNode n)
  648. {
  649. if (n.NextSibling != null) {
  650. switch (n.NextSibling.NodeType) {
  651. case XmlNodeType.EntityReference:
  652. XmlNode c = GetFirstChild (n.NextSibling);
  653. if (c != null)
  654. return c;
  655. else // empty entity reference etc.
  656. return GetNextSibling (n.NextSibling);
  657. case XmlNodeType.XmlDeclaration:
  658. case XmlNodeType.DocumentType:
  659. return GetNextSibling (n.NextSibling);
  660. default:
  661. return n.NextSibling;
  662. }
  663. } else {
  664. if (n.ParentNode == null || n.ParentNode.NodeType != XmlNodeType.EntityReference)
  665. return null;
  666. return GetNextSibling (n.ParentNode);
  667. }
  668. }
  669. private XmlNode GetParentNode (XmlNode n)
  670. {
  671. if (n.ParentNode == null)
  672. return null;
  673. for (XmlNode p = n.ParentNode; p != null; p = p.ParentNode)
  674. if (p.NodeType != XmlNodeType.EntityReference)
  675. return p;
  676. return null;
  677. }
  678. #endregion
  679. }
  680. }