mscorlib(4.0.0.0) API with additions
XmlAttribute.cs
1 using System.Xml.Schema;
2 using System.Xml.XPath;
3 
4 namespace System.Xml
5 {
7  public class XmlAttribute : XmlNode
8  {
9  private XmlName name;
10 
11  private XmlLinkedNode lastChild;
12 
13  internal int LocalNameHash => name.HashCode;
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 XmlNode ParentNode => null;
30 
33  public override string Name => name.Name;
34 
37  public override string LocalName => name.LocalName;
38 
41  public override string NamespaceURI => name.NamespaceURI;
42 
47  public override string Prefix
48  {
49  get
50  {
51  return name.Prefix;
52  }
53  set
54  {
55  name = name.OwnerDocument.AddAttrXmlName(value, LocalName, NamespaceURI, SchemaInfo);
56  }
57  }
58 
61  public override XmlNodeType NodeType => XmlNodeType.Attribute;
62 
65  public override XmlDocument OwnerDocument => name.OwnerDocument;
66 
70  public override string Value
71  {
72  get
73  {
74  return InnerText;
75  }
76  set
77  {
78  InnerText = value;
79  }
80  }
81 
84  public override IXmlSchemaInfo SchemaInfo => name;
85 
88  public override string InnerText
89  {
90  set
91  {
92  if (PrepareOwnerElementInElementIdAttrMap())
93  {
94  string innerText = base.InnerText;
95  base.InnerText = value;
96  ResetOwnerElementInElementIdAttrMap(innerText);
97  }
98  else
99  {
100  base.InnerText = value;
101  }
102  }
103  }
104 
105  internal override bool IsContainer => true;
106 
107  internal override XmlLinkedNode LastNode
108  {
109  get
110  {
111  return lastChild;
112  }
113  set
114  {
115  lastChild = value;
116  }
117  }
118 
122  public virtual bool Specified => true;
123 
126  public virtual XmlElement OwnerElement => parentNode as XmlElement;
127 
131  public override string InnerXml
132  {
133  set
134  {
135  RemoveAll();
136  XmlLoader xmlLoader = new XmlLoader();
137  xmlLoader.LoadInnerXmlAttribute(this, value);
138  }
139  }
140 
143  public override string BaseURI
144  {
145  get
146  {
147  if (OwnerElement != null)
148  {
149  return OwnerElement.BaseURI;
150  }
151  return string.Empty;
152  }
153  }
154 
155  internal override XmlSpace XmlSpace
156  {
157  get
158  {
159  if (OwnerElement != null)
160  {
161  return OwnerElement.XmlSpace;
162  }
163  return XmlSpace.None;
164  }
165  }
166 
167  internal override string XmlLang
168  {
169  get
170  {
171  if (OwnerElement != null)
172  {
173  return OwnerElement.XmlLang;
174  }
175  return string.Empty;
176  }
177  }
178 
179  internal override XPathNodeType XPNodeType
180  {
181  get
182  {
183  if (IsNamespace)
184  {
185  return XPathNodeType.Namespace;
186  }
187  return XPathNodeType.Attribute;
188  }
189  }
190 
191  internal override string XPLocalName
192  {
193  get
194  {
195  if (name.Prefix.Length == 0 && name.LocalName == "xmlns")
196  {
197  return string.Empty;
198  }
199  return name.LocalName;
200  }
201  }
202 
203  internal bool IsNamespace => Ref.Equal(name.NamespaceURI, name.OwnerDocument.strReservedXmlns);
204 
205  internal XmlAttribute(XmlName name, XmlDocument doc)
206  : base(doc)
207  {
208  parentNode = null;
209  if (!doc.IsLoading)
210  {
211  XmlDocument.CheckName(name.Prefix);
212  XmlDocument.CheckName(name.LocalName);
213  }
214  if (name.LocalName.Length == 0)
215  {
216  throw new ArgumentException(Res.GetString("Xdom_Attr_Name"));
217  }
218  this.name = name;
219  }
220 
226  protected internal XmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc)
227  : this(doc.AddAttrXmlName(prefix, localName, namespaceURI, null), doc)
228  {
229  }
230 
235  public override XmlNode CloneNode(bool deep)
236  {
237  XmlDocument ownerDocument = OwnerDocument;
238  XmlAttribute xmlAttribute = ownerDocument.CreateAttribute(Prefix, LocalName, NamespaceURI);
239  xmlAttribute.CopyChildren(ownerDocument, this, deep: true);
240  return xmlAttribute;
241  }
242 
243  internal bool PrepareOwnerElementInElementIdAttrMap()
244  {
245  XmlDocument ownerDocument = OwnerDocument;
246  if (ownerDocument.DtdSchemaInfo != null)
247  {
248  XmlElement ownerElement = OwnerElement;
249  if (ownerElement != null)
250  {
251  return ownerElement.Attributes.PrepareParentInElementIdAttrMap(Prefix, LocalName);
252  }
253  }
254  return false;
255  }
256 
257  internal void ResetOwnerElementInElementIdAttrMap(string oldInnerText)
258  {
259  OwnerElement?.Attributes.ResetParentInElementIdAttrMap(oldInnerText, InnerText);
260  }
261 
262  internal override XmlNode AppendChildForLoad(XmlNode newChild, XmlDocument doc)
263  {
264  XmlNodeChangedEventArgs insertEventArgsForLoad = doc.GetInsertEventArgsForLoad(newChild, this);
265  if (insertEventArgsForLoad != null)
266  {
267  doc.BeforeEvent(insertEventArgsForLoad);
268  }
269  XmlLinkedNode xmlLinkedNode = (XmlLinkedNode)newChild;
270  if (lastChild == null)
271  {
272  xmlLinkedNode.next = xmlLinkedNode;
273  lastChild = xmlLinkedNode;
274  xmlLinkedNode.SetParentForLoad(this);
275  }
276  else
277  {
278  XmlLinkedNode xmlLinkedNode2 = lastChild;
279  xmlLinkedNode.next = xmlLinkedNode2.next;
280  xmlLinkedNode2.next = xmlLinkedNode;
281  lastChild = xmlLinkedNode;
282  if (xmlLinkedNode2.IsText && xmlLinkedNode.IsText)
283  {
284  XmlNode.NestTextNodes(xmlLinkedNode2, xmlLinkedNode);
285  }
286  else
287  {
288  xmlLinkedNode.SetParentForLoad(this);
289  }
290  }
291  if (insertEventArgsForLoad != null)
292  {
293  doc.AfterEvent(insertEventArgsForLoad);
294  }
295  return xmlLinkedNode;
296  }
297 
298  internal override bool IsValidChildType(XmlNodeType type)
299  {
300  if (type != XmlNodeType.Text)
301  {
302  return type == XmlNodeType.EntityReference;
303  }
304  return true;
305  }
306 
313  public override XmlNode InsertBefore(XmlNode newChild, XmlNode refChild)
314  {
315  XmlNode result;
316  if (PrepareOwnerElementInElementIdAttrMap())
317  {
318  string innerText = InnerText;
319  result = base.InsertBefore(newChild, refChild);
320  ResetOwnerElementInElementIdAttrMap(innerText);
321  }
322  else
323  {
324  result = base.InsertBefore(newChild, refChild);
325  }
326  return result;
327  }
328 
335  public override XmlNode InsertAfter(XmlNode newChild, XmlNode refChild)
336  {
337  XmlNode result;
338  if (PrepareOwnerElementInElementIdAttrMap())
339  {
340  string innerText = InnerText;
341  result = base.InsertAfter(newChild, refChild);
342  ResetOwnerElementInElementIdAttrMap(innerText);
343  }
344  else
345  {
346  result = base.InsertAfter(newChild, refChild);
347  }
348  return result;
349  }
350 
357  public override XmlNode ReplaceChild(XmlNode newChild, XmlNode oldChild)
358  {
359  XmlNode result;
360  if (PrepareOwnerElementInElementIdAttrMap())
361  {
362  string innerText = InnerText;
363  result = base.ReplaceChild(newChild, oldChild);
364  ResetOwnerElementInElementIdAttrMap(innerText);
365  }
366  else
367  {
368  result = base.ReplaceChild(newChild, oldChild);
369  }
370  return result;
371  }
372 
377  public override XmlNode RemoveChild(XmlNode oldChild)
378  {
379  XmlNode result;
380  if (PrepareOwnerElementInElementIdAttrMap())
381  {
382  string innerText = InnerText;
383  result = base.RemoveChild(oldChild);
384  ResetOwnerElementInElementIdAttrMap(innerText);
385  }
386  else
387  {
388  result = base.RemoveChild(oldChild);
389  }
390  return result;
391  }
392 
398  public override XmlNode PrependChild(XmlNode newChild)
399  {
400  XmlNode result;
401  if (PrepareOwnerElementInElementIdAttrMap())
402  {
403  string innerText = InnerText;
404  result = base.PrependChild(newChild);
405  ResetOwnerElementInElementIdAttrMap(innerText);
406  }
407  else
408  {
409  result = base.PrependChild(newChild);
410  }
411  return result;
412  }
413 
419  public override XmlNode AppendChild(XmlNode newChild)
420  {
421  XmlNode result;
422  if (PrepareOwnerElementInElementIdAttrMap())
423  {
424  string innerText = InnerText;
425  result = base.AppendChild(newChild);
426  ResetOwnerElementInElementIdAttrMap(innerText);
427  }
428  else
429  {
430  result = base.AppendChild(newChild);
431  }
432  return result;
433  }
434 
437  public override void WriteTo(XmlWriter w)
438  {
440  WriteContentTo(w);
441  w.WriteEndAttribute();
442  }
443 
446  public override void WriteContentTo(XmlWriter w)
447  {
448  for (XmlNode xmlNode = FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
449  {
450  xmlNode.WriteTo(w);
451  }
452  }
453 
454  internal override void SetParent(XmlNode node)
455  {
456  parentNode = node;
457  }
458  }
459 }
override XmlNode PrependChild(XmlNode newChild)
Adds the specified node to the beginning of the list of child nodes for this node.
Represents an XML document. You can use this class to load, validate, edit, add, and position XML in ...
Definition: XmlDocument.cs:13
internal XmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc)
Initializes a new instance of the T:System.Xml.XmlAttribute class.
override XmlNode RemoveChild(XmlNode oldChild)
Removes the specified child node.
Defines the post-schema-validation infoset of a validated XML node.
override XmlNode InsertBefore(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately before the specified reference node.
override XmlNode InsertAfter(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately after the specified reference node.
virtual XmlNode ReplaceChild(XmlNode newChild, XmlNode oldChild)
Replaces the child node oldChild with newChild node.
Definition: XmlNode.cs:734
Definition: __Canon.cs:3
Represents an attribute. Valid and default values for the attribute are defined in a document type de...
Definition: XmlAttribute.cs:7
abstract void WriteEndAttribute()
When overridden in a derived class, closes the previous M:System.Xml.XmlWriter.WriteStartAttribute(Sy...
override IXmlSchemaInfo SchemaInfo
Gets the post-schema-validation-infoset that has been assigned to this node as a result of schema val...
Definition: XmlAttribute.cs:84
void WriteStartAttribute(string localName, string ns)
Writes the start of an attribute with the specified local name and namespace URI.
Definition: XmlWriter.cs:199
virtual XmlNode InsertBefore(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately before the specified reference node.
Definition: XmlNode.cs:514
XmlNodeType
Specifies the type of node.
Definition: XmlNodeType.cs:5
virtual XmlNode PrependChild(XmlNode newChild)
Adds the specified node to the beginning of the list of child nodes for this node.
Definition: XmlNode.cs:823
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
override string InnerText
Sets the concatenated values of the node and all its children.
Definition: XmlAttribute.cs:89
override string BaseURI
Gets the base Uniform Resource Identifier (URI) of the node.
Represents a writer that provides a fast, non-cached, forward-only way to generate streams or files t...
Definition: XmlWriter.cs:12
override string NamespaceURI
Gets the namespace URI of this node.
Definition: XmlAttribute.cs:41
virtual bool Specified
Gets a value indicating whether the attribute value was explicitly set.
Gets the node immediately preceding or following this node.
Definition: XmlLinkedNode.cs:4
virtual string BaseURI
Gets the base URI of the current node.
Definition: XmlNode.cs:273
override XmlNode ReplaceChild(XmlNode newChild, XmlNode oldChild)
Replaces the child node specified with the new child node specified.
override void WriteContentTo(XmlWriter w)
Saves all the children of the node to the specified T:System.Xml.XmlWriter.
Represents an element.
Definition: XmlElement.cs:7
override string InnerXml
Sets the value of the attribute.
XmlAttribute CreateAttribute(string name)
Creates an T:System.Xml.XmlAttribute with the specified P:System.Xml.XmlDocument.Name.
Definition: XmlDocument.cs:786
override XmlNode AppendChild(XmlNode newChild)
Adds the specified node to the end of the list of child nodes, of this node.
override string LocalName
Gets the local name of the node.
Definition: XmlAttribute.cs:37
virtual XmlElement OwnerElement
Gets the T:System.Xml.XmlElement to which the attribute belongs.
virtual XmlNode RemoveChild(XmlNode oldChild)
Removes specified child node.
Definition: XmlNode.cs:746
override XmlNode ParentNode
Gets the parent of this node. For XmlAttribute nodes, this property always returns null.
Definition: XmlAttribute.cs:29
override void WriteTo(XmlWriter w)
Saves the node to the specified T:System.Xml.XmlWriter.
virtual XmlNode FirstChild
Gets the first child of the node.
Definition: XmlNode.cs:117
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
virtual XmlNode NextSibling
Gets the node immediately following this node.
Definition: XmlNode.cs:95
virtual void RemoveAll()
Removes all the child nodes and/or attributes of the current node.
Definition: XmlNode.cs:1143
XmlSpace
Specifies the current xml:space scope.
Definition: XmlSpace.cs:5
override XmlNodeType NodeType
Gets the type of the current node.
Definition: XmlAttribute.cs:61
virtual XmlNode InsertAfter(XmlNode newChild, XmlNode refChild)
Inserts the specified node immediately after the specified reference node.
Definition: XmlNode.cs:623
override string Value
Gets or sets the value of the node.
Definition: XmlAttribute.cs:71
override XmlDocument OwnerDocument
Gets the T:System.Xml.XmlDocument to which this node belongs.
Definition: XmlAttribute.cs:65
override string Prefix
Gets or sets the namespace prefix of this node.
Definition: XmlAttribute.cs:48
override string Name
Gets the qualified name of the node.
Definition: XmlAttribute.cs:33
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