TreeIterator.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //------------------------------------------------------------------------------
  2. // <copyright file="TreeIterator.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. #pragma warning disable 618 // ignore obsolete warning about XmlDataDocument
  9. namespace System.Xml {
  10. using System.Diagnostics;
  11. // Iterates over non-attribute nodes
  12. internal sealed class TreeIterator : BaseTreeIterator {
  13. private XmlNode nodeTop;
  14. private XmlNode currentNode;
  15. internal TreeIterator( XmlNode nodeTop ) : base( ((XmlDataDocument)(nodeTop.OwnerDocument)).Mapper ) {
  16. Debug.Assert( nodeTop != null );
  17. this.nodeTop = nodeTop;
  18. this.currentNode = nodeTop;
  19. }
  20. internal override void Reset() {
  21. currentNode = nodeTop;
  22. }
  23. internal override XmlNode CurrentNode {
  24. get {
  25. return currentNode;
  26. }
  27. }
  28. internal override bool Next() {
  29. XmlNode nextNode;
  30. // Try to move to the first child
  31. nextNode = currentNode.FirstChild;
  32. // No children, try next sibling
  33. if ( nextNode != null ) {
  34. currentNode = nextNode;
  35. return true;
  36. }
  37. return NextRight();
  38. }
  39. internal override bool NextRight() {
  40. // Make sure we do not get past the nodeTop if we call NextRight on a just initialized iterator and nodeTop has no children
  41. if ( currentNode == nodeTop ) {
  42. currentNode = null;
  43. return false;
  44. }
  45. XmlNode nextNode = currentNode.NextSibling;
  46. if ( nextNode != null ) {
  47. currentNode = nextNode;
  48. return true;
  49. }
  50. // No next sibling, try the first sibling of from the parent chain
  51. nextNode = currentNode;
  52. while ( nextNode != nodeTop && nextNode.NextSibling == null )
  53. nextNode = nextNode.ParentNode;
  54. if ( nextNode == nodeTop ) {
  55. currentNode = null;
  56. return false;
  57. }
  58. currentNode = nextNode.NextSibling;
  59. Debug.Assert( currentNode != null );
  60. return true;
  61. }
  62. }
  63. }