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);
318 context = PopulateParserContext (settings, url);
319 XmlTextReader xtr = new XmlTextReader (false, settings.XmlResolver, url, GetNodeType (settings), context);
320 XmlReader ret = CreateCustomizedTextReader (xtr, settings);
321 xtr.CloseInput = true; // forced. See XmlReaderCommonTests.CreateFromUrlClose().
325 public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
327 settings = PopulateSettings (settings);
329 context = PopulateParserContext (settings, String.Empty);
330 return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings);
333 public static XmlReader Create (TextReader reader, XmlReaderSettings settings, XmlParserContext context)
335 settings = PopulateSettings (settings);
337 context = PopulateParserContext (settings, String.Empty);
338 return CreateCustomizedTextReader (new XmlTextReader (context.BaseURI, reader, GetNodeType (settings), context), settings);
341 private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlReaderSettings settings)
343 reader.XmlResolver = settings.XmlResolver;
344 // Normalization is set true by default.
345 reader.Normalization = true;
346 reader.EntityHandling = EntityHandling.ExpandEntities;
348 if (settings.ProhibitDtd)
349 reader.ProhibitDtd = true;
351 if (!settings.CheckCharacters)
352 reader.CharacterChecking = false;
354 // I guess it might be changed in 2.0 RTM to set true
355 // as default, or just disappear. It goes against
356 // XmlTextReader's default usage and users will have
357 // to close input manually (that's annoying). Moreover,
358 // MS XmlTextReader consumes text input more than
359 // actually read and users can acquire those extra
360 // consumption by GetRemainder() that returns different
362 reader.CloseInput = settings.CloseInput;
364 // I would like to support it in detail later;
365 // MSDN description looks source of confusion. We don't
366 // need examples, but precise list of how it works.
367 reader.Conformance = settings.ConformanceLevel;
369 reader.AdjustLineInfoOffset (settings.LineNumberOffset,
370 settings.LinePositionOffset);
372 if (settings.NameTable != null)
373 reader.SetNameTable (settings.NameTable);
375 XmlReader r = CreateFilteredXmlReader (reader, settings);
376 r.settings = settings;
380 private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlReaderSettings settings)
382 ConformanceLevel conf = ConformanceLevel.Auto;
383 if (reader is XmlTextReader)
384 conf = ((XmlTextReader) reader).Conformance;
385 else if (reader.Settings != null)
386 conf = reader.Settings.ConformanceLevel;
388 conf = settings.ConformanceLevel;
389 if (settings.ConformanceLevel != ConformanceLevel.Auto &&
390 conf != settings.ConformanceLevel)
391 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));
392 settings.ConformanceLevel = conf;
394 reader = CreateValidatingXmlReader (reader, settings);
396 if ( settings.IgnoreComments ||
397 settings.IgnoreProcessingInstructions ||
398 settings.IgnoreWhitespace)
399 return new XmlFilterReader (reader, settings);
401 reader.settings = settings;
406 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
411 XmlValidatingReader xvr = null;
412 switch (settings.ValidationType) {
413 // Auto and XDR are obsoleted in 2.0 and therefore ignored.
416 case ValidationType.DTD:
417 xvr = new XmlValidatingReader (reader);
418 xvr.XmlResolver = settings.XmlResolver;
419 xvr.ValidationType = ValidationType.DTD;
421 case ValidationType.Schema:
422 return new XmlSchemaValidatingReader (reader, settings);
425 // Actually I don't think they are treated in DTD validation though...
426 if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
427 throw new NotImplementedException ();
428 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
429 // throw new NotImplementedException ();
430 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
431 // throw new NotImplementedException ();
432 //if ((settings.ValidationFlags & XmlSchemaValidationFlags.ReportValidationWarnings) == 0)
433 // throw new NotImplementedException ();
435 return xvr != null ? xvr : reader;
439 void IDisposable.Dispose ()
444 protected virtual void Dispose (bool disposing)
446 if (ReadState != ReadState.Closed)
451 public abstract string GetAttribute (int i);
453 public abstract string GetAttribute (string name);
455 public abstract string GetAttribute (
457 string namespaceName);
459 public static bool IsName (string s)
461 return s != null && XmlChar.IsName (s);
464 public static bool IsNameToken (string s)
466 return s != null && XmlChar.IsNmToken (s);
469 public virtual bool IsStartElement ()
471 return (MoveToContent () == XmlNodeType.Element);
474 public virtual bool IsStartElement (string name)
476 if (!IsStartElement ())
479 return (Name == name);
482 public virtual bool IsStartElement (string localName, string namespaceName)
484 if (!IsStartElement ())
487 return (LocalName == localName && NamespaceURI == namespaceName);
490 public abstract string LookupNamespace (string prefix);
493 public virtual void MoveToAttribute (int i)
495 if (i >= AttributeCount)
496 throw new ArgumentOutOfRangeException ();
497 MoveToFirstAttribute ();
498 for (int a = 0; a < i; a++)
499 MoveToNextAttribute ();
502 public abstract void MoveToAttribute (int i);
505 public abstract bool MoveToAttribute (string name);
507 public abstract bool MoveToAttribute (
509 string namespaceName);
511 private bool IsContent (XmlNodeType nodeType)
514 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
517 case XmlNodeType.Text:
519 case XmlNodeType.CDATA:
521 case XmlNodeType.Element:
523 case XmlNodeType.EndElement:
525 case XmlNodeType.EntityReference:
527 case XmlNodeType.EndEntity:
534 public virtual XmlNodeType MoveToContent ()
536 if (NodeType == XmlNodeType.Attribute)
540 if (IsContent (NodeType))
544 return XmlNodeType.None;
547 public abstract bool MoveToElement ();
549 public abstract bool MoveToFirstAttribute ();
551 public abstract bool MoveToNextAttribute ();
553 public abstract bool Read ();
556 public abstract bool ReadAttributeValue ();
559 public virtual string ReadElementString ()
561 if (MoveToContent () != XmlNodeType.Element) {
562 string error = String.Format ("'{0}' is an invalid node type.",
563 NodeType.ToString ());
564 throw XmlError (error);
567 string result = String.Empty;
568 if (!IsEmptyElement) {
570 result = ReadString ();
571 if (NodeType != XmlNodeType.EndElement) {
572 string error = String.Format ("'{0}' is an invalid node type.",
573 NodeType.ToString ());
574 throw XmlError (error);
582 public virtual string ReadElementString (string name)
584 if (MoveToContent () != XmlNodeType.Element) {
585 string error = String.Format ("'{0}' is an invalid node type.",
586 NodeType.ToString ());
587 throw XmlError (error);
591 string error = String.Format ("The {0} tag from namespace {1} is expected.",
593 throw XmlError (error);
596 string result = String.Empty;
597 if (!IsEmptyElement) {
599 result = ReadString ();
600 if (NodeType != XmlNodeType.EndElement) {
601 string error = String.Format ("'{0}' is an invalid node type.",
602 NodeType.ToString ());
603 throw XmlError (error);
611 public virtual string ReadElementString (string localName, string namespaceName)
613 if (MoveToContent () != XmlNodeType.Element) {
614 string error = String.Format ("'{0}' is an invalid node type.",
615 NodeType.ToString ());
616 throw XmlError (error);
619 if (localName != LocalName || NamespaceURI != namespaceName) {
620 string error = String.Format ("The {0} tag from namespace {1} is expected.",
621 LocalName, NamespaceURI);
622 throw XmlError (error);
625 string result = String.Empty;
626 if (!IsEmptyElement) {
628 result = ReadString ();
629 if (NodeType != XmlNodeType.EndElement) {
630 string error = String.Format ("'{0}' is an invalid node type.",
631 NodeType.ToString ());
632 throw XmlError (error);
640 public virtual void ReadEndElement ()
642 if (MoveToContent () != XmlNodeType.EndElement) {
643 string error = String.Format ("'{0}' is an invalid node type.",
644 NodeType.ToString ());
645 throw XmlError (error);
651 public virtual string ReadInnerXml ()
653 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
656 if (IsEmptyElement) {
660 StringWriter sw = new StringWriter ();
661 XmlTextWriter xtw = new XmlTextWriter (sw);
662 if (NodeType == XmlNodeType.Element) {
663 int startDepth = Depth;
665 while (startDepth < Depth) {
666 if (ReadState != ReadState.Interactive)
667 throw XmlError ("Unexpected end of the XML reader.");
668 xtw.WriteNode (this, false);
670 // reader is now end element, then proceed once more.
674 xtw.WriteNode (this, false);
676 return sw.ToString ();
679 public virtual string ReadOuterXml ()
681 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
685 case XmlNodeType.Element:
686 case XmlNodeType.Attribute:
687 StringWriter sw = new StringWriter ();
688 XmlTextWriter xtw = new XmlTextWriter (sw);
689 xtw.WriteNode (this, false);
690 return sw.ToString ();
697 public virtual void ReadStartElement ()
699 if (MoveToContent () != XmlNodeType.Element) {
700 string error = String.Format ("'{0}' is an invalid node type.",
701 NodeType.ToString ());
702 throw XmlError (error);
708 public virtual void ReadStartElement (string name)
710 if (MoveToContent () != XmlNodeType.Element) {
711 string error = String.Format ("'{0}' is an invalid node type.",
712 NodeType.ToString ());
713 throw XmlError (error);
717 string error = String.Format ("The {0} tag from namespace {1} is expected.",
719 throw XmlError (error);
725 public virtual void ReadStartElement (string localName, string namespaceName)
727 if (MoveToContent () != XmlNodeType.Element) {
728 string error = String.Format ("'{0}' is an invalid node type.",
729 NodeType.ToString ());
730 throw XmlError (error);
733 if (localName != LocalName || NamespaceURI != namespaceName) {
734 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
735 localName, namespaceName,
736 LocalName, NamespaceURI);
737 throw XmlError (error);
743 public virtual string ReadString ()
745 if (readStringBuffer == null)
746 readStringBuffer = new StringBuilder ();
747 readStringBuffer.Length = 0;
754 case XmlNodeType.Element:
760 case XmlNodeType.Text:
761 case XmlNodeType.CDATA:
762 case XmlNodeType.Whitespace:
763 case XmlNodeType.SignificantWhitespace:
764 readStringBuffer.Append (Value);
770 case XmlNodeType.Text:
771 case XmlNodeType.CDATA:
772 case XmlNodeType.Whitespace:
773 case XmlNodeType.SignificantWhitespace:
776 case XmlNodeType.Text:
777 case XmlNodeType.CDATA:
778 case XmlNodeType.Whitespace:
779 case XmlNodeType.SignificantWhitespace:
780 readStringBuffer.Append (Value);
788 string ret = readStringBuffer.ToString ();
789 readStringBuffer.Length = 0;
794 public virtual Type ValueType {
795 get { return typeof (string); }
798 public virtual bool ReadToDescendant (string name)
800 if (ReadState == ReadState.Initial) {
802 if (IsStartElement (name))
805 if (NodeType != XmlNodeType.Element || IsEmptyElement)
808 for (Read (); depth < Depth; Read ())
809 if (NodeType == XmlNodeType.Element && name == Name)
814 public virtual bool ReadToDescendant (string localName, string namespaceURI)
816 if (ReadState == ReadState.Initial) {
818 if (IsStartElement (localName, namespaceURI))
821 if (NodeType != XmlNodeType.Element || IsEmptyElement)
824 for (Read (); depth < Depth; Read ())
825 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
830 public virtual bool ReadToFollowing (string name)
833 if (NodeType == XmlNodeType.Element && name == Name)
838 public virtual bool ReadToFollowing (string localName, string namespaceURI)
841 if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
846 public virtual bool ReadToNextSibling (string name)
848 if (ReadState != ReadState.Interactive)
851 for (Skip (); depth <= Depth; Skip ())
852 if (NodeType == XmlNodeType.Element && name == Name)
857 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
859 if (ReadState != ReadState.Interactive)
862 for (Skip (); depth <= Depth; Skip ())
863 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
868 public virtual XmlReader ReadSubtree ()
870 return new SubtreeXmlReader (this);
873 private string ReadContentString ()
875 if (NodeType == XmlNodeType.Attribute)
877 return ReadContentString (true);
880 private string ReadContentString (bool isText)
884 case XmlNodeType.Text:
885 case XmlNodeType.SignificantWhitespace:
886 case XmlNodeType.Whitespace:
887 case XmlNodeType.CDATA:
890 throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
894 string value = String.Empty;
897 case XmlNodeType.Element:
900 throw XmlError ("Child element is not expected in this operation.");
901 case XmlNodeType.EndElement:
903 case XmlNodeType.Text:
904 case XmlNodeType.CDATA:
905 case XmlNodeType.SignificantWhitespace:
906 case XmlNodeType.Whitespace:
911 throw XmlError ("Unexpected end of document.");
914 string GetLocation ()
916 IXmlLineInfo li = this as IXmlLineInfo;
917 return li != null && li.HasLineInfo () ?
918 String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
922 public virtual object ReadElementContentAsObject ()
924 return ReadElementContentAs (ValueType, null);
928 public virtual object ReadElementContentAsObject (string localName, string namespaceURI)
930 return ReadElementContentAs (ValueType, null, localName, namespaceURI);
934 public virtual object ReadContentAsObject ()
936 return ReadContentAs (ValueType, null);
939 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
941 bool isEmpty = IsEmptyElement;
943 object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
949 public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
951 ReadStartElement (localName, namespaceURI);
952 object obj = ReadContentAs (type, resolver);
957 public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
959 return ValueAs (ReadContentString (), type, resolver);
962 private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
965 if (type == typeof (object))
967 if (type == typeof (XmlQualifiedName)) {
968 if (resolver != null)
969 return XmlQualifiedName.Parse (text, resolver);
971 return XmlQualifiedName.Parse (text, this);
974 switch (Type.GetTypeCode (type)) {
975 case TypeCode.Boolean:
976 return XQueryConvert.StringToBoolean (text);
977 case TypeCode.DateTime:
978 return XQueryConvert.StringToDateTime (text);
979 case TypeCode.Decimal:
980 return XQueryConvert.StringToDecimal (text);
981 case TypeCode.Double:
982 return XQueryConvert.StringToDouble (text);
984 return XQueryConvert.StringToInt (text);
986 return XQueryConvert.StringToInteger (text);
987 case TypeCode.Single:
988 return XQueryConvert.StringToFloat (text);
989 case TypeCode.String:
992 } catch (Exception ex) {
993 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);
995 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
998 public virtual bool ReadElementContentAsBoolean ()
1001 return XQueryConvert.StringToBoolean (ReadElementContentAsString ());
1002 } catch (FormatException ex) {
1003 throw XmlError ("Typed value is invalid.", ex);
1007 public virtual DateTime ReadElementContentAsDateTime ()
1010 return XQueryConvert.StringToDateTime (ReadElementContentAsString ());
1011 } catch (FormatException ex) {
1012 throw XmlError ("Typed value is invalid.", ex);
1016 public virtual decimal ReadElementContentAsDecimal ()
1019 return XQueryConvert.StringToDecimal (ReadElementContentAsString ());
1020 } catch (FormatException ex) {
1021 throw XmlError ("Typed value is invalid.", ex);
1025 public virtual double ReadElementContentAsDouble ()
1028 return XQueryConvert.StringToDouble (ReadElementContentAsString ());
1029 } catch (FormatException ex) {
1030 throw XmlError ("Typed value is invalid.", ex);
1034 public virtual float ReadElementContentAsFloat ()
1037 return XQueryConvert.StringToFloat (ReadElementContentAsString ());
1038 } catch (FormatException ex) {
1039 throw XmlError ("Typed value is invalid.", ex);
1043 public virtual int ReadElementContentAsInt ()
1046 return XQueryConvert.StringToInt (ReadElementContentAsString ());
1047 } catch (FormatException ex) {
1048 throw XmlError ("Typed value is invalid.", ex);
1052 public virtual long ReadElementContentAsLong ()
1055 return XQueryConvert.StringToInteger (ReadElementContentAsString ());
1056 } catch (FormatException ex) {
1057 throw XmlError ("Typed value is invalid.", ex);
1061 public virtual string ReadElementContentAsString ()
1063 bool isEmpty = IsEmptyElement;
1064 ReadStartElement ();
1066 return String.Empty;
1067 string s = ReadContentString (false);
1072 public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
1075 return XQueryConvert.StringToBoolean (ReadElementContentAsString (localName, namespaceURI));
1076 } catch (FormatException ex) {
1077 throw XmlError ("Typed value is invalid.", ex);
1081 public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
1084 return XQueryConvert.StringToDateTime (ReadElementContentAsString (localName, namespaceURI));
1085 } catch (FormatException ex) {
1086 throw XmlError ("Typed value is invalid.", ex);
1090 public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
1093 return XQueryConvert.StringToDecimal (ReadElementContentAsString (localName, namespaceURI));
1094 } catch (FormatException ex) {
1095 throw XmlError ("Typed value is invalid.", ex);
1099 public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
1102 return XQueryConvert.StringToDouble (ReadElementContentAsString (localName, namespaceURI));
1103 } catch (FormatException ex) {
1104 throw XmlError ("Typed value is invalid.", ex);
1108 public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
1111 return XQueryConvert.StringToFloat (ReadElementContentAsString (localName, namespaceURI));
1112 } catch (FormatException ex) {
1113 throw XmlError ("Typed value is invalid.", ex);
1117 public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
1120 return XQueryConvert.StringToInt (ReadElementContentAsString (localName, namespaceURI));
1121 } catch (FormatException ex) {
1122 throw XmlError ("Typed value is invalid.", ex);
1126 public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
1129 return XQueryConvert.StringToInteger (ReadElementContentAsString (localName, namespaceURI));
1130 } catch (FormatException ex) {
1131 throw XmlError ("Typed value is invalid.", ex);
1135 public virtual string ReadElementContentAsString (string localName, string namespaceURI)
1137 bool isEmpty = IsEmptyElement;
1138 ReadStartElement (localName, namespaceURI);
1140 return String.Empty;
1141 string s = ReadContentString (false);
1146 public virtual bool ReadContentAsBoolean ()
1149 return XQueryConvert.StringToBoolean (ReadContentString ());
1150 } catch (FormatException ex) {
1151 throw XmlError ("Typed value is invalid.", ex);
1155 public virtual DateTime ReadContentAsDateTime ()
1158 return XQueryConvert.StringToDateTime (ReadContentString ());
1159 } catch (FormatException ex) {
1160 throw XmlError ("Typed value is invalid.", ex);
1164 public virtual decimal ReadContentAsDecimal ()
1167 return XQueryConvert.StringToDecimal (ReadContentString ());
1168 } catch (FormatException ex) {
1169 throw XmlError ("Typed value is invalid.", ex);
1173 public virtual double ReadContentAsDouble ()
1176 return XQueryConvert.StringToDouble (ReadContentString ());
1177 } catch (FormatException ex) {
1178 throw XmlError ("Typed value is invalid.", ex);
1182 public virtual float ReadContentAsFloat ()
1185 return XQueryConvert.StringToFloat (ReadContentString ());
1186 } catch (FormatException ex) {
1187 throw XmlError ("Typed value is invalid.", ex);
1191 public virtual int ReadContentAsInt ()
1194 return XQueryConvert.StringToInt (ReadContentString ());
1195 } catch (FormatException ex) {
1196 throw XmlError ("Typed value is invalid.", ex);
1200 public virtual long ReadContentAsLong ()
1203 return XQueryConvert.StringToInteger (ReadContentString ());
1204 } catch (FormatException ex) {
1205 throw XmlError ("Typed value is invalid.", ex);
1209 public virtual string ReadContentAsString ()
1211 return ReadContentString ();
1214 public virtual int ReadContentAsBase64 (
1215 byte [] buffer, int offset, int length)
1218 return binary.ReadContentAsBase64 (
1219 buffer, offset, length);
1222 public virtual int ReadContentAsBinHex (
1223 byte [] buffer, int offset, int length)
1226 return binary.ReadContentAsBinHex (
1227 buffer, offset, length);
1230 public virtual int ReadElementContentAsBase64 (
1231 byte [] buffer, int offset, int length)
1234 return binary.ReadElementContentAsBase64 (
1235 buffer, offset, length);
1238 public virtual int ReadElementContentAsBinHex (
1239 byte [] buffer, int offset, int length)
1242 return binary.ReadElementContentAsBinHex (
1243 buffer, offset, length);
1248 public virtual int ReadValueChunk (
1249 char [] buffer, int offset, int length)
1251 internal virtual int ReadValueChunk (
1252 char [] buffer, int offset, int length)
1255 if (!CanReadValueChunk)
1256 throw new NotSupportedException ();
1258 binary = new XmlReaderBinarySupport (this);
1259 return binary.ReadValueChunk (buffer, offset, length);
1262 private void CheckSupport ()
1264 // Default implementation expects both.
1265 if (!CanReadBinaryContent || !CanReadValueChunk)
1266 throw new NotSupportedException ();
1268 binary = new XmlReaderBinarySupport (this);
1272 public abstract void ResolveEntity ();
1275 public virtual void Skip ()
1277 if (ReadState != ReadState.Interactive)
1281 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
1287 while (Read () && depth < Depth)
1289 if (NodeType == XmlNodeType.EndElement)
1293 private XmlException XmlError (string message)
1295 return new XmlException (this as IXmlLineInfo, BaseURI, message);
1298 private XmlException XmlError (string message, Exception innerException)
1300 return new XmlException (this as IXmlLineInfo, BaseURI, message);