5 // Jason Diamond (jason@injektilo.org)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
9 // (C) 2001, 2002 Jason Diamond http://injektilo.org/
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Atsushi Enomoto
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Diagnostics;
39 using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
40 using System.Xml.Serialization; // only required for NET_2_0 (SchemaInfo)
41 using Mono.Xml.Schema; // only required for NET_2_0
43 using Mono.Xml; // only required for NET_2_0
48 public abstract class XmlReader : IDisposable
50 public abstract class XmlReader
53 private StringBuilder readStringBuffer;
54 private XmlReaderBinarySupport binary;
56 private XmlReaderSettings settings;
61 protected XmlReader ()
69 public abstract int AttributeCount { get; }
71 public abstract string BaseURI { get; }
73 internal XmlReaderBinarySupport Binary {
74 get { return binary; }
77 internal XmlReaderBinarySupport.CharGetter BinaryCharGetter {
78 get { return binary != null ? binary.Getter : null; }
81 binary = new XmlReaderBinarySupport (this);
82 binary.Getter = value;
87 // To enable it internally in sys.xml, just insert these
88 // two lines into Read():
91 // if (Binary != null)
95 public virtual bool CanReadBinaryContent {
99 public virtual bool CanReadValueChunk {
100 get { return false; }
103 internal virtual bool CanReadBinaryContent {
104 get { return false; }
107 internal virtual bool CanReadValueChunk {
108 get { return false; }
112 public virtual bool CanResolveEntity
114 get { return false; }
117 public abstract int Depth { get; }
119 public abstract bool EOF { get; }
121 public virtual bool HasAttributes
123 get { return AttributeCount > 0; }
127 public abstract bool HasValue { get; }
130 public abstract bool IsEmptyElement { get; }
133 public virtual bool IsDefault {
134 get { return false; }
137 public virtual string this [int i] {
138 get { return GetAttribute (i); }
141 public virtual string this [string name] {
142 get { return GetAttribute (name); }
145 public virtual string this [string name, string namespaceURI] {
146 get { return GetAttribute (name, namespaceURI); }
149 public abstract bool IsDefault { get; }
151 public abstract string this [int i] { get; }
153 public abstract string this [string name] { get; }
155 public abstract string this [string localName, string namespaceName] { get; }
158 public abstract string LocalName { get; }
161 public virtual string Name {
163 return Prefix.Length > 0 ?
164 String.Concat (Prefix, ":", LocalName) :
169 public abstract string Name { get; }
172 public abstract string NamespaceURI { get; }
174 public abstract XmlNameTable NameTable { get; }
176 public abstract XmlNodeType NodeType { get; }
178 public abstract string Prefix { get; }
181 public virtual char QuoteChar {
185 public abstract char QuoteChar { get; }
188 public abstract ReadState ReadState { get; }
192 public virtual IXmlSchemaInfo SchemaInfo {
197 public virtual XmlReaderSettings Settings {
198 get { return settings; }
202 public abstract string Value { get; }
205 public virtual string XmlLang {
206 get { return String.Empty; }
209 public virtual XmlSpace XmlSpace {
210 get { return XmlSpace.None; }
213 public abstract string XmlLang { get; }
215 public abstract XmlSpace XmlSpace { get; }
222 public abstract void Close ();
225 private static XmlNameTable PopulateNameTable (
226 XmlReaderSettings settings)
228 XmlNameTable nameTable = settings.NameTable;
229 if (nameTable == null)
230 nameTable = new NameTable ();
234 private static XmlParserContext PopulateParserContext (
235 XmlReaderSettings settings, string baseUri)
237 XmlNameTable nt = PopulateNameTable (settings);
238 return new XmlParserContext (nt,
239 new XmlNamespaceManager (nt),
244 private static XmlNodeType GetNodeType (
245 XmlReaderSettings settings)
247 ConformanceLevel level = settings != null ? settings.ConformanceLevel : ConformanceLevel.Auto;
249 level == ConformanceLevel.Fragment ?
250 XmlNodeType.Element :
251 XmlNodeType.Document;
254 public static XmlReader Create (Stream stream)
256 return Create (stream, null);
259 public static XmlReader Create (string url)
261 return Create (url, null);
264 public static XmlReader Create (TextReader reader)
266 return Create (reader, null);
269 public static XmlReader Create (string url, XmlReaderSettings settings)
271 return Create (url, settings, null);
274 public static XmlReader Create (Stream stream, XmlReaderSettings settings)
276 return Create (stream, settings, String.Empty);
279 public static XmlReader Create (TextReader reader, XmlReaderSettings settings)
281 return Create (reader, settings, String.Empty);
284 static XmlReaderSettings PopulateSettings (XmlReaderSettings src)
287 return new XmlReaderSettings ();
292 public static XmlReader Create (Stream stream, XmlReaderSettings settings, string baseUri)
294 settings = PopulateSettings (settings);
295 return Create (stream, settings,
296 PopulateParserContext (settings, baseUri));
299 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, string baseUri)
301 settings = PopulateSettings (settings);
302 return Create (reader, settings,
303 PopulateParserContext (settings, baseUri));
306 public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
308 settings = PopulateSettings (settings);
309 XmlReader r = CreateFilteredXmlReader (reader, settings);
310 r.settings = settings;
314 public static XmlReader Create (string url, XmlReaderSettings settings, XmlParserContext context)
316 settings = PopulateSettings (settings);
317 bool closeInputBak = settings.CloseInput;
319 settings.CloseInput = true; // forced. See XmlReaderCommonTests.CreateFromUrlClose().
321 context = PopulateParserContext (settings, url);
322 XmlTextReader xtr = new XmlTextReader (false, settings.XmlResolver, url, GetNodeType (settings), context);
323 XmlReader ret = CreateCustomizedTextReader (xtr, settings);
326 settings.CloseInput = closeInputBak;
330 public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
332 settings = PopulateSettings (settings);
334 context = PopulateParserContext (settings, String.Empty);
335 return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings);
338 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, XmlParserContext context)
340 settings = PopulateSettings (settings);
342 context = PopulateParserContext (settings, String.Empty);
343 return CreateCustomizedTextReader (new XmlTextReader (context.BaseURI, reader, GetNodeType (settings), context), settings);
346 private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlReaderSettings settings)
348 reader.XmlResolver = settings.XmlResolver;
349 // Normalization is set true by default.
350 reader.Normalization = true;
351 reader.EntityHandling = EntityHandling.ExpandEntities;
353 if (settings.ProhibitDtd)
354 reader.ProhibitDtd = true;
356 if (!settings.CheckCharacters)
357 reader.CharacterChecking = false;
359 // I guess it might be changed in 2.0 RTM to set true
360 // as default, or just disappear. It goes against
361 // XmlTextReader's default usage and users will have
362 // to close input manually (that's annoying). Moreover,
363 // MS XmlTextReader consumes text input more than
364 // actually read and users can acquire those extra
365 // consumption by GetRemainder() that returns different
367 reader.CloseInput = settings.CloseInput;
369 // I would like to support it in detail later;
370 // MSDN description looks source of confusion. We don't
371 // need examples, but precise list of how it works.
372 reader.Conformance = settings.ConformanceLevel;
374 reader.AdjustLineInfoOffset (settings.LineNumberOffset,
375 settings.LinePositionOffset);
377 if (settings.NameTable != null)
378 reader.SetNameTable (settings.NameTable);
380 XmlReader r = CreateFilteredXmlReader (reader, settings);
381 r.settings = settings;
385 private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlReaderSettings settings)
387 ConformanceLevel conf = ConformanceLevel.Auto;
388 if (reader is XmlTextReader)
389 conf = ((XmlTextReader) reader).Conformance;
390 else if (reader.Settings != null)
391 conf = reader.Settings.ConformanceLevel;
393 conf = settings.ConformanceLevel;
394 if (settings.ConformanceLevel != ConformanceLevel.Auto &&
395 conf != settings.ConformanceLevel)
396 throw new InvalidOperationException (String.Format ("ConformanceLevel cannot be overwritten by a wrapping XmlReader. The source reader has {0}, while {1} is specified.", conf, settings.ConformanceLevel));
397 settings.ConformanceLevel = conf;
399 reader = CreateValidatingXmlReader (reader, settings);
401 if ( settings.IgnoreComments ||
402 settings.IgnoreProcessingInstructions ||
403 settings.IgnoreWhitespace)
404 return new XmlFilterReader (reader, settings);
406 reader.settings = settings;
411 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
416 XmlValidatingReader xvr = null;
417 switch (settings.ValidationType) {
418 // Auto and XDR are obsoleted in 2.0 and therefore ignored.
421 case ValidationType.DTD:
422 xvr = new XmlValidatingReader (reader);
423 xvr.XmlResolver = settings.XmlResolver;
424 xvr.ValidationType = ValidationType.DTD;
426 case ValidationType.Schema:
427 return new XmlSchemaValidatingReader (reader, settings);
430 // Actually I don't think they are treated in DTD validation though...
431 if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
432 throw new NotImplementedException ();
433 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
434 // throw new NotImplementedException ();
435 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
436 // throw new NotImplementedException ();
437 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ReportValidationWarnings) == 0)
438 // throw new NotImplementedException ();
440 return xvr != null ? xvr : reader;
444 void IDisposable.Dispose ()
449 protected virtual void Dispose (bool disposing)
451 if (ReadState != ReadState.Closed)
456 public abstract string GetAttribute (int i);
458 public abstract string GetAttribute (string name);
460 public abstract string GetAttribute (
462 string namespaceName);
464 public static bool IsName (string s)
466 return s != null && XmlChar.IsName (s);
469 public static bool IsNameToken (string s)
471 return s != null && XmlChar.IsNmToken (s);
474 public virtual bool IsStartElement ()
476 return (MoveToContent () == XmlNodeType.Element);
479 public virtual bool IsStartElement (string name)
481 if (!IsStartElement ())
484 return (Name == name);
487 public virtual bool IsStartElement (string localName, string namespaceName)
489 if (!IsStartElement ())
492 return (LocalName == localName && NamespaceURI == namespaceName);
495 public abstract string LookupNamespace (string prefix);
498 public virtual void MoveToAttribute (int i)
500 if (i >= AttributeCount)
501 throw new ArgumentOutOfRangeException ();
502 MoveToFirstAttribute ();
503 for (int a = 0; a < i; a++)
504 MoveToNextAttribute ();
507 public abstract void MoveToAttribute (int i);
510 public abstract bool MoveToAttribute (string name);
512 public abstract bool MoveToAttribute (
514 string namespaceName);
516 private bool IsContent (XmlNodeType nodeType)
519 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
522 case XmlNodeType.Text:
524 case XmlNodeType.CDATA:
526 case XmlNodeType.Element:
528 case XmlNodeType.EndElement:
530 case XmlNodeType.EntityReference:
532 case XmlNodeType.EndEntity:
539 public virtual XmlNodeType MoveToContent ()
541 if (NodeType == XmlNodeType.Attribute)
545 if (IsContent (NodeType))
549 return XmlNodeType.None;
552 public abstract bool MoveToElement ();
554 public abstract bool MoveToFirstAttribute ();
556 public abstract bool MoveToNextAttribute ();
558 public abstract bool Read ();
561 public abstract bool ReadAttributeValue ();
564 public virtual string ReadElementString ()
566 if (MoveToContent () != XmlNodeType.Element) {
567 string error = String.Format ("'{0}' is an invalid node type.",
568 NodeType.ToString ());
569 throw XmlError (error);
572 string result = String.Empty;
573 if (!IsEmptyElement) {
575 result = ReadString ();
576 if (NodeType != XmlNodeType.EndElement) {
577 string error = String.Format ("'{0}' is an invalid node type.",
578 NodeType.ToString ());
579 throw XmlError (error);
587 public virtual string ReadElementString (string name)
589 if (MoveToContent () != XmlNodeType.Element) {
590 string error = String.Format ("'{0}' is an invalid node type.",
591 NodeType.ToString ());
592 throw XmlError (error);
596 string error = String.Format ("The {0} tag from namespace {1} is expected.",
598 throw XmlError (error);
601 string result = String.Empty;
602 if (!IsEmptyElement) {
604 result = ReadString ();
605 if (NodeType != XmlNodeType.EndElement) {
606 string error = String.Format ("'{0}' is an invalid node type.",
607 NodeType.ToString ());
608 throw XmlError (error);
616 public virtual string ReadElementString (string localName, string namespaceName)
618 if (MoveToContent () != XmlNodeType.Element) {
619 string error = String.Format ("'{0}' is an invalid node type.",
620 NodeType.ToString ());
621 throw XmlError (error);
624 if (localName != LocalName || NamespaceURI != namespaceName) {
625 string error = String.Format ("The {0} tag from namespace {1} is expected.",
626 LocalName, NamespaceURI);
627 throw XmlError (error);
630 string result = String.Empty;
631 if (!IsEmptyElement) {
633 result = ReadString ();
634 if (NodeType != XmlNodeType.EndElement) {
635 string error = String.Format ("'{0}' is an invalid node type.",
636 NodeType.ToString ());
637 throw XmlError (error);
645 public virtual void ReadEndElement ()
647 if (MoveToContent () != XmlNodeType.EndElement) {
648 string error = String.Format ("'{0}' is an invalid node type.",
649 NodeType.ToString ());
650 throw XmlError (error);
656 public virtual string ReadInnerXml ()
658 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
661 if (IsEmptyElement) {
665 StringWriter sw = new StringWriter ();
666 XmlTextWriter xtw = new XmlTextWriter (sw);
667 if (NodeType == XmlNodeType.Element) {
668 int startDepth = Depth;
670 while (startDepth < Depth) {
671 if (ReadState != ReadState.Interactive)
672 throw XmlError ("Unexpected end of the XML reader.");
673 xtw.WriteNode (this, false);
675 // reader is now end element, then proceed once more.
679 xtw.WriteNode (this, false);
681 return sw.ToString ();
684 public virtual string ReadOuterXml ()
686 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
690 case XmlNodeType.Element:
691 case XmlNodeType.Attribute:
692 StringWriter sw = new StringWriter ();
693 XmlTextWriter xtw = new XmlTextWriter (sw);
694 xtw.WriteNode (this, false);
695 return sw.ToString ();
702 public virtual void ReadStartElement ()
704 if (MoveToContent () != XmlNodeType.Element) {
705 string error = String.Format ("'{0}' is an invalid node type.",
706 NodeType.ToString ());
707 throw XmlError (error);
713 public virtual void ReadStartElement (string name)
715 if (MoveToContent () != XmlNodeType.Element) {
716 string error = String.Format ("'{0}' is an invalid node type.",
717 NodeType.ToString ());
718 throw XmlError (error);
722 string error = String.Format ("The {0} tag from namespace {1} is expected.",
724 throw XmlError (error);
730 public virtual void ReadStartElement (string localName, string namespaceName)
732 if (MoveToContent () != XmlNodeType.Element) {
733 string error = String.Format ("'{0}' is an invalid node type.",
734 NodeType.ToString ());
735 throw XmlError (error);
738 if (localName != LocalName || NamespaceURI != namespaceName) {
739 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
740 localName, namespaceName,
741 LocalName, NamespaceURI);
742 throw XmlError (error);
748 public virtual string ReadString ()
750 if (readStringBuffer == null)
751 readStringBuffer = new StringBuilder ();
752 readStringBuffer.Length = 0;
759 case XmlNodeType.Element:
765 case XmlNodeType.Text:
766 case XmlNodeType.CDATA:
767 case XmlNodeType.Whitespace:
768 case XmlNodeType.SignificantWhitespace:
769 readStringBuffer.Append (Value);
775 case XmlNodeType.Text:
776 case XmlNodeType.CDATA:
777 case XmlNodeType.Whitespace:
778 case XmlNodeType.SignificantWhitespace:
781 case XmlNodeType.Text:
782 case XmlNodeType.CDATA:
783 case XmlNodeType.Whitespace:
784 case XmlNodeType.SignificantWhitespace:
785 readStringBuffer.Append (Value);
793 string ret = readStringBuffer.ToString ();
794 readStringBuffer.Length = 0;
799 public virtual Type ValueType {
800 get { return typeof (string); }
803 public virtual bool ReadToDescendant (string name)
805 if (ReadState == ReadState.Initial) {
807 if (IsStartElement (name))
810 if (NodeType != XmlNodeType.Element || IsEmptyElement)
813 for (Read (); depth < Depth; Read ())
814 if (NodeType == XmlNodeType.Element && name == Name)
819 public virtual bool ReadToDescendant (string localName, string namespaceURI)
821 if (ReadState == ReadState.Initial) {
823 if (IsStartElement (localName, namespaceURI))
826 if (NodeType != XmlNodeType.Element || IsEmptyElement)
829 for (Read (); depth < Depth; Read ())
830 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
835 public virtual bool ReadToFollowing (string name)
838 if (NodeType == XmlNodeType.Element && name == Name)
843 public virtual bool ReadToFollowing (string localName, string namespaceURI)
846 if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
851 public virtual bool ReadToNextSibling (string name)
853 if (ReadState != ReadState.Interactive)
857 for (; !EOF && depth <= Depth; Skip ())
858 if (NodeType == XmlNodeType.Element && name == Name)
863 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
865 if (ReadState != ReadState.Interactive)
869 for (; !EOF && depth <= Depth; Skip ())
870 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
875 public virtual XmlReader ReadSubtree ()
877 return new SubtreeXmlReader (this);
880 private string ReadContentString ()
882 if (NodeType == XmlNodeType.Attribute)
884 return ReadContentString (true);
887 private string ReadContentString (bool isText)
891 case XmlNodeType.Text:
892 case XmlNodeType.SignificantWhitespace:
893 case XmlNodeType.Whitespace:
894 case XmlNodeType.CDATA:
897 throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
901 string value = String.Empty;
904 case XmlNodeType.Element:
907 throw XmlError ("Child element is not expected in this operation.");
908 case XmlNodeType.EndElement:
910 case XmlNodeType.Text:
911 case XmlNodeType.CDATA:
912 case XmlNodeType.SignificantWhitespace:
913 case XmlNodeType.Whitespace:
918 throw XmlError ("Unexpected end of document.");
921 string GetLocation ()
923 IXmlLineInfo li = this as IXmlLineInfo;
924 return li != null && li.HasLineInfo () ?
925 String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
929 public virtual object ReadElementContentAsObject ()
931 return ReadElementContentAs (ValueType, null);
935 public virtual object ReadElementContentAsObject (string localName, string namespaceURI)
937 return ReadElementContentAs (ValueType, null, localName, namespaceURI);
941 public virtual object ReadContentAsObject ()
943 return ReadContentAs (ValueType, null);
946 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
948 bool isEmpty = IsEmptyElement;
950 object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
956 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
958 ReadStartElement (localName, namespaceURI);
959 object obj = ReadContentAs (type, resolver);
964 public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
966 return ValueAs (ReadContentString (), type, resolver);
969 private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
972 if (type == typeof (object))
974 if (type == typeof (XmlQualifiedName)) {
975 if (resolver != null)
976 return XmlQualifiedName.Parse (text, resolver);
978 return XmlQualifiedName.Parse (text, this);
981 switch (Type.GetTypeCode (type)) {
982 case TypeCode.Boolean:
983 return XQueryConvert.StringToBoolean (text);
984 case TypeCode.DateTime:
985 return XQueryConvert.StringToDateTime (text);
986 case TypeCode.Decimal:
987 return XQueryConvert.StringToDecimal (text);
988 case TypeCode.Double:
989 return XQueryConvert.StringToDouble (text);
991 return XQueryConvert.StringToInt (text);
993 return XQueryConvert.StringToInteger (text);
994 case TypeCode.Single:
995 return XQueryConvert.StringToFloat (text);
996 case TypeCode.String:
999 } catch (Exception ex) {
1000 throw XmlError (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'. {2}", text, type, ex != null ? ex.Message : String.Empty), ex);
1002 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
1005 public virtual bool ReadElementContentAsBoolean ()
1008 return XQueryConvert.StringToBoolean (ReadElementContentAsString ());
1009 } catch (FormatException ex) {
1010 throw XmlError ("Typed value is invalid.", ex);
1014 public virtual DateTime ReadElementContentAsDateTime ()
1017 return XQueryConvert.StringToDateTime (ReadElementContentAsString ());
1018 } catch (FormatException ex) {
1019 throw XmlError ("Typed value is invalid.", ex);
1023 public virtual decimal ReadElementContentAsDecimal ()
1026 return XQueryConvert.StringToDecimal (ReadElementContentAsString ());
1027 } catch (FormatException ex) {
1028 throw XmlError ("Typed value is invalid.", ex);
1032 public virtual double ReadElementContentAsDouble ()
1035 return XQueryConvert.StringToDouble (ReadElementContentAsString ());
1036 } catch (FormatException ex) {
1037 throw XmlError ("Typed value is invalid.", ex);
1041 public virtual float ReadElementContentAsFloat ()
1044 return XQueryConvert.StringToFloat (ReadElementContentAsString ());
1045 } catch (FormatException ex) {
1046 throw XmlError ("Typed value is invalid.", ex);
1050 public virtual int ReadElementContentAsInt ()
1053 return XQueryConvert.StringToInt (ReadElementContentAsString ());
1054 } catch (FormatException ex) {
1055 throw XmlError ("Typed value is invalid.", ex);
1059 public virtual long ReadElementContentAsLong ()
1062 return XQueryConvert.StringToInteger (ReadElementContentAsString ());
1063 } catch (FormatException ex) {
1064 throw XmlError ("Typed value is invalid.", ex);
1068 public virtual string ReadElementContentAsString ()
1070 bool isEmpty = IsEmptyElement;
1071 ReadStartElement ();
1073 return String.Empty;
1074 string s = ReadContentString (false);
1079 public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
1082 return XQueryConvert.StringToBoolean (ReadElementContentAsString (localName, namespaceURI));
1083 } catch (FormatException ex) {
1084 throw XmlError ("Typed value is invalid.", ex);
1088 public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
1091 return XQueryConvert.StringToDateTime (ReadElementContentAsString (localName, namespaceURI));
1092 } catch (FormatException ex) {
1093 throw XmlError ("Typed value is invalid.", ex);
1097 public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
1100 return XQueryConvert.StringToDecimal (ReadElementContentAsString (localName, namespaceURI));
1101 } catch (FormatException ex) {
1102 throw XmlError ("Typed value is invalid.", ex);
1106 public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
1109 return XQueryConvert.StringToDouble (ReadElementContentAsString (localName, namespaceURI));
1110 } catch (FormatException ex) {
1111 throw XmlError ("Typed value is invalid.", ex);
1115 public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
1118 return XQueryConvert.StringToFloat (ReadElementContentAsString (localName, namespaceURI));
1119 } catch (FormatException ex) {
1120 throw XmlError ("Typed value is invalid.", ex);
1124 public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
1127 return XQueryConvert.StringToInt (ReadElementContentAsString (localName, namespaceURI));
1128 } catch (FormatException ex) {
1129 throw XmlError ("Typed value is invalid.", ex);
1133 public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
1136 return XQueryConvert.StringToInteger (ReadElementContentAsString (localName, namespaceURI));
1137 } catch (FormatException ex) {
1138 throw XmlError ("Typed value is invalid.", ex);
1142 public virtual string ReadElementContentAsString (string localName, string namespaceURI)
1144 bool isEmpty = IsEmptyElement;
1145 ReadStartElement (localName, namespaceURI);
1147 return String.Empty;
1148 string s = ReadContentString (false);
1153 public virtual bool ReadContentAsBoolean ()
1156 return XQueryConvert.StringToBoolean (ReadContentString ());
1157 } catch (FormatException ex) {
1158 throw XmlError ("Typed value is invalid.", ex);
1162 public virtual DateTime ReadContentAsDateTime ()
1165 return XQueryConvert.StringToDateTime (ReadContentString ());
1166 } catch (FormatException ex) {
1167 throw XmlError ("Typed value is invalid.", ex);
1171 public virtual decimal ReadContentAsDecimal ()
1174 return XQueryConvert.StringToDecimal (ReadContentString ());
1175 } catch (FormatException ex) {
1176 throw XmlError ("Typed value is invalid.", ex);
1180 public virtual double ReadContentAsDouble ()
1183 return XQueryConvert.StringToDouble (ReadContentString ());
1184 } catch (FormatException ex) {
1185 throw XmlError ("Typed value is invalid.", ex);
1189 public virtual float ReadContentAsFloat ()
1192 return XQueryConvert.StringToFloat (ReadContentString ());
1193 } catch (FormatException ex) {
1194 throw XmlError ("Typed value is invalid.", ex);
1198 public virtual int ReadContentAsInt ()
1201 return XQueryConvert.StringToInt (ReadContentString ());
1202 } catch (FormatException ex) {
1203 throw XmlError ("Typed value is invalid.", ex);
1207 public virtual long ReadContentAsLong ()
1210 return XQueryConvert.StringToInteger (ReadContentString ());
1211 } catch (FormatException ex) {
1212 throw XmlError ("Typed value is invalid.", ex);
1216 public virtual string ReadContentAsString ()
1218 return ReadContentString ();
1221 public virtual int ReadContentAsBase64 (
1222 byte [] buffer, int offset, int length)
1225 return binary.ReadContentAsBase64 (
1226 buffer, offset, length);
1229 public virtual int ReadContentAsBinHex (
1230 byte [] buffer, int offset, int length)
1233 return binary.ReadContentAsBinHex (
1234 buffer, offset, length);
1237 public virtual int ReadElementContentAsBase64 (
1238 byte [] buffer, int offset, int length)
1241 return binary.ReadElementContentAsBase64 (
1242 buffer, offset, length);
1245 public virtual int ReadElementContentAsBinHex (
1246 byte [] buffer, int offset, int length)
1249 return binary.ReadElementContentAsBinHex (
1250 buffer, offset, length);
1255 public virtual int ReadValueChunk (
1256 char [] buffer, int offset, int length)
1258 internal virtual int ReadValueChunk (
1259 char [] buffer, int offset, int length)
1262 if (!CanReadValueChunk)
1263 throw new NotSupportedException ();
1265 binary = new XmlReaderBinarySupport (this);
1266 return binary.ReadValueChunk (buffer, offset, length);
1269 private void CheckSupport ()
1271 // Default implementation expects both.
1272 if (!CanReadBinaryContent || !CanReadValueChunk)
1273 throw new NotSupportedException ();
1275 binary = new XmlReaderBinarySupport (this);
1279 public abstract void ResolveEntity ();
1282 public virtual void Skip ()
1284 if (ReadState != ReadState.Interactive)
1288 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
1294 while (Read () && depth < Depth)
1296 if (NodeType == XmlNodeType.EndElement)
1300 private XmlException XmlError (string message)
1302 return new XmlException (this as IXmlLineInfo, BaseURI, message);
1305 private XmlException XmlError (string message, Exception innerException)
1307 return new XmlException (this as IXmlLineInfo, BaseURI, message);