XmlNamedNodeMap.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // System.Xml.XmlNamedNodeMap
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Duncan Mak ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. //
  10. using System;
  11. using System.Collections;
  12. namespace System.Xml
  13. {
  14. public class XmlNamedNodeMap : IEnumerable
  15. {
  16. XmlNode parent;
  17. ArrayList nodeList;
  18. bool readOnly = false;
  19. internal XmlNamedNodeMap (XmlNode parent)
  20. {
  21. this.parent = parent;
  22. nodeList = new ArrayList ();
  23. }
  24. public virtual int Count {
  25. get { return nodeList.Count; }
  26. }
  27. public virtual IEnumerator GetEnumerator ()
  28. {
  29. return nodeList.GetEnumerator ();
  30. }
  31. public virtual XmlNode GetNamedItem (string name)
  32. {
  33. for (int i = 0; i < nodeList.Count; i++) {
  34. XmlNode node = (XmlNode) nodeList [i];
  35. if (node.Name == name)
  36. return node;
  37. }
  38. return null;
  39. }
  40. public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
  41. {
  42. for (int i = 0; i < nodeList.Count; i++) {
  43. XmlNode node = (XmlNode) nodeList [i];
  44. if ((node.LocalName == localName)
  45. && (node.NamespaceURI == namespaceURI))
  46. return node;
  47. }
  48. return null;
  49. }
  50. public virtual XmlNode Item (int index)
  51. {
  52. if (index < 0 || index >= nodeList.Count)
  53. return null;
  54. else
  55. return (XmlNode) nodeList [index];
  56. }
  57. public virtual XmlNode RemoveNamedItem (string name)
  58. {
  59. for (int i = 0; i < nodeList.Count; i++) {
  60. XmlNode node = (XmlNode) nodeList [i];
  61. if (node.Name == name) {
  62. if (node.IsReadOnly)
  63. throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
  64. nodeList.Remove (node);
  65. return node;
  66. }
  67. }
  68. return null;
  69. }
  70. public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
  71. {
  72. for (int i = 0; i < nodeList.Count; i++) {
  73. XmlNode node = (XmlNode) nodeList [i];
  74. if ((node.LocalName == localName)
  75. && (node.NamespaceURI == namespaceURI)) {
  76. nodeList.Remove (node);
  77. return node;
  78. }
  79. }
  80. return null;
  81. }
  82. public virtual XmlNode SetNamedItem (XmlNode node)
  83. {
  84. return SetNamedItem (node, -1, true);
  85. }
  86. internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
  87. {
  88. return SetNamedItem (node, -1, raiseEvent);
  89. }
  90. internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
  91. {
  92. if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
  93. throw new ArgumentException ("Cannot add to NodeMap.");
  94. if (raiseEvent)
  95. parent.OwnerDocument.onNodeInserting (node, parent);
  96. try {
  97. for (int i = 0; i < nodeList.Count; i++) {
  98. XmlNode x = (XmlNode) nodeList [i];
  99. if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
  100. nodeList.Remove (x);
  101. if (pos < 0)
  102. nodeList.Add (node);
  103. else
  104. nodeList.Insert (pos, node);
  105. return x;
  106. }
  107. }
  108. if(pos < 0)
  109. nodeList.Add (node);
  110. else
  111. nodeList.Insert (pos, node);
  112. return null;
  113. } finally {
  114. if (raiseEvent)
  115. parent.OwnerDocument.onNodeInserted (node, parent);
  116. }
  117. }
  118. internal ArrayList Nodes { get { return nodeList; } }
  119. }
  120. }