XmlReader.cs 22 KB

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