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 ( settings.IgnoreComments ||
395 settings.IgnoreProcessingInstructions ||
396 settings.IgnoreWhitespace)
397 return new XmlFilterReader (reader, settings);
399 reader.settings = settings;
404 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
409 XmlValidatingReader xvr = null;
410 switch (settings.ValidationType) {
411 // Auto and XDR are obsoleted in 2.0 and therefore ignored.
414 case ValidationType.DTD:
415 xvr = new XmlValidatingReader (reader);
416 xvr.XmlResolver = settings.XmlResolver;
417 xvr.ValidationType = ValidationType.DTD;
419 case ValidationType.Schema:
420 return new XmlSchemaValidatingReader (reader, settings);
423 // Actually I don't think they are treated in DTD validation though...
424 if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
425 throw new NotImplementedException ();
426 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
427 // throw new NotImplementedException ();
428 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
429 // throw new NotImplementedException ();
430 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ReportValidationWarnings) == 0)
431 // throw new NotImplementedException ();
433 return xvr != null ? xvr : reader;
437 void IDisposable.Dispose ()
442 protected virtual void Dispose (bool disposing)
444 if (ReadState != ReadState.Closed)
449 public abstract string GetAttribute (int i);
451 public abstract string GetAttribute (string name);
453 public abstract string GetAttribute (
455 string namespaceName);
457 public static bool IsName (string s)
459 return s != null && XmlChar.IsName (s);
462 public static bool IsNameToken (string s)
464 return s != null && XmlChar.IsNmToken (s);
467 public virtual bool IsStartElement ()
469 return (MoveToContent () == XmlNodeType.Element);
472 public virtual bool IsStartElement (string name)
474 if (!IsStartElement ())
477 return (Name == name);
480 public virtual bool IsStartElement (string localName, string namespaceName)
482 if (!IsStartElement ())
485 return (LocalName == localName && NamespaceURI == namespaceName);
488 public abstract string LookupNamespace (string prefix);
491 public virtual void MoveToAttribute (int i)
493 if (i >= AttributeCount)
494 throw new ArgumentOutOfRangeException ();
495 MoveToFirstAttribute ();
496 for (int a = 0; a < i; a++)
497 MoveToNextAttribute ();
500 public abstract void MoveToAttribute (int i);
503 public abstract bool MoveToAttribute (string name);
505 public abstract bool MoveToAttribute (
507 string namespaceName);
509 private bool IsContent (XmlNodeType nodeType)
512 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
515 case XmlNodeType.Text:
517 case XmlNodeType.CDATA:
519 case XmlNodeType.Element:
521 case XmlNodeType.EndElement:
523 case XmlNodeType.EntityReference:
525 case XmlNodeType.EndEntity:
532 public virtual XmlNodeType MoveToContent ()
534 if (NodeType == XmlNodeType.Attribute)
538 if (IsContent (NodeType))
542 return XmlNodeType.None;
545 public abstract bool MoveToElement ();
547 public abstract bool MoveToFirstAttribute ();
549 public abstract bool MoveToNextAttribute ();
551 public abstract bool Read ();
553 public abstract bool ReadAttributeValue ();
555 public virtual string ReadElementString ()
557 if (MoveToContent () != XmlNodeType.Element) {
558 string error = String.Format ("'{0}' is an invalid node type.",
559 NodeType.ToString ());
560 throw XmlError (error);
563 string result = String.Empty;
564 if (!IsEmptyElement) {
566 result = ReadString ();
567 if (NodeType != XmlNodeType.EndElement) {
568 string error = String.Format ("'{0}' is an invalid node type.",
569 NodeType.ToString ());
570 throw XmlError (error);
578 public virtual string ReadElementString (string name)
580 if (MoveToContent () != XmlNodeType.Element) {
581 string error = String.Format ("'{0}' is an invalid node type.",
582 NodeType.ToString ());
583 throw XmlError (error);
587 string error = String.Format ("The {0} tag from namespace {1} is expected.",
589 throw XmlError (error);
592 string result = String.Empty;
593 if (!IsEmptyElement) {
595 result = ReadString ();
596 if (NodeType != XmlNodeType.EndElement) {
597 string error = String.Format ("'{0}' is an invalid node type.",
598 NodeType.ToString ());
599 throw XmlError (error);
607 public virtual string ReadElementString (string localName, string namespaceName)
609 if (MoveToContent () != XmlNodeType.Element) {
610 string error = String.Format ("'{0}' is an invalid node type.",
611 NodeType.ToString ());
612 throw XmlError (error);
615 if (localName != LocalName || NamespaceURI != namespaceName) {
616 string error = String.Format ("The {0} tag from namespace {1} is expected.",
617 LocalName, NamespaceURI);
618 throw XmlError (error);
621 string result = String.Empty;
622 if (!IsEmptyElement) {
624 result = ReadString ();
625 if (NodeType != XmlNodeType.EndElement) {
626 string error = String.Format ("'{0}' is an invalid node type.",
627 NodeType.ToString ());
628 throw XmlError (error);
636 public virtual void ReadEndElement ()
638 if (MoveToContent () != XmlNodeType.EndElement) {
639 string error = String.Format ("'{0}' is an invalid node type.",
640 NodeType.ToString ());
641 throw XmlError (error);
647 public virtual string ReadInnerXml ()
649 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
652 if (IsEmptyElement) {
656 StringWriter sw = new StringWriter ();
657 XmlTextWriter xtw = new XmlTextWriter (sw);
658 if (NodeType == XmlNodeType.Element) {
659 int startDepth = Depth;
661 while (startDepth < Depth) {
662 if (ReadState != ReadState.Interactive)
663 throw XmlError ("Unexpected end of the XML reader.");
664 xtw.WriteNode (this, false);
666 // reader is now end element, then proceed once more.
670 xtw.WriteNode (this, false);
672 return sw.ToString ();
675 public virtual string ReadOuterXml ()
677 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
681 case XmlNodeType.Element:
682 case XmlNodeType.Attribute:
683 StringWriter sw = new StringWriter ();
684 XmlTextWriter xtw = new XmlTextWriter (sw);
685 xtw.WriteNode (this, false);
686 return sw.ToString ();
693 public virtual void ReadStartElement ()
695 if (MoveToContent () != XmlNodeType.Element) {
696 string error = String.Format ("'{0}' is an invalid node type.",
697 NodeType.ToString ());
698 throw XmlError (error);
704 public virtual void ReadStartElement (string name)
706 if (MoveToContent () != XmlNodeType.Element) {
707 string error = String.Format ("'{0}' is an invalid node type.",
708 NodeType.ToString ());
709 throw XmlError (error);
713 string error = String.Format ("The {0} tag from namespace {1} is expected.",
715 throw XmlError (error);
721 public virtual void ReadStartElement (string localName, string namespaceName)
723 if (MoveToContent () != XmlNodeType.Element) {
724 string error = String.Format ("'{0}' is an invalid node type.",
725 NodeType.ToString ());
726 throw XmlError (error);
729 if (localName != LocalName || NamespaceURI != namespaceName) {
730 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
731 localName, namespaceName,
732 LocalName, NamespaceURI);
733 throw XmlError (error);
739 public virtual string ReadString ()
741 if (readStringBuffer == null)
742 readStringBuffer = new StringBuilder ();
743 readStringBuffer.Length = 0;
750 case XmlNodeType.Element:
756 case XmlNodeType.Text:
757 case XmlNodeType.CDATA:
758 case XmlNodeType.Whitespace:
759 case XmlNodeType.SignificantWhitespace:
760 readStringBuffer.Append (Value);
766 case XmlNodeType.Text:
767 case XmlNodeType.CDATA:
768 case XmlNodeType.Whitespace:
769 case XmlNodeType.SignificantWhitespace:
772 case XmlNodeType.Text:
773 case XmlNodeType.CDATA:
774 case XmlNodeType.Whitespace:
775 case XmlNodeType.SignificantWhitespace:
776 readStringBuffer.Append (Value);
784 string ret = readStringBuffer.ToString ();
785 readStringBuffer.Length = 0;
790 public virtual Type ValueType {
791 get { return typeof (string); }
794 public virtual bool ReadToDescendant (string name)
796 if (ReadState == ReadState.Initial) {
798 if (IsStartElement (name))
801 if (NodeType != XmlNodeType.Element || IsEmptyElement)
804 for (Read (); depth < Depth; Read ())
805 if (NodeType == XmlNodeType.Element && name == Name)
810 public virtual bool ReadToDescendant (string localName, string namespaceURI)
812 if (ReadState == ReadState.Initial) {
814 if (IsStartElement (localName, namespaceURI))
817 if (NodeType != XmlNodeType.Element || IsEmptyElement)
820 for (Read (); depth < Depth; Read ())
821 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
826 public virtual bool ReadToFollowing (string name)
829 if (NodeType == XmlNodeType.Element && name == Name)
834 public virtual bool ReadToFollowing (string localName, string namespaceURI)
837 if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
842 public virtual bool ReadToNextSibling (string name)
844 if (ReadState != ReadState.Interactive)
847 for (Skip (); depth <= Depth; Skip ())
848 if (NodeType == XmlNodeType.Element && name == Name)
853 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
855 if (ReadState != ReadState.Interactive)
858 for (Skip (); depth <= Depth; Skip ())
859 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
864 public virtual XmlReader ReadSubtree ()
866 return new SubtreeXmlReader (this);
869 private string ReadContentString ()
871 if (NodeType == XmlNodeType.Attribute)
873 return ReadContentString (true);
876 private string ReadContentString (bool isText)
880 case XmlNodeType.Text:
881 case XmlNodeType.SignificantWhitespace:
882 case XmlNodeType.Whitespace:
883 case XmlNodeType.CDATA:
886 throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
890 string value = String.Empty;
893 case XmlNodeType.Element:
896 throw XmlError ("Child element is not expected in this operation.");
897 case XmlNodeType.EndElement:
899 case XmlNodeType.Text:
900 case XmlNodeType.CDATA:
901 case XmlNodeType.SignificantWhitespace:
902 case XmlNodeType.Whitespace:
907 throw XmlError ("Unexpected end of document.");
910 string GetLocation ()
912 IXmlLineInfo li = this as IXmlLineInfo;
913 return li != null && li.HasLineInfo () ?
914 String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
918 public virtual object ReadElementContentAsObject ()
920 return ReadElementContentAs (ValueType, null);
924 public virtual object ReadElementContentAsObject (string localName, string namespaceURI)
926 return ReadElementContentAs (ValueType, null, localName, namespaceURI);
930 public virtual object ReadContentAsObject ()
932 return ReadContentAs (ValueType, null);
935 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
937 bool isEmpty = IsEmptyElement;
939 object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
945 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
947 ReadStartElement (localName, namespaceURI);
948 object obj = ReadContentAs (type, resolver);
953 public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
955 return ValueAs (ReadContentString (), type, resolver);
958 private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
961 if (type == typeof (object))
963 if (type == typeof (XmlQualifiedName)) {
964 if (resolver != null)
965 return XmlQualifiedName.Parse (text, resolver);
967 return XmlQualifiedName.Parse (text, this);
970 switch (Type.GetTypeCode (type)) {
971 case TypeCode.Boolean:
972 return XQueryConvert.StringToBoolean (text);
973 case TypeCode.DateTime:
974 return XQueryConvert.StringToDateTime (text);
975 case TypeCode.Decimal:
976 return XQueryConvert.StringToDecimal (text);
977 case TypeCode.Double:
978 return XQueryConvert.StringToDouble (text);
980 return XQueryConvert.StringToInt (text);
982 return XQueryConvert.StringToInteger (text);
983 case TypeCode.Single:
984 return XQueryConvert.StringToFloat (text);
985 case TypeCode.String:
988 } catch (Exception ex) {
989 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);
991 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
994 public virtual bool ReadElementContentAsBoolean ()
997 return XQueryConvert.StringToBoolean (ReadElementContentAsString ());
998 } catch (FormatException ex) {
999 throw XmlError ("Typed value is invalid.", ex);
1003 public virtual DateTime ReadElementContentAsDateTime ()
1006 return XQueryConvert.StringToDateTime (ReadElementContentAsString ());
1007 } catch (FormatException ex) {
1008 throw XmlError ("Typed value is invalid.", ex);
1012 public virtual decimal ReadElementContentAsDecimal ()
1015 return XQueryConvert.StringToDecimal (ReadElementContentAsString ());
1016 } catch (FormatException ex) {
1017 throw XmlError ("Typed value is invalid.", ex);
1021 public virtual double ReadElementContentAsDouble ()
1024 return XQueryConvert.StringToDouble (ReadElementContentAsString ());
1025 } catch (FormatException ex) {
1026 throw XmlError ("Typed value is invalid.", ex);
1030 public virtual float ReadElementContentAsFloat ()
1033 return XQueryConvert.StringToFloat (ReadElementContentAsString ());
1034 } catch (FormatException ex) {
1035 throw XmlError ("Typed value is invalid.", ex);
1039 public virtual int ReadElementContentAsInt ()
1042 return XQueryConvert.StringToInt (ReadElementContentAsString ());
1043 } catch (FormatException ex) {
1044 throw XmlError ("Typed value is invalid.", ex);
1048 public virtual long ReadElementContentAsLong ()
1051 return XQueryConvert.StringToInteger (ReadElementContentAsString ());
1052 } catch (FormatException ex) {
1053 throw XmlError ("Typed value is invalid.", ex);
1057 public virtual string ReadElementContentAsString ()
1059 bool isEmpty = IsEmptyElement;
1060 ReadStartElement ();
1062 return String.Empty;
1063 string s = ReadContentString (false);
1068 public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
1071 return XQueryConvert.StringToBoolean (ReadElementContentAsString (localName, namespaceURI));
1072 } catch (FormatException ex) {
1073 throw XmlError ("Typed value is invalid.", ex);
1077 public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
1080 return XQueryConvert.StringToDateTime (ReadElementContentAsString (localName, namespaceURI));
1081 } catch (FormatException ex) {
1082 throw XmlError ("Typed value is invalid.", ex);
1086 public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
1089 return XQueryConvert.StringToDecimal (ReadElementContentAsString (localName, namespaceURI));
1090 } catch (FormatException ex) {
1091 throw XmlError ("Typed value is invalid.", ex);
1095 public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
1098 return XQueryConvert.StringToDouble (ReadElementContentAsString (localName, namespaceURI));
1099 } catch (FormatException ex) {
1100 throw XmlError ("Typed value is invalid.", ex);
1104 public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
1107 return XQueryConvert.StringToFloat (ReadElementContentAsString (localName, namespaceURI));
1108 } catch (FormatException ex) {
1109 throw XmlError ("Typed value is invalid.", ex);
1113 public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
1116 return XQueryConvert.StringToInt (ReadElementContentAsString (localName, namespaceURI));
1117 } catch (FormatException ex) {
1118 throw XmlError ("Typed value is invalid.", ex);
1122 public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
1125 return XQueryConvert.StringToInteger (ReadElementContentAsString (localName, namespaceURI));
1126 } catch (FormatException ex) {
1127 throw XmlError ("Typed value is invalid.", ex);
1131 public virtual string ReadElementContentAsString (string localName, string namespaceURI)
1133 bool isEmpty = IsEmptyElement;
1134 ReadStartElement (localName, namespaceURI);
1136 return String.Empty;
1137 string s = ReadContentString (false);
1142 public virtual bool ReadContentAsBoolean ()
1145 return XQueryConvert.StringToBoolean (ReadContentString ());
1146 } catch (FormatException ex) {
1147 throw XmlError ("Typed value is invalid.", ex);
1151 public virtual DateTime ReadContentAsDateTime ()
1154 return XQueryConvert.StringToDateTime (ReadContentString ());
1155 } catch (FormatException ex) {
1156 throw XmlError ("Typed value is invalid.", ex);
1160 public virtual decimal ReadContentAsDecimal ()
1163 return XQueryConvert.StringToDecimal (ReadContentString ());
1164 } catch (FormatException ex) {
1165 throw XmlError ("Typed value is invalid.", ex);
1169 public virtual double ReadContentAsDouble ()
1172 return XQueryConvert.StringToDouble (ReadContentString ());
1173 } catch (FormatException ex) {
1174 throw XmlError ("Typed value is invalid.", ex);
1178 public virtual float ReadContentAsFloat ()
1181 return XQueryConvert.StringToFloat (ReadContentString ());
1182 } catch (FormatException ex) {
1183 throw XmlError ("Typed value is invalid.", ex);
1187 public virtual int ReadContentAsInt ()
1190 return XQueryConvert.StringToInt (ReadContentString ());
1191 } catch (FormatException ex) {
1192 throw XmlError ("Typed value is invalid.", ex);
1196 public virtual long ReadContentAsLong ()
1199 return XQueryConvert.StringToInteger (ReadContentString ());
1200 } catch (FormatException ex) {
1201 throw XmlError ("Typed value is invalid.", ex);
1205 public virtual string ReadContentAsString ()
1207 return ReadContentString ();
1210 public virtual int ReadContentAsBase64 (
1211 byte [] buffer, int offset, int length)
1214 return binary.ReadContentAsBase64 (
1215 buffer, offset, length);
1218 public virtual int ReadContentAsBinHex (
1219 byte [] buffer, int offset, int length)
1222 return binary.ReadContentAsBinHex (
1223 buffer, offset, length);
1226 public virtual int ReadElementContentAsBase64 (
1227 byte [] buffer, int offset, int length)
1230 return binary.ReadElementContentAsBase64 (
1231 buffer, offset, length);
1234 public virtual int ReadElementContentAsBinHex (
1235 byte [] buffer, int offset, int length)
1238 return binary.ReadElementContentAsBinHex (
1239 buffer, offset, length);
1244 public virtual int ReadValueChunk (
1245 char [] buffer, int offset, int length)
1247 internal virtual int ReadValueChunk (
1248 char [] buffer, int offset, int length)
1251 if (!CanReadValueChunk)
1252 throw new NotSupportedException ();
1254 binary = new XmlReaderBinarySupport (this);
1255 return binary.ReadValueChunk (buffer, offset, length);
1258 private void CheckSupport ()
1260 // Default implementation expects both.
1261 if (!CanReadBinaryContent || !CanReadValueChunk)
1262 throw new NotSupportedException ();
1264 binary = new XmlReaderBinarySupport (this);
1267 public abstract void ResolveEntity ();
1269 public virtual void Skip ()
1271 if (ReadState != ReadState.Interactive)
1275 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
1281 while (Read () && depth < Depth)
1283 if (NodeType == XmlNodeType.EndElement)
1287 private XmlException XmlError (string message)
1289 return new XmlException (this as IXmlLineInfo, BaseURI, message);
1292 private XmlException XmlError (string message, Exception innerException)
1294 return new XmlException (this as IXmlLineInfo, BaseURI, message);