XmlNodeListChildren.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // System.Xml.XmlNodeList
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.Collections;
  11. namespace System.Xml
  12. {
  13. internal class XmlNodeListChildren : XmlNodeList
  14. {
  15. #region Enumerator
  16. private class Enumerator : IEnumerator
  17. {
  18. XmlNode parent;
  19. XmlLinkedNode currentChild;
  20. bool passedLastNode;
  21. internal Enumerator (XmlNode parent)
  22. {
  23. currentChild = null;
  24. this.parent = parent;
  25. passedLastNode = false;
  26. }
  27. public virtual object Current {
  28. get {
  29. if ((currentChild == null) ||
  30. (parent.LastLinkedChild == null) ||
  31. (passedLastNode == true))
  32. throw new InvalidOperationException();
  33. return currentChild;
  34. }
  35. }
  36. public virtual bool MoveNext()
  37. {
  38. bool movedNext = true;
  39. if (parent.LastLinkedChild == null) {
  40. movedNext = false;
  41. }
  42. else if (currentChild == null) {
  43. currentChild = parent.LastLinkedChild.NextLinkedSibling;
  44. }
  45. else {
  46. if (Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
  47. movedNext = false;
  48. passedLastNode = true;
  49. }
  50. else {
  51. currentChild = currentChild.NextLinkedSibling;
  52. }
  53. }
  54. return movedNext;
  55. }
  56. public virtual void Reset()
  57. {
  58. currentChild = null;
  59. }
  60. }
  61. #endregion
  62. #region Fields
  63. XmlNode parent;
  64. #endregion
  65. #region Constructors
  66. public XmlNodeListChildren(XmlNode parent)
  67. {
  68. this.parent = parent;
  69. }
  70. #endregion
  71. #region Properties
  72. public override int Count {
  73. get {
  74. int count = 0;
  75. if (parent.LastLinkedChild != null) {
  76. XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
  77. count = 1;
  78. while (!Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
  79. currentChild = currentChild.NextLinkedSibling;
  80. count++;
  81. }
  82. }
  83. return count;
  84. }
  85. }
  86. #endregion
  87. #region Methods
  88. public override IEnumerator GetEnumerator ()
  89. {
  90. return new Enumerator(parent);
  91. }
  92. public override XmlNode Item (int index)
  93. {
  94. XmlNode requestedNode = null;
  95. // Return null if index is out of range. by DOM design.
  96. if (Count <= index)
  97. return null;
  98. // Instead of checking for && index < Count which has to walk
  99. // the whole list to get a count, we'll just keep a count since
  100. // we have to walk the list anyways to get to index.
  101. if ((index >= 0) && (parent.LastLinkedChild != null)) {
  102. XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
  103. int count = 0;
  104. while ((count < index) && !Object.ReferenceEquals(currentChild, parent.LastLinkedChild))
  105. {
  106. currentChild = currentChild.NextLinkedSibling;
  107. count++;
  108. }
  109. if (count == index) {
  110. requestedNode = currentChild;
  111. }
  112. }
  113. return requestedNode;
  114. }
  115. #endregion
  116. }
  117. }