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