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; }
126 public abstract bool HasValue { get; }
128 public abstract bool IsEmptyElement { get; }
131 public virtual bool IsDefault {
132 get { return false; }
135 public virtual string this [int i] {
136 get { return GetAttribute (i); }
139 public virtual string this [string name] {
140 get { return GetAttribute (name); }
143 public virtual string this [string name, string namespaceURI] {
144 get { return GetAttribute (name, namespaceURI); }
147 public abstract bool IsDefault { get; }
149 public abstract string this [int i] { get; }
151 public abstract string this [string name] { get; }
153 public abstract string this [string localName, string namespaceName] { get; }
156 public abstract string LocalName { get; }
159 public virtual string Name {
161 return Prefix.Length > 0 ?
162 String.Concat (Prefix, ":", LocalName) :
167 public abstract string Name { get; }
170 public abstract string NamespaceURI { get; }
172 public abstract XmlNameTable NameTable { get; }
174 public abstract XmlNodeType NodeType { get; }
176 public abstract string Prefix { get; }
179 public virtual char QuoteChar {
183 public abstract char QuoteChar { get; }
186 public abstract ReadState ReadState { get; }
190 public virtual IXmlSchemaInfo SchemaInfo {
195 public virtual XmlReaderSettings Settings {
196 get { return settings; }
200 public abstract string Value { get; }
203 public virtual string XmlLang {
204 get { return String.Empty; }
207 public virtual XmlSpace XmlSpace {
208 get { return XmlSpace.None; }
211 public abstract string XmlLang { get; }
213 public abstract XmlSpace XmlSpace { get; }
220 public abstract void Close ();
223 private static XmlNameTable PopulateNameTable (
224 XmlReaderSettings settings)
226 XmlNameTable nameTable = settings.NameTable;
227 if (nameTable == null)
228 nameTable = new NameTable ();
232 private static XmlParserContext PopulateParserContext (
233 XmlReaderSettings settings, string baseUri)
235 XmlNameTable nt = PopulateNameTable (settings);
236 return new XmlParserContext (nt,
237 new XmlNamespaceManager (nt),
242 private static XmlNodeType GetNodeType (
243 XmlReaderSettings settings)
245 ConformanceLevel level = settings != null ? settings.ConformanceLevel : ConformanceLevel.Auto;
247 level == ConformanceLevel.Fragment ?
248 XmlNodeType.Element :
249 XmlNodeType.Document;
252 public static XmlReader Create (Stream stream)
254 return Create (stream, null);
257 public static XmlReader Create (string url)
259 return Create (url, null);
262 public static XmlReader Create (TextReader reader)
264 return Create (reader, null);
267 public static XmlReader Create (string url, XmlReaderSettings settings)
269 return Create (url, settings, null);
272 public static XmlReader Create (Stream stream, XmlReaderSettings settings)
274 return Create (stream, settings, String.Empty);
277 public static XmlReader Create (TextReader reader, XmlReaderSettings settings)
279 return Create (reader, settings, String.Empty);
282 static XmlReaderSettings PopulateSettings (XmlReaderSettings src)
285 return new XmlReaderSettings ();
290 public static XmlReader Create (Stream stream, XmlReaderSettings settings, string baseUri)
292 settings = PopulateSettings (settings);
293 return Create (stream, settings,
294 PopulateParserContext (settings, baseUri));
297 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, string baseUri)
299 settings = PopulateSettings (settings);
300 return Create (reader, settings,
301 PopulateParserContext (settings, baseUri));
304 public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
306 settings = PopulateSettings (settings);
307 XmlReader r = CreateFilteredXmlReader (reader, settings);
308 r.settings = settings;
312 public static XmlReader Create (string url, XmlReaderSettings settings, XmlParserContext context)
314 settings = PopulateSettings (settings);
316 context = PopulateParserContext (settings, url);
317 XmlTextReader xtr = new XmlTextReader (false, settings.XmlResolver, url, GetNodeType (settings), context);
318 XmlReader ret = CreateCustomizedTextReader (xtr, settings);
319 xtr.CloseInput = true; // forced. See XmlReaderCommonTests.CreateFromUrlClose().
323 public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
325 settings = PopulateSettings (settings);
327 context = PopulateParserContext (settings, String.Empty);
328 return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings);
331 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, XmlParserContext context)
333 settings = PopulateSettings (settings);
335 context = PopulateParserContext (settings, String.Empty);
336 return CreateCustomizedTextReader (new XmlTextReader (context.BaseURI, reader, GetNodeType (settings), context), settings);
339 private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlReaderSettings settings)
341 reader.XmlResolver = settings.XmlResolver;
342 // Normalization is set true by default.
343 reader.Normalization = true;
344 reader.EntityHandling = EntityHandling.ExpandEntities;
346 if (settings.ProhibitDtd)
347 reader.ProhibitDtd = true;
349 if (!settings.CheckCharacters)
350 reader.CharacterChecking = false;
352 // I guess it might be changed in 2.0 RTM to set true
353 // as default, or just disappear. It goes against
354 // XmlTextReader's default usage and users will have
355 // to close input manually (that's annoying). Moreover,
356 // MS XmlTextReader consumes text input more than
357 // actually read and users can acquire those extra
358 // consumption by GetRemainder() that returns different
360 reader.CloseInput = settings.CloseInput;
362 // I would like to support it in detail later;
363 // MSDN description looks source of confusion. We don't
364 // need examples, but precise list of how it works.
365 reader.Conformance = settings.ConformanceLevel;
367 reader.AdjustLineInfoOffset (settings.LineNumberOffset,
368 settings.LinePositionOffset);
370 if (settings.NameTable != null)
371 reader.SetNameTable (settings.NameTable);
373 XmlReader r = CreateFilteredXmlReader (reader, settings);
374 r.settings = settings;
378 private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlReaderSettings settings)
380 ConformanceLevel conf = ConformanceLevel.Auto;
381 if (reader is XmlTextReader)
382 conf = ((XmlTextReader) reader).Conformance;
383 else if (reader.Settings != null)
384 conf = reader.Settings.ConformanceLevel;
386 conf = settings.ConformanceLevel;
387 if (settings.ConformanceLevel != ConformanceLevel.Auto &&
388 conf != settings.ConformanceLevel)
389 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));
390 settings.ConformanceLevel = conf;
392 reader = CreateValidatingXmlReader (reader, settings);
394 if (reader.Settings != null &&
395 (settings.IgnoreComments ||
396 settings.IgnoreProcessingInstructions ||
397 settings.IgnoreWhitespace))
398 return new XmlFilterReader (reader, settings);
400 reader.settings = settings;
405 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
410 XmlValidatingReader xvr = null;
411 switch (settings.ValidationType) {
412 // Auto and XDR are obsoleted in 2.0 and therefore ignored.
415 case ValidationType.DTD:
416 xvr = new XmlValidatingReader (reader);
417 xvr.XmlResolver = settings.XmlResolver;
418 xvr.ValidationType = ValidationType.DTD;
420 case ValidationType.Schema:
421 return new XmlSchemaValidatingReader (reader, settings);
424 // Actually I don't think they are treated in DTD validation though...
425 if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
426 throw new NotImplementedException ();
427 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
428 // throw new NotImplementedException ();
429 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
430 // throw new NotImplementedException ();
431 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ReportValidationWarnings) == 0)
432 // throw new NotImplementedException ();
434 return xvr != null ? xvr : reader;
438 void IDisposable.Dispose ()
443 protected virtual void Dispose (bool disposing)
445 if (ReadState != ReadState.Closed)
450 public abstract string GetAttribute (int i);
452 public abstract string GetAttribute (string name);
454 public abstract string GetAttribute (
456 string namespaceName);
458 public static bool IsName (string s)
460 return s != null && XmlChar.IsName (s);
463 public static bool IsNameToken (string s)
465 return s != null && XmlChar.IsNmToken (s);
468 public virtual bool IsStartElement ()
470 return (MoveToContent () == XmlNodeType.Element);
473 public virtual bool IsStartElement (string name)
475 if (!IsStartElement ())
478 return (Name == name);
481 public virtual bool IsStartElement (string localName, string namespaceName)
483 if (!IsStartElement ())
486 return (LocalName == localName && NamespaceURI == namespaceName);
489 public abstract string LookupNamespace (string prefix);
492 public virtual void MoveToAttribute (int i)
494 if (i >= AttributeCount)
495 throw new ArgumentOutOfRangeException ();
496 MoveToFirstAttribute ();
497 for (int a = 0; a < i; a++)
498 MoveToNextAttribute ();
501 public abstract void MoveToAttribute (int i);
504 public abstract bool MoveToAttribute (string name);
506 public abstract bool MoveToAttribute (
508 string namespaceName);
510 private bool IsContent (XmlNodeType nodeType)
513 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
516 case XmlNodeType.Text:
518 case XmlNodeType.CDATA:
520 case XmlNodeType.Element:
522 case XmlNodeType.EndElement:
524 case XmlNodeType.EntityReference:
526 case XmlNodeType.EndEntity:
533 public virtual XmlNodeType MoveToContent ()
535 if (NodeType == XmlNodeType.Attribute)
539 if (IsContent (NodeType))
543 return XmlNodeType.None;
546 public abstract bool MoveToElement ();
548 public abstract bool MoveToFirstAttribute ();
550 public abstract bool MoveToNextAttribute ();
552 public abstract bool Read ();
554 public abstract bool ReadAttributeValue ();
556 public virtual string ReadElementString ()
558 if (MoveToContent () != XmlNodeType.Element) {
559 string error = String.Format ("'{0}' is an invalid node type.",
560 NodeType.ToString ());
561 throw XmlError (error);
564 string result = String.Empty;
565 if (!IsEmptyElement) {
567 result = ReadString ();
568 if (NodeType != XmlNodeType.EndElement) {
569 string error = String.Format ("'{0}' is an invalid node type.",
570 NodeType.ToString ());
571 throw XmlError (error);
579 public virtual string ReadElementString (string name)
581 if (MoveToContent () != XmlNodeType.Element) {
582 string error = String.Format ("'{0}' is an invalid node type.",
583 NodeType.ToString ());
584 throw XmlError (error);
588 string error = String.Format ("The {0} tag from namespace {1} is expected.",
590 throw XmlError (error);
593 string result = String.Empty;
594 if (!IsEmptyElement) {
596 result = ReadString ();
597 if (NodeType != XmlNodeType.EndElement) {
598 string error = String.Format ("'{0}' is an invalid node type.",
599 NodeType.ToString ());
600 throw XmlError (error);
608 public virtual string ReadElementString (string localName, string namespaceName)
610 if (MoveToContent () != XmlNodeType.Element) {
611 string error = String.Format ("'{0}' is an invalid node type.",
612 NodeType.ToString ());
613 throw XmlError (error);
616 if (localName != LocalName || NamespaceURI != namespaceName) {
617 string error = String.Format ("The {0} tag from namespace {1} is expected.",
618 LocalName, NamespaceURI);
619 throw XmlError (error);
622 string result = String.Empty;
623 if (!IsEmptyElement) {
625 result = ReadString ();
626 if (NodeType != XmlNodeType.EndElement) {
627 string error = String.Format ("'{0}' is an invalid node type.",
628 NodeType.ToString ());
629 throw XmlError (error);
637 public virtual void ReadEndElement ()
639 if (MoveToContent () != XmlNodeType.EndElement) {
640 string error = String.Format ("'{0}' is an invalid node type.",
641 NodeType.ToString ());
642 throw XmlError (error);
648 public virtual string ReadInnerXml ()
650 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
653 if (IsEmptyElement) {
657 StringWriter sw = new StringWriter ();
658 XmlTextWriter xtw = new XmlTextWriter (sw);
659 if (NodeType == XmlNodeType.Element) {
660 int startDepth = Depth;
662 while (startDepth < Depth) {
663 if (ReadState != ReadState.Interactive)
664 throw XmlError ("Unexpected end of the XML reader.");
665 xtw.WriteNode (this, false);
667 // reader is now end element, then proceed once more.
671 xtw.WriteNode (this, false);
673 return sw.ToString ();
676 public virtual string ReadOuterXml ()
678 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
682 case XmlNodeType.Element:
683 case XmlNodeType.Attribute:
684 StringWriter sw = new StringWriter ();
685 XmlTextWriter xtw = new XmlTextWriter (sw);
686 xtw.WriteNode (this, false);
687 return sw.ToString ();
694 public virtual void ReadStartElement ()
696 if (MoveToContent () != XmlNodeType.Element) {
697 string error = String.Format ("'{0}' is an invalid node type.",
698 NodeType.ToString ());
699 throw XmlError (error);
705 public virtual void ReadStartElement (string name)
707 if (MoveToContent () != XmlNodeType.Element) {
708 string error = String.Format ("'{0}' is an invalid node type.",
709 NodeType.ToString ());
710 throw XmlError (error);
714 string error = String.Format ("The {0} tag from namespace {1} is expected.",
716 throw XmlError (error);
722 public virtual void ReadStartElement (string localName, string namespaceName)
724 if (MoveToContent () != XmlNodeType.Element) {
725 string error = String.Format ("'{0}' is an invalid node type.",
726 NodeType.ToString ());
727 throw XmlError (error);
730 if (localName != LocalName || NamespaceURI != namespaceName) {
731 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
732 localName, namespaceName,
733 LocalName, NamespaceURI);
734 throw XmlError (error);
740 public virtual string ReadString ()
742 if (readStringBuffer == null)
743 readStringBuffer = new StringBuilder ();
744 readStringBuffer.Length = 0;
751 case XmlNodeType.Element:
757 case XmlNodeType.Text:
758 case XmlNodeType.CDATA:
759 case XmlNodeType.Whitespace:
760 case XmlNodeType.SignificantWhitespace:
761 readStringBuffer.Append (Value);
767 case XmlNodeType.Text:
768 case XmlNodeType.CDATA:
769 case XmlNodeType.Whitespace:
770 case XmlNodeType.SignificantWhitespace:
773 case XmlNodeType.Text:
774 case XmlNodeType.CDATA:
775 case XmlNodeType.Whitespace:
776 case XmlNodeType.SignificantWhitespace:
777 readStringBuffer.Append (Value);
785 string ret = readStringBuffer.ToString ();
786 readStringBuffer.Length = 0;
791 public virtual Type ValueType {
792 get { return typeof (string); }
795 public virtual bool ReadToDescendant (string name)
797 if (ReadState == ReadState.Initial) {
799 if (IsStartElement (name))
802 if (NodeType != XmlNodeType.Element || IsEmptyElement)
805 for (Read (); depth < Depth; Read ())
806 if (NodeType == XmlNodeType.Element && name == Name)
811 public virtual bool ReadToDescendant (string localName, string namespaceURI)
813 if (ReadState == ReadState.Initial) {
815 if (IsStartElement (localName, namespaceURI))
818 if (NodeType != XmlNodeType.Element || IsEmptyElement)
821 for (Read (); depth < Depth; Read ())
822 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
827 public virtual bool ReadToFollowing (string name)
830 if (NodeType == XmlNodeType.Element && name == Name)
835 public virtual bool ReadToFollowing (string localName, string namespaceURI)
838 if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
843 public virtual bool ReadToNextSibling (string name)
845 if (ReadState != ReadState.Interactive)
848 for (Skip (); depth <= Depth; Skip ())
849 if (NodeType == XmlNodeType.Element && name == Name)
854 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
856 if (ReadState != ReadState.Interactive)
859 for (Skip (); depth <= Depth; Skip ())
860 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
865 public virtual XmlReader ReadSubtree ()
867 return new SubtreeXmlReader (this);
870 private string ReadContentString ()
872 if (NodeType == XmlNodeType.Attribute)
874 return ReadContentString (true);
877 private string ReadContentString (bool isText)
881 case XmlNodeType.Text:
882 case XmlNodeType.SignificantWhitespace:
883 case XmlNodeType.Whitespace:
884 case XmlNodeType.CDATA:
887 throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
891 string value = String.Empty;
894 case XmlNodeType.Element:
897 throw XmlError ("Child element is not expected in this operation.");
898 case XmlNodeType.EndElement:
900 case XmlNodeType.Text:
901 case XmlNodeType.CDATA:
902 case XmlNodeType.SignificantWhitespace:
903 case XmlNodeType.Whitespace:
908 throw XmlError ("Unexpected end of document.");
911 string GetLocation ()
913 IXmlLineInfo li = this as IXmlLineInfo;
914 return li != null && li.HasLineInfo () ?
915 String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
919 public virtual object ReadElementContentAsObject ()
921 return ReadElementContentAs (ValueType, null);
925 public virtual object ReadElementContentAsObject (string localName, string namespaceURI)
927 return ReadElementContentAs (ValueType, null, localName, namespaceURI);
931 public virtual object ReadContentAsObject ()
933 return ReadContentAs (ValueType, null);
936 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
938 bool isEmpty = IsEmptyElement;
940 object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
946 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
948 ReadStartElement (localName, namespaceURI);
949 object obj = ReadContentAs (type, resolver);
954 public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
956 return ValueAs (ReadContentString (), type, resolver);
959 private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
962 if (type == typeof (object))
964 if (type == typeof (XmlQualifiedName)) {
965 if (resolver != null)
966 return XmlQualifiedName.Parse (text, resolver);
968 return XmlQualifiedName.Parse (text, this);
971 switch (Type.GetTypeCode (type)) {
972 case TypeCode.Boolean:
973 return XQueryConvert.StringToBoolean (text);
974 case TypeCode.DateTime:
975 return XQueryConvert.StringToDateTime (text);
976 case TypeCode.Decimal:
977 return XQueryConvert.StringToDecimal (text);
978 case TypeCode.Double:
979 return XQueryConvert.StringToDouble (text);
981 return XQueryConvert.StringToInt (text);
983 return XQueryConvert.StringToInteger (text);
984 case TypeCode.Single:
985 return XQueryConvert.StringToFloat (text);
986 case TypeCode.String:
989 } catch (Exception ex) {
990 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);
992 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
995 public virtual bool ReadElementContentAsBoolean ()
998 return XQueryConvert.StringToBoolean (ReadElementContentAsString ());
999 } catch (FormatException ex) {
1000 throw XmlError ("Typed value is invalid.", ex);
1004 public virtual DateTime ReadElementContentAsDateTime ()
1007 return XQueryConvert.StringToDateTime (ReadElementContentAsString ());
1008 } catch (FormatException ex) {
1009 throw XmlError ("Typed value is invalid.", ex);
1013 public virtual decimal ReadElementContentAsDecimal ()
1016 return XQueryConvert.StringToDecimal (ReadElementContentAsString ());
1017 } catch (FormatException ex) {
1018 throw XmlError ("Typed value is invalid.", ex);
1022 public virtual double ReadElementContentAsDouble ()
1025 return XQueryConvert.StringToDouble (ReadElementContentAsString ());
1026 } catch (FormatException ex) {
1027 throw XmlError ("Typed value is invalid.", ex);
1031 public virtual float ReadElementContentAsFloat ()
1034 return XQueryConvert.StringToFloat (ReadElementContentAsString ());
1035 } catch (FormatException ex) {
1036 throw XmlError ("Typed value is invalid.", ex);
1040 public virtual int ReadElementContentAsInt ()
1043 return XQueryConvert.StringToInt (ReadElementContentAsString ());
1044 } catch (FormatException ex) {
1045 throw XmlError ("Typed value is invalid.", ex);
1049 public virtual long ReadElementContentAsLong ()
1052 return XQueryConvert.StringToInteger (ReadElementContentAsString ());
1053 } catch (FormatException ex) {
1054 throw XmlError ("Typed value is invalid.", ex);
1058 public virtual string ReadElementContentAsString ()
1060 bool isEmpty = IsEmptyElement;
1061 ReadStartElement ();
1063 return String.Empty;
1064 string s = ReadContentString (false);
1069 public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
1072 return XQueryConvert.StringToBoolean (ReadElementContentAsString (localName, namespaceURI));
1073 } catch (FormatException ex) {
1074 throw XmlError ("Typed value is invalid.", ex);
1078 public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
1081 return XQueryConvert.StringToDateTime (ReadElementContentAsString (localName, namespaceURI));
1082 } catch (FormatException ex) {
1083 throw XmlError ("Typed value is invalid.", ex);
1087 public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
1090 return XQueryConvert.StringToDecimal (ReadElementContentAsString (localName, namespaceURI));
1091 } catch (FormatException ex) {
1092 throw XmlError ("Typed value is invalid.", ex);
1096 public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
1099 return XQueryConvert.StringToDouble (ReadElementContentAsString (localName, namespaceURI));
1100 } catch (FormatException ex) {
1101 throw XmlError ("Typed value is invalid.", ex);
1105 public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
1108 return XQueryConvert.StringToFloat (ReadElementContentAsString (localName, namespaceURI));
1109 } catch (FormatException ex) {
1110 throw XmlError ("Typed value is invalid.", ex);
1114 public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
1117 return XQueryConvert.StringToInt (ReadElementContentAsString (localName, namespaceURI));
1118 } catch (FormatException ex) {
1119 throw XmlError ("Typed value is invalid.", ex);
1123 public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
1126 return XQueryConvert.StringToInteger (ReadElementContentAsString (localName, namespaceURI));
1127 } catch (FormatException ex) {
1128 throw XmlError ("Typed value is invalid.", ex);
1132 public virtual string ReadElementContentAsString (string localName, string namespaceURI)
1134 bool isEmpty = IsEmptyElement;
1135 ReadStartElement (localName, namespaceURI);
1137 return String.Empty;
1138 string s = ReadContentString (false);
1143 public virtual bool ReadContentAsBoolean ()
1146 return XQueryConvert.StringToBoolean (ReadContentString ());
1147 } catch (FormatException ex) {
1148 throw XmlError ("Typed value is invalid.", ex);
1152 public virtual DateTime ReadContentAsDateTime ()
1155 return XQueryConvert.StringToDateTime (ReadContentString ());
1156 } catch (FormatException ex) {
1157 throw XmlError ("Typed value is invalid.", ex);
1161 public virtual decimal ReadContentAsDecimal ()
1164 return XQueryConvert.StringToDecimal (ReadContentString ());
1165 } catch (FormatException ex) {
1166 throw XmlError ("Typed value is invalid.", ex);
1170 public virtual double ReadContentAsDouble ()
1173 return XQueryConvert.StringToDouble (ReadContentString ());
1174 } catch (FormatException ex) {
1175 throw XmlError ("Typed value is invalid.", ex);
1179 public virtual float ReadContentAsFloat ()
1182 return XQueryConvert.StringToFloat (ReadContentString ());
1183 } catch (FormatException ex) {
1184 throw XmlError ("Typed value is invalid.", ex);
1188 public virtual int ReadContentAsInt ()
1191 return XQueryConvert.StringToInt (ReadContentString ());
1192 } catch (FormatException ex) {
1193 throw XmlError ("Typed value is invalid.", ex);
1197 public virtual long ReadContentAsLong ()
1200 return XQueryConvert.StringToInteger (ReadContentString ());
1201 } catch (FormatException ex) {
1202 throw XmlError ("Typed value is invalid.", ex);
1206 public virtual string ReadContentAsString ()
1208 return ReadContentString ();
1211 public virtual int ReadContentAsBase64 (
1212 byte [] buffer, int offset, int length)
1215 return binary.ReadContentAsBase64 (
1216 buffer, offset, length);
1219 public virtual int ReadContentAsBinHex (
1220 byte [] buffer, int offset, int length)
1223 return binary.ReadContentAsBinHex (
1224 buffer, offset, length);
1227 public virtual int ReadElementContentAsBase64 (
1228 byte [] buffer, int offset, int length)
1231 return binary.ReadElementContentAsBase64 (
1232 buffer, offset, length);
1235 public virtual int ReadElementContentAsBinHex (
1236 byte [] buffer, int offset, int length)
1239 return binary.ReadElementContentAsBinHex (
1240 buffer, offset, length);
1245 public virtual int ReadValueChunk (
1246 char [] buffer, int offset, int length)
1248 internal virtual int ReadValueChunk (
1249 char [] buffer, int offset, int length)
1252 if (!CanReadValueChunk)
1253 throw new NotSupportedException ();
1255 binary = new XmlReaderBinarySupport (this);
1256 return binary.ReadValueChunk (buffer, offset, length);
1259 private void CheckSupport ()
1261 // Default implementation expects both.
1262 if (!CanReadBinaryContent || !CanReadValueChunk)
1263 throw new NotSupportedException ();
1265 binary = new XmlReaderBinarySupport (this);
1268 public abstract void ResolveEntity ();
1270 public virtual void Skip ()
1272 if (ReadState != ReadState.Interactive)
1276 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
1282 while (Read () && depth < Depth)
1284 if (NodeType == XmlNodeType.EndElement)
1288 private XmlException XmlError (string message)
1290 return new XmlException (this as IXmlLineInfo, BaseURI, message);
1293 private XmlException XmlError (string message, Exception innerException)
1295 return new XmlException (this as IXmlLineInfo, BaseURI, message);