-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathXmlDocument.cs
More file actions
1829 lines (1620 loc) · 66.3 KB
/
Copy pathXmlDocument.cs
File metadata and controls
1829 lines (1620 loc) · 66.3 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Security;
using System.Globalization;
using System.Runtime.Versioning;
namespace System.Xml
{
// Represents an entire document. An XmlDocument contains XML data.
public class XmlDocument : XmlNode
{
private const string DocumentName = "#document";
private const string DocumentFragmentName = "#document-fragment";
private const string CommentName = "#comment";
private const string TextName = "#text";
private const string CDataSectionName = "#cdata-section";
private const string EntityName = "#entity";
private const string ID = "id";
private const string Xmlns = "xmlns";
private const string Xml = "xml";
private const string Space = "space";
private const string Lang = "lang";
private const string NonSignificantWhitespaceName = "#whitespace";
private const string SignificantWhitespaceName = "#significant-whitespace";
// The seed index below requires that the constant strings above and the seed array below are
// kept in the same order.
private const int DocumentNameSeedIndex = 0;
private const int DocumentFragmentNameSeedIndex = 1;
private const int CommentNameSeedIndex = 2;
private const int TextNameSeedIndex = 3;
private const int CDataSectionNameSeedIndex = 4;
private const int EntityNameSeedIndex = 5;
private const int IDSeedIndex = 6;
private const int XmlnsSeedIndex = 7;
private const int XmlSeedIndex = 8;
private const int SpaceSeedIndex = 9;
private const int LangSeedIndex = 10;
private const int NonSignificantWhitespaceNameSeedIndex = 11;
private const int SignificantWhitespaceNameSeedIndex = 12;
private const int NsXmlNsSeedIndex = 13;
private const int NsXmlSeedIndex = 14;
// If changing the array below ensure that the seed indexes before match
private static readonly (string key, int hash)[] s_nameTableSeeds = new[]
{
(DocumentName, System.Xml.NameTable.ComputeHash32(DocumentName)),
(DocumentFragmentName, System.Xml.NameTable.ComputeHash32(DocumentFragmentName)),
(CommentName, System.Xml.NameTable.ComputeHash32(CommentName)),
(TextName, System.Xml.NameTable.ComputeHash32(TextName)),
(CDataSectionName, System.Xml.NameTable.ComputeHash32(CDataSectionName)),
(EntityName, System.Xml.NameTable.ComputeHash32(EntityName)),
(ID, System.Xml.NameTable.ComputeHash32(ID)),
(Xmlns, System.Xml.NameTable.ComputeHash32(Xmlns)),
(Xml, System.Xml.NameTable.ComputeHash32(Xml)),
(Space, System.Xml.NameTable.ComputeHash32(Space)),
(Lang, System.Xml.NameTable.ComputeHash32(Lang)),
(NonSignificantWhitespaceName, System.Xml.NameTable.ComputeHash32(NonSignificantWhitespaceName)),
(SignificantWhitespaceName, System.Xml.NameTable.ComputeHash32(SignificantWhitespaceName)),
(XmlReservedNs.NsXmlNs, System.Xml.NameTable.ComputeHash32(XmlReservedNs.NsXmlNs)),
(XmlReservedNs.NsXml, System.Xml.NameTable.ComputeHash32(XmlReservedNs.NsXml))
};
private readonly XmlImplementation _implementation;
private readonly DomNameTable _domNameTable; // hash table of XmlName
private XmlLinkedNode? _lastChild;
private XmlNamedNodeMap? _entities;
private Hashtable? _htElementIdMap;
private Hashtable? _htElementIDAttrDecl; //key: id; object: the ArrayList of the elements that have the same id (connected or disconnected)
private SchemaInfo? _schemaInfo;
private XmlSchemaSet? _schemas; // schemas associated with the cache
private bool _reportValidity;
//This variable represents the actual loading status. Since, IsLoading will
//be manipulated sometimes for adding content to EntityReference this variable
//has been added which would always represent the loading status of document.
private bool _actualLoadingStatus;
private XmlNodeChangedEventHandler? _onNodeInsertingDelegate;
private XmlNodeChangedEventHandler? _onNodeInsertedDelegate;
private XmlNodeChangedEventHandler? _onNodeRemovingDelegate;
private XmlNodeChangedEventHandler? _onNodeRemovedDelegate;
private XmlNodeChangedEventHandler? _onNodeChangingDelegate;
private XmlNodeChangedEventHandler? _onNodeChangedDelegate;
// false if there are no ent-ref present, true if ent-ref nodes are or were present (i.e. if all ent-ref were removed, the doc will not clear this flag)
internal bool fEntRefNodesPresent;
internal bool fCDataNodesPresent;
private bool _preserveWhitespace;
private bool _isLoading;
// special name strings for
internal string strDocumentName;
internal string strDocumentFragmentName;
internal string strCommentName;
internal string strTextName;
internal string strCDataSectionName;
internal string strEntityName;
internal string strID;
internal string strXmlns;
internal string strXml;
internal string strSpace;
internal string strLang;
internal string strNonSignificantWhitespaceName;
internal string strSignificantWhitespaceName;
internal string strReservedXmlns;
internal string strReservedXml;
internal string baseURI;
private XmlResolver? _resolver;
internal bool bSetResolver;
internal object objLock;
private XmlAttribute? _namespaceXml;
internal static EmptyEnumerator EmptyEnumerator = new EmptyEnumerator();
internal static IXmlSchemaInfo NotKnownSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.NotKnown);
internal static IXmlSchemaInfo ValidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Valid);
internal static IXmlSchemaInfo InvalidSchemaInfo = new XmlSchemaInfo(XmlSchemaValidity.Invalid);
// Initializes a new instance of the XmlDocument class.
public XmlDocument() : this(new XmlImplementation())
{
}
// Initializes a new instance
// of the XmlDocument class with the specified XmlNameTable.
public XmlDocument(XmlNameTable nt) : this(new XmlImplementation(nt))
{
}
protected internal XmlDocument(XmlImplementation imp) : base()
{
_implementation = imp;
_domNameTable = new DomNameTable(this);
strXmlns = Xmlns;
strXml = Xml;
strReservedXmlns = XmlReservedNs.NsXmlNs;
strReservedXml = XmlReservedNs.NsXml;
baseURI = string.Empty;
objLock = new object();
if (imp.NameTable.GetType() == typeof(NameTable))
{
// When the name table being used is of type NameTable avoid re-calculating the hash codes.
NameTable nt = (NameTable)imp.NameTable;
strDocumentName = nt.GetOrAddEntry(s_nameTableSeeds[DocumentNameSeedIndex].key, s_nameTableSeeds[DocumentNameSeedIndex].hash);
strDocumentFragmentName = nt.GetOrAddEntry(s_nameTableSeeds[DocumentFragmentNameSeedIndex].key, s_nameTableSeeds[DocumentFragmentNameSeedIndex].hash);
strCommentName = nt.GetOrAddEntry(s_nameTableSeeds[CommentNameSeedIndex].key, s_nameTableSeeds[CommentNameSeedIndex].hash);
strTextName = nt.GetOrAddEntry(s_nameTableSeeds[TextNameSeedIndex].key, s_nameTableSeeds[TextNameSeedIndex].hash);
strCDataSectionName = nt.GetOrAddEntry(s_nameTableSeeds[CDataSectionNameSeedIndex].key, s_nameTableSeeds[CDataSectionNameSeedIndex].hash);
strEntityName = nt.GetOrAddEntry(s_nameTableSeeds[EntityNameSeedIndex].key, s_nameTableSeeds[EntityNameSeedIndex].hash);
strID = nt.GetOrAddEntry(s_nameTableSeeds[IDSeedIndex].key, s_nameTableSeeds[IDSeedIndex].hash);
strNonSignificantWhitespaceName = nt.GetOrAddEntry(s_nameTableSeeds[NonSignificantWhitespaceNameSeedIndex].key, s_nameTableSeeds[NonSignificantWhitespaceNameSeedIndex].hash);
strSignificantWhitespaceName = nt.GetOrAddEntry(s_nameTableSeeds[SignificantWhitespaceNameSeedIndex].key, s_nameTableSeeds[SignificantWhitespaceNameSeedIndex].hash);
strXmlns = nt.GetOrAddEntry(s_nameTableSeeds[XmlnsSeedIndex].key, s_nameTableSeeds[XmlnsSeedIndex].hash);
strXml = nt.GetOrAddEntry(s_nameTableSeeds[XmlSeedIndex].key, s_nameTableSeeds[XmlSeedIndex].hash);
strSpace = nt.GetOrAddEntry(s_nameTableSeeds[SpaceSeedIndex].key, s_nameTableSeeds[SpaceSeedIndex].hash);
strLang = nt.GetOrAddEntry(s_nameTableSeeds[LangSeedIndex].key, s_nameTableSeeds[LangSeedIndex].hash);
strReservedXmlns = nt.GetOrAddEntry(s_nameTableSeeds[NsXmlNsSeedIndex].key, s_nameTableSeeds[NsXmlNsSeedIndex].hash);
strReservedXml = nt.GetOrAddEntry(s_nameTableSeeds[NsXmlSeedIndex].key, s_nameTableSeeds[NsXmlSeedIndex].hash);
}
else
{
XmlNameTable customNameTable = imp.NameTable;
strDocumentName = customNameTable.Add(DocumentName);
strDocumentFragmentName = customNameTable.Add(DocumentFragmentName);
strCommentName = customNameTable.Add(CommentName);
strTextName = customNameTable.Add(TextName);
strCDataSectionName = customNameTable.Add(CDataSectionName);
strEntityName = customNameTable.Add(EntityName);
strID = customNameTable.Add(ID);
strNonSignificantWhitespaceName = customNameTable.Add(NonSignificantWhitespaceName);
strSignificantWhitespaceName = customNameTable.Add(SignificantWhitespaceName);
strXmlns = customNameTable.Add(Xmlns);
strXml = customNameTable.Add(Xml);
strSpace = customNameTable.Add(Space);
strLang = customNameTable.Add(Lang);
strReservedXmlns = customNameTable.Add(XmlReservedNs.NsXmlNs);
strReservedXml = customNameTable.Add(XmlReservedNs.NsXml);
}
}
internal SchemaInfo? DtdSchemaInfo
{
get { return _schemaInfo; }
set { _schemaInfo = value; }
}
// NOTE: This does not correctly check start name char, but we cannot change it since it would be a breaking change.
internal static void CheckName(string name)
{
int endPos = ValidateNames.ParseNmtoken(name, 0);
if (endPos < name.Length)
{
throw new XmlException(SR.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos));
}
}
internal XmlName AddXmlName(string? prefix, string localName, string? namespaceURI, IXmlSchemaInfo? schemaInfo)
{
XmlName n = _domNameTable.AddName(prefix, localName, namespaceURI, schemaInfo);
Debug.Assert((prefix == null) ? (n.Prefix.Length == 0) : (prefix == n.Prefix));
Debug.Assert(n.LocalName == localName);
Debug.Assert((namespaceURI == null) ? (n.NamespaceURI.Length == 0) : (n.NamespaceURI == namespaceURI));
return n;
}
internal XmlName? GetXmlName(string? prefix, string localName, string? namespaceURI, IXmlSchemaInfo? schemaInfo)
{
XmlName? n = _domNameTable.GetName(prefix, localName, namespaceURI, schemaInfo);
Debug.Assert(n == null || ((prefix == null) ? (n.Prefix.Length == 0) : (prefix == n.Prefix)));
Debug.Assert(n == null || n.LocalName == localName);
Debug.Assert(n == null || ((namespaceURI == null) ? (n.NamespaceURI.Length == 0) : (n.NamespaceURI == namespaceURI)));
return n;
}
internal XmlName AddAttrXmlName(string? prefix, string localName, string? namespaceURI, IXmlSchemaInfo? schemaInfo)
{
XmlName xmlName = AddXmlName(prefix, localName, namespaceURI, schemaInfo);
Debug.Assert((prefix == null) ? (xmlName.Prefix.Length == 0) : (prefix == xmlName.Prefix));
Debug.Assert(xmlName.LocalName == localName);
Debug.Assert((namespaceURI == null) ? (xmlName.NamespaceURI.Length == 0) : (xmlName.NamespaceURI == namespaceURI));
if (!this.IsLoading)
{
// Use atomized versions instead of prefix, localName and nsURI
object oPrefix = xmlName.Prefix;
object oNamespaceURI = xmlName.NamespaceURI;
object oLocalName = xmlName.LocalName;
if ((oPrefix == (object)strXmlns || (xmlName.Prefix.Length == 0 && oLocalName == (object)strXmlns)) ^ (oNamespaceURI == (object)strReservedXmlns))
throw new ArgumentException(SR.Format(SR.Xdom_Attr_Reserved_XmlNS, namespaceURI));
}
return xmlName;
}
internal bool AddIdInfo(XmlName eleName, XmlName attrName)
{
//when XmlLoader call XmlDocument.AddInfo, the element.XmlName and attr.XmlName
//have already been replaced with the ones that don't have namespace values (or just
//string.Empty) because in DTD, the namespace is not supported
if (_htElementIDAttrDecl == null || _htElementIDAttrDecl[eleName] == null)
{
_htElementIDAttrDecl ??= new Hashtable();
_htElementIDAttrDecl.Add(eleName, attrName);
return true;
}
return false;
}
private XmlName? GetIDInfoByElement_(XmlName eleName)
{
//When XmlDocument is getting the IDAttribute for a given element,
//we need only compare the prefix and localname of element.XmlName with
//the registered htElementIDAttrDecl.
XmlName? newName = GetXmlName(eleName.Prefix, eleName.LocalName, string.Empty, null);
if (newName != null)
{
return (XmlName?)(_htElementIDAttrDecl![newName]);
}
return null;
}
internal XmlName? GetIDInfoByElement(XmlName eleName)
{
if (_htElementIDAttrDecl == null)
return null;
else
return GetIDInfoByElement_(eleName);
}
private static WeakReference<XmlElement>? GetElement(ArrayList elementList, XmlElement elem)
{
ArrayList gcElemRefs = new ArrayList();
foreach (WeakReference<XmlElement> elemRef in elementList)
{
if (!elemRef.TryGetTarget(out XmlElement? target))
{
//take notes on the garbage collected nodes
gcElemRefs.Add(elemRef);
}
else
{
if (target == elem)
return elemRef;
}
}
//Clear out the gced elements
foreach (WeakReference<XmlElement> elemRef in gcElemRefs)
elementList.Remove(elemRef);
return null;
}
internal void AddElementWithId(string id, XmlElement elem)
{
if (_htElementIdMap == null || !_htElementIdMap.Contains(id))
{
_htElementIdMap ??= new Hashtable();
ArrayList elementList = new ArrayList();
elementList.Add(new WeakReference<XmlElement>(elem));
_htElementIdMap.Add(id, elementList);
}
else
{
// there are other element(s) that has the same id
ArrayList elementList = (ArrayList)(_htElementIdMap[id]!);
if (GetElement(elementList, elem) == null)
elementList.Add(new WeakReference<XmlElement>(elem));
}
}
internal void RemoveElementWithId(string id, XmlElement elem)
{
if (_htElementIdMap != null && _htElementIdMap.Contains(id))
{
ArrayList elementList = (ArrayList)(_htElementIdMap[id]!);
WeakReference<XmlElement>? elemRef = GetElement(elementList, elem);
if (elemRef != null)
{
elementList.Remove(elemRef);
if (elementList.Count == 0)
_htElementIdMap.Remove(id);
}
}
}
// Creates a duplicate of this node.
public override XmlNode CloneNode(bool deep)
{
XmlDocument clone = Implementation.CreateDocument();
clone.SetBaseURI(this.baseURI);
if (deep)
clone.ImportChildren(this, clone, deep);
return clone;
}
// Gets the type of the current node.
public override XmlNodeType NodeType
{
get { return XmlNodeType.Document; }
}
public override XmlNode? ParentNode
{
get { return null; }
}
// Gets the node for the DOCTYPE declaration.
public virtual XmlDocumentType? DocumentType
{
get { return (XmlDocumentType?)FindChild(XmlNodeType.DocumentType); }
}
internal virtual XmlDeclaration? Declaration
{
get
{
if (HasChildNodes)
{
XmlDeclaration? dec = FirstChild as XmlDeclaration;
return dec;
}
return null;
}
}
// Gets the XmlImplementation object for this document.
public XmlImplementation Implementation
{
get { return _implementation; }
}
// Gets the name of the node.
public override string Name
{
get { return strDocumentName; }
}
// Gets the name of the current node without the namespace prefix.
public override string LocalName
{
get { return strDocumentName; }
}
// Gets the root XmlElement for the document.
public XmlElement? DocumentElement
{
get { return (XmlElement?)FindChild(XmlNodeType.Element); }
}
internal override bool IsContainer
{
get { return true; }
}
internal override XmlLinkedNode? LastNode
{
get { return _lastChild; }
set { _lastChild = value; }
}
// Gets the XmlDocument that contains this node.
public override XmlDocument? OwnerDocument
{
get { return null; }
}
public XmlSchemaSet Schemas
{
get => _schemas ??= new XmlSchemaSet(NameTable);
set => _schemas = value;
}
internal bool CanReportValidity
{
get { return _reportValidity; }
}
internal bool HasSetResolver
{
get { return bSetResolver; }
}
internal XmlResolver? GetResolver()
{
return _resolver;
}
public virtual XmlResolver? XmlResolver
{
set
{
_resolver = value;
if (!bSetResolver)
bSetResolver = true;
XmlDocumentType? dtd = this.DocumentType;
if (dtd != null)
{
dtd.DtdSchemaInfo = null;
}
}
}
internal override bool IsValidChildType(XmlNodeType type)
{
switch (type)
{
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return true;
case XmlNodeType.DocumentType:
if (DocumentType != null)
throw new InvalidOperationException(SR.Xdom_DualDocumentTypeNode);
return true;
case XmlNodeType.Element:
if (DocumentElement != null)
throw new InvalidOperationException(SR.Xdom_DualDocumentElementNode);
return true;
case XmlNodeType.XmlDeclaration:
if (Declaration != null)
throw new InvalidOperationException(SR.Xdom_DualDeclarationNode);
return true;
default:
return false;
}
}
// the function examines all the siblings before the refNode
// if any of the nodes has type equals to "nt", return true; otherwise, return false;
private static bool HasNodeTypeInPrevSiblings(XmlNodeType nt, XmlNode? refNode)
{
if (refNode == null)
return false;
XmlNode? node = null;
if (refNode.ParentNode != null)
node = refNode.ParentNode.FirstChild;
while (node != null)
{
if (node.NodeType == nt)
return true;
if (node == refNode)
break;
node = node.NextSibling;
}
return false;
}
// the function examines all the siblings after the refNode
// if any of the nodes has the type equals to "nt", return true; otherwise, return false;
private static bool HasNodeTypeInNextSiblings(XmlNodeType nt, XmlNode? refNode)
{
XmlNode? node = refNode;
while (node != null)
{
if (node.NodeType == nt)
return true;
node = node.NextSibling;
}
return false;
}
internal override bool CanInsertBefore(XmlNode newChild, XmlNode? refChild)
{
refChild ??= FirstChild;
if (refChild == null)
return true;
switch (newChild.NodeType)
{
case XmlNodeType.XmlDeclaration:
return (refChild == FirstChild);
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
return refChild.NodeType != XmlNodeType.XmlDeclaration;
case XmlNodeType.DocumentType:
{
if (refChild.NodeType != XmlNodeType.XmlDeclaration)
{
//if refChild is not the XmlDeclaration node, only need to go through the sibling before and including refChild to
// make sure no Element ( rootElem node ) before the current position
return !HasNodeTypeInPrevSiblings(XmlNodeType.Element, refChild.PreviousSibling);
}
}
break;
case XmlNodeType.Element:
{
if (refChild.NodeType != XmlNodeType.XmlDeclaration)
{
//if refChild is not the XmlDeclaration node, only need to go through the siblings after and including the refChild to
// make sure no DocType node and XmlDeclaration node after the current position.
return !HasNodeTypeInNextSiblings(XmlNodeType.DocumentType, refChild);
}
}
break;
}
return false;
}
internal override bool CanInsertAfter(XmlNode newChild, XmlNode? refChild)
{
refChild ??= LastChild;
if (refChild == null)
return true;
switch (newChild.NodeType)
{
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.Comment:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return true;
case XmlNodeType.DocumentType:
{
//we will have to go through all the siblings before the refChild just to make sure no Element node ( rootElem )
// before the current position
return !HasNodeTypeInPrevSiblings(XmlNodeType.Element, refChild);
}
case XmlNodeType.Element:
{
return !HasNodeTypeInNextSiblings(XmlNodeType.DocumentType, refChild.NextSibling);
}
}
return false;
}
// Creates an XmlAttribute with the specified name.
public XmlAttribute CreateAttribute(string name)
{
string prefix;
string localName;
string namespaceURI = string.Empty;
SplitName(name, out prefix, out localName);
SetDefaultNamespace(prefix, localName, ref namespaceURI);
return CreateAttribute(prefix, localName, namespaceURI);
}
internal void SetDefaultNamespace(string prefix, string localName, ref string namespaceURI)
{
if (prefix == strXmlns || (prefix.Length == 0 && localName == strXmlns))
{
namespaceURI = strReservedXmlns;
}
else if (prefix == strXml)
{
namespaceURI = strReservedXml;
}
}
// Creates a XmlCDataSection containing the specified data.
public virtual XmlCDataSection CreateCDataSection(string? data)
{
fCDataNodesPresent = true;
return new XmlCDataSection(data, this);
}
// Creates an XmlComment containing the specified data.
public virtual XmlComment CreateComment(string? data)
{
return new XmlComment(data, this);
}
// Returns a new XmlDocumentType object.
public virtual XmlDocumentType CreateDocumentType(string name, string? publicId, string? systemId, string? internalSubset)
{
return new XmlDocumentType(name, publicId, systemId, internalSubset, this);
}
// Creates an XmlDocumentFragment.
public virtual XmlDocumentFragment CreateDocumentFragment()
{
return new XmlDocumentFragment(this);
}
// Creates an element with the specified name.
public XmlElement CreateElement(string name)
{
string prefix;
string localName;
SplitName(name, out prefix, out localName);
return CreateElement(prefix, localName, string.Empty);
}
internal void AddDefaultAttributes(XmlElement elem)
{
SchemaInfo? schInfo = DtdSchemaInfo;
SchemaElementDecl? ed = GetSchemaElementDecl(elem);
if (ed != null && ed.AttDefs != null)
{
foreach (KeyValuePair<XmlQualifiedName, SchemaAttDef> attrDefs in ed.AttDefs)
{
SchemaAttDef attdef = attrDefs.Value;
if (attdef.Presence == SchemaDeclBase.Use.Default ||
attdef.Presence == SchemaDeclBase.Use.Fixed)
{
//build a default attribute and return
string attrPrefix;
string attrLocalname = attdef.Name.Name;
string attrNamespaceURI = string.Empty;
if (schInfo!.SchemaType == SchemaType.DTD)
{
attrPrefix = attdef.Name.Namespace;
}
else
{
attrPrefix = attdef.Prefix;
attrNamespaceURI = attdef.Name.Namespace;
}
XmlAttribute defattr = PrepareDefaultAttribute(attdef, attrPrefix, attrLocalname, attrNamespaceURI);
elem.SetAttributeNode(defattr);
}
}
}
}
private SchemaElementDecl? GetSchemaElementDecl(XmlElement elem)
{
SchemaInfo? schInfo = DtdSchemaInfo;
if (schInfo != null)
{
//build XmlQualifiedName used to identify the element schema declaration
XmlQualifiedName qname = new XmlQualifiedName(elem.LocalName, schInfo.SchemaType == SchemaType.DTD ? elem.Prefix : elem.NamespaceURI);
//get the schema info for the element
SchemaElementDecl? elemDecl;
if (schInfo.ElementDecls.TryGetValue(qname, out elemDecl))
{
return elemDecl;
}
}
return null;
}
//Will be used by AddDeafulatAttributes() and GetDefaultAttribute() methods
private XmlAttribute PrepareDefaultAttribute(SchemaAttDef attdef, string attrPrefix, string attrLocalname, string attrNamespaceURI)
{
SetDefaultNamespace(attrPrefix, attrLocalname, ref attrNamespaceURI);
XmlAttribute defattr = CreateDefaultAttribute(attrPrefix, attrLocalname, attrNamespaceURI);
//parsing the default value for the default attribute
defattr.InnerXml = attdef.DefaultValueRaw;
//during the expansion of the tree, the flag could be set to true, we need to set it back.
XmlUnspecifiedAttribute? unspAttr = defattr as XmlUnspecifiedAttribute;
unspAttr?.SetSpecified(false);
return defattr;
}
// Creates an XmlEntityReference with the specified name.
public virtual XmlEntityReference CreateEntityReference(string name)
{
return new XmlEntityReference(name, this);
}
// Creates a XmlProcessingInstruction with the specified name
// and data strings.
public virtual XmlProcessingInstruction CreateProcessingInstruction(string target, string? data)
{
ArgumentNullException.ThrowIfNull(target);
return new XmlProcessingInstruction(target, data, this);
}
// Creates a XmlDeclaration node with the specified values.
public virtual XmlDeclaration CreateXmlDeclaration(string version, string? encoding, string? standalone)
{
return new XmlDeclaration(version, encoding, standalone, this);
}
// Creates an XmlText with the specified text.
public virtual XmlText CreateTextNode(string? text)
{
return new XmlText(text, this);
}
// Creates a XmlSignificantWhitespace node.
public virtual XmlSignificantWhitespace CreateSignificantWhitespace(string? text)
{
return new XmlSignificantWhitespace(text, this);
}
public override XPathNavigator? CreateNavigator()
{
return CreateNavigator(this);
}
protected internal virtual XPathNavigator? CreateNavigator(XmlNode node)
{
XmlNodeType nodeType = node.NodeType;
XmlNode? parent;
XmlNodeType parentType;
switch (nodeType)
{
case XmlNodeType.EntityReference:
case XmlNodeType.Entity:
case XmlNodeType.DocumentType:
case XmlNodeType.Notation:
case XmlNodeType.XmlDeclaration:
return null;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.SignificantWhitespace:
parent = node.ParentNode;
if (parent != null)
{
do
{
parentType = parent.NodeType;
if (parentType == XmlNodeType.Attribute)
{
return null;
}
else if (parentType == XmlNodeType.EntityReference)
{
parent = parent.ParentNode;
}
else
{
break;
}
}
while (parent != null);
}
node = NormalizeText(node)!;
break;
case XmlNodeType.Whitespace:
parent = node.ParentNode;
if (parent != null)
{
do
{
parentType = parent.NodeType;
if (parentType == XmlNodeType.Document
|| parentType == XmlNodeType.Attribute)
{
return null;
}
else if (parentType == XmlNodeType.EntityReference)
{
parent = parent.ParentNode;
}
else
{
break;
}
}
while (parent != null);
}
node = NormalizeText(node)!;
break;
default:
break;
}
return new DocumentXPathNavigator(this, node);
}
internal static bool IsTextNode(XmlNodeType nt)
{
switch (nt)
{
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return true;
default:
return false;
}
}
private static XmlNode? NormalizeText(XmlNode node)
{
XmlNode? retnode = null;
XmlNode? n = node;
while (IsTextNode(n.NodeType))
{
retnode = n;
n = n.PreviousSibling;
if (n == null)
{
XmlNode intnode = retnode;
while (true)
{
if (intnode.ParentNode != null && intnode.ParentNode.NodeType == XmlNodeType.EntityReference)
{
if (intnode.ParentNode.PreviousSibling != null)
{
n = intnode.ParentNode.PreviousSibling;
break;
}
else
{
intnode = intnode.ParentNode;
if (intnode == null)
break;
}
}
else
break;
}
}
if (n == null)
break;
while (n.NodeType == XmlNodeType.EntityReference)
{
n = n.LastChild!;
}
}
return retnode;
}
// Creates a XmlWhitespace node.
public virtual XmlWhitespace CreateWhitespace(string? text)
{
return new XmlWhitespace(text, this);
}
// Returns an XmlNodeList containing
// a list of all descendant elements that match the specified name.
public virtual XmlNodeList GetElementsByTagName(string name)
{
return new XmlElementList(this, name);
}
// DOM Level 2
// Creates an XmlAttribute with the specified LocalName
// and NamespaceURI.
public XmlAttribute CreateAttribute(string qualifiedName, string? namespaceURI)
{
string prefix;
string localName;
SplitName(qualifiedName, out prefix, out localName);
return CreateAttribute(prefix, localName, namespaceURI);
}
// Creates an XmlElement with the specified LocalName and
// NamespaceURI.
public XmlElement CreateElement(string qualifiedName, string? namespaceURI)
{
string prefix;
string localName;
SplitName(qualifiedName, out prefix, out localName);
return CreateElement(prefix, localName, namespaceURI);
}
// Returns a XmlNodeList containing
// a list of all descendant elements that match the specified name.
public virtual XmlNodeList GetElementsByTagName(string localName, string namespaceURI)
{
return new XmlElementList(this, localName, namespaceURI);
}
// Returns the XmlElement with the specified ID.
public virtual XmlElement? GetElementById(string elementId)
{
if (_htElementIdMap != null)
{
ArrayList? elementList = (ArrayList?)(_htElementIdMap[elementId]);
if (elementList != null)
{
foreach (WeakReference<XmlElement> elemRef in elementList)
{
if (elemRef.TryGetTarget(out XmlElement? elem) && elem.IsConnected())
return elem;
}
}
}
return null;
}
// Imports a node from another document to this document.
public virtual XmlNode ImportNode(XmlNode node, bool deep)
{
return ImportNodeInternal(node, deep);
}
private XmlNode ImportNodeInternal(XmlNode node, bool deep)
{
if (node == null)
{
throw new InvalidOperationException(SR.Xdom_Import_NullNode);
}
else
{
XmlNode newNode;
switch (node.NodeType)
{
case XmlNodeType.Element:
newNode = CreateElement(node.Prefix, node.LocalName, node.NamespaceURI);
ImportAttributes(node, newNode);
if (deep)
ImportChildren(node, newNode, deep);
break;
case XmlNodeType.Attribute:
Debug.Assert(((XmlAttribute)node).Specified);
newNode = CreateAttribute(node.Prefix, node.LocalName, node.NamespaceURI);
ImportChildren(node, newNode, true);
break;
case XmlNodeType.Text:
newNode = CreateTextNode(node.Value);
break;
case XmlNodeType.Comment:
newNode = CreateComment(node.Value);
break;
case XmlNodeType.ProcessingInstruction:
newNode = CreateProcessingInstruction(node.Name, node.Value!);
break;
case XmlNodeType.XmlDeclaration:
XmlDeclaration decl = (XmlDeclaration)node;
newNode = CreateXmlDeclaration(decl.Version, decl.Encoding, decl.Standalone);
break;
case XmlNodeType.CDATA:
newNode = CreateCDataSection(node.Value);
break;
case XmlNodeType.DocumentType:
XmlDocumentType docType = (XmlDocumentType)node;
newNode = CreateDocumentType(docType.Name, docType.PublicId, docType.SystemId, docType.InternalSubset);