XmlReader.cs 22 KB

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