mscorlib(4.0.0.0) API with additions
XmlAttributeCollection.cs
1 using System.Collections;
3 
4 namespace System.Xml
5 {
8  {
13  [IndexerName("ItemOf")]
14  public XmlAttribute this[int i]
15  {
16  get
17  {
18  try
19  {
20  return (XmlAttribute)nodes[i];
21  }
23  {
24  throw new IndexOutOfRangeException(Res.GetString("Xdom_IndexOutOfRange"));
25  }
26  }
27  }
28 
32  [IndexerName("ItemOf")]
33  public XmlAttribute this[string name]
34  {
35  get
36  {
37  int hashCode = XmlName.GetHashCode(name);
38  for (int i = 0; i < nodes.Count; i++)
39  {
40  XmlAttribute xmlAttribute = (XmlAttribute)nodes[i];
41  if (hashCode == xmlAttribute.LocalNameHash && name == xmlAttribute.Name)
42  {
43  return xmlAttribute;
44  }
45  }
46  return null;
47  }
48  }
49 
54  [IndexerName("ItemOf")]
55  public XmlAttribute this[string localName, string namespaceURI]
56  {
57  get
58  {
59  int hashCode = XmlName.GetHashCode(localName);
60  for (int i = 0; i < nodes.Count; i++)
61  {
62  XmlAttribute xmlAttribute = (XmlAttribute)nodes[i];
63  if (hashCode == xmlAttribute.LocalNameHash && localName == xmlAttribute.LocalName && namespaceURI == xmlAttribute.NamespaceURI)
64  {
65  return xmlAttribute;
66  }
67  }
68  return null;
69  }
70  }
71 
75  {
76  get
77  {
78  return false;
79  }
80  }
81 
84  object ICollection.SyncRoot
85  {
86  get
87  {
88  return this;
89  }
90  }
91 
94  int ICollection.Count
95  {
96  get
97  {
98  return base.Count;
99  }
100  }
101 
102  internal XmlAttributeCollection(XmlNode parent)
103  : base(parent)
104  {
105  }
106 
107  internal int FindNodeOffset(XmlAttribute node)
108  {
109  for (int i = 0; i < nodes.Count; i++)
110  {
111  XmlAttribute xmlAttribute = (XmlAttribute)nodes[i];
112  if (xmlAttribute.LocalNameHash == node.LocalNameHash && xmlAttribute.Name == node.Name && xmlAttribute.NamespaceURI == node.NamespaceURI)
113  {
114  return i;
115  }
116  }
117  return -1;
118  }
119 
120  internal int FindNodeOffsetNS(XmlAttribute node)
121  {
122  for (int i = 0; i < nodes.Count; i++)
123  {
124  XmlAttribute xmlAttribute = (XmlAttribute)nodes[i];
125  if (xmlAttribute.LocalNameHash == node.LocalNameHash && xmlAttribute.LocalName == node.LocalName && xmlAttribute.NamespaceURI == node.NamespaceURI)
126  {
127  return i;
128  }
129  }
130  return -1;
131  }
132 
140  public override XmlNode SetNamedItem(XmlNode node)
141  {
142  if (node != null && !(node is XmlAttribute))
143  {
144  throw new ArgumentException(Res.GetString("Xdom_AttrCol_Object"));
145  }
146  int num = FindNodeOffset(node.LocalName, node.NamespaceURI);
147  if (num == -1)
148  {
149  return InternalAppendAttribute((XmlAttribute)node);
150  }
151  XmlNode result = base.RemoveNodeAt(num);
152  InsertNodeAt(num, node);
153  return result;
154  }
155 
160  {
161  if (node.OwnerDocument != null && node.OwnerDocument != parent.OwnerDocument)
162  {
163  throw new ArgumentException(Res.GetString("Xdom_NamedNode_Context"));
164  }
165  if (node.OwnerElement != null)
166  {
167  Detach(node);
168  }
169  RemoveDuplicateAttribute(node);
170  InsertNodeAt(0, node);
171  return node;
172  }
173 
180  {
181  XmlDocument ownerDocument = node.OwnerDocument;
182  if (ownerDocument == null || !ownerDocument.IsLoading)
183  {
184  if (ownerDocument != null && ownerDocument != parent.OwnerDocument)
185  {
186  throw new ArgumentException(Res.GetString("Xdom_NamedNode_Context"));
187  }
188  if (node.OwnerElement != null)
189  {
190  Detach(node);
191  }
192  AddNode(node);
193  }
194  else
195  {
196  base.AddNodeForLoad(node, ownerDocument);
197  InsertParentIntoElementIdAttrMap(node);
198  }
199  return node;
200  }
201 
208  {
209  if (newNode == refNode)
210  {
211  return newNode;
212  }
213  if (refNode == null)
214  {
215  return Append(newNode);
216  }
217  if (refNode.OwnerElement != parent)
218  {
219  throw new ArgumentException(Res.GetString("Xdom_AttrCol_Insert"));
220  }
221  if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
222  {
223  throw new ArgumentException(Res.GetString("Xdom_NamedNode_Context"));
224  }
225  if (newNode.OwnerElement != null)
226  {
227  Detach(newNode);
228  }
229  int num = FindNodeOffset(refNode.LocalName, refNode.NamespaceURI);
230  int num2 = RemoveDuplicateAttribute(newNode);
231  if (num2 >= 0 && num2 < num)
232  {
233  num--;
234  }
235  InsertNodeAt(num, newNode);
236  return newNode;
237  }
238 
245  {
246  if (newNode == refNode)
247  {
248  return newNode;
249  }
250  if (refNode == null)
251  {
252  return Prepend(newNode);
253  }
254  if (refNode.OwnerElement != parent)
255  {
256  throw new ArgumentException(Res.GetString("Xdom_AttrCol_Insert"));
257  }
258  if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
259  {
260  throw new ArgumentException(Res.GetString("Xdom_NamedNode_Context"));
261  }
262  if (newNode.OwnerElement != null)
263  {
264  Detach(newNode);
265  }
266  int num = FindNodeOffset(refNode.LocalName, refNode.NamespaceURI);
267  int num2 = RemoveDuplicateAttribute(newNode);
268  if (num2 >= 0 && num2 < num)
269  {
270  num--;
271  }
272  InsertNodeAt(num + 1, newNode);
273  return newNode;
274  }
275 
280  {
281  int count = nodes.Count;
282  for (int i = 0; i < count; i++)
283  {
284  if (nodes[i] == node)
285  {
286  RemoveNodeAt(i);
287  return node;
288  }
289  }
290  return null;
291  }
292 
296  public XmlAttribute RemoveAt(int i)
297  {
298  if (i < 0 || i >= Count)
299  {
300  return null;
301  }
302  return (XmlAttribute)RemoveNodeAt(i);
303  }
304 
306  public void RemoveAll()
307  {
308  int num = Count;
309  while (num > 0)
310  {
311  num--;
312  RemoveAt(num);
313  }
314  }
315 
319  void ICollection.CopyTo(Array array, int index)
320  {
321  int num = 0;
322  int count = Count;
323  while (num < count)
324  {
325  array.SetValue(nodes[num], index);
326  num++;
327  index++;
328  }
329  }
330 
334  public void CopyTo(XmlAttribute[] array, int index)
335  {
336  int num = 0;
337  int count = Count;
338  while (num < count)
339  {
340  array[index] = (XmlAttribute)((XmlNode)nodes[num]).CloneNode(deep: true);
341  num++;
342  index++;
343  }
344  }
345 
346  internal override XmlNode AddNode(XmlNode node)
347  {
348  RemoveDuplicateAttribute((XmlAttribute)node);
349  XmlNode result = base.AddNode(node);
350  InsertParentIntoElementIdAttrMap((XmlAttribute)node);
351  return result;
352  }
353 
354  internal override XmlNode InsertNodeAt(int i, XmlNode node)
355  {
356  XmlNode result = base.InsertNodeAt(i, node);
357  InsertParentIntoElementIdAttrMap((XmlAttribute)node);
358  return result;
359  }
360 
361  internal override XmlNode RemoveNodeAt(int i)
362  {
363  XmlNode xmlNode = base.RemoveNodeAt(i);
364  RemoveParentFromElementIdAttrMap((XmlAttribute)xmlNode);
365  XmlAttribute defaultAttribute = parent.OwnerDocument.GetDefaultAttribute((XmlElement)parent, xmlNode.Prefix, xmlNode.LocalName, xmlNode.NamespaceURI);
366  if (defaultAttribute != null)
367  {
368  InsertNodeAt(i, defaultAttribute);
369  }
370  return xmlNode;
371  }
372 
373  internal void Detach(XmlAttribute attr)
374  {
375  attr.OwnerElement.Attributes.Remove(attr);
376  }
377 
378  internal void InsertParentIntoElementIdAttrMap(XmlAttribute attr)
379  {
380  XmlElement xmlElement = parent as XmlElement;
381  if (xmlElement != null && parent.OwnerDocument != null)
382  {
383  XmlName iDInfoByElement = parent.OwnerDocument.GetIDInfoByElement(xmlElement.XmlName);
384  if (iDInfoByElement != null && iDInfoByElement.Prefix == attr.XmlName.Prefix && iDInfoByElement.LocalName == attr.XmlName.LocalName)
385  {
386  parent.OwnerDocument.AddElementWithId(attr.Value, xmlElement);
387  }
388  }
389  }
390 
391  internal void RemoveParentFromElementIdAttrMap(XmlAttribute attr)
392  {
393  XmlElement xmlElement = parent as XmlElement;
394  if (xmlElement != null && parent.OwnerDocument != null)
395  {
396  XmlName iDInfoByElement = parent.OwnerDocument.GetIDInfoByElement(xmlElement.XmlName);
397  if (iDInfoByElement != null && iDInfoByElement.Prefix == attr.XmlName.Prefix && iDInfoByElement.LocalName == attr.XmlName.LocalName)
398  {
399  parent.OwnerDocument.RemoveElementWithId(attr.Value, xmlElement);
400  }
401  }
402  }
403 
404  internal int RemoveDuplicateAttribute(XmlAttribute attr)
405  {
406  int num = FindNodeOffset(attr.LocalName, attr.NamespaceURI);
407  if (num != -1)
408  {
409  XmlAttribute attr2 = (XmlAttribute)nodes[num];
410  base.RemoveNodeAt(num);
411  RemoveParentFromElementIdAttrMap(attr2);
412  }
413  return num;
414  }
415 
416  internal bool PrepareParentInElementIdAttrMap(string attrPrefix, string attrLocalName)
417  {
418  XmlElement xmlElement = parent as XmlElement;
419  XmlDocument ownerDocument = parent.OwnerDocument;
420  XmlName iDInfoByElement = ownerDocument.GetIDInfoByElement(xmlElement.XmlName);
421  if (iDInfoByElement != null && iDInfoByElement.Prefix == attrPrefix && iDInfoByElement.LocalName == attrLocalName)
422  {
423  return true;
424  }
425  return false;
426  }
427 
428  internal void ResetParentInElementIdAttrMap(string oldVal, string newVal)
429  {
430  XmlElement elem = parent as XmlElement;
431  XmlDocument ownerDocument = parent.OwnerDocument;
432  ownerDocument.RemoveElementWithId(oldVal, elem);
433  ownerDocument.AddElementWithId(newVal, elem);
434  }
435 
436  internal XmlAttribute InternalAppendAttribute(XmlAttribute node)
437  {
438  XmlNode xmlNode = base.AddNode(node);
439  InsertParentIntoElementIdAttrMap(node);
440  return (XmlAttribute)xmlNode;
441  }
442  }
443 }
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: ICollection.cs:23
XmlAttribute Remove(XmlAttribute node)
Removes the specified attribute from the collection.
abstract string LocalName
Gets the local name of the node, when overridden in a derived class.
Definition: XmlNode.cs:163
Definition: __Canon.cs:3
void CopyTo(XmlAttribute[] array, int index)
Copies all the T:System.Xml.XmlAttribute objects from this collection into the given array.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Represents an attribute. Valid and default values for the attribute are defined in a document type de...
Definition: XmlAttribute.cs:7
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.
override string NamespaceURI
Gets the namespace URI of this node.
Definition: XmlAttribute.cs:41
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...
override XmlNode SetNamedItem(XmlNode node)
Adds a T:System.Xml.XmlNode using its P:System.Xml.XmlNode.Name property
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.ICollection is synchronized (threa...
Definition: ICollection.cs:33
XmlAttribute Append(XmlAttribute node)
Inserts the specified attribute as the last node in the collection.
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which the current node belongs.
Definition: XmlDocument.cs:173
The exception that is thrown when one of the arguments provided to a method is not valid.
override string LocalName
Gets the local name of the node.
Definition: XmlAttribute.cs:37
XmlAttribute Prepend(XmlAttribute node)
Inserts the specified attribute as the first node in the collection.
virtual XmlElement OwnerElement
Gets the T:System.Xml.XmlElement to which the attribute belongs.
void RemoveAll()
Removes all attributes from the collection.
XmlAttribute InsertAfter(XmlAttribute newNode, XmlAttribute refNode)
Inserts the specified attribute immediately after the specified reference attribute.
Represents a collection of attributes that can be accessed by name or index.
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
XmlAttribute RemoveAt(int i)
Removes the attribute corresponding to the specified index from the collection.
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which this node belongs.
Definition: XmlAttribute.cs:65
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
XmlAttribute InsertBefore(XmlAttribute newNode, XmlAttribute refNode)
Inserts the specified attribute immediately before the specified reference attribute.
override string Name
Gets the qualified name of the node.
Definition: XmlAttribute.cs:33
Represents a single node in the XML document.
Definition: XmlNode.cs:13