XmlReader.cs 23 KB

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