mscorlib(4.0.0.0) API with additions
XmlElement.cs
1 using System.Xml.Schema;
2 using System.Xml.XPath;
3 
4 namespace System.Xml
5 {
7  public class XmlElement : XmlLinkedNode
8  {
9  private XmlName name;
10 
11  private XmlAttributeCollection attributes;
12 
13  private XmlLinkedNode lastChild;
14 
15  internal XmlName XmlName
16  {
17  get
18  {
19  return name;
20  }
21  set
22  {
23  name = value;
24  }
25  }
26 
29  public override string Name => name.Name;
30 
33  public override string LocalName => name.LocalName;
34 
37  public override string NamespaceURI => name.NamespaceURI;
38 
43  public override string Prefix
44  {
45  get
46  {
47  return name.Prefix;
48  }
49  set
50  {
51  name = name.OwnerDocument.AddXmlName(value, LocalName, NamespaceURI, SchemaInfo);
52  }
53  }
54 
57  public override XmlNodeType NodeType => XmlNodeType.Element;
58 
61  public override XmlNode ParentNode => parentNode;
62 
65  public override XmlDocument OwnerDocument => name.OwnerDocument;
66 
67  internal override bool IsContainer => true;
68 
71  public bool IsEmpty
72  {
73  get
74  {
75  return lastChild == this;
76  }
77  set
78  {
79  if (value)
80  {
81  if (lastChild != this)
82  {
83  RemoveAllChildren();
84  lastChild = this;
85  }
86  }
87  else if (lastChild == this)
88  {
89  lastChild = null;
90  }
91  }
92  }
93 
94  internal override XmlLinkedNode LastNode
95  {
96  get
97  {
98  if (lastChild != this)
99  {
100  return lastChild;
101  }
102  return null;
103  }
104  set
105  {
106  lastChild = value;
107  }
108  }
109 
113  public override XmlAttributeCollection Attributes
114  {
115  get
116  {
117  if (attributes == null)
118  {
119  lock (OwnerDocument.objLock)
120  {
121  if (attributes == null)
122  {
123  attributes = new XmlAttributeCollection(this);
124  }
125  }
126  }
127  return attributes;
128  }
129  }
130 
134  public virtual bool HasAttributes
135  {
136  get
137  {
138  if (attributes == null)
139  {
140  return false;
141  }
142  return attributes.Count > 0;
143  }
144  }
145 
148  public override IXmlSchemaInfo SchemaInfo => name;
149 
153  public override string InnerXml
154  {
155  get
156  {
157  return base.InnerXml;
158  }
159  set
160  {
161  RemoveAllChildren();
162  XmlLoader xmlLoader = new XmlLoader();
163  xmlLoader.LoadInnerXmlElement(this, value);
164  }
165  }
166 
169  public override string InnerText
170  {
171  get
172  {
173  return base.InnerText;
174  }
175  set
176  {
177  XmlLinkedNode lastNode = LastNode;
178  if (lastNode != null && lastNode.NodeType == XmlNodeType.Text && lastNode.next == lastNode)
179  {
180  lastNode.Value = value;
181  return;
182  }
183  RemoveAllChildren();
185  }
186  }
187 
190  public override XmlNode NextSibling
191  {
192  get
193  {
194  if (parentNode != null && parentNode.LastNode != this)
195  {
196  return next;
197  }
198  return null;
199  }
200  }
201 
202  internal override XPathNodeType XPNodeType => XPathNodeType.Element;
203 
204  internal override string XPLocalName => LocalName;
205 
206  internal XmlElement(XmlName name, bool empty, XmlDocument doc)
207  : base(doc)
208  {
209  parentNode = null;
210  if (!doc.IsLoading)
211  {
212  XmlDocument.CheckName(name.Prefix);
213  XmlDocument.CheckName(name.LocalName);
214  }
215  if (name.LocalName.Length == 0)
216  {
217  throw new ArgumentException(Res.GetString("Xdom_Empty_LocalName"));
218  }
219  this.name = name;
220  if (empty)
221  {
222  lastChild = this;
223  }
224  }
225 
231  protected internal XmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc)
232  : this(doc.AddXmlName(prefix, localName, namespaceURI, null), empty: true, doc)
233  {
234  }
235 
240  public override XmlNode CloneNode(bool deep)
241  {
242  XmlDocument ownerDocument = OwnerDocument;
243  bool isLoading = ownerDocument.IsLoading;
244  ownerDocument.IsLoading = true;
245  XmlElement xmlElement = ownerDocument.CreateElement(Prefix, LocalName, NamespaceURI);
246  ownerDocument.IsLoading = isLoading;
247  if (xmlElement.IsEmpty != IsEmpty)
248  {
249  xmlElement.IsEmpty = IsEmpty;
250  }
251  if (HasAttributes)
252  {
253  foreach (XmlAttribute attribute in Attributes)
254  {
255  XmlAttribute xmlAttribute2 = (XmlAttribute)attribute.CloneNode(deep: true);
256  if (attribute is XmlUnspecifiedAttribute && !attribute.Specified)
257  {
258  ((XmlUnspecifiedAttribute)xmlAttribute2).SetSpecified(f: false);
259  }
260  xmlElement.Attributes.InternalAppendAttribute(xmlAttribute2);
261  }
262  }
263  if (deep)
264  {
265  xmlElement.CopyChildren(ownerDocument, this, deep);
266  }
267  return xmlElement;
268  }
269 
270  internal override XmlNode AppendChildForLoad(XmlNode newChild, XmlDocument doc)
271  {
272  XmlNodeChangedEventArgs insertEventArgsForLoad = doc.GetInsertEventArgsForLoad(newChild, this);
273  if (insertEventArgsForLoad != null)
274  {
275  doc.BeforeEvent(insertEventArgsForLoad);
276  }
277  XmlLinkedNode xmlLinkedNode = (XmlLinkedNode)newChild;
278  if (lastChild == null || lastChild == this)
279  {
280  xmlLinkedNode.next = xmlLinkedNode;
281  lastChild = xmlLinkedNode;
282  xmlLinkedNode.SetParentForLoad(this);
283  }
284  else
285  {
286  XmlLinkedNode xmlLinkedNode2 = lastChild;
287  xmlLinkedNode.next = xmlLinkedNode2.next;
288  xmlLinkedNode2.next = xmlLinkedNode;
289  lastChild = xmlLinkedNode;
290  if (xmlLinkedNode2.IsText && xmlLinkedNode.IsText)
291  {
292  XmlNode.NestTextNodes(xmlLinkedNode2, xmlLinkedNode);
293  }
294  else
295  {
296  xmlLinkedNode.SetParentForLoad(this);
297  }
298  }
299  if (insertEventArgsForLoad != null)
300  {
301  doc.AfterEvent(insertEventArgsForLoad);
302  }
303  return xmlLinkedNode;
304  }
305 
306  internal override bool IsValidChildType(XmlNodeType type)
307  {
308  switch (type)
309  {
310  case XmlNodeType.Element:
311  case XmlNodeType.Text:
312  case XmlNodeType.CDATA:
313  case XmlNodeType.EntityReference:
314  case XmlNodeType.ProcessingInstruction:
315  case XmlNodeType.Comment:
316  case XmlNodeType.Whitespace:
317  case XmlNodeType.SignificantWhitespace:
318  return true;
319  default:
320  return false;
321  }
322  }
323 
327  public virtual string GetAttribute(string name)
328  {
329  XmlAttribute attributeNode = GetAttributeNode(name);
330  if (attributeNode != null)
331  {
332  return attributeNode.Value;
333  }
334  return string.Empty;
335  }
336 
342  public virtual void SetAttribute(string name, string value)
343  {
344  XmlAttribute attributeNode = GetAttributeNode(name);
345  if (attributeNode == null)
346  {
347  attributeNode = OwnerDocument.CreateAttribute(name);
348  attributeNode.Value = value;
349  Attributes.InternalAppendAttribute(attributeNode);
350  }
351  else
352  {
353  attributeNode.Value = value;
354  }
355  }
356 
360  public virtual void RemoveAttribute(string name)
361  {
362  if (HasAttributes)
363  {
365  }
366  }
367 
371  public virtual XmlAttribute GetAttributeNode(string name)
372  {
373  if (HasAttributes)
374  {
375  return Attributes[name];
376  }
377  return null;
378  }
379 
386  {
387  if (newAttr.OwnerElement != null)
388  {
389  throw new InvalidOperationException(Res.GetString("Xdom_Attr_InUse"));
390  }
391  return (XmlAttribute)Attributes.SetNamedItem(newAttr);
392  }
393 
399  {
400  if (HasAttributes)
401  {
402  return Attributes.Remove(oldAttr);
403  }
404  return null;
405  }
406 
410  public virtual XmlNodeList GetElementsByTagName(string name)
411  {
412  return new XmlElementList(this, name);
413  }
414 
419  public virtual string GetAttribute(string localName, string namespaceURI)
420  {
421  XmlAttribute attributeNode = GetAttributeNode(localName, namespaceURI);
422  if (attributeNode != null)
423  {
424  return attributeNode.Value;
425  }
426  return string.Empty;
427  }
428 
434  public virtual string SetAttribute(string localName, string namespaceURI, string value)
435  {
436  XmlAttribute attributeNode = GetAttributeNode(localName, namespaceURI);
437  if (attributeNode == null)
438  {
439  attributeNode = OwnerDocument.CreateAttribute(string.Empty, localName, namespaceURI);
440  attributeNode.Value = value;
441  Attributes.InternalAppendAttribute(attributeNode);
442  }
443  else
444  {
445  attributeNode.Value = value;
446  }
447  return value;
448  }
449 
454  public virtual void RemoveAttribute(string localName, string namespaceURI)
455  {
456  RemoveAttributeNode(localName, namespaceURI);
457  }
458 
463  public virtual XmlAttribute GetAttributeNode(string localName, string namespaceURI)
464  {
465  if (HasAttributes)
466  {
467  return Attributes[localName, namespaceURI];
468  }
469  return null;
470  }
471 
476  public virtual XmlAttribute SetAttributeNode(string localName, string namespaceURI)
477  {
478  XmlAttribute xmlAttribute = GetAttributeNode(localName, namespaceURI);
479  if (xmlAttribute == null)
480  {
481  xmlAttribute = OwnerDocument.CreateAttribute(string.Empty, localName, namespaceURI);
482  Attributes.InternalAppendAttribute(xmlAttribute);
483  }
484  return xmlAttribute;
485  }
486 
492  public virtual XmlAttribute RemoveAttributeNode(string localName, string namespaceURI)
493  {
494  if (HasAttributes)
495  {
496  XmlAttribute attributeNode = GetAttributeNode(localName, namespaceURI);
497  Attributes.Remove(attributeNode);
498  return attributeNode;
499  }
500  return null;
501  }
502 
507  public virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
508  {
509  return new XmlElementList(this, localName, namespaceURI);
510  }
511 
516  public virtual bool HasAttribute(string name)
517  {
518  return GetAttributeNode(name) != null;
519  }
520 
526  public virtual bool HasAttribute(string localName, string namespaceURI)
527  {
528  return GetAttributeNode(localName, namespaceURI) != null;
529  }
530 
533  public override void WriteTo(XmlWriter w)
534  {
535  if (GetType() == typeof(XmlElement))
536  {
537  WriteElementTo(w, this);
538  return;
539  }
540  WriteStartElement(w);
541  if (IsEmpty)
542  {
543  w.WriteEndElement();
544  return;
545  }
546  WriteContentTo(w);
548  }
549 
550  private static void WriteElementTo(XmlWriter writer, XmlElement e)
551  {
552  XmlNode xmlNode = e;
553  XmlNode xmlNode2 = e;
554  while (true)
555  {
556  e = (xmlNode2 as XmlElement);
557  if (e != null && e.GetType() == typeof(XmlElement))
558  {
559  e.WriteStartElement(writer);
560  if (e.IsEmpty)
561  {
562  writer.WriteEndElement();
563  }
564  else
565  {
566  if (e.lastChild != null)
567  {
568  xmlNode2 = e.FirstChild;
569  continue;
570  }
571  writer.WriteFullEndElement();
572  }
573  }
574  else
575  {
576  xmlNode2.WriteTo(writer);
577  }
578  while (xmlNode2 != xmlNode && xmlNode2 == xmlNode2.ParentNode.LastChild)
579  {
580  xmlNode2 = xmlNode2.ParentNode;
581  writer.WriteFullEndElement();
582  }
583  if (xmlNode2 != xmlNode)
584  {
585  xmlNode2 = xmlNode2.NextSibling;
586  continue;
587  }
588  break;
589  }
590  }
591 
592  private void WriteStartElement(XmlWriter w)
593  {
594  w.WriteStartElement(Prefix, LocalName, NamespaceURI);
595  if (HasAttributes)
596  {
597  XmlAttributeCollection xmlAttributeCollection = Attributes;
598  for (int i = 0; i < xmlAttributeCollection.Count; i++)
599  {
600  XmlAttribute xmlAttribute = xmlAttributeCollection[i];
601  xmlAttribute.WriteTo(w);
602  }
603  }
604  }
605 
608  public override void WriteContentTo(XmlWriter w)
609  {
610  for (XmlNode xmlNode = FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
611  {
612  xmlNode.WriteTo(w);
613  }
614  }
615 
619  public virtual XmlNode RemoveAttributeAt(int i)
620  {
621  if (HasAttributes)
622  {
623  return attributes.RemoveAt(i);
624  }
625  return null;
626  }
627 
629  public virtual void RemoveAllAttributes()
630  {
631  if (HasAttributes)
632  {
633  attributes.RemoveAll();
634  }
635  }
636 
638  public override void RemoveAll()
639  {
640  base.RemoveAll();
642  }
643 
644  internal void RemoveAllChildren()
645  {
646  base.RemoveAll();
647  }
648 
649  internal override void SetParent(XmlNode node)
650  {
651  parentNode = node;
652  }
653 
654  internal override string GetXPAttribute(string localName, string ns)
655  {
656  if (ns == OwnerDocument.strReservedXmlns)
657  {
658  return null;
659  }
660  XmlAttribute attributeNode = GetAttributeNode(localName, ns);
661  if (attributeNode != null)
662  {
663  return attributeNode.Value;
664  }
665  return string.Empty;
666  }
667  }
668 }
virtual XmlAttribute GetAttributeNode(string localName, string namespaceURI)
Returns the T:System.Xml.XmlAttribute with the specified local name and namespace URI.
Definition: XmlElement.cs:463
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
virtual string GetAttribute(string localName, string namespaceURI)
Returns the value for the attribute with the specified local name and namespace URI.
Definition: XmlElement.cs:419
XmlAttribute Remove(XmlAttribute node)
Removes the specified attribute from the collection.
virtual void RemoveAllAttributes()
Removes all specified attributes from the element. Default attributes are not removed.
Definition: XmlElement.cs:629
override void RemoveAll()
Removes all specified attributes and children of the current node. Default attributes are not removed...
Definition: XmlElement.cs:638
Defines the post-schema-validation infoset of a validated XML node.
virtual XmlNode ParentNode
Gets the parent of this node (for nodes that can have parents).
Definition: XmlNode.cs:60
Represents an ordered collection of nodes.
Definition: XmlNodeList.cs:7
override string LocalName
Gets the local name of the current node.
Definition: XmlElement.cs:33
abstract void WriteTo(XmlWriter w)
Saves the current node to the specified T:System.Xml.XmlWriter, when overridden in a derived class.
virtual bool HasAttribute(string localName, string namespaceURI)
Determines whether the current node has an attribute with the specified local name and namespace URI.
Definition: XmlElement.cs:526
virtual XmlAttribute GetAttributeNode(string name)
Returns the XmlAttribute with the specified name.
Definition: XmlElement.cs:371
XmlElement CreateElement(string name)
Creates an element with the specified name.
Definition: XmlDocument.cs:847
virtual XmlNode LastChild
Gets the last child of the node.
Definition: XmlNode.cs:121
Definition: __Canon.cs:3
virtual XmlNodeList GetElementsByTagName(string name)
Returns an T:System.Xml.XmlNodeList containing a list of all descendant elements that match the speci...
Definition: XmlElement.cs:410
Represents an attribute. Valid and default values for the attribute are defined in a document type de...
Definition: XmlAttribute.cs:7
override XmlNode CloneNode(bool deep)
Creates a duplicate of this node.
Definition: XmlElement.cs:240
virtual XmlAttribute RemoveAttributeNode(XmlAttribute oldAttr)
Removes the specified T:System.Xml.XmlAttribute.
Definition: XmlElement.cs:398
override string InnerXml
Gets or sets the markup representing just the children of this node.
Definition: XmlElement.cs:154
bool IsEmpty
Gets or sets the tag format of the element.
Definition: XmlElement.cs:72
XmlNodeType
Specifies the type of node.
Definition: XmlNodeType.cs:5
override string Name
Gets the qualified name of the node.
Definition: XmlElement.cs:29
override XmlNode CloneNode(bool deep)
Creates a duplicate of this node.
XPathNodeType
Defines the XPath node types that can be returned from the T:System.Xml.XPath.XPathNavigator class.
Definition: XPathNodeType.cs:4
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
virtual string GetAttribute(string name)
Returns the value for the attribute with the specified name.
Definition: XmlElement.cs:327
Provides data for the E:System.Xml.XmlDocument.NodeChanged, E:System.Xml.XmlDocument....
virtual XmlText CreateTextNode(string text)
Creates an T:System.Xml.XmlText with the specified text.
Definition: XmlDocument.cs:942
virtual void RemoveAttribute(string localName, string namespaceURI)
Removes an attribute with the specified local name and namespace URI. (If the removed attribute has a...
Definition: XmlElement.cs:454
virtual bool HasAttribute(string name)
Determines whether the current node has an attribute with the specified name.
Definition: XmlElement.cs:516
virtual bool Specified
Gets a value indicating whether the attribute value was explicitly set.
override string InnerText
Gets or sets the concatenated values of the node and all its children.
Definition: XmlElement.cs:170
abstract void WriteFullEndElement()
When overridden in a derived class, closes one element and pops the corresponding namespace scope.
Gets the node immediately preceding or following this node.
Definition: XmlLinkedNode.cs:4
override XmlNode SetNamedItem(XmlNode node)
Adds a T:System.Xml.XmlNode using its P:System.Xml.XmlNode.Name property
virtual string Value
Gets or sets the value of the node.
Definition: XmlNode.cs:36
virtual bool HasAttributes
Gets a boolean value indicating whether the current node has any attributes.
Definition: XmlElement.cs:135
override void WriteTo(XmlWriter w)
Saves the current node to the specified T:System.Xml.XmlWriter.
Definition: XmlElement.cs:533
Represents an element.
Definition: XmlElement.cs:7
override XmlNodeType NodeType
Gets the type of the current node.
Definition: XmlElement.cs:57
XmlAttribute CreateAttribute(string name)
Creates an T:System.Xml.XmlAttribute with the specified P:System.Xml.XmlDocument.Name.
Definition: XmlDocument.cs:786
abstract void WriteEndElement()
When overridden in a derived class, closes one element and pops the corresponding namespace scope.
internal XmlElement(string prefix, string localName, string namespaceURI, XmlDocument doc)
Initializes a new instance of the T:System.Xml.XmlElement class.
Definition: XmlElement.cs:231
override IXmlSchemaInfo SchemaInfo
Gets the post schema validation infoset that has been assigned to this node as a result of schema val...
Definition: XmlElement.cs:148
virtual void RemoveAttribute(string name)
Removes an attribute by name.
Definition: XmlElement.cs:360
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual string SetAttribute(string localName, string namespaceURI, string value)
Sets the value of the attribute with the specified local name and namespace URI.
Definition: XmlElement.cs:434
override string NamespaceURI
Gets the namespace URI of this node.
Definition: XmlElement.cs:37
virtual XmlAttribute SetAttributeNode(XmlAttribute newAttr)
Adds the specified T:System.Xml.XmlAttribute.
Definition: XmlElement.cs:385
virtual XmlElement OwnerElement
Gets the T:System.Xml.XmlElement to which the attribute belongs.
void RemoveAll()
Removes all attributes from the collection.
virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
Returns an T:System.Xml.XmlNodeList containing a list of all descendant elements that match the speci...
Definition: XmlElement.cs:507
virtual XmlNode RemoveAttributeAt(int i)
Removes the attribute node with the specified index from the element. (If the removed attribute has a...
Definition: XmlElement.cs:619
virtual void SetAttribute(string name, string value)
Sets the value of the attribute with the specified name.
Definition: XmlElement.cs:342
virtual XmlNode FirstChild
Gets the first child of the node.
Definition: XmlNode.cs:117
override string Prefix
Gets or sets the namespace prefix of this node.
Definition: XmlElement.cs:44
virtual XmlNode AppendChild(XmlNode newChild)
Adds the specified node to the end of the list of child nodes, of this node.
Definition: XmlNode.cs:833
Represents a collection of attributes that can be accessed by name or index.
The exception that is thrown when a method call is invalid for the object's current state.
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which this node belongs.
Definition: XmlElement.cs:65
virtual XmlNode NextSibling
Gets the node immediately following this node.
Definition: XmlNode.cs:95
virtual XmlAttribute RemoveAttributeNode(string localName, string namespaceURI)
Removes the T:System.Xml.XmlAttribute specified by the local name and namespace URI....
Definition: XmlElement.cs:492
XmlAttribute RemoveAt(int i)
Removes the attribute corresponding to the specified index from the collection.
override void WriteContentTo(XmlWriter w)
Saves all the children of the node to the specified T:System.Xml.XmlWriter.
Definition: XmlElement.cs:608
override XmlNode ParentNode
Gets the parent of this node (for nodes that can have parents).
Definition: XmlElement.cs:61
override string Value
Gets or sets the value of the node.
Definition: XmlAttribute.cs:71
override XmlAttributeCollection Attributes
Gets an T:System.Xml.XmlAttributeCollection containing the list of attributes for this node.
Definition: XmlElement.cs:114
Represents a single node in the XML document.
Definition: XmlNode.cs:13
override XmlNode NextSibling
Gets the T:System.Xml.XmlNode immediately following this element.
Definition: XmlElement.cs:191
abstract XmlNodeType NodeType
Gets the type of the current node, when overridden in a derived class.
Definition: XmlNode.cs:53
virtual XmlAttribute SetAttributeNode(string localName, string namespaceURI)
Adds the specified T:System.Xml.XmlAttribute.
Definition: XmlElement.cs:476
virtual XmlNode RemoveNamedItem(string name)
Removes the node from the XmlNamedNodeMap.