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;
38 using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
39 using System.Xml.Serialization; // only required for NET_2_0 (SchemaInfo)
40 using Mono.Xml; // only required for NET_2_0
41 using Mono.Xml.Schema; // only required for NET_2_0
46 public abstract class XmlReader : IDisposable
48 public abstract class XmlReader
51 private StringBuilder readStringBuffer;
52 private XmlReaderBinarySupport binary;
54 private XmlReaderSettings settings;
59 protected XmlReader ()
67 public abstract int AttributeCount { get; }
69 public abstract string BaseURI { get; }
71 internal XmlReaderBinarySupport Binary {
72 get { return binary; }
75 internal XmlReaderBinarySupport.CharGetter BinaryCharGetter {
76 get { return binary != null ? binary.Getter : null; }
79 binary = new XmlReaderBinarySupport (this);
80 binary.Getter = value;
85 // To enable it internally in sys.xml, just insert these
86 // two lines into Read():
89 // if (Binary != null)
93 public virtual bool CanReadBinaryContent {
97 public virtual bool CanReadValueChunk {
101 internal virtual bool CanReadBinaryContent {
102 get { return false; }
105 internal virtual bool CanReadValueChunk {
106 get { return false; }
110 public virtual bool CanResolveEntity
112 get { return false; }
115 public abstract int Depth { get; }
117 public abstract bool EOF { get; }
119 public virtual bool HasAttributes
121 get { return AttributeCount > 0; }
124 public abstract bool HasValue { get; }
126 public abstract bool IsEmptyElement { get; }
129 public virtual bool IsDefault {
130 get { return false; }
133 public virtual string this [int i] {
134 get { return GetAttribute (i); }
137 public virtual string this [string name] {
138 get { return GetAttribute (name); }
141 public virtual string this [string name, string namespaceURI] {
142 get { return GetAttribute (name, namespaceURI); }
145 public abstract bool IsDefault { get; }
147 public abstract string this [int i] { get; }
149 public abstract string this [string name] { get; }
151 public abstract string this [string localName, string namespaceName] { get; }
154 public abstract string LocalName { get; }
157 public virtual string Name {
159 return Prefix.Length > 0 ?
160 String.Concat (Prefix, ":", LocalName) :
165 public abstract string Name { get; }
168 public abstract string NamespaceURI { get; }
170 public abstract XmlNameTable NameTable { get; }
172 public abstract XmlNodeType NodeType { get; }
174 public abstract string Prefix { get; }
177 public virtual char QuoteChar {
181 public abstract char QuoteChar { get; }
184 public abstract ReadState ReadState { get; }
187 public virtual IXmlSchemaInfo SchemaInfo {
191 public virtual XmlReaderSettings Settings {
192 get { return settings; }
196 public abstract string Value { get; }
199 public virtual string XmlLang {
200 get { return String.Empty; }
203 public virtual XmlSpace XmlSpace {
204 get { return XmlSpace.None; }
207 public abstract string XmlLang { get; }
209 public abstract XmlSpace XmlSpace { get; }
216 public abstract void Close ();
219 private static XmlNameTable PopulateNameTable (
220 XmlReaderSettings settings)
222 XmlNameTable nameTable = settings.NameTable;
223 if (nameTable == null)
224 nameTable = new NameTable ();
228 private static XmlParserContext PopulateParserContext (
229 XmlReaderSettings settings, string baseUri)
231 XmlNameTable nt = PopulateNameTable (settings);
232 return new XmlParserContext (nt,
233 new XmlNamespaceManager (nt),
238 private static XmlNodeType GetNodeType (
239 XmlReaderSettings settings)
241 ConformanceLevel level = settings != null ? settings.ConformanceLevel : ConformanceLevel.Auto;
243 level == ConformanceLevel.Fragment ?
244 XmlNodeType.Element :
245 XmlNodeType.Document;
248 public static XmlReader Create (Stream stream)
250 return Create (stream, null);
253 public static XmlReader Create (string url)
255 return Create (url, null);
258 public static XmlReader Create (TextReader reader)
260 return Create (reader, null);
263 public static XmlReader Create (string url, XmlReaderSettings settings)
265 return Create (url, settings, null);
268 public static XmlReader Create (Stream stream, XmlReaderSettings settings)
270 return Create (stream, settings, String.Empty);
273 public static XmlReader Create (TextReader reader, XmlReaderSettings settings)
275 return Create (reader, settings, String.Empty);
278 static XmlReaderSettings PopulateSettings (XmlReaderSettings src)
281 return new XmlReaderSettings ();
286 public static XmlReader Create (Stream stream, XmlReaderSettings settings, string baseUri)
288 settings = PopulateSettings (settings);
289 return Create (stream, settings,
290 PopulateParserContext (settings, baseUri));
293 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, string baseUri)
295 settings = PopulateSettings (settings);
296 return Create (reader, settings,
297 PopulateParserContext (settings, baseUri));
300 public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
302 settings = PopulateSettings (settings);
303 XmlReader r = CreateFilteredXmlReader (reader, settings);
304 r.settings = settings;
308 public static XmlReader Create (string url, XmlReaderSettings settings, XmlParserContext context)
310 settings = PopulateSettings (settings);
312 context = PopulateParserContext (settings, url);
313 XmlTextReader xtr = new XmlTextReader (false, settings.XmlResolver, url, GetNodeType (settings), context);
314 XmlReader ret = CreateCustomizedTextReader (xtr, settings);
315 xtr.CloseInput = true; // forced. See XmlReaderCommonTests.CreateFromUrlClose().
319 public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
321 settings = PopulateSettings (settings);
323 context = PopulateParserContext (settings, String.Empty);
324 return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings);
327 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, XmlParserContext context)
329 settings = PopulateSettings (settings);
331 context = PopulateParserContext (settings, String.Empty);
332 return CreateCustomizedTextReader (new XmlTextReader (context.BaseURI, reader, GetNodeType (settings), context), settings);
335 private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlReaderSettings settings)
337 reader.XmlResolver = settings.XmlResolver;
338 // Normalization is set true by default.
339 reader.Normalization = true;
340 reader.EntityHandling = EntityHandling.ExpandEntities;
342 if (settings.ProhibitDtd)
343 reader.ProhibitDtd = true;
345 if (!settings.CheckCharacters)
346 reader.CharacterChecking = false;
348 // I guess it might be changed in 2.0 RTM to set true
349 // as default, or just disappear. It goes against
350 // XmlTextReader's default usage and users will have
351 // to close input manually (that's annoying). Moreover,
352 // MS XmlTextReader consumes text input more than
353 // actually read and users can acquire those extra
354 // consumption by GetRemainder() that returns different
356 reader.CloseInput = settings.CloseInput;
358 // I would like to support it in detail later;
359 // MSDN description looks source of confusion. We don't
360 // need examples, but precise list of how it works.
361 reader.Conformance = settings.ConformanceLevel;
363 reader.AdjustLineInfoOffset (settings.LineNumberOffset,
364 settings.LinePositionOffset);
366 if (settings.NameTable != null)
367 reader.SetNameTable (settings.NameTable);
369 XmlReader r = CreateFilteredXmlReader (reader, settings);
370 r.settings = settings;
374 private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlReaderSettings settings)
376 ConformanceLevel conf = ConformanceLevel.Auto;
377 if (reader is XmlTextReader)
378 conf = ((XmlTextReader) reader).Conformance;
379 else if (reader.Settings != null)
380 conf = reader.Settings.ConformanceLevel;
382 conf = settings.ConformanceLevel;
383 if (settings.ConformanceLevel != ConformanceLevel.Auto &&
384 conf != settings.ConformanceLevel)
385 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));
386 settings.ConformanceLevel = conf;
388 reader = CreateValidatingXmlReader (reader, settings);
390 if (reader.Settings != null &&
391 (settings.IgnoreComments ||
392 settings.IgnoreProcessingInstructions ||
393 settings.IgnoreWhitespace))
394 return new XmlFilterReader (reader, settings);
396 reader.settings = settings;
401 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
403 XmlValidatingReader xvr = null;
404 switch (settings.ValidationType) {
405 // Auto and XDR are obsoleted in 2.0 and therefore ignored.
408 case ValidationType.DTD:
409 xvr = new XmlValidatingReader (reader);
410 xvr.XmlResolver = settings.XmlResolver;
411 xvr.ValidationType = ValidationType.DTD;
413 case ValidationType.Schema:
414 return new XmlSchemaValidatingReader (reader, settings);
417 // Actually I don't think they are treated in DTD validation though...
418 if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
419 throw new NotImplementedException ();
420 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
421 // throw new NotImplementedException ();
422 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
423 // throw new NotImplementedException ();
424 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ReportValidationWarnings) == 0)
425 // throw new NotImplementedException ();
427 return xvr != null ? xvr : reader;
432 void IDisposable.Dispose ()
437 protected virtual void Dispose (bool disposing)
439 if (ReadState != ReadState.Closed)
444 public abstract string GetAttribute (int i);
446 public abstract string GetAttribute (string name);
448 public abstract string GetAttribute (
450 string namespaceName);
452 public static bool IsName (string s)
454 return s != null && XmlChar.IsName (s);
457 public static bool IsNameToken (string s)
459 return s != null && XmlChar.IsNmToken (s);
462 public virtual bool IsStartElement ()
464 return (MoveToContent () == XmlNodeType.Element);
467 public virtual bool IsStartElement (string name)
469 if (!IsStartElement ())
472 return (Name == name);
475 public virtual bool IsStartElement (string localName, string namespaceName)
477 if (!IsStartElement ())
480 return (LocalName == localName && NamespaceURI == namespaceName);
483 public abstract string LookupNamespace (string prefix);
486 public virtual void MoveToAttribute (int i)
488 if (i >= AttributeCount)
489 throw new ArgumentOutOfRangeException ();
490 MoveToFirstAttribute ();
491 for (int a = 0; a < i; a++)
492 MoveToNextAttribute ();
495 public abstract void MoveToAttribute (int i);
498 public abstract bool MoveToAttribute (string name);
500 public abstract bool MoveToAttribute (
502 string namespaceName);
504 private bool IsContent (XmlNodeType nodeType)
507 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
510 case XmlNodeType.Text:
512 case XmlNodeType.CDATA:
514 case XmlNodeType.Element:
516 case XmlNodeType.EndElement:
518 case XmlNodeType.EntityReference:
520 case XmlNodeType.EndEntity:
527 public virtual XmlNodeType MoveToContent ()
529 if (NodeType == XmlNodeType.Attribute)
533 if (IsContent (NodeType))
537 return XmlNodeType.None;
540 public abstract bool MoveToElement ();
542 public abstract bool MoveToFirstAttribute ();
544 public abstract bool MoveToNextAttribute ();
546 public abstract bool Read ();
548 public abstract bool ReadAttributeValue ();
550 public virtual string ReadElementString ()
552 if (MoveToContent () != XmlNodeType.Element) {
553 string error = String.Format ("'{0}' is an invalid node type.",
554 NodeType.ToString ());
555 throw XmlError (error);
558 string result = String.Empty;
559 if (!IsEmptyElement) {
561 result = ReadString ();
562 if (NodeType != XmlNodeType.EndElement) {
563 string error = String.Format ("'{0}' is an invalid node type.",
564 NodeType.ToString ());
565 throw XmlError (error);
573 public virtual string ReadElementString (string name)
575 if (MoveToContent () != XmlNodeType.Element) {
576 string error = String.Format ("'{0}' is an invalid node type.",
577 NodeType.ToString ());
578 throw XmlError (error);
582 string error = String.Format ("The {0} tag from namespace {1} is expected.",
584 throw XmlError (error);
587 string result = String.Empty;
588 if (!IsEmptyElement) {
590 result = ReadString ();
591 if (NodeType != XmlNodeType.EndElement) {
592 string error = String.Format ("'{0}' is an invalid node type.",
593 NodeType.ToString ());
594 throw XmlError (error);
602 public virtual string ReadElementString (string localName, string namespaceName)
604 if (MoveToContent () != XmlNodeType.Element) {
605 string error = String.Format ("'{0}' is an invalid node type.",
606 NodeType.ToString ());
607 throw XmlError (error);
610 if (localName != LocalName || NamespaceURI != namespaceName) {
611 string error = String.Format ("The {0} tag from namespace {1} is expected.",
612 LocalName, NamespaceURI);
613 throw XmlError (error);
616 string result = String.Empty;
617 if (!IsEmptyElement) {
619 result = ReadString ();
620 if (NodeType != XmlNodeType.EndElement) {
621 string error = String.Format ("'{0}' is an invalid node type.",
622 NodeType.ToString ());
623 throw XmlError (error);
631 public virtual void ReadEndElement ()
633 if (MoveToContent () != XmlNodeType.EndElement) {
634 string error = String.Format ("'{0}' is an invalid node type.",
635 NodeType.ToString ());
636 throw XmlError (error);
642 public virtual string ReadInnerXml ()
644 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
647 if (IsEmptyElement) {
651 StringWriter sw = new StringWriter ();
652 XmlTextWriter xtw = new XmlTextWriter (sw);
653 if (NodeType == XmlNodeType.Element) {
654 int startDepth = Depth;
656 while (startDepth < Depth) {
657 if (ReadState != ReadState.Interactive)
658 throw XmlError ("Unexpected end of the XML reader.");
659 xtw.WriteNode (this, false);
661 // reader is now end element, then proceed once more.
665 xtw.WriteNode (this, false);
667 return sw.ToString ();
670 public virtual string ReadOuterXml ()
672 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
676 case XmlNodeType.Element:
677 case XmlNodeType.Attribute:
678 StringWriter sw = new StringWriter ();
679 XmlTextWriter xtw = new XmlTextWriter (sw);
680 xtw.WriteNode (this, false);
681 return sw.ToString ();
688 public virtual void ReadStartElement ()
690 if (MoveToContent () != XmlNodeType.Element) {
691 string error = String.Format ("'{0}' is an invalid node type.",
692 NodeType.ToString ());
693 throw XmlError (error);
699 public virtual void ReadStartElement (string name)
701 if (MoveToContent () != XmlNodeType.Element) {
702 string error = String.Format ("'{0}' is an invalid node type.",
703 NodeType.ToString ());
704 throw XmlError (error);
708 string error = String.Format ("The {0} tag from namespace {1} is expected.",
710 throw XmlError (error);
716 public virtual void ReadStartElement (string localName, string namespaceName)
718 if (MoveToContent () != XmlNodeType.Element) {
719 string error = String.Format ("'{0}' is an invalid node type.",
720 NodeType.ToString ());
721 throw XmlError (error);
724 if (localName != LocalName || NamespaceURI != namespaceName) {
725 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
726 localName, namespaceName,
727 LocalName, NamespaceURI);
728 throw XmlError (error);
734 public virtual string ReadString ()
736 if (readStringBuffer == null)
737 readStringBuffer = new StringBuilder ();
738 readStringBuffer.Length = 0;
745 case XmlNodeType.Element:
751 case XmlNodeType.Text:
752 case XmlNodeType.CDATA:
753 case XmlNodeType.Whitespace:
754 case XmlNodeType.SignificantWhitespace:
755 readStringBuffer.Append (Value);
761 case XmlNodeType.Text:
762 case XmlNodeType.CDATA:
763 case XmlNodeType.Whitespace:
764 case XmlNodeType.SignificantWhitespace:
767 case XmlNodeType.Text:
768 case XmlNodeType.CDATA:
769 case XmlNodeType.Whitespace:
770 case XmlNodeType.SignificantWhitespace:
771 readStringBuffer.Append (Value);
779 string ret = readStringBuffer.ToString ();
780 readStringBuffer.Length = 0;
785 public virtual Type ValueType {
786 get { return typeof (string); }
789 public virtual bool ReadToDescendant (string name)
791 if (ReadState == ReadState.Initial) {
793 if (IsStartElement (name))
796 if (NodeType != XmlNodeType.Element || IsEmptyElement)
799 for (Read (); depth < Depth; Read ())
800 if (NodeType == XmlNodeType.Element && name == Name)
805 public virtual bool ReadToDescendant (string localName, string namespaceURI)
807 if (ReadState == ReadState.Initial) {
809 if (IsStartElement (localName, namespaceURI))
812 if (NodeType != XmlNodeType.Element || IsEmptyElement)
815 for (Read (); depth < Depth; Read ())
816 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
821 public virtual bool ReadToFollowing (string name)
824 if (NodeType == XmlNodeType.Element && name == Name)
829 public virtual bool ReadToFollowing (string localName, string namespaceURI)
832 if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
837 public virtual bool ReadToNextSibling (string name)
839 if (ReadState != ReadState.Interactive)
842 for (Skip (); depth >= Depth; Skip ())
843 if (NodeType == XmlNodeType.Element && name == Name)
848 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
850 if (ReadState != ReadState.Interactive)
853 for (Skip (); depth >= Depth; Skip ())
854 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
859 public virtual XmlReader ReadSubtree ()
861 return new SubtreeXmlReader (this);
864 private string ReadContentString ()
866 if (NodeType == XmlNodeType.Attribute)
868 return ReadContentString (true);
871 private string ReadContentString (bool isText)
875 case XmlNodeType.Text:
876 case XmlNodeType.SignificantWhitespace:
877 case XmlNodeType.Whitespace:
878 case XmlNodeType.CDATA:
881 throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
885 string value = String.Empty;
888 case XmlNodeType.Element:
891 throw XmlError ("Child element is not expected in this operation.");
892 case XmlNodeType.EndElement:
894 case XmlNodeType.Text:
895 case XmlNodeType.CDATA:
896 case XmlNodeType.SignificantWhitespace:
897 case XmlNodeType.Whitespace:
902 throw XmlError ("Unexpected end of document.");
905 string GetLocation ()
907 IXmlLineInfo li = this as IXmlLineInfo;
908 return li != null && li.HasLineInfo () ?
909 String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
913 public virtual object ReadElementContentAsObject ()
915 return ReadElementContentAs (ValueType, null);
919 public virtual object ReadElementContentAsObject (string localName, string namespaceURI)
921 return ReadElementContentAs (ValueType, null, localName, namespaceURI);
925 public virtual object ReadContentAsObject ()
927 return ReadContentAs (ValueType, null);
930 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
932 bool isEmpty = IsEmptyElement;
934 object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
940 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
942 ReadStartElement (localName, namespaceURI);
943 object obj = ReadContentAs (type, resolver);
948 public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
950 return ValueAs (ReadContentString (), type, resolver);
953 private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
956 if (type == typeof (object))
958 if (type == typeof (XmlQualifiedName)) {
959 if (resolver != null)
960 return XmlQualifiedName.Parse (text, resolver);
962 return XmlQualifiedName.Parse (text, this);
965 switch (Type.GetTypeCode (type)) {
966 case TypeCode.Boolean:
967 return XQueryConvert.StringToBoolean (text);
968 case TypeCode.DateTime:
969 return XQueryConvert.StringToDateTime (text);
970 case TypeCode.Decimal:
971 return XQueryConvert.StringToDecimal (text);
972 case TypeCode.Double:
973 return XQueryConvert.StringToDouble (text);
975 return XQueryConvert.StringToInt (text);
977 return XQueryConvert.StringToInteger (text);
978 case TypeCode.Single:
979 return XQueryConvert.StringToFloat (text);
980 case TypeCode.String:
983 } catch (Exception ex) {
984 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);
986 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
989 public virtual bool ReadElementContentAsBoolean ()
992 return XQueryConvert.StringToBoolean (ReadElementContentAsString ());
993 } catch (FormatException ex) {
994 throw XmlError ("Typed value is invalid.", ex);
998 public virtual DateTime ReadElementContentAsDateTime ()
1001 return XQueryConvert.StringToDateTime (ReadElementContentAsString ());
1002 } catch (FormatException ex) {
1003 throw XmlError ("Typed value is invalid.", ex);
1007 public virtual decimal ReadElementContentAsDecimal ()
1010 return XQueryConvert.StringToDecimal (ReadElementContentAsString ());
1011 } catch (FormatException ex) {
1012 throw XmlError ("Typed value is invalid.", ex);
1016 public virtual double ReadElementContentAsDouble ()
1019 return XQueryConvert.StringToDouble (ReadElementContentAsString ());
1020 } catch (FormatException ex) {
1021 throw XmlError ("Typed value is invalid.", ex);
1025 public virtual float ReadElementContentAsFloat ()
1028 return XQueryConvert.StringToFloat (ReadElementContentAsString ());
1029 } catch (FormatException ex) {
1030 throw XmlError ("Typed value is invalid.", ex);
1034 public virtual int ReadElementContentAsInt ()
1037 return XQueryConvert.StringToInt (ReadElementContentAsString ());
1038 } catch (FormatException ex) {
1039 throw XmlError ("Typed value is invalid.", ex);
1043 public virtual long ReadElementContentAsLong ()
1046 return XQueryConvert.StringToInteger (ReadElementContentAsString ());
1047 } catch (FormatException ex) {
1048 throw XmlError ("Typed value is invalid.", ex);
1052 public virtual string ReadElementContentAsString ()
1054 bool isEmpty = IsEmptyElement;
1055 ReadStartElement ();
1057 return String.Empty;
1058 string s = ReadContentString (false);
1063 public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
1066 return XQueryConvert.StringToBoolean (ReadElementContentAsString (localName, namespaceURI));
1067 } catch (FormatException ex) {
1068 throw XmlError ("Typed value is invalid.", ex);
1072 public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
1075 return XQueryConvert.StringToDateTime (ReadElementContentAsString (localName, namespaceURI));
1076 } catch (FormatException ex) {
1077 throw XmlError ("Typed value is invalid.", ex);
1081 public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
1084 return XQueryConvert.StringToDecimal (ReadElementContentAsString (localName, namespaceURI));
1085 } catch (FormatException ex) {
1086 throw XmlError ("Typed value is invalid.", ex);
1090 public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
1093 return XQueryConvert.StringToDouble (ReadElementContentAsString (localName, namespaceURI));
1094 } catch (FormatException ex) {
1095 throw XmlError ("Typed value is invalid.", ex);
1099 public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
1102 return XQueryConvert.StringToFloat (ReadElementContentAsString (localName, namespaceURI));
1103 } catch (FormatException ex) {
1104 throw XmlError ("Typed value is invalid.", ex);
1108 public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
1111 return XQueryConvert.StringToInt (ReadElementContentAsString (localName, namespaceURI));
1112 } catch (FormatException ex) {
1113 throw XmlError ("Typed value is invalid.", ex);
1117 public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
1120 return XQueryConvert.StringToInteger (ReadElementContentAsString (localName, namespaceURI));
1121 } catch (FormatException ex) {
1122 throw XmlError ("Typed value is invalid.", ex);
1126 public virtual string ReadElementContentAsString (string localName, string namespaceURI)
1128 bool isEmpty = IsEmptyElement;
1129 ReadStartElement (localName, namespaceURI);
1131 return String.Empty;
1132 string s = ReadContentString (false);
1137 public virtual bool ReadContentAsBoolean ()
1140 return XQueryConvert.StringToBoolean (ReadContentString ());
1141 } catch (FormatException ex) {
1142 throw XmlError ("Typed value is invalid.", ex);
1146 public virtual DateTime ReadContentAsDateTime ()
1149 return XQueryConvert.StringToDateTime (ReadContentString ());
1150 } catch (FormatException ex) {
1151 throw XmlError ("Typed value is invalid.", ex);
1155 public virtual decimal ReadContentAsDecimal ()
1158 return XQueryConvert.StringToDecimal (ReadContentString ());
1159 } catch (FormatException ex) {
1160 throw XmlError ("Typed value is invalid.", ex);
1164 public virtual double ReadContentAsDouble ()
1167 return XQueryConvert.StringToDouble (ReadContentString ());
1168 } catch (FormatException ex) {
1169 throw XmlError ("Typed value is invalid.", ex);
1173 public virtual float ReadContentAsFloat ()
1176 return XQueryConvert.StringToFloat (ReadContentString ());
1177 } catch (FormatException ex) {
1178 throw XmlError ("Typed value is invalid.", ex);
1182 public virtual int ReadContentAsInt ()
1185 return XQueryConvert.StringToInt (ReadContentString ());
1186 } catch (FormatException ex) {
1187 throw XmlError ("Typed value is invalid.", ex);
1191 public virtual long ReadContentAsLong ()
1194 return XQueryConvert.StringToInteger (ReadContentString ());
1195 } catch (FormatException ex) {
1196 throw XmlError ("Typed value is invalid.", ex);
1200 public virtual string ReadContentAsString ()
1202 return ReadContentString ();
1205 public virtual int ReadContentAsBase64 (
1206 byte [] buffer, int offset, int length)
1209 return binary.ReadContentAsBase64 (
1210 buffer, offset, length);
1213 public virtual int ReadContentAsBinHex (
1214 byte [] buffer, int offset, int length)
1217 return binary.ReadContentAsBinHex (
1218 buffer, offset, length);
1221 public virtual int ReadElementContentAsBase64 (
1222 byte [] buffer, int offset, int length)
1225 return binary.ReadElementContentAsBase64 (
1226 buffer, offset, length);
1229 public virtual int ReadElementContentAsBinHex (
1230 byte [] buffer, int offset, int length)
1233 return binary.ReadElementContentAsBinHex (
1234 buffer, offset, length);
1239 public virtual int ReadValueChunk (
1240 char [] buffer, int offset, int length)
1242 internal virtual int ReadValueChunk (
1243 char [] buffer, int offset, int length)
1246 if (!CanReadValueChunk)
1247 throw new NotSupportedException ();
1249 binary = new XmlReaderBinarySupport (this);
1250 return binary.ReadValueChunk (buffer, offset, length);
1253 private void CheckSupport ()
1255 // Default implementation expects both.
1256 if (!CanReadBinaryContent || !CanReadValueChunk)
1257 throw new NotSupportedException ();
1259 binary = new XmlReaderBinarySupport (this);
1262 public abstract void ResolveEntity ();
1264 public virtual void Skip ()
1266 if (ReadState != ReadState.Interactive)
1270 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
1276 while (Read () && depth < Depth)
1278 if (NodeType == XmlNodeType.EndElement)
1282 private XmlException XmlError (string message)
1284 return new XmlException (this as IXmlLineInfo, BaseURI, message);
1287 private XmlException XmlError (string message, Exception innerException)
1289 return new XmlException (this as IXmlLineInfo, BaseURI, message);