/
XmlNodeReader.cs
1942 lines (1769 loc) · 63.8 KB
/
XmlNodeReader.cs
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.Globalization;
using System.IO;
using System.Text;
using System.Xml.Schema;
namespace System.Xml
{
internal sealed class XmlNodeReaderNavigator
{
private XmlNode _curNode;
private XmlNode? _elemNode;
private XmlNode _logNode;
private int _attrIndex;
private int _logAttrIndex;
//presave these 2 variables since they shouldn't change.
private readonly XmlNameTable _nameTable;
private readonly XmlDocument _doc;
private int _nAttrInd; //used to identify virtual attributes of DocumentType node and XmlDeclaration node
private const string strPublicID = "PUBLIC";
private const string strSystemID = "SYSTEM";
private const string strVersion = "version";
private const string strStandalone = "standalone";
private const string strEncoding = "encoding";
//caching variables for perf reasons
private int _nDeclarationAttrCount;
private int _nDocTypeAttrCount;
//variables for roll back the moves
private int _nLogLevel;
private int _nLogAttrInd;
private bool _bLogOnAttrVal;
private readonly bool _bCreatedOnAttribute;
internal struct VirtualAttribute
{
internal string? name;
internal string? value;
internal VirtualAttribute(string? name, string? value)
{
this.name = name;
this.value = value;
}
};
internal VirtualAttribute[] decNodeAttributes = {
new VirtualAttribute( null, null ),
new VirtualAttribute( null, null ),
new VirtualAttribute( null, null )
};
internal VirtualAttribute[] docTypeNodeAttributes = {
new VirtualAttribute( null, null ),
new VirtualAttribute( null, null )
};
private bool _bOnAttrVal;
public XmlNodeReaderNavigator(XmlNode node)
{
_curNode = node;
_logNode = node;
XmlNodeType nt = _curNode.NodeType;
if (nt == XmlNodeType.Attribute)
{
_elemNode = null;
_attrIndex = -1;
_bCreatedOnAttribute = true;
}
else
{
_elemNode = node;
_attrIndex = -1;
_bCreatedOnAttribute = false;
}
//presave this for pref reason since it shouldn't change.
if (nt == XmlNodeType.Document)
_doc = (XmlDocument)_curNode;
else
_doc = node.OwnerDocument!;
_nameTable = _doc.NameTable;
_nAttrInd = -1;
//initialize the caching variables
_nDeclarationAttrCount = -1;
_nDocTypeAttrCount = -1;
_bOnAttrVal = false;
_bLogOnAttrVal = false;
}
public XmlNodeType NodeType
{
get
{
XmlNodeType nt = _curNode.NodeType;
if (_nAttrInd != -1)
{
Debug.Assert(nt == XmlNodeType.XmlDeclaration || nt == XmlNodeType.DocumentType);
if (_bOnAttrVal)
return XmlNodeType.Text;
else
return XmlNodeType.Attribute;
}
return nt;
}
}
public string NamespaceURI
{
get { return _curNode.NamespaceURI; }
}
public string Name
{
get
{
if (_nAttrInd != -1)
{
Debug.Assert(_curNode.NodeType == XmlNodeType.XmlDeclaration || _curNode.NodeType == XmlNodeType.DocumentType);
if (_bOnAttrVal)
return string.Empty; //Text node's name is String.Empty
else
{
Debug.Assert(_nAttrInd >= 0 && _nAttrInd < AttributeCount);
if (_curNode.NodeType == XmlNodeType.XmlDeclaration)
return decNodeAttributes[_nAttrInd].name!;
else
return docTypeNodeAttributes[_nAttrInd].name!;
}
}
if (IsLocalNameEmpty(_curNode.NodeType))
return string.Empty;
return _curNode.Name;
}
}
public string LocalName
{
get
{
if (_nAttrInd != -1)
//for the nodes in this case, their LocalName should be the same as their name
return Name;
if (IsLocalNameEmpty(_curNode.NodeType))
return string.Empty;
return _curNode.LocalName;
}
}
internal bool CreatedOnAttribute
{
get
{
return _bCreatedOnAttribute;
}
}
private static bool IsLocalNameEmpty(XmlNodeType nt)
{
switch (nt)
{
case XmlNodeType.None:
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Comment:
case XmlNodeType.Document:
case XmlNodeType.DocumentFragment:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
case XmlNodeType.EndElement:
case XmlNodeType.EndEntity:
return true;
case XmlNodeType.Element:
case XmlNodeType.Attribute:
case XmlNodeType.EntityReference:
case XmlNodeType.Entity:
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.DocumentType:
case XmlNodeType.Notation:
case XmlNodeType.XmlDeclaration:
return false;
default:
return true;
}
}
public string Prefix
{
get { return _curNode.Prefix; }
}
public bool HasValue
{
//In DOM, DocumentType node and XmlDeclaration node doesn't value
//In XPathNavigator, XmlDeclaration node's value is its InnerText; DocumentType doesn't have value
//In XmlReader, DocumentType node's value is its InternalSubset which is never null ( at least String.Empty )
get
{
if (_nAttrInd != -1)
{
//Pointing at the one of virtual attributes of Declaration or DocumentType nodes
Debug.Assert(_curNode.NodeType == XmlNodeType.XmlDeclaration || _curNode.NodeType == XmlNodeType.DocumentType);
Debug.Assert(_nAttrInd >= 0 && _nAttrInd < AttributeCount);
return true;
}
if (_curNode.Value != null || _curNode.NodeType == XmlNodeType.DocumentType)
return true;
return false;
}
}
public string Value
{
//See comments in HasValue
get
{
string? retValue;
XmlNodeType nt = _curNode.NodeType;
if (_nAttrInd != -1)
{
//Pointing at the one of virtual attributes of Declaration or DocumentType nodes
Debug.Assert(nt == XmlNodeType.XmlDeclaration || nt == XmlNodeType.DocumentType);
Debug.Assert(_nAttrInd >= 0 && _nAttrInd < AttributeCount);
return _curNode.NodeType == XmlNodeType.XmlDeclaration ?
decNodeAttributes[_nAttrInd].value! :
docTypeNodeAttributes[_nAttrInd].value!;
}
if (nt == XmlNodeType.DocumentType)
{
retValue = ((XmlDocumentType)_curNode).InternalSubset; //in this case nav.Value will be null
}
else if (nt == XmlNodeType.XmlDeclaration)
{
StringBuilder strb = new StringBuilder(string.Empty);
if (_nDeclarationAttrCount == -1)
{
InitDecAttr();
}
for (int i = 0; i < _nDeclarationAttrCount; i++)
{
strb.Append($"{decNodeAttributes[i].name}=\"{decNodeAttributes[i].value}\"");
if (i != (_nDeclarationAttrCount - 1))
{
strb.Append(' ');
}
}
retValue = strb.ToString();
}
else
{
retValue = _curNode.Value;
}
return retValue ?? string.Empty;
}
}
public string BaseURI
{
get { return _curNode.BaseURI; }
}
public XmlSpace XmlSpace
{
get { return _curNode.XmlSpace; }
}
public string XmlLang
{
get { return _curNode.XmlLang; }
}
public bool IsEmptyElement
{
get
{
if (_curNode.NodeType == XmlNodeType.Element)
{
return ((XmlElement)_curNode).IsEmpty;
}
return false;
}
}
public bool IsDefault
{
get
{
if (_curNode.NodeType == XmlNodeType.Attribute)
{
return !((XmlAttribute)_curNode).Specified;
}
return false;
}
}
public IXmlSchemaInfo SchemaInfo
{
get
{
return _curNode.SchemaInfo;
}
}
public XmlNameTable NameTable
{
get { return _nameTable; }
}
public int AttributeCount
{
get
{
if (_bCreatedOnAttribute)
return 0;
XmlNodeType nt = _curNode.NodeType;
if (nt == XmlNodeType.Element)
return ((XmlElement)_curNode).Attributes.Count;
else if (nt == XmlNodeType.Attribute
|| (_bOnAttrVal && nt != XmlNodeType.XmlDeclaration && nt != XmlNodeType.DocumentType))
return _elemNode!.Attributes!.Count;
else if (nt == XmlNodeType.XmlDeclaration)
{
if (_nDeclarationAttrCount != -1)
return _nDeclarationAttrCount;
InitDecAttr();
return _nDeclarationAttrCount;
}
else if (nt == XmlNodeType.DocumentType)
{
if (_nDocTypeAttrCount != -1)
return _nDocTypeAttrCount;
InitDocTypeAttr();
return _nDocTypeAttrCount;
}
return 0;
}
}
private void CheckIndexCondition(int attributeIndex)
{
ArgumentOutOfRangeException.ThrowIfNegative(attributeIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(attributeIndex, AttributeCount);
}
//8 functions below are the helper functions to deal with virtual attributes of XmlDeclaration nodes and DocumentType nodes.
private void InitDecAttr()
{
int i = 0;
string? strTemp = _doc.Version;
if (!string.IsNullOrEmpty(strTemp))
{
decNodeAttributes[i].name = strVersion;
decNodeAttributes[i].value = strTemp;
i++;
}
strTemp = _doc.Encoding;
if (!string.IsNullOrEmpty(strTemp))
{
decNodeAttributes[i].name = strEncoding;
decNodeAttributes[i].value = strTemp;
i++;
}
strTemp = _doc.Standalone;
if (!string.IsNullOrEmpty(strTemp))
{
decNodeAttributes[i].name = strStandalone;
decNodeAttributes[i].value = strTemp;
i++;
}
_nDeclarationAttrCount = i;
}
public static string? GetDeclarationAttr(XmlDeclaration decl, string name)
{
//PreCondition: curNode is pointing at Declaration node or one of its virtual attributes
if (name == strVersion)
return decl.Version;
if (name == strEncoding)
return decl.Encoding;
if (name == strStandalone)
return decl.Standalone;
return null;
}
public string? GetDeclarationAttr(int i)
{
if (_nDeclarationAttrCount == -1)
InitDecAttr();
return decNodeAttributes[i].value;
}
public int GetDecAttrInd(string name)
{
if (_nDeclarationAttrCount == -1)
InitDecAttr();
for (int i = 0; i < _nDeclarationAttrCount; i++)
{
if (decNodeAttributes[i].name == name)
return i;
}
return -1;
}
private void InitDocTypeAttr()
{
int i = 0;
XmlDocumentType? docType = _doc.DocumentType;
if (docType == null)
{
_nDocTypeAttrCount = 0;
return;
}
string? strTemp = docType.PublicId;
if (strTemp != null)
{
docTypeNodeAttributes[i].name = strPublicID;
docTypeNodeAttributes[i].value = strTemp;
i++;
}
strTemp = docType.SystemId;
if (strTemp != null)
{
docTypeNodeAttributes[i].name = strSystemID;
docTypeNodeAttributes[i].value = strTemp;
i++;
}
_nDocTypeAttrCount = i;
}
public static string? GetDocumentTypeAttr(XmlDocumentType docType, string name)
{
//PreCondition: nav is pointing at DocumentType node or one of its virtual attributes
if (name == strPublicID)
return docType.PublicId;
if (name == strSystemID)
return docType.SystemId;
return null;
}
public string? GetDocumentTypeAttr(int i)
{
if (_nDocTypeAttrCount == -1)
InitDocTypeAttr();
return docTypeNodeAttributes[i].value;
}
public int GetDocTypeAttrInd(string name)
{
if (_nDocTypeAttrCount == -1)
InitDocTypeAttr();
for (int i = 0; i < _nDocTypeAttrCount; i++)
{
if (docTypeNodeAttributes[i].name == name)
return i;
}
return -1;
}
private static string? GetAttributeFromElement(XmlElement elem, string name)
{
XmlAttribute? attr = elem.GetAttributeNode(name);
if (attr != null)
return attr.Value;
return null;
}
public string? GetAttribute(string name)
{
if (_bCreatedOnAttribute)
return null;
return _curNode.NodeType switch
{
XmlNodeType.Element => GetAttributeFromElement((XmlElement)_curNode!, name),
XmlNodeType.Attribute => GetAttributeFromElement((XmlElement)_elemNode!, name),
XmlNodeType.XmlDeclaration => GetDeclarationAttr((XmlDeclaration)_curNode, name),
XmlNodeType.DocumentType => GetDocumentTypeAttr((XmlDocumentType)_curNode, name),
_ => null,
};
}
private static string? GetAttributeFromElement(XmlElement elem, string name, string? ns)
{
XmlAttribute? attr = elem.GetAttributeNode(name, ns);
if (attr != null)
return attr.Value;
return null;
}
public string? GetAttribute(string name, string ns)
{
if (_bCreatedOnAttribute)
return null;
return _curNode.NodeType switch
{
XmlNodeType.Element => GetAttributeFromElement((XmlElement)_curNode, name, ns),
XmlNodeType.Attribute => GetAttributeFromElement((XmlElement)_elemNode!, name, ns),
XmlNodeType.XmlDeclaration => (ns.Length == 0) ? GetDeclarationAttr((XmlDeclaration)_curNode, name) : null,
XmlNodeType.DocumentType => (ns.Length == 0) ? GetDocumentTypeAttr((XmlDocumentType)_curNode, name) : null,
_ => null,
};
}
public string? GetAttribute(int attributeIndex)
{
if (_bCreatedOnAttribute)
return null;
switch (_curNode.NodeType)
{
case XmlNodeType.Element:
CheckIndexCondition(attributeIndex);
return ((XmlElement)_curNode).Attributes[attributeIndex].Value;
case XmlNodeType.Attribute:
CheckIndexCondition(attributeIndex);
return ((XmlElement)_elemNode!).Attributes[attributeIndex].Value;
case XmlNodeType.XmlDeclaration:
{
CheckIndexCondition(attributeIndex);
return GetDeclarationAttr(attributeIndex);
}
case XmlNodeType.DocumentType:
{
CheckIndexCondition(attributeIndex);
return GetDocumentTypeAttr(attributeIndex);
}
}
throw new ArgumentOutOfRangeException(nameof(attributeIndex)); //for other senario, AttributeCount is 0, i has to be out of range
}
public void LogMove(int level)
{
_logNode = _curNode;
_nLogLevel = level;
_nLogAttrInd = _nAttrInd;
_logAttrIndex = _attrIndex;
_bLogOnAttrVal = _bOnAttrVal;
}
//The function has to be used in pair with ResetMove when the operation fails after LogMove() is
// called because it relies on the values of nOrigLevel, logNav and nOrigAttrInd to be accurate.
public void RollBackMove(ref int level)
{
_curNode = _logNode;
level = _nLogLevel;
_nAttrInd = _nLogAttrInd;
_attrIndex = _logAttrIndex;
_bOnAttrVal = _bLogOnAttrVal;
}
private bool IsOnDeclOrDocType
{
get
{
XmlNodeType nt = _curNode.NodeType;
return (nt == XmlNodeType.XmlDeclaration || nt == XmlNodeType.DocumentType);
}
}
public void ResetToAttribute(ref int level)
{
//the current cursor is pointing at one of the attribute children -- this could be caused by
// the calls to ReadAttributeValue(..)
if (_bCreatedOnAttribute)
return;
if (_bOnAttrVal)
{
if (IsOnDeclOrDocType)
{
level -= 2;
}
else
{
while (_curNode.NodeType != XmlNodeType.Attribute && ((_curNode = _curNode.ParentNode!) != null))
level--;
}
_bOnAttrVal = false;
}
}
public void ResetMove(ref int level, ref XmlNodeType nt)
{
LogMove(level);
if (_bCreatedOnAttribute)
return;
if (_nAttrInd != -1)
{
Debug.Assert(IsOnDeclOrDocType);
if (_bOnAttrVal)
{
level--;
_bOnAttrVal = false;
}
_nLogAttrInd = _nAttrInd;
level--;
_nAttrInd = -1;
nt = _curNode.NodeType;
return;
}
if (_bOnAttrVal && _curNode.NodeType != XmlNodeType.Attribute)
ResetToAttribute(ref level);
if (_curNode.NodeType == XmlNodeType.Attribute)
{
_curNode = ((XmlAttribute)_curNode).OwnerElement!;
_attrIndex = -1;
level--;
nt = XmlNodeType.Element;
}
if (_curNode.NodeType == XmlNodeType.Element)
_elemNode = _curNode;
}
public bool MoveToAttribute(string name)
{
return MoveToAttribute(name, string.Empty);
}
private bool MoveToAttributeFromElement(XmlElement elem, string name, string ns)
{
XmlAttribute? attr;
if (ns.Length == 0)
attr = elem.GetAttributeNode(name);
else
attr = elem.GetAttributeNode(name, ns);
if (attr != null)
{
_bOnAttrVal = false;
_elemNode = elem;
_curNode = attr;
_attrIndex = elem.Attributes.FindNodeOffsetNS(attr);
if (_attrIndex != -1)
{
return true;
}
}
return false;
}
public bool MoveToAttribute(string name, string namespaceURI)
{
if (_bCreatedOnAttribute)
return false;
XmlNodeType nt = _curNode.NodeType;
if (nt == XmlNodeType.Element)
return MoveToAttributeFromElement((XmlElement)_curNode, name, namespaceURI);
else if (nt == XmlNodeType.Attribute)
return MoveToAttributeFromElement((XmlElement)_elemNode!, name, namespaceURI);
else if (nt == XmlNodeType.XmlDeclaration && namespaceURI.Length == 0)
{
if ((_nAttrInd = GetDecAttrInd(name)) != -1)
{
_bOnAttrVal = false;
return true;
}
}
else if (nt == XmlNodeType.DocumentType && namespaceURI.Length == 0)
{
if ((_nAttrInd = GetDocTypeAttrInd(name)) != -1)
{
_bOnAttrVal = false;
return true;
}
}
return false;
}
public void MoveToAttribute(int attributeIndex)
{
if (_bCreatedOnAttribute)
return;
XmlAttribute? attr;
switch (_curNode.NodeType)
{
case XmlNodeType.Element:
CheckIndexCondition(attributeIndex);
attr = ((XmlElement)_curNode).Attributes[attributeIndex];
if (attr != null)
{
_elemNode = _curNode;
_curNode = (XmlNode)attr;
_attrIndex = attributeIndex;
}
break;
case XmlNodeType.Attribute:
CheckIndexCondition(attributeIndex);
attr = ((XmlElement)_elemNode!).Attributes[attributeIndex];
if (attr != null)
{
_curNode = (XmlNode)attr;
_attrIndex = attributeIndex;
}
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.DocumentType:
CheckIndexCondition(attributeIndex);
_nAttrInd = attributeIndex;
break;
}
}
public bool MoveToNextAttribute(ref int level)
{
if (_bCreatedOnAttribute)
return false;
XmlNodeType nt = _curNode.NodeType;
if (nt == XmlNodeType.Attribute)
{
if (_attrIndex >= (_elemNode!.Attributes!.Count - 1))
return false;
else
{
_curNode = _elemNode.Attributes[++_attrIndex];
return true;
}
}
else if (nt == XmlNodeType.Element)
{
if (_curNode.Attributes!.Count > 0)
{
level++;
_elemNode = _curNode;
_curNode = _curNode.Attributes[0];
_attrIndex = 0;
return true;
}
}
else if (nt == XmlNodeType.XmlDeclaration)
{
if (_nDeclarationAttrCount == -1)
InitDecAttr();
_nAttrInd++;
if (_nAttrInd < _nDeclarationAttrCount)
{
if (_nAttrInd == 0) level++;
_bOnAttrVal = false;
return true;
}
_nAttrInd--;
}
else if (nt == XmlNodeType.DocumentType)
{
if (_nDocTypeAttrCount == -1)
InitDocTypeAttr();
_nAttrInd++;
if (_nAttrInd < _nDocTypeAttrCount)
{
if (_nAttrInd == 0) level++;
_bOnAttrVal = false;
return true;
}
_nAttrInd--;
}
return false;
}
public bool MoveToParent()
{
XmlNode? parent = _curNode.ParentNode;
if (parent != null)
{
_curNode = parent;
if (!_bOnAttrVal)
_attrIndex = 0;
return true;
}
return false;
}
public bool MoveToFirstChild()
{
XmlNode? firstChild = _curNode.FirstChild;
if (firstChild != null)
{
_curNode = firstChild;
if (!_bOnAttrVal)
_attrIndex = -1;
return true;
}
return false;
}
private bool MoveToNextSibling(XmlNode node)
{
XmlNode? nextSibling = node.NextSibling;
if (nextSibling != null)
{
_curNode = nextSibling;
if (!_bOnAttrVal)
_attrIndex = -1;
return true;
}
return false;
}
public bool MoveToNext()
{
if (_curNode.NodeType != XmlNodeType.Attribute)
return MoveToNextSibling(_curNode);
else
return MoveToNextSibling(_elemNode!);
}
public bool MoveToElement()
{
if (_bCreatedOnAttribute)
return false;
switch (_curNode.NodeType)
{
case XmlNodeType.Attribute:
if (_elemNode != null)
{
_curNode = _elemNode;
_attrIndex = -1;
return true;
}
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.DocumentType:
{
if (_nAttrInd != -1)
{
_nAttrInd = -1;
return true;
}
break;
}
}
return false;
}
public string? LookupNamespace(string prefix)
{
if (_bCreatedOnAttribute)
return null;
if (prefix == "xmlns")
{
return _nameTable.Add(XmlReservedNs.NsXmlNs);
}
if (prefix == "xml")
{
return _nameTable.Add(XmlReservedNs.NsXml);
}
// construct the name of the xmlns attribute
prefix ??= string.Empty;
string attrName = prefix.Length == 0 ?
"xmlns" :
$"xmlns:{prefix}";
// walk up the XmlNode parent chain, looking for the xmlns attribute
XmlNode? node = _curNode;
while (node != null)
{
if (node.NodeType == XmlNodeType.Element)
{
XmlElement elem = (XmlElement)node;
if (elem.HasAttributes)
{
XmlAttribute? attr = elem.GetAttributeNode(attrName);
if (attr != null)
{
return attr.Value;
}
}
}
else if (node.NodeType == XmlNodeType.Attribute)
{
node = ((XmlAttribute)node).OwnerElement;
continue;
}
node = node.ParentNode;
}
if (prefix.Length == 0)
{
return string.Empty;
}
return null;
}
internal string? DefaultLookupNamespace(string prefix)
{
if (!_bCreatedOnAttribute)
{
if (prefix == "xmlns")
{
return _nameTable.Add(XmlReservedNs.NsXmlNs);
}
if (prefix == "xml")
{
return _nameTable.Add(XmlReservedNs.NsXml);
}
if (prefix == string.Empty)
{
return _nameTable.Add(string.Empty);
}
}
return null;
}
internal string? LookupPrefix(string namespaceName)
{
if (_bCreatedOnAttribute || namespaceName == null)
{
return null;
}
if (namespaceName == XmlReservedNs.NsXmlNs)
{
return _nameTable.Add("xmlns");
}
if (namespaceName == XmlReservedNs.NsXml)
{
return _nameTable.Add("xml");
}
if (namespaceName.Length == 0)
{
return string.Empty;
}
// walk up the XmlNode parent chain, looking for the xmlns attribute with namespaceName value
XmlNode? node = _curNode;
while (node != null)
{
if (node.NodeType == XmlNodeType.Element)
{
XmlElement elem = (XmlElement)node;
if (elem.HasAttributes)
{
XmlAttributeCollection attrs = elem.Attributes;
for (int i = 0; i < attrs.Count; i++)
{
XmlAttribute a = attrs[i];
if (a.Value == namespaceName)
{
if (a.Prefix.Length == 0 && a.LocalName == "xmlns")
{
if (LookupNamespace(string.Empty) == namespaceName)
{
return string.Empty;
}
}
else if (a.Prefix == "xmlns")
{
string pref = a.LocalName;
if (LookupNamespace(pref) == namespaceName)
{
return _nameTable.Add(pref);
}
}
}
}
}
}
else if (node.NodeType == XmlNodeType.Attribute)
{
node = ((XmlAttribute)node).OwnerElement;
continue;
}
node = node.ParentNode;
}
return null;
}
internal IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
if (_bCreatedOnAttribute)
return dict;
// walk up the XmlNode parent chain and add all namespace declarations to the dictionary
XmlNode? node = _curNode;
while (node != null)
{
if (node.NodeType == XmlNodeType.Element)
{
XmlElement elem = (XmlElement)node;
if (elem.HasAttributes)
{
XmlAttributeCollection attrs = elem.Attributes;