/
XmlReader.cs
1812 lines (1570 loc) · 69.3 KB
/
XmlReader.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.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml.Schema;
namespace System.Xml
{
// Represents a reader that provides fast, non-cached forward only stream access to XML data.
[DebuggerDisplay($"{{{nameof(DebuggerDisplayProxy)}}}")]
public abstract partial class XmlReader : IDisposable
{
private const uint IsTextualNodeBitmap = 0x6018; // 00 0110 0000 0001 1000
// 0 None,
// 0 Element,
// 0 Attribute,
// 1 Text,
// 1 CDATA,
// 0 EntityReference,
// 0 Entity,
// 0 ProcessingInstruction,
// 0 Comment,
// 0 Document,
// 0 DocumentType,
// 0 DocumentFragment,
// 0 Notation,
// 1 Whitespace,
// 1 SignificantWhitespace,
// 0 EndElement,
// 0 EndEntity,
// 0 XmlDeclaration
private const uint CanReadContentAsBitmap = 0x1E1BC; // 01 1110 0001 1011 1100
// 0 None,
// 0 Element,
// 1 Attribute,
// 1 Text,
// 1 CDATA,
// 1 EntityReference,
// 0 Entity,
// 1 ProcessingInstruction,
// 1 Comment,
// 0 Document,
// 0 DocumentType,
// 0 DocumentFragment,
// 0 Notation,
// 1 Whitespace,
// 1 SignificantWhitespace,
// 1 EndElement,
// 1 EndEntity,
// 0 XmlDeclaration
private const uint HasValueBitmap = 0x2659C; // 10 0110 0101 1001 1100
// 0 None,
// 0 Element,
// 1 Attribute,
// 1 Text,
// 1 CDATA,
// 0 EntityReference,
// 0 Entity,
// 1 ProcessingInstruction,
// 1 Comment,
// 0 Document,
// 1 DocumentType,
// 0 DocumentFragment,
// 0 Notation,
// 1 Whitespace,
// 1 SignificantWhitespace,
// 0 EndElement,
// 0 EndEntity,
// 1 XmlDeclaration
//
// Constants
//
internal const int DefaultBufferSize = 4096;
internal const int BiggerBufferSize = 8192;
internal const int MaxStreamLengthForDefaultBufferSize = 64 * 1024; // 64kB
// Chosen to be small enough that the character buffer in XmlTextReader when using Async = true
// is not allocated on the Large Object Heap (LOH)
internal const int AsyncBufferSize = 32 * 1024;
// Settings
public virtual XmlReaderSettings? Settings => null;
// Node Properties
// Get the type of the current node.
public abstract XmlNodeType NodeType { get; }
// Gets the name of the current node, including the namespace prefix.
public virtual string Name => Prefix.Length == 0 ? LocalName : NameTable.Add($"{Prefix}:{LocalName}");
// Gets the name of the current node without the namespace prefix.
public abstract string LocalName { get; }
// Gets the namespace URN (as defined in the W3C Namespace Specification) of the current namespace scope.
public abstract string NamespaceURI { get; }
// Gets the namespace prefix associated with the current node.
public abstract string Prefix { get; }
// Gets a value indicating whether
public virtual bool HasValue => HasValueInternal(NodeType);
// Gets the text value of the current node.
public abstract string Value { get; }
// Gets the depth of the current node in the XML element stack.
public abstract int Depth { get; }
// Gets the base URI of the current node.
public abstract string BaseURI { get; }
// Gets a value indicating whether the current node is an empty element (for example, <MyElement/>).
public abstract bool IsEmptyElement { get; }
// Gets a value indicating whether the current node is an attribute that was generated from the default value defined
// in the DTD or schema.
public virtual bool IsDefault => false;
// Gets the quotation mark character used to enclose the value of an attribute node.
public virtual char QuoteChar => '"';
// Gets the current xml:space scope.
public virtual XmlSpace XmlSpace => XmlSpace.None;
// Gets the current xml:lang scope.
public virtual string XmlLang => string.Empty;
// returns the schema info interface of the reader
public virtual IXmlSchemaInfo? SchemaInfo => this as IXmlSchemaInfo;
// returns the type of the current node
public virtual Type ValueType => typeof(string);
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and returns the content as the most appropriate type (by default as string). Stops at start tags and end tags.
public virtual object ReadContentAsObject()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsObject));
}
return InternalReadContentAsString();
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a boolean. Stops at start tags and end tags.
public virtual bool ReadContentAsBoolean()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsBoolean));
}
try
{
return XmlConvert.ToBoolean(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Boolean", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a DateTime. Stops at start tags and end tags.
public virtual DateTime ReadContentAsDateTime()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsDateTime));
}
try
{
return XmlConvert.ToDateTime(InternalReadContentAsString(), XmlDateTimeSerializationMode.RoundtripKind);
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "DateTime", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a DateTimeOffset. Stops at start tags and end tags.
public virtual DateTimeOffset ReadContentAsDateTimeOffset()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsDateTimeOffset));
}
try
{
return XmlConvert.ToDateTimeOffset(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "DateTimeOffset", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a double. Stops at start tags and end tags.
public virtual double ReadContentAsDouble()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsDouble));
}
try
{
return XmlConvert.ToDouble(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Double", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a float. Stops at start tags and end tags.
public virtual float ReadContentAsFloat()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsFloat));
}
try
{
return XmlConvert.ToSingle(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Float", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a decimal. Stops at start tags and end tags.
public virtual decimal ReadContentAsDecimal()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsDecimal));
}
try
{
return XmlConvert.ToDecimal(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Decimal", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to an int. Stops at start tags and end tags.
public virtual int ReadContentAsInt()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsInt));
}
try
{
return XmlConvert.ToInt32(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Int", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to a long. Stops at start tags and end tags.
public virtual long ReadContentAsLong()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsLong));
}
try
{
return XmlConvert.ToInt64(InternalReadContentAsString());
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, "Long", e, this as IXmlLineInfo);
}
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and returns the content as a string. Stops at start tags and end tags.
public virtual string ReadContentAsString()
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAsString));
}
return InternalReadContentAsString();
}
// Concatenates values of textual nodes of the current content, ignoring comments and PIs, expanding entity references,
// and converts the content to the requested type. Stops at start tags and end tags.
public virtual object ReadContentAs(Type returnType, IXmlNamespaceResolver? namespaceResolver)
{
if (!CanReadContentAs())
{
throw CreateReadContentAsException(nameof(ReadContentAs));
}
string strContentValue = InternalReadContentAsString();
if (returnType == typeof(string))
{
return strContentValue;
}
try
{
return XmlUntypedConverter.Untyped.ChangeType(strContentValue, returnType, namespaceResolver ?? this as IXmlNamespaceResolver);
}
catch (FormatException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo);
}
catch (InvalidCastException e)
{
throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo);
}
}
// Returns the content of the current element as the most appropriate type. Moves to the node following the element's end tag.
public virtual object ReadElementContentAsObject()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsObject"))
{
object value = ReadContentAsObject();
FinishReadElementContentAsXxx();
return value;
}
return string.Empty;
}
// Checks local name and namespace of the current element and returns its content as the most appropriate type. Moves to the node following the element's end tag.
public virtual object ReadElementContentAsObject(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsObject();
}
// Returns the content of the current element as a boolean. Moves to the node following the element's end tag.
public virtual bool ReadElementContentAsBoolean()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsBoolean"))
{
bool value = ReadContentAsBoolean();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToBoolean(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as a boolean. Moves to the node following the element's end tag.
public virtual bool ReadElementContentAsBoolean(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsBoolean();
}
// Returns the content of the current element as a DateTime. Moves to the node following the element's end tag.
public virtual DateTime ReadElementContentAsDateTime()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsDateTime"))
{
DateTime value = ReadContentAsDateTime();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToDateTime(string.Empty, XmlDateTimeSerializationMode.RoundtripKind);
}
// Checks local name and namespace of the current element and returns its content as a DateTime.
// Moves to the node following the element's end tag.
public virtual DateTime ReadElementContentAsDateTime(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsDateTime();
}
// Returns the content of the current element as a double. Moves to the node following the element's end tag.
public virtual double ReadElementContentAsDouble()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsDouble"))
{
double value = ReadContentAsDouble();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToDouble(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as a double.
// Moves to the node following the element's end tag.
public virtual double ReadElementContentAsDouble(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsDouble();
}
// Returns the content of the current element as a float. Moves to the node following the element's end tag.
public virtual float ReadElementContentAsFloat()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsFloat"))
{
float value = ReadContentAsFloat();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToSingle(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as a float.
// Moves to the node following the element's end tag.
public virtual float ReadElementContentAsFloat(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsFloat();
}
// Returns the content of the current element as a decimal. Moves to the node following the element's end tag.
public virtual decimal ReadElementContentAsDecimal()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsDecimal"))
{
decimal value = ReadContentAsDecimal();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToDecimal(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as a decimal.
// Moves to the node following the element's end tag.
public virtual decimal ReadElementContentAsDecimal(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsDecimal();
}
// Returns the content of the current element as an int. Moves to the node following the element's end tag.
public virtual int ReadElementContentAsInt()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsInt"))
{
int value = ReadContentAsInt();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToInt32(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as an int.
// Moves to the node following the element's end tag.
public virtual int ReadElementContentAsInt(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsInt();
}
// Returns the content of the current element as a long. Moves to the node following the element's end tag.
public virtual long ReadElementContentAsLong()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsLong"))
{
long value = ReadContentAsLong();
FinishReadElementContentAsXxx();
return value;
}
return XmlConvert.ToInt64(string.Empty);
}
// Checks local name and namespace of the current element and returns its content as a long.
// Moves to the node following the element's end tag.
public virtual long ReadElementContentAsLong(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsLong();
}
// Returns the content of the current element as a string. Moves to the node following the element's end tag.
public virtual string ReadElementContentAsString()
{
if (SetupReadElementContentAsXxx("ReadElementContentAsString"))
{
string value = ReadContentAsString();
FinishReadElementContentAsXxx();
return value;
}
return string.Empty;
}
// Checks local name and namespace of the current element and returns its content as a string.
// Moves to the node following the element's end tag.
public virtual string ReadElementContentAsString(string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAsString();
}
// Returns the content of the current element as the requested type. Moves to the node following the element's end tag.
public virtual object ReadElementContentAs(Type returnType, IXmlNamespaceResolver namespaceResolver)
{
if (SetupReadElementContentAsXxx("ReadElementContentAs"))
{
object value = ReadContentAs(returnType, namespaceResolver);
FinishReadElementContentAsXxx();
return value;
}
return returnType == typeof(string) ? string.Empty : XmlUntypedConverter.Untyped.ChangeType(string.Empty, returnType, namespaceResolver);
}
// Checks local name and namespace of the current element and returns its content as the requested type.
// Moves to the node following the element's end tag.
public virtual object ReadElementContentAs(Type returnType, IXmlNamespaceResolver namespaceResolver, string localName, string namespaceURI)
{
CheckElement(localName, namespaceURI);
return ReadElementContentAs(returnType, namespaceResolver);
}
// Attribute Accessors
// The number of attributes on the current node.
public abstract int AttributeCount { get; }
// Gets the value of the attribute with the specified Name
public abstract string? GetAttribute(string name);
// Gets the value of the attribute with the LocalName and NamespaceURI
public abstract string? GetAttribute(string name, string? namespaceURI);
// Gets the value of the attribute with the specified index.
public abstract string GetAttribute(int i);
// Gets the value of the attribute with the specified index.
public virtual string this[int i] => GetAttribute(i);
// Gets the value of the attribute with the specified Name.
public virtual string? this[string name] => GetAttribute(name);
// Gets the value of the attribute with the LocalName and NamespaceURI
public virtual string? this[string name, string? namespaceURI] => GetAttribute(name, namespaceURI);
// Moves to the attribute with the specified Name.
public abstract bool MoveToAttribute(string name);
// Moves to the attribute with the specified LocalName and NamespaceURI.
public abstract bool MoveToAttribute(string name, string? ns);
// Moves to the attribute with the specified index.
public virtual void MoveToAttribute(int i)
{
ArgumentOutOfRangeException.ThrowIfNegative(i);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(i, AttributeCount);
MoveToElement();
MoveToFirstAttribute();
int j = 0;
while (j < i)
{
MoveToNextAttribute();
j++;
}
}
// Moves to the first attribute of the current node.
public abstract bool MoveToFirstAttribute();
// Moves to the next attribute.
public abstract bool MoveToNextAttribute();
// Moves to the element that contains the current attribute node.
public abstract bool MoveToElement();
// Parses the attribute value into one or more Text and/or EntityReference node types.
public abstract bool ReadAttributeValue();
// Moving through the Stream
// Reads the next node from the stream.
public abstract bool Read();
// Returns true when the XmlReader is positioned at the end of the stream.
public abstract bool EOF { get; }
// Closes the stream/TextReader (if CloseInput==true), changes the ReadState to Closed, and sets all the properties back to zero/empty string.
public virtual void Close() { }
// Returns the read state of the XmlReader.
public abstract ReadState ReadState { get; }
// Skips to the end tag of the current element.
public virtual void Skip()
{
if (ReadState != ReadState.Interactive)
{
return;
}
SkipSubtree();
}
// Gets the XmlNameTable associated with the XmlReader.
public abstract XmlNameTable NameTable { get; }
// Resolves a namespace prefix in the current element's scope.
public abstract string? LookupNamespace(string prefix);
// Returns true if the XmlReader can expand general entities.
public virtual bool CanResolveEntity => false;
// Resolves the entity reference for nodes of NodeType EntityReference.
public abstract void ResolveEntity();
// Binary content access methods
// Returns true if the reader supports call to ReadContentAsBase64, ReadElementContentAsBase64, ReadContentAsBinHex and ReadElementContentAsBinHex.
public virtual bool CanReadBinaryContent => false;
// Returns decoded bytes of the current base64 text content. Call this methods until it returns 0 to get all the data.
public virtual int ReadContentAsBase64(byte[] buffer, int index, int count)
{
throw new NotSupportedException(SR.Format(SR.Xml_ReadBinaryContentNotSupported, "ReadContentAsBase64"));
}
// Returns decoded bytes of the current base64 element content. Call this methods until it returns 0 to get all the data.
public virtual int ReadElementContentAsBase64(byte[] buffer, int index, int count)
{
throw new NotSupportedException(SR.Format(SR.Xml_ReadBinaryContentNotSupported, "ReadElementContentAsBase64"));
}
// Returns decoded bytes of the current binhex text content. Call this methods until it returns 0 to get all the data.
public virtual int ReadContentAsBinHex(byte[] buffer, int index, int count)
{
throw new NotSupportedException(SR.Format(SR.Xml_ReadBinaryContentNotSupported, "ReadContentAsBinHex"));
}
// Returns decoded bytes of the current binhex element content. Call this methods until it returns 0 to get all the data.
public virtual int ReadElementContentAsBinHex(byte[] buffer, int index, int count)
{
throw new NotSupportedException(SR.Format(SR.Xml_ReadBinaryContentNotSupported, "ReadElementContentAsBinHex"));
}
// Text streaming methods
// Returns true if the XmlReader supports calls to ReadValueChunk.
public virtual bool CanReadValueChunk => false;
// Returns a chunk of the value of the current node. Call this method in a loop to get all the data.
// Use this method to get a streaming access to the value of the current node.
public virtual int ReadValueChunk(char[] buffer, int index, int count)
{
throw new NotSupportedException(SR.Xml_ReadValueChunkNotSupported);
}
// Virtual helper methods
// Reads the contents of an element as a string. Stops of comments, PIs or entity references.
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual string ReadString()
{
if (ReadState != ReadState.Interactive)
{
return string.Empty;
}
MoveToElement();
if (NodeType == XmlNodeType.Element)
{
if (IsEmptyElement)
{
return string.Empty;
}
if (!Read())
{
throw new InvalidOperationException(SR.Xml_InvalidOperation);
}
if (NodeType == XmlNodeType.EndElement)
{
return string.Empty;
}
}
string result = string.Empty;
while (IsTextualNode(NodeType))
{
result += Value;
if (!Read())
{
break;
}
}
return result;
}
// Checks whether the current node is a content (non-whitespace text, CDATA, Element, EndElement, EntityReference
// or EndEntity) node. If the node is not a content node, then the method skips ahead to the next content node or
// end of file. Skips over nodes of type ProcessingInstruction, DocumentType, Comment, Whitespace and SignificantWhitespace.
public virtual XmlNodeType MoveToContent()
{
do
{
switch (NodeType)
{
case XmlNodeType.Attribute:
MoveToElement();
return NodeType;
case XmlNodeType.Element:
case XmlNodeType.EndElement:
case XmlNodeType.CDATA:
case XmlNodeType.Text:
case XmlNodeType.EntityReference:
case XmlNodeType.EndEntity:
return NodeType;
}
} while (Read());
return NodeType;
}
// Checks that the current node is an element and advances the reader to the next node.
public virtual void ReadStartElement()
{
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
Read();
}
// Checks that the current content node is an element with the given Name and advances the reader to the next node.
public virtual void ReadStartElement(string name)
{
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
if (Name == name)
{
Read();
}
else
{
throw new XmlException(SR.Xml_ElementNotFound, name, this as IXmlLineInfo);
}
}
// Checks that the current content node is an element with the given LocalName and NamespaceURI
// and advances the reader to the next node.
public virtual void ReadStartElement(string localname, string ns)
{
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
if (LocalName == localname && NamespaceURI == ns)
{
Read();
}
else
{
throw new XmlException(SR.Xml_ElementNotFoundNs, new string[] { localname, ns }, this as IXmlLineInfo);
}
}
// Reads a text-only element.
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual string ReadElementString()
{
string result = string.Empty;
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
if (!IsEmptyElement)
{
Read();
result = ReadString();
if (NodeType != XmlNodeType.EndElement)
{
throw new XmlException(SR.Xml_UnexpectedNodeInSimpleContent, new string[] { NodeType.ToString(), "ReadElementString" }, this as IXmlLineInfo);
}
Read();
}
else
{
Read();
}
return result;
}
// Checks that the Name property of the element found matches the given string before reading a text-only element.
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual string ReadElementString(string name)
{
string result = string.Empty;
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
if (Name != name)
{
throw new XmlException(SR.Xml_ElementNotFound, name, this as IXmlLineInfo);
}
if (!IsEmptyElement)
{
//Read();
result = ReadString();
if (NodeType != XmlNodeType.EndElement)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
Read();
}
else
{
Read();
}
return result;
}
// Checks that the LocalName and NamespaceURI properties of the element found matches the given strings
// before reading a text-only element.
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual string ReadElementString(string localname, string ns)
{
string result = string.Empty;
if (MoveToContent() != XmlNodeType.Element)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
if (LocalName != localname || NamespaceURI != ns)
{
throw new XmlException(SR.Xml_ElementNotFoundNs, new string[] { localname, ns }, this as IXmlLineInfo);
}
if (!IsEmptyElement)
{
//Read();
result = ReadString();
if (NodeType != XmlNodeType.EndElement)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
Read();
}
else
{
Read();
}
return result;
}
// Checks that the current content node is an end tag and advances the reader to the next node.
public virtual void ReadEndElement()
{
if (MoveToContent() != XmlNodeType.EndElement)
{
throw new XmlException(SR.Xml_InvalidNodeType, NodeType.ToString(), this as IXmlLineInfo);
}
Read();
}
// Calls MoveToContent and tests if the current content node is a start tag or empty element tag (XmlNodeType.Element).
public virtual bool IsStartElement()
{
return MoveToContent() == XmlNodeType.Element;
}
// Calls MoveToContent and tests if the current content node is a start tag or empty element tag (XmlNodeType.Element) and if the
// Name property of the element found matches the given argument.
public virtual bool IsStartElement(string name)
{
return MoveToContent() == XmlNodeType.Element && Name == name;
}
// Calls MoveToContent and tests if the current content node is a start tag or empty element tag (XmlNodeType.Element) and if
// the LocalName and NamespaceURI properties of the element found match the given strings.
public virtual bool IsStartElement(string localname, string ns)
{
return MoveToContent() == XmlNodeType.Element && LocalName == localname && NamespaceURI == ns;
}
// Reads to the following element with the given Name.
public virtual bool ReadToFollowing(string name)
{
ArgumentException.ThrowIfNullOrEmpty(name);
// atomize name
name = NameTable.Add(name);
// find following element with that name
while (Read())
{
if (NodeType == XmlNodeType.Element && Ref.Equal(name, Name))
{
return true;
}
}
return false;
}
// Reads to the following element with the given LocalName and NamespaceURI.
public virtual bool ReadToFollowing(string localName, string namespaceURI)
{
ArgumentException.ThrowIfNullOrEmpty(localName);
ArgumentNullException.ThrowIfNull(namespaceURI);
// atomize local name and namespace
localName = NameTable.Add(localName);
namespaceURI = NameTable.Add(namespaceURI);
// find following element with that name
while (Read())
{
if (NodeType == XmlNodeType.Element && Ref.Equal(localName, LocalName) && Ref.Equal(namespaceURI, NamespaceURI))
{
return true;
}
}
return false;
}
// Reads to the first descendant of the current element with the given Name.
public virtual bool ReadToDescendant(string name)
{
ArgumentException.ThrowIfNullOrEmpty(name);
// save the element or root depth
int parentDepth = Depth;
if (NodeType != XmlNodeType.Element)
{
// adjust the depth if we are on root node
if (ReadState == ReadState.Initial)
{
Debug.Assert(parentDepth == 0);
parentDepth--;
}
else
{
return false;
}
}
else if (IsEmptyElement)
{
return false;
}
// atomize name
name = NameTable.Add(name);
// find the descendant
while (Read() && Depth > parentDepth)
{
if (NodeType == XmlNodeType.Element && Ref.Equal(name, Name))
{
return true;
}
}
Debug.Assert(NodeType == XmlNodeType.EndElement || NodeType == XmlNodeType.None || ReadState == ReadState.Error);
return false;
}
// Reads to the first descendant of the current element with the given LocalName and NamespaceURI.
public virtual bool ReadToDescendant(string localName, string namespaceURI)
{
ArgumentException.ThrowIfNullOrEmpty(localName);
ArgumentNullException.ThrowIfNull(namespaceURI);
// save the element or root depth
int parentDepth = Depth;
if (NodeType != XmlNodeType.Element)