XmlReader.cs 22 KB

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