XmlReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. //
  2. // XmlReader.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. // (C) 2003 Atsushi Enomoto
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. using System.IO;
  35. using System.Security.Policy;
  36. using System.Text;
  37. using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
  38. using Mono.Xml; // only required for NET_2_0
  39. namespace System.Xml
  40. {
  41. #if NET_2_0
  42. public abstract class XmlReader : IDisposable, IXmlDataEvidence
  43. #else
  44. public abstract class XmlReader
  45. #endif
  46. {
  47. private StringBuilder readStringBuffer;
  48. private Evidence evidence;
  49. #if NET_2_0
  50. private XmlReaderSettings settings;
  51. #endif
  52. #region Constructor
  53. protected XmlReader ()
  54. {
  55. }
  56. #endregion
  57. #region Properties
  58. public abstract int AttributeCount { get; }
  59. public abstract string BaseURI { get; }
  60. public virtual bool CanResolveEntity
  61. {
  62. get { return false; }
  63. }
  64. public abstract int Depth { get; }
  65. public abstract bool EOF { get; }
  66. #if NET_2_0
  67. [MonoTODO]
  68. public virtual Evidence Evidence {
  69. get { return evidence; }
  70. }
  71. #endif
  72. public virtual bool HasAttributes
  73. {
  74. get { return AttributeCount > 0; }
  75. }
  76. public abstract bool HasValue { get; }
  77. #if NET_2_0
  78. public virtual bool IsDefault {
  79. get { return false; }
  80. }
  81. public virtual bool IsEmptyElement {
  82. get { return false; }
  83. }
  84. public virtual string this [int i] {
  85. get { return GetAttribute (i); }
  86. }
  87. public virtual string this [string name] {
  88. get { return GetAttribute (name); }
  89. }
  90. public virtual string this [string name, string namespaceURI] {
  91. get { return GetAttribute (name, namespaceURI); }
  92. }
  93. #else
  94. public abstract bool IsDefault { get; }
  95. public abstract bool IsEmptyElement { get; }
  96. public abstract string this [int i] { get; }
  97. public abstract string this [string name] { get; }
  98. public abstract string this [string localName, string namespaceName] { get; }
  99. #endif
  100. public abstract string LocalName { get; }
  101. public abstract string Name { get; }
  102. public abstract string NamespaceURI { get; }
  103. public abstract XmlNameTable NameTable { get; }
  104. public abstract XmlNodeType NodeType { get; }
  105. public abstract string Prefix { get; }
  106. #if NET_2_0
  107. public virtual char QuoteChar {
  108. get { return '\"'; }
  109. }
  110. #else
  111. public abstract char QuoteChar { get; }
  112. #endif
  113. public abstract ReadState ReadState { get; }
  114. #if NET_2_0
  115. [MonoTODO]
  116. public virtual IXmlSchemaInfo SchemaInfo {
  117. get {
  118. throw new NotImplementedException ();
  119. }
  120. }
  121. public virtual XmlReaderSettings Settings {
  122. get { return settings; }
  123. }
  124. #endif
  125. public abstract string Value { get; }
  126. #if NET_2_0
  127. public virtual string XmlLang {
  128. get { return String.Empty; }
  129. }
  130. public virtual XmlSpace XmlSpace {
  131. get { return XmlSpace.None; }
  132. }
  133. #else
  134. public abstract string XmlLang { get; }
  135. public abstract XmlSpace XmlSpace { get; }
  136. #endif
  137. #endregion
  138. #region Methods
  139. public abstract void Close ();
  140. #if NET_2_0
  141. public static XmlReader Create (Stream stream)
  142. {
  143. return Create (stream, null, null, new XmlUrlResolver (), null);
  144. }
  145. public static XmlReader Create (string url)
  146. {
  147. return Create (url, null);
  148. }
  149. public static XmlReader Create (TextReader reader)
  150. {
  151. return Create (reader, null, new XmlUrlResolver (), null);
  152. }
  153. public static XmlReader Create (string url, XmlReaderSettings settings)
  154. {
  155. return Create (url, null, new XmlUrlResolver (), settings);
  156. }
  157. public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
  158. {
  159. return Create (reader, new XmlUrlResolver (), settings);
  160. }
  161. [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
  162. public static XmlReader Create (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  163. {
  164. return CreateFilteredXmlReader (reader, resolver, settings);
  165. }
  166. [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.; Encoding")]
  167. public static XmlReader Create (string url, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
  168. {
  169. return CreateCustomizedTextReader (new XmlTextReader (url), resolver, settings);
  170. }
  171. [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
  172. public static XmlReader Create (TextReader reader, string baseUri, XmlResolver resolver, XmlReaderSettings settings)
  173. {
  174. return CreateCustomizedTextReader (new XmlTextReader (baseUri, reader), resolver, settings);
  175. }
  176. [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
  177. public static XmlReader Create (Stream stream, string baseUri, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
  178. {
  179. return CreateCustomizedTextReader (
  180. encoding == null ?
  181. new XmlTextReader (baseUri, stream) :
  182. new XmlTextReader (baseUri, new StreamReader (stream, encoding)),
  183. resolver,
  184. settings);
  185. }
  186. private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlResolver resolver, XmlReaderSettings settings)
  187. {
  188. reader.XmlResolver = resolver;
  189. // Normalization is set true by default.
  190. reader.Normalization = true;
  191. if (settings == null)
  192. settings = new XmlReaderSettings ();
  193. if (settings.ProhibitDtd)
  194. reader.ProhibitDtd = true;
  195. if (!settings.CheckCharacters)
  196. reader.CharacterChecking = false;
  197. // I guess it might be changed in 2.0 RTM to set true
  198. // as default, or just disappear. It goes against
  199. // XmlTextReader's default usage and users will have
  200. // to close input manually (that's annoying). Moreover,
  201. // MS XmlTextReader consumes text input more than
  202. // actually read and users can acquire those extra
  203. // consumption by GetRemainder() that returns different
  204. // TextReader.
  205. reader.CloseInput = settings.CloseInput;
  206. // I would like to support it in detail later;
  207. // MSDN description looks source of confusion. We don't
  208. // need examples, but precise list of how it works.
  209. reader.Conformance = settings.ConformanceLevel;
  210. reader.AdjustLineInfoOffset (settings.LineNumberOffset,
  211. settings.LinePositionOffset);
  212. // FIXME: maybe we had better create XmlParserContext.
  213. if (settings.NameTable != null)
  214. reader.SetNameTable (settings.NameTable);
  215. return CreateFilteredXmlReader (reader, resolver, settings);
  216. }
  217. private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  218. {
  219. reader = CreateValidatingXmlReader (reader, settings);
  220. if (reader.Settings != null ||
  221. settings.IgnoreComments ||
  222. settings.IgnoreProcessingInstructions ||
  223. settings.IgnoreWhitespace)
  224. return new XmlFilterReader (reader, settings);
  225. else {
  226. reader.settings = settings;
  227. return reader;
  228. }
  229. }
  230. private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
  231. {
  232. XmlValidatingReader xvr = null;
  233. if (settings.DtdValidate) {
  234. xvr = new XmlValidatingReader (reader);
  235. if (!settings.XsdValidate)
  236. xvr.ValidationType = ValidationType.DTD;
  237. // otherwise .Auto by default.
  238. } else if (settings.XsdValidate) {
  239. xvr = new XmlValidatingReader (reader);
  240. xvr.ValidationType = ValidationType.Schema;
  241. }
  242. if (xvr != null)
  243. xvr.SetSchemas (settings.Schemas);
  244. if (settings.IgnoreIdentityConstraints)
  245. throw new NotImplementedException ();
  246. if (!settings.IgnoreInlineSchema)
  247. throw new NotImplementedException ();
  248. if (!settings.IgnoreSchemaLocation)
  249. throw new NotImplementedException ();
  250. if (!settings.IgnoreValidationWarnings)
  251. throw new NotImplementedException ();
  252. return xvr != null ? xvr : reader;
  253. }
  254. #endif
  255. #if NET_2_0
  256. public virtual void Dispose ()
  257. {
  258. if (ReadState != ReadState.Closed)
  259. Close ();
  260. }
  261. #endif
  262. public abstract string GetAttribute (int i);
  263. public abstract string GetAttribute (string name);
  264. public abstract string GetAttribute (
  265. string localName,
  266. string namespaceName);
  267. public static bool IsName (string s)
  268. {
  269. return s != null && XmlChar.IsName (s);
  270. }
  271. public static bool IsNameToken (string s)
  272. {
  273. return s != null && XmlChar.IsNmToken (s);
  274. }
  275. public virtual bool IsStartElement ()
  276. {
  277. return (MoveToContent () == XmlNodeType.Element);
  278. }
  279. public virtual bool IsStartElement (string name)
  280. {
  281. if (!IsStartElement ())
  282. return false;
  283. return (Name == name);
  284. }
  285. public virtual bool IsStartElement (string localName, string namespaceName)
  286. {
  287. if (!IsStartElement ())
  288. return false;
  289. return (LocalName == localName && NamespaceURI == namespaceName);
  290. }
  291. public abstract string LookupNamespace (string prefix);
  292. #if NET_2_0
  293. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  294. #else
  295. internal virtual string LookupNamespace (string prefix, bool atomizedNames)
  296. #endif
  297. {
  298. return LookupNamespace (prefix);
  299. }
  300. public abstract void MoveToAttribute (int i);
  301. public abstract bool MoveToAttribute (string name);
  302. public abstract bool MoveToAttribute (
  303. string localName,
  304. string namespaceName);
  305. private bool IsContent (XmlNodeType nodeType)
  306. {
  307. /* MS doc says:
  308. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  309. */
  310. switch (nodeType) {
  311. case XmlNodeType.Text:
  312. return true;
  313. case XmlNodeType.CDATA:
  314. return true;
  315. case XmlNodeType.Element:
  316. return true;
  317. case XmlNodeType.EndElement:
  318. return true;
  319. case XmlNodeType.EntityReference:
  320. return true;
  321. case XmlNodeType.EndEntity:
  322. return true;
  323. }
  324. return false;
  325. }
  326. public virtual XmlNodeType MoveToContent ()
  327. {
  328. if (NodeType == XmlNodeType.Attribute)
  329. MoveToElement ();
  330. do {
  331. if (IsContent (NodeType))
  332. return NodeType;
  333. Read ();
  334. } while (!EOF);
  335. return XmlNodeType.None;
  336. }
  337. public abstract bool MoveToElement ();
  338. public abstract bool MoveToFirstAttribute ();
  339. public abstract bool MoveToNextAttribute ();
  340. public abstract bool Read ();
  341. public abstract bool ReadAttributeValue ();
  342. public virtual string ReadElementString ()
  343. {
  344. if (MoveToContent () != XmlNodeType.Element) {
  345. string error = String.Format ("'{0}' is an invalid node type.",
  346. NodeType.ToString ());
  347. throw new XmlException (this as IXmlLineInfo, error);
  348. }
  349. string result = String.Empty;
  350. if (!IsEmptyElement) {
  351. Read ();
  352. result = ReadString ();
  353. if (NodeType != XmlNodeType.EndElement) {
  354. string error = String.Format ("'{0}' is an invalid node type.",
  355. NodeType.ToString ());
  356. throw new XmlException (this as IXmlLineInfo, error);
  357. }
  358. }
  359. Read ();
  360. return result;
  361. }
  362. public virtual string ReadElementString (string name)
  363. {
  364. if (MoveToContent () != XmlNodeType.Element) {
  365. string error = String.Format ("'{0}' is an invalid node type.",
  366. NodeType.ToString ());
  367. throw new XmlException (this as IXmlLineInfo, error);
  368. }
  369. if (name != Name) {
  370. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  371. Name, NamespaceURI);
  372. throw new XmlException (this as IXmlLineInfo, error);
  373. }
  374. string result = String.Empty;
  375. if (!IsEmptyElement) {
  376. Read ();
  377. result = ReadString ();
  378. if (NodeType != XmlNodeType.EndElement) {
  379. string error = String.Format ("'{0}' is an invalid node type.",
  380. NodeType.ToString ());
  381. throw new XmlException (this as IXmlLineInfo, error);
  382. }
  383. }
  384. Read ();
  385. return result;
  386. }
  387. public virtual string ReadElementString (string localName, string namespaceName)
  388. {
  389. if (MoveToContent () != XmlNodeType.Element) {
  390. string error = String.Format ("'{0}' is an invalid node type.",
  391. NodeType.ToString ());
  392. throw new XmlException (this as IXmlLineInfo, error);
  393. }
  394. if (localName != LocalName || NamespaceURI != namespaceName) {
  395. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  396. LocalName, NamespaceURI);
  397. throw new XmlException (this as IXmlLineInfo, error);
  398. }
  399. string result = String.Empty;
  400. if (!IsEmptyElement) {
  401. Read ();
  402. result = ReadString ();
  403. if (NodeType != XmlNodeType.EndElement) {
  404. string error = String.Format ("'{0}' is an invalid node type.",
  405. NodeType.ToString ());
  406. throw new XmlException (this as IXmlLineInfo, error);
  407. }
  408. }
  409. Read ();
  410. return result;
  411. }
  412. public virtual void ReadEndElement ()
  413. {
  414. if (MoveToContent () != XmlNodeType.EndElement) {
  415. string error = String.Format ("'{0}' is an invalid node type.",
  416. NodeType.ToString ());
  417. throw new XmlException (this as IXmlLineInfo, error);
  418. }
  419. Read ();
  420. }
  421. #if NET_1_0
  422. public abstract string ReadInnerXml ();
  423. public abstract string ReadOuterXml ();
  424. #else
  425. public virtual string ReadInnerXml ()
  426. {
  427. return ReadInnerXmlInternal ();
  428. }
  429. public virtual string ReadOuterXml ()
  430. {
  431. return ReadOuterXmlInternal ();
  432. }
  433. #endif
  434. internal string ReadInnerXmlInternal ()
  435. {
  436. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  437. return String.Empty;
  438. StringWriter sw = new StringWriter ();
  439. XmlTextWriter xtw = new XmlTextWriter (sw);
  440. if (NodeType == XmlNodeType.Element) {
  441. if (IsEmptyElement) {
  442. Read ();
  443. return String.Empty;
  444. }
  445. int startDepth = Depth;
  446. Read ();
  447. while (startDepth < Depth) {
  448. if (ReadState != ReadState.Interactive)
  449. throw new XmlException ("Unexpected end of the XML reader.");
  450. xtw.WriteNode (this, false);
  451. }
  452. // reader is now end element, then proceed once more.
  453. Read ();
  454. }
  455. else
  456. xtw.WriteNode (this, false);
  457. return sw.ToString ();
  458. }
  459. internal string ReadOuterXmlInternal ()
  460. {
  461. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  462. return String.Empty;
  463. StringWriter sw = new StringWriter ();
  464. XmlTextWriter xtw = new XmlTextWriter (sw);
  465. xtw.WriteNode (this, false);
  466. return sw.ToString ();
  467. }
  468. public virtual void ReadStartElement ()
  469. {
  470. if (MoveToContent () != XmlNodeType.Element) {
  471. string error = String.Format ("'{0}' is an invalid node type.",
  472. NodeType.ToString ());
  473. throw new XmlException (this as IXmlLineInfo, error);
  474. }
  475. Read ();
  476. }
  477. public virtual void ReadStartElement (string name)
  478. {
  479. if (MoveToContent () != XmlNodeType.Element) {
  480. string error = String.Format ("'{0}' is an invalid node type.",
  481. NodeType.ToString ());
  482. throw new XmlException (this as IXmlLineInfo, error);
  483. }
  484. if (name != Name) {
  485. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  486. Name, NamespaceURI);
  487. throw new XmlException (this as IXmlLineInfo, error);
  488. }
  489. Read ();
  490. }
  491. public virtual void ReadStartElement (string localName, string namespaceName)
  492. {
  493. if (MoveToContent () != XmlNodeType.Element) {
  494. string error = String.Format ("'{0}' is an invalid node type.",
  495. NodeType.ToString ());
  496. throw new XmlException (this as IXmlLineInfo, error);
  497. }
  498. if (localName != LocalName || NamespaceURI != namespaceName) {
  499. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  500. localName, namespaceName,
  501. LocalName, NamespaceURI);
  502. throw new XmlException (this as IXmlLineInfo, error);
  503. }
  504. Read ();
  505. }
  506. #if NET_1_0
  507. public abstract string ReadString ();
  508. #else
  509. public virtual string ReadString ()
  510. {
  511. return ReadStringInternal ();
  512. }
  513. #endif
  514. internal string ReadStringInternal ()
  515. {
  516. if (readStringBuffer == null)
  517. readStringBuffer = new StringBuilder ();
  518. readStringBuffer.Length = 0;
  519. MoveToElement ();
  520. switch (NodeType) {
  521. default:
  522. return String.Empty;
  523. case XmlNodeType.Element:
  524. if (IsEmptyElement)
  525. return String.Empty;
  526. do {
  527. Read ();
  528. switch (NodeType) {
  529. case XmlNodeType.Text:
  530. case XmlNodeType.CDATA:
  531. case XmlNodeType.Whitespace:
  532. case XmlNodeType.SignificantWhitespace:
  533. readStringBuffer.Append (Value);
  534. continue;
  535. }
  536. break;
  537. } while (true);
  538. break;
  539. case XmlNodeType.Text:
  540. case XmlNodeType.CDATA:
  541. case XmlNodeType.Whitespace:
  542. case XmlNodeType.SignificantWhitespace:
  543. do {
  544. switch (NodeType) {
  545. case XmlNodeType.Text:
  546. case XmlNodeType.CDATA:
  547. case XmlNodeType.Whitespace:
  548. case XmlNodeType.SignificantWhitespace:
  549. readStringBuffer.Append (Value);
  550. Read ();
  551. continue;
  552. }
  553. break;
  554. } while (true);
  555. break;
  556. }
  557. string ret = readStringBuffer.ToString ();
  558. readStringBuffer.Length = 0;
  559. return ret;
  560. }
  561. #if NET_2_0
  562. [MonoTODO]
  563. public Type ValueType {
  564. get { throw new NotImplementedException (); }
  565. }
  566. [MonoTODO]
  567. public virtual bool ReadToDescendant (string name)
  568. {
  569. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  570. return false;
  571. int depth = Depth;
  572. for (Read (); depth < Depth; Read ())
  573. if (NodeType == XmlNodeType.Element && name == Name)
  574. return true;
  575. return false;
  576. }
  577. [MonoTODO]
  578. public virtual bool ReadToDescendant (string localName, string namespaceURI)
  579. {
  580. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  581. return false;
  582. int depth = Depth;
  583. for (Read (); depth < Depth; Read ())
  584. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  585. return true;
  586. return false;
  587. }
  588. [MonoTODO]
  589. public virtual bool ReadToNextSibling (string name)
  590. {
  591. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  592. return false;
  593. int depth = Depth;
  594. for (Skip (); depth < Depth; Skip ())
  595. if (NodeType == XmlNodeType.Element && name == Name)
  596. return true;
  597. return false;
  598. }
  599. [MonoTODO]
  600. public virtual bool ReadToNextSibling (string localName, string namespaceURI)
  601. {
  602. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  603. return false;
  604. int depth = Depth;
  605. for (Skip (); depth < Depth; Skip ())
  606. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  607. return true;
  608. return false;
  609. }
  610. [MonoTODO]
  611. public object ReadAsObject (Type type)
  612. {
  613. throw new NotImplementedException ();
  614. }
  615. [MonoTODO]
  616. public XmlReader ReadSubtree ()
  617. {
  618. return new SubtreeXmlReader (this);
  619. }
  620. [MonoTODO]
  621. public virtual object ReadTypedValue ()
  622. {
  623. return ReadAsObject (ValueType);
  624. }
  625. [MonoTODO]
  626. public virtual object ReadValueAs (Type type)
  627. {
  628. return ReadValueAs (type, this as IXmlNamespaceResolver);
  629. }
  630. [MonoTODO]
  631. public virtual object ReadValueAs (Type type, IXmlNamespaceResolver resolver)
  632. {
  633. string text = ReadString ();
  634. try {
  635. if (type == typeof (XmlQualifiedName))
  636. return XmlQualifiedName.Parse (text, resolver);
  637. switch (Type.GetTypeCode (type)) {
  638. case TypeCode.Boolean:
  639. return ReadValueAsBoolean ();
  640. case TypeCode.DateTime:
  641. return ReadValueAsDateTime ();
  642. case TypeCode.Decimal:
  643. return ReadValueAsDecimal ();
  644. case TypeCode.Double:
  645. return ReadValueAsDouble ();
  646. case TypeCode.Int32:
  647. return ReadValueAsInt32 ();
  648. case TypeCode.Int64:
  649. return ReadValueAsInt64 ();
  650. case TypeCode.Single:
  651. return ReadValueAsSingle ();
  652. case TypeCode.String:
  653. return ReadValueAsString ();
  654. }
  655. } catch (Exception ex) {
  656. return new FormatException (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'.", text, type));
  657. }
  658. throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
  659. }
  660. [MonoTODO]
  661. public virtual bool ReadValueAsBoolean ()
  662. {
  663. return XQueryConvert.StringToBoolean (ReadString ());
  664. }
  665. [MonoTODO]
  666. public virtual DateTime ReadValueAsDateTime ()
  667. {
  668. return XQueryConvert.StringToDateTime (ReadString ());
  669. }
  670. [MonoTODO]
  671. public virtual decimal ReadValueAsDecimal ()
  672. {
  673. return XQueryConvert.StringToDecimal (ReadString ());
  674. }
  675. [MonoTODO]
  676. public virtual double ReadValueAsDouble ()
  677. {
  678. return XQueryConvert.StringToDouble (ReadString ());
  679. }
  680. [MonoTODO]
  681. public virtual int ReadValueAsInt32 ()
  682. {
  683. return XQueryConvert.StringToInt (ReadString ());
  684. }
  685. [MonoTODO]
  686. public virtual long ReadValueAsInt64 ()
  687. {
  688. return XQueryConvert.StringToInteger (ReadString ());
  689. }
  690. [MonoTODO]
  691. public virtual ICollection ReadValueAsList ()
  692. {
  693. throw new NotImplementedException ();
  694. }
  695. [MonoTODO]
  696. public virtual float ReadValueAsSingle ()
  697. {
  698. return XQueryConvert.StringToFloat (ReadString ());
  699. }
  700. [MonoTODO]
  701. public virtual string ReadValueAsString ()
  702. {
  703. return ReadString ();
  704. }
  705. #endif
  706. public abstract void ResolveEntity ();
  707. public virtual void Skip ()
  708. {
  709. if (ReadState != ReadState.Interactive)
  710. return;
  711. MoveToElement ();
  712. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  713. Read ();
  714. return;
  715. }
  716. int depth = Depth;
  717. while (Read() && depth < Depth);
  718. if (NodeType == XmlNodeType.EndElement)
  719. Read ();
  720. }
  721. #endregion
  722. }
  723. }