XmlReader.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. switch (settings.ValidationType) {
  225. case ValidationType.DTD:
  226. xvr = new XmlValidatingReader (reader);
  227. xvr.ValidationType = ValidationType.DTD;
  228. break;
  229. case ValidationType.Schema:
  230. // xvr = new XmlValidatingReader (reader);
  231. // xvr.ValidationType = ValidationType.Schema;
  232. return new Mono.Xml.Schema.XmlSchemaValidatingReader (reader, settings);
  233. case ValidationType.XDR:
  234. throw new NotSupportedException ();
  235. }
  236. if (xvr != null)
  237. xvr.SetSchemas (settings.Schemas);
  238. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreIdentityConstraints) != 0)
  239. throw new NotImplementedException ();
  240. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreInlineSchema) != 0)
  241. throw new NotImplementedException ();
  242. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreSchemaLocation) != 0)
  243. throw new NotImplementedException ();
  244. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreValidationWarnings) == 0)
  245. throw new NotImplementedException ();
  246. return xvr != null ? xvr : reader;
  247. }
  248. #endif
  249. #if NET_2_0
  250. public virtual void Dispose ()
  251. {
  252. if (ReadState != ReadState.Closed)
  253. Close ();
  254. }
  255. #endif
  256. public abstract string GetAttribute (int i);
  257. public abstract string GetAttribute (string name);
  258. public abstract string GetAttribute (
  259. string localName,
  260. string namespaceName);
  261. public static bool IsName (string s)
  262. {
  263. return s != null && XmlChar.IsName (s);
  264. }
  265. public static bool IsNameToken (string s)
  266. {
  267. return s != null && XmlChar.IsNmToken (s);
  268. }
  269. public virtual bool IsStartElement ()
  270. {
  271. return (MoveToContent () == XmlNodeType.Element);
  272. }
  273. public virtual bool IsStartElement (string name)
  274. {
  275. if (!IsStartElement ())
  276. return false;
  277. return (Name == name);
  278. }
  279. public virtual bool IsStartElement (string localName, string namespaceName)
  280. {
  281. if (!IsStartElement ())
  282. return false;
  283. return (LocalName == localName && NamespaceURI == namespaceName);
  284. }
  285. public abstract string LookupNamespace (string prefix);
  286. #if NET_2_0
  287. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  288. #else
  289. internal virtual string LookupNamespace (string prefix, bool atomizedNames)
  290. #endif
  291. {
  292. return LookupNamespace (prefix);
  293. }
  294. public abstract void MoveToAttribute (int i);
  295. public abstract bool MoveToAttribute (string name);
  296. public abstract bool MoveToAttribute (
  297. string localName,
  298. string namespaceName);
  299. private bool IsContent (XmlNodeType nodeType)
  300. {
  301. /* MS doc says:
  302. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  303. */
  304. switch (nodeType) {
  305. case XmlNodeType.Text:
  306. return true;
  307. case XmlNodeType.CDATA:
  308. return true;
  309. case XmlNodeType.Element:
  310. return true;
  311. case XmlNodeType.EndElement:
  312. return true;
  313. case XmlNodeType.EntityReference:
  314. return true;
  315. case XmlNodeType.EndEntity:
  316. return true;
  317. }
  318. return false;
  319. }
  320. public virtual XmlNodeType MoveToContent ()
  321. {
  322. if (NodeType == XmlNodeType.Attribute)
  323. MoveToElement ();
  324. do {
  325. if (IsContent (NodeType))
  326. return NodeType;
  327. Read ();
  328. } while (!EOF);
  329. return XmlNodeType.None;
  330. }
  331. public abstract bool MoveToElement ();
  332. public abstract bool MoveToFirstAttribute ();
  333. public abstract bool MoveToNextAttribute ();
  334. public abstract bool Read ();
  335. public abstract bool ReadAttributeValue ();
  336. public virtual string ReadElementString ()
  337. {
  338. if (MoveToContent () != XmlNodeType.Element) {
  339. string error = String.Format ("'{0}' is an invalid node type.",
  340. NodeType.ToString ());
  341. throw XmlError (error);
  342. }
  343. string result = String.Empty;
  344. if (!IsEmptyElement) {
  345. Read ();
  346. result = ReadString ();
  347. if (NodeType != XmlNodeType.EndElement) {
  348. string error = String.Format ("'{0}' is an invalid node type.",
  349. NodeType.ToString ());
  350. throw XmlError (error);
  351. }
  352. }
  353. Read ();
  354. return result;
  355. }
  356. public virtual string ReadElementString (string name)
  357. {
  358. if (MoveToContent () != XmlNodeType.Element) {
  359. string error = String.Format ("'{0}' is an invalid node type.",
  360. NodeType.ToString ());
  361. throw XmlError (error);
  362. }
  363. if (name != Name) {
  364. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  365. Name, NamespaceURI);
  366. throw XmlError (error);
  367. }
  368. string result = String.Empty;
  369. if (!IsEmptyElement) {
  370. Read ();
  371. result = ReadString ();
  372. if (NodeType != XmlNodeType.EndElement) {
  373. string error = String.Format ("'{0}' is an invalid node type.",
  374. NodeType.ToString ());
  375. throw XmlError (error);
  376. }
  377. }
  378. Read ();
  379. return result;
  380. }
  381. public virtual string ReadElementString (string localName, string namespaceName)
  382. {
  383. if (MoveToContent () != XmlNodeType.Element) {
  384. string error = String.Format ("'{0}' is an invalid node type.",
  385. NodeType.ToString ());
  386. throw XmlError (error);
  387. }
  388. if (localName != LocalName || NamespaceURI != namespaceName) {
  389. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  390. LocalName, NamespaceURI);
  391. throw XmlError (error);
  392. }
  393. string result = String.Empty;
  394. if (!IsEmptyElement) {
  395. Read ();
  396. result = ReadString ();
  397. if (NodeType != XmlNodeType.EndElement) {
  398. string error = String.Format ("'{0}' is an invalid node type.",
  399. NodeType.ToString ());
  400. throw XmlError (error);
  401. }
  402. }
  403. Read ();
  404. return result;
  405. }
  406. public virtual void ReadEndElement ()
  407. {
  408. if (MoveToContent () != XmlNodeType.EndElement) {
  409. string error = String.Format ("'{0}' is an invalid node type.",
  410. NodeType.ToString ());
  411. throw XmlError (error);
  412. }
  413. Read ();
  414. }
  415. #if NET_1_0
  416. public abstract string ReadInnerXml ();
  417. public abstract string ReadOuterXml ();
  418. #else
  419. public virtual string ReadInnerXml ()
  420. {
  421. return ReadInnerXmlInternal ();
  422. }
  423. public virtual string ReadOuterXml ()
  424. {
  425. return ReadOuterXmlInternal ();
  426. }
  427. #endif
  428. internal string ReadInnerXmlInternal ()
  429. {
  430. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  431. return String.Empty;
  432. StringWriter sw = new StringWriter ();
  433. XmlTextWriter xtw = new XmlTextWriter (sw);
  434. if (NodeType == XmlNodeType.Element) {
  435. if (IsEmptyElement) {
  436. Read ();
  437. return String.Empty;
  438. }
  439. int startDepth = Depth;
  440. Read ();
  441. while (startDepth < Depth) {
  442. if (ReadState != ReadState.Interactive)
  443. throw XmlError ("Unexpected end of the XML reader.");
  444. xtw.WriteNode (this, false);
  445. }
  446. // reader is now end element, then proceed once more.
  447. Read ();
  448. }
  449. else
  450. xtw.WriteNode (this, false);
  451. return sw.ToString ();
  452. }
  453. internal string ReadOuterXmlInternal ()
  454. {
  455. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  456. return String.Empty;
  457. StringWriter sw = new StringWriter ();
  458. XmlTextWriter xtw = new XmlTextWriter (sw);
  459. xtw.WriteNode (this, false);
  460. return sw.ToString ();
  461. }
  462. public virtual void ReadStartElement ()
  463. {
  464. if (MoveToContent () != XmlNodeType.Element) {
  465. string error = String.Format ("'{0}' is an invalid node type.",
  466. NodeType.ToString ());
  467. throw XmlError (error);
  468. }
  469. Read ();
  470. }
  471. public virtual void ReadStartElement (string name)
  472. {
  473. if (MoveToContent () != XmlNodeType.Element) {
  474. string error = String.Format ("'{0}' is an invalid node type.",
  475. NodeType.ToString ());
  476. throw XmlError (error);
  477. }
  478. if (name != Name) {
  479. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  480. Name, NamespaceURI);
  481. throw XmlError (error);
  482. }
  483. Read ();
  484. }
  485. public virtual void ReadStartElement (string localName, string namespaceName)
  486. {
  487. if (MoveToContent () != XmlNodeType.Element) {
  488. string error = String.Format ("'{0}' is an invalid node type.",
  489. NodeType.ToString ());
  490. throw XmlError (error);
  491. }
  492. if (localName != LocalName || NamespaceURI != namespaceName) {
  493. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  494. localName, namespaceName,
  495. LocalName, NamespaceURI);
  496. throw XmlError (error);
  497. }
  498. Read ();
  499. }
  500. #if NET_1_0
  501. public abstract string ReadString ();
  502. #else
  503. public virtual string ReadString ()
  504. {
  505. return ReadStringInternal ();
  506. }
  507. #endif
  508. internal string ReadStringInternal ()
  509. {
  510. if (readStringBuffer == null)
  511. readStringBuffer = new StringBuilder ();
  512. readStringBuffer.Length = 0;
  513. MoveToElement ();
  514. switch (NodeType) {
  515. default:
  516. return String.Empty;
  517. case XmlNodeType.Element:
  518. if (IsEmptyElement)
  519. return String.Empty;
  520. do {
  521. Read ();
  522. switch (NodeType) {
  523. case XmlNodeType.Text:
  524. case XmlNodeType.CDATA:
  525. case XmlNodeType.Whitespace:
  526. case XmlNodeType.SignificantWhitespace:
  527. readStringBuffer.Append (Value);
  528. continue;
  529. }
  530. break;
  531. } while (true);
  532. break;
  533. case XmlNodeType.Text:
  534. case XmlNodeType.CDATA:
  535. case XmlNodeType.Whitespace:
  536. case XmlNodeType.SignificantWhitespace:
  537. do {
  538. switch (NodeType) {
  539. case XmlNodeType.Text:
  540. case XmlNodeType.CDATA:
  541. case XmlNodeType.Whitespace:
  542. case XmlNodeType.SignificantWhitespace:
  543. readStringBuffer.Append (Value);
  544. Read ();
  545. continue;
  546. }
  547. break;
  548. } while (true);
  549. break;
  550. }
  551. string ret = readStringBuffer.ToString ();
  552. readStringBuffer.Length = 0;
  553. return ret;
  554. }
  555. #if NET_2_0
  556. public virtual Type ValueType {
  557. get { return typeof (string); }
  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 virtual XmlReader ReadSubtree ()
  605. {
  606. return new SubtreeXmlReader (this);
  607. }
  608. [MonoTODO]
  609. [Obsolete]
  610. public virtual object ReadTypedValue ()
  611. {
  612. if (NodeType == XmlNodeType.Element)
  613. return ReadElementContentAs (ValueType, this as IXmlNamespaceResolver);
  614. else
  615. return ReadContentAs (ValueType, this as IXmlNamespaceResolver);
  616. }
  617. private string ReadContentString ()
  618. {
  619. switch (NodeType) {
  620. case XmlNodeType.Text:
  621. case XmlNodeType.CDATA:
  622. case XmlNodeType.SignificantWhitespace:
  623. case XmlNodeType.Whitespace:
  624. break;
  625. default:
  626. throw new InvalidOperationException (String.Format ("This method does not support node type {0}.", NodeType));
  627. }
  628. return ReadString ();
  629. }
  630. [MonoTODO]
  631. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
  632. {
  633. return ValueAs (ReadElementString (), type, resolver);
  634. }
  635. [MonoTODO]
  636. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
  637. {
  638. return ValueAs (ReadElementString (localName, namespaceURI), type, resolver);
  639. }
  640. [MonoTODO]
  641. public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
  642. {
  643. return ValueAs (ReadContentString (), type, resolver);
  644. }
  645. private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
  646. {
  647. try {
  648. if (type == typeof (XmlQualifiedName))
  649. return XmlQualifiedName.Parse (text, resolver);
  650. switch (Type.GetTypeCode (type)) {
  651. case TypeCode.Boolean:
  652. return ReadContentAsBoolean ();
  653. case TypeCode.DateTime:
  654. return ReadContentAsDateTime ();
  655. case TypeCode.Double:
  656. return ReadContentAsDouble ();
  657. case TypeCode.Int32:
  658. return ReadContentAsInt ();
  659. case TypeCode.String:
  660. return ReadContentAsString ();
  661. }
  662. } catch (Exception ex) {
  663. return new FormatException (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'.", text, type));
  664. }
  665. throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
  666. }
  667. [MonoTODO]
  668. public virtual bool ReadElementContentAsBoolean ()
  669. {
  670. return XQueryConvert.StringToBoolean (ReadElementString ());
  671. }
  672. [MonoTODO]
  673. public virtual DateTime ReadElementContentAsDateTime ()
  674. {
  675. return XQueryConvert.StringToDateTime (ReadElementString ());
  676. }
  677. [MonoTODO]
  678. public virtual double ReadElementContentAsDouble ()
  679. {
  680. return XQueryConvert.StringToDouble (ReadElementString ());
  681. }
  682. [MonoTODO]
  683. public virtual int ReadElementContentAsInt ()
  684. {
  685. return XQueryConvert.StringToInt (ReadElementString ());
  686. }
  687. [MonoTODO]
  688. public virtual string ReadElementContentAsString ()
  689. {
  690. return ReadElementString ();
  691. }
  692. [MonoTODO]
  693. public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
  694. {
  695. return XQueryConvert.StringToBoolean (ReadElementString (localName, namespaceURI));
  696. }
  697. [MonoTODO]
  698. public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
  699. {
  700. return XQueryConvert.StringToDateTime (ReadElementString (localName, namespaceURI));
  701. }
  702. [MonoTODO]
  703. public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
  704. {
  705. return XQueryConvert.StringToDouble (ReadElementString (localName, namespaceURI));
  706. }
  707. [MonoTODO]
  708. public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
  709. {
  710. return XQueryConvert.StringToInt (ReadElementString (localName, namespaceURI));
  711. }
  712. [MonoTODO]
  713. public virtual string ReadElementContentAsString (string localName, string namespaceURI)
  714. {
  715. return ReadElementString (localName, namespaceURI);
  716. }
  717. [MonoTODO]
  718. public virtual bool ReadContentAsBoolean ()
  719. {
  720. return XQueryConvert.StringToBoolean (ReadContentString ());
  721. }
  722. [MonoTODO]
  723. public virtual DateTime ReadContentAsDateTime ()
  724. {
  725. return XQueryConvert.StringToDateTime (ReadContentString ());
  726. }
  727. [MonoTODO]
  728. public virtual double ReadContentAsDouble ()
  729. {
  730. return XQueryConvert.StringToDouble (ReadContentString ());
  731. }
  732. [MonoTODO]
  733. public virtual int ReadContentAsInt ()
  734. {
  735. return XQueryConvert.StringToInt (ReadContentString ());
  736. }
  737. [MonoTODO]
  738. public virtual string ReadContentAsString ()
  739. {
  740. return ReadContentString ();
  741. }
  742. #endif
  743. public abstract void ResolveEntity ();
  744. public virtual void Skip ()
  745. {
  746. if (ReadState != ReadState.Interactive)
  747. return;
  748. MoveToElement ();
  749. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  750. Read ();
  751. return;
  752. }
  753. int depth = Depth;
  754. while (Read () && depth < Depth)
  755. ;
  756. if (NodeType == XmlNodeType.EndElement)
  757. Read ();
  758. }
  759. private XmlException XmlError (string message)
  760. {
  761. return new XmlException (this as IXmlLineInfo, BaseURI, message);
  762. }
  763. #endregion
  764. }
  765. }