2
0

XmlNodeListChildren.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public class XmlNodeListChildren : XmlNodeList
  14. {
  15. #region Enumerator
  16. ///////////////////////////////////////////////////////////////////////
  17. //
  18. // Enumerator
  19. //
  20. ///////////////////////////////////////////////////////////////////////
  21. private class Enumerator : IEnumerator
  22. {
  23. XmlLinkedNode currentChild;
  24. XmlLinkedNode lastChild;
  25. internal Enumerator (XmlLinkedNode lastChild)
  26. {
  27. currentChild = null;
  28. this.lastChild = lastChild;
  29. }
  30. public virtual object Current {
  31. get {
  32. return currentChild;
  33. }
  34. }
  35. public virtual bool MoveNext()
  36. {
  37. bool passedEndOfCollection = Object.ReferenceEquals(currentChild, lastChild);
  38. if (currentChild == null) {
  39. currentChild = lastChild;
  40. }
  41. currentChild = currentChild.NextLinkedSibling;
  42. return passedEndOfCollection;
  43. }
  44. public virtual void Reset()
  45. {
  46. currentChild = null;
  47. }
  48. }
  49. #endregion
  50. #region Fields
  51. ///////////////////////////////////////////////////////////////////////
  52. //
  53. // Fields
  54. //
  55. ///////////////////////////////////////////////////////////////////////
  56. XmlLinkedNode lastChild;
  57. #endregion
  58. #region Constructors
  59. ///////////////////////////////////////////////////////////////////////
  60. //
  61. // Constructors
  62. //
  63. ///////////////////////////////////////////////////////////////////////
  64. public XmlNodeListChildren(XmlLinkedNode lastChild)
  65. {
  66. this.lastChild = lastChild;
  67. }
  68. #endregion
  69. #region Properties
  70. ///////////////////////////////////////////////////////////////////////
  71. //
  72. // Properties
  73. //
  74. ///////////////////////////////////////////////////////////////////////
  75. public override int Count {
  76. get {
  77. int count = 0;
  78. if (lastChild != null) {
  79. XmlLinkedNode currentChild = lastChild.NextLinkedSibling;
  80. count = 1;
  81. while (!Object.ReferenceEquals(currentChild, lastChild)) {
  82. currentChild = currentChild.NextLinkedSibling;
  83. count++;
  84. }
  85. }
  86. return count;
  87. }
  88. }
  89. #endregion
  90. #region Methods
  91. ///////////////////////////////////////////////////////////////////////
  92. //
  93. // Methods
  94. //
  95. ///////////////////////////////////////////////////////////////////////
  96. public override IEnumerator GetEnumerator ()
  97. {
  98. return new Enumerator(lastChild);
  99. }
  100. public override XmlNode Item (int index)
  101. {
  102. XmlNode requestedNode = null;
  103. // Instead of checking for && index < Count which has to walk
  104. // the whole list to get a count, we'll just keep a count since
  105. // we have to walk the list anyways to get to index.
  106. if ((index >= 0) && (lastChild != null)) {
  107. XmlLinkedNode currentChild = lastChild.NextLinkedSibling;
  108. int count = 0;
  109. while ((count < index) && !Object.ReferenceEquals(currentChild, lastChild))
  110. {
  111. currentChild = currentChild.NextLinkedSibling;
  112. count++;
  113. }
  114. if (count == index) {
  115. requestedNode = currentChild;
  116. }
  117. }
  118. return requestedNode;
  119. }
  120. #endregion
  121. }
  122. }