mscorlib(4.0.0.0) API with additions
XmlNamedNodeMap.cs
1 using System.Collections;
2 
3 namespace System.Xml
4 {
6  public class XmlNamedNodeMap : IEnumerable
7  {
8  internal struct SmallXmlNodeList
9  {
10  private class SingleObjectEnumerator : IEnumerator
11  {
12  private object loneValue;
13 
14  private int position = -1;
15 
16  public object Current
17  {
18  get
19  {
20  if (position != 0)
21  {
22  throw new InvalidOperationException();
23  }
24  return loneValue;
25  }
26  }
27 
28  public SingleObjectEnumerator(object value)
29  {
30  loneValue = value;
31  }
32 
33  public bool MoveNext()
34  {
35  if (position < 0)
36  {
37  position = 0;
38  return true;
39  }
40  position = 1;
41  return false;
42  }
43 
44  public void Reset()
45  {
46  position = -1;
47  }
48  }
49 
50  private object field;
51 
52  public int Count
53  {
54  get
55  {
56  if (field == null)
57  {
58  return 0;
59  }
60  return (field as ArrayList)?.Count ?? 1;
61  }
62  }
63 
64  public object this[int index]
65  {
66  get
67  {
68  if (field == null)
69  {
70  throw new ArgumentOutOfRangeException("index");
71  }
72  ArrayList arrayList = field as ArrayList;
73  if (arrayList != null)
74  {
75  return arrayList[index];
76  }
77  if (index != 0)
78  {
79  throw new ArgumentOutOfRangeException("index");
80  }
81  return field;
82  }
83  }
84 
85  public void Add(object value)
86  {
87  if (field == null)
88  {
89  if (value == null)
90  {
91  ArrayList arrayList = new ArrayList();
92  arrayList.Add(null);
93  field = arrayList;
94  }
95  else
96  {
97  field = value;
98  }
99  return;
100  }
101  ArrayList arrayList2 = field as ArrayList;
102  if (arrayList2 != null)
103  {
104  arrayList2.Add(value);
105  return;
106  }
107  arrayList2 = new ArrayList();
108  arrayList2.Add(field);
109  arrayList2.Add(value);
110  field = arrayList2;
111  }
112 
113  public void RemoveAt(int index)
114  {
115  if (field == null)
116  {
117  throw new ArgumentOutOfRangeException("index");
118  }
119  ArrayList arrayList = field as ArrayList;
120  if (arrayList != null)
121  {
122  arrayList.RemoveAt(index);
123  return;
124  }
125  if (index != 0)
126  {
127  throw new ArgumentOutOfRangeException("index");
128  }
129  field = null;
130  }
131 
132  public void Insert(int index, object value)
133  {
134  if (field == null)
135  {
136  if (index != 0)
137  {
138  throw new ArgumentOutOfRangeException("index");
139  }
140  Add(value);
141  return;
142  }
143  ArrayList arrayList = field as ArrayList;
144  if (arrayList != null)
145  {
146  arrayList.Insert(index, value);
147  return;
148  }
149  switch (index)
150  {
151  case 0:
152  arrayList = new ArrayList();
153  arrayList.Add(value);
154  arrayList.Add(field);
155  field = arrayList;
156  break;
157  case 1:
158  arrayList = new ArrayList();
159  arrayList.Add(field);
160  arrayList.Add(value);
161  field = arrayList;
162  break;
163  default:
164  throw new ArgumentOutOfRangeException("index");
165  }
166  }
167 
168  public IEnumerator GetEnumerator()
169  {
170  if (field == null)
171  {
172  return XmlDocument.EmptyEnumerator;
173  }
174  ArrayList arrayList = field as ArrayList;
175  if (arrayList != null)
176  {
177  return arrayList.GetEnumerator();
178  }
179  return new SingleObjectEnumerator(field);
180  }
181  }
182 
183  internal XmlNode parent;
184 
185  internal SmallXmlNodeList nodes;
186 
189  public virtual int Count => nodes.Count;
190 
191  internal XmlNamedNodeMap(XmlNode parent)
192  {
193  this.parent = parent;
194  }
195 
199  public virtual XmlNode GetNamedItem(string name)
200  {
201  int num = FindNodeOffset(name);
202  if (num >= 0)
203  {
204  return (XmlNode)nodes[num];
205  }
206  return null;
207  }
208 
213  public virtual XmlNode SetNamedItem(XmlNode node)
214  {
215  if (node == null)
216  {
217  return null;
218  }
219  int num = FindNodeOffset(node.LocalName, node.NamespaceURI);
220  if (num == -1)
221  {
222  AddNode(node);
223  return null;
224  }
225  return ReplaceNodeAt(num, node);
226  }
227 
231  public virtual XmlNode RemoveNamedItem(string name)
232  {
233  int num = FindNodeOffset(name);
234  if (num >= 0)
235  {
236  return RemoveNodeAt(num);
237  }
238  return null;
239  }
240 
244  public virtual XmlNode Item(int index)
245  {
246  if (index < 0 || index >= nodes.Count)
247  {
248  return null;
249  }
250  try
251  {
252  return (XmlNode)nodes[index];
253  }
255  {
256  throw new IndexOutOfRangeException(Res.GetString("Xdom_IndexOutOfRange"));
257  }
258  }
259 
264  public virtual XmlNode GetNamedItem(string localName, string namespaceURI)
265  {
266  int num = FindNodeOffset(localName, namespaceURI);
267  if (num >= 0)
268  {
269  return (XmlNode)nodes[num];
270  }
271  return null;
272  }
273 
278  public virtual XmlNode RemoveNamedItem(string localName, string namespaceURI)
279  {
280  int num = FindNodeOffset(localName, namespaceURI);
281  if (num >= 0)
282  {
283  return RemoveNodeAt(num);
284  }
285  return null;
286  }
287 
290  public virtual IEnumerator GetEnumerator()
291  {
292  return nodes.GetEnumerator();
293  }
294 
295  internal int FindNodeOffset(string name)
296  {
297  int count = Count;
298  for (int i = 0; i < count; i++)
299  {
300  XmlNode xmlNode = (XmlNode)nodes[i];
301  if (name == xmlNode.Name)
302  {
303  return i;
304  }
305  }
306  return -1;
307  }
308 
309  internal int FindNodeOffset(string localName, string namespaceURI)
310  {
311  int count = Count;
312  for (int i = 0; i < count; i++)
313  {
314  XmlNode xmlNode = (XmlNode)nodes[i];
315  if (xmlNode.LocalName == localName && xmlNode.NamespaceURI == namespaceURI)
316  {
317  return i;
318  }
319  }
320  return -1;
321  }
322 
323  internal virtual XmlNode AddNode(XmlNode node)
324  {
325  XmlNode oldParent = (node.NodeType != XmlNodeType.Attribute) ? node.ParentNode : ((XmlAttribute)node).OwnerElement;
326  string value = node.Value;
327  XmlNodeChangedEventArgs eventArgs = parent.GetEventArgs(node, oldParent, parent, value, value, XmlNodeChangedAction.Insert);
328  if (eventArgs != null)
329  {
330  parent.BeforeEvent(eventArgs);
331  }
332  nodes.Add(node);
333  node.SetParent(parent);
334  if (eventArgs != null)
335  {
336  parent.AfterEvent(eventArgs);
337  }
338  return node;
339  }
340 
341  internal virtual XmlNode AddNodeForLoad(XmlNode node, XmlDocument doc)
342  {
343  XmlNodeChangedEventArgs insertEventArgsForLoad = doc.GetInsertEventArgsForLoad(node, parent);
344  if (insertEventArgsForLoad != null)
345  {
346  doc.BeforeEvent(insertEventArgsForLoad);
347  }
348  nodes.Add(node);
349  node.SetParent(parent);
350  if (insertEventArgsForLoad != null)
351  {
352  doc.AfterEvent(insertEventArgsForLoad);
353  }
354  return node;
355  }
356 
357  internal virtual XmlNode RemoveNodeAt(int i)
358  {
359  XmlNode xmlNode = (XmlNode)nodes[i];
360  string value = xmlNode.Value;
361  XmlNodeChangedEventArgs eventArgs = parent.GetEventArgs(xmlNode, parent, null, value, value, XmlNodeChangedAction.Remove);
362  if (eventArgs != null)
363  {
364  parent.BeforeEvent(eventArgs);
365  }
366  nodes.RemoveAt(i);
367  xmlNode.SetParent(null);
368  if (eventArgs != null)
369  {
370  parent.AfterEvent(eventArgs);
371  }
372  return xmlNode;
373  }
374 
375  internal XmlNode ReplaceNodeAt(int i, XmlNode node)
376  {
377  XmlNode result = RemoveNodeAt(i);
378  InsertNodeAt(i, node);
379  return result;
380  }
381 
382  internal virtual XmlNode InsertNodeAt(int i, XmlNode node)
383  {
384  XmlNode oldParent = (node.NodeType != XmlNodeType.Attribute) ? node.ParentNode : ((XmlAttribute)node).OwnerElement;
385  string value = node.Value;
386  XmlNodeChangedEventArgs eventArgs = parent.GetEventArgs(node, oldParent, parent, value, value, XmlNodeChangedAction.Insert);
387  if (eventArgs != null)
388  {
389  parent.BeforeEvent(eventArgs);
390  }
391  nodes.Insert(i, node);
392  node.SetParent(parent);
393  if (eventArgs != null)
394  {
395  parent.AfterEvent(eventArgs);
396  }
397  return node;
398  }
399  }
400 }
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
abstract string LocalName
Gets the local name of the node, when overridden in a derived class.
Definition: XmlNode.cs:163
virtual void Insert(int index, object value)
Inserts an element into the T:System.Collections.ArrayList at the specified index.
Definition: ArrayList.cs:2698
virtual void RemoveAt(int index)
Removes the element at the specified index of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2847
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual XmlNode RemoveNamedItem(string localName, string namespaceURI)
Removes a node with the matching P:System.Xml.XmlNode.LocalName and P:System.Xml.XmlNode....
XmlNodeType
Specifies the type of node.
Definition: XmlNodeType.cs:5
virtual IEnumerator GetEnumerator()
Provides support for the "foreach" style iteration over the collection of nodes in the XmlNamedNodeMa...
virtual string NamespaceURI
Gets the namespace URI of this node.
Definition: XmlNode.cs:143
Represents a collection of nodes that can be accessed by name or index.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
The exception that is thrown when an attempt is made to access an element of an array or collection w...
virtual XmlNode Item(int index)
Retrieves the node at the specified index in the XmlNamedNodeMap.
XmlNodeChangedAction
Specifies the type of node change.
virtual XmlNode GetNamedItem(string name)
Retrieves an T:System.Xml.XmlNode specified by name.
The Add key (the addition key on the numeric keypad).
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
virtual int Count
Gets the number of nodes in the XmlNamedNodeMap.
virtual XmlNode GetNamedItem(string localName, string namespaceURI)
Retrieves a node with the matching P:System.Xml.XmlNode.LocalName and P:System.Xml....
virtual XmlNode SetNamedItem(XmlNode node)
Adds an T:System.Xml.XmlNode using its P:System.Xml.XmlNode.Name property.
A node is being inserted in the tree.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
The exception that is thrown when a method call is invalid for the object's current state.
Represents a single node in the XML document.
Definition: XmlNode.cs:13
abstract string Name
Gets the qualified name of the node, when overridden in a derived class.
Definition: XmlNode.cs:20
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
virtual XmlNode RemoveNamedItem(string name)
Removes the node from the XmlNamedNodeMap.