1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
//------------------------------------------------------------------------------
// <copyright file="XmlAttribute.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Diagnostics;
// Represents an attribute of the XMLElement object. Valid and default
// values for the attribute are defined in a DTD or schema.
public class XmlAttribute : XmlNode {
XmlName name;
XmlLinkedNode lastChild;
internal XmlAttribute( XmlName name, XmlDocument doc ): base( doc ) {
Debug.Assert(name!=null);
Debug.Assert(doc!=null);
this.parentNode = null;
if ( !doc.IsLoading ) {
XmlDocument.CheckName( name.Prefix );
XmlDocument.CheckName( name.LocalName );
}
if (name.LocalName.Length == 0)
throw new ArgumentException(Res.GetString(Res.Xdom_Attr_Name));
this.name = name;
}
internal int LocalNameHash {
get { return name.HashCode; }
}
protected internal XmlAttribute( string prefix, string localName, string namespaceURI, XmlDocument doc )
: this(doc.AddAttrXmlName(prefix, localName, namespaceURI, null), doc) {
}
internal XmlName XmlName {
get { return name; }
set { name = value; }
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep) {
// CloneNode for attributes is deep irrespective of parameter 'deep' value
Debug.Assert( OwnerDocument != null );
XmlDocument doc = OwnerDocument;
XmlAttribute attr = doc.CreateAttribute( Prefix, LocalName, NamespaceURI );
attr.CopyChildren( doc, this, true );
return attr;
}
// Gets the parent of this node (for nodes that can have parents).
public override XmlNode ParentNode {
get { return null;}
}
// Gets the name of the node.
public override String Name {
get { return name.Name;}
}
// Gets the name of the node without the namespace prefix.
public override String LocalName {
get { return name.LocalName;}
}
// Gets the namespace URI of this node.
public override String NamespaceURI {
get { return name.NamespaceURI;}
}
// Gets or sets the namespace prefix of this node.
public override String Prefix {
get { return name.Prefix;}
set { name = name.OwnerDocument.AddAttrXmlName( value, LocalName, NamespaceURI, SchemaInfo ); }
}
// Gets the type of the current node.
public override XmlNodeType NodeType {
get { return XmlNodeType.Attribute;}
}
// Gets the XmlDocument that contains this node.
public override XmlDocument OwnerDocument {
get {
return name.OwnerDocument;
}
}
// Gets or sets the value of the node.
public override String Value {
get { return InnerText; }
set { InnerText = value; } //use InnerText which has perf optimization
}
public override IXmlSchemaInfo SchemaInfo {
get {
return name;
}
}
public override String InnerText {
set {
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = base.InnerText;
base.InnerText = value;
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
base.InnerText = value;
}
}
}
internal bool PrepareOwnerElementInElementIdAttrMap() {
XmlDocument ownerDocument = OwnerDocument;
if (ownerDocument.DtdSchemaInfo != null) { // DTD exists
XmlElement ownerElement = OwnerElement;
if (ownerElement != null) {
return ownerElement.Attributes.PrepareParentInElementIdAttrMap(Prefix, LocalName);
}
}
return false;
}
internal void ResetOwnerElementInElementIdAttrMap(string oldInnerText) {
XmlElement ownerElement = OwnerElement;
if (ownerElement != null) {
ownerElement.Attributes.ResetParentInElementIdAttrMap(oldInnerText, InnerText);
}
}
internal override bool IsContainer {
get { return true;}
}
//the function is provided only at Load time to speed up Load process
internal override XmlNode AppendChildForLoad(XmlNode newChild, XmlDocument doc) {
XmlNodeChangedEventArgs args = doc.GetInsertEventArgsForLoad( newChild, this );
if (args != null)
doc.BeforeEvent( args );
XmlLinkedNode newNode = (XmlLinkedNode)newChild;
if (lastChild == null) { // if LastNode == null
newNode.next = newNode;
lastChild = newNode;
newNode.SetParentForLoad(this);
}
else {
XmlLinkedNode refNode = lastChild; // refNode = LastNode;
newNode.next = refNode.next;
refNode.next = newNode;
lastChild = newNode; // LastNode = newNode;
if (refNode.IsText
&& newNode.IsText) {
NestTextNodes(refNode, newNode);
}
else {
newNode.SetParentForLoad(this);
}
}
if (args != null)
doc.AfterEvent( args );
return newNode;
}
internal override XmlLinkedNode LastNode {
get { return lastChild;}
set { lastChild = value;}
}
internal override bool IsValidChildType( XmlNodeType type ) {
return(type == XmlNodeType.Text) || (type == XmlNodeType.EntityReference);
}
// Gets a value indicating whether the value was explicitly set.
public virtual bool Specified {
get { return true;}
}
public override XmlNode InsertBefore(XmlNode newChild, XmlNode refChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.InsertBefore(newChild, refChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.InsertBefore(newChild, refChild);
}
return node;
}
public override XmlNode InsertAfter(XmlNode newChild, XmlNode refChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.InsertAfter(newChild, refChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.InsertAfter(newChild, refChild);
}
return node;
}
public override XmlNode ReplaceChild(XmlNode newChild, XmlNode oldChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.ReplaceChild(newChild, oldChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.ReplaceChild(newChild, oldChild);
}
return node;
}
public override XmlNode RemoveChild(XmlNode oldChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.RemoveChild(oldChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.RemoveChild(oldChild);
}
return node;
}
public override XmlNode PrependChild(XmlNode newChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.PrependChild(newChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.PrependChild(newChild);
}
return node;
}
public override XmlNode AppendChild(XmlNode newChild) {
XmlNode node;
if (PrepareOwnerElementInElementIdAttrMap()) {
string innerText = InnerText;
node = base.AppendChild(newChild);
ResetOwnerElementInElementIdAttrMap(innerText);
}
else {
node = base.AppendChild(newChild);
}
return node;
}
// DOM Level 2
// Gets the XmlElement node that contains this attribute.
public virtual XmlElement OwnerElement {
get {
return parentNode as XmlElement;
}
}
// Gets or sets the markup representing just the children of this node.
public override string InnerXml {
set {
RemoveAll();
XmlLoader loader = new XmlLoader();
loader.LoadInnerXmlAttribute( this, value );
}
}
// Saves the node to the specified XmlWriter.
public override void WriteTo(XmlWriter w) {
w.WriteStartAttribute( Prefix, LocalName, NamespaceURI );
WriteContentTo(w);
w.WriteEndAttribute();
}
// Saves all the children of the node to the specified XmlWriter.
public override void WriteContentTo(XmlWriter w) {
for (XmlNode node = FirstChild; node != null; node = node.NextSibling) {
node.WriteTo(w);
}
}
public override String BaseURI {
get {
if ( OwnerElement != null )
return OwnerElement.BaseURI;
return String.Empty;
}
}
internal override void SetParent(XmlNode node) {
this.parentNode = node;
}
internal override XmlSpace XmlSpace {
get {
if ( OwnerElement != null )
return OwnerElement.XmlSpace;
return XmlSpace.None;
}
}
internal override String XmlLang {
get {
if ( OwnerElement != null )
return OwnerElement.XmlLang;
return String.Empty;
}
}
internal override XPathNodeType XPNodeType {
get {
if (IsNamespace) {
return XPathNodeType.Namespace;
}
return XPathNodeType.Attribute;
}
}
internal override string XPLocalName {
get {
if (name.Prefix.Length == 0 && name.LocalName == "xmlns") return string.Empty;
return name.LocalName;
}
}
internal bool IsNamespace {
get {
return Ref.Equal(name.NamespaceURI, name.OwnerDocument.strReservedXmlns);
}
}
}
}
|