XmlReader.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. using Mono.Xml.Schema; // only required for NET_2_0
  40. namespace System.Xml
  41. {
  42. #if NET_2_0
  43. public abstract class XmlReader : IDisposable
  44. #else
  45. public abstract class XmlReader
  46. #endif
  47. {
  48. private StringBuilder readStringBuffer;
  49. private XmlReaderBinarySupport binary;
  50. #if NET_2_0
  51. private XmlReaderSettings settings;
  52. #endif
  53. #region Constructor
  54. protected XmlReader ()
  55. {
  56. }
  57. #endregion
  58. #region Properties
  59. public abstract int AttributeCount { get; }
  60. public abstract string BaseURI { get; }
  61. internal XmlReaderBinarySupport Binary {
  62. get { return binary; }
  63. }
  64. internal XmlReaderBinarySupport.CharGetter BinaryCharGetter {
  65. get { return binary != null ? binary.Getter : null; }
  66. set {
  67. if (binary == null)
  68. binary = new XmlReaderBinarySupport (this);
  69. binary.Getter = value;
  70. }
  71. }
  72. #if NET_2_0
  73. // To enable it internally in sys.xml, just insert these
  74. // two lines into Read():
  75. //
  76. // #if NET_2_0
  77. // if (Binary != null)
  78. // Binary.Reset ();
  79. // #endif
  80. //
  81. public virtual bool CanReadBinaryContent {
  82. get { return false; }
  83. }
  84. public virtual bool CanReadValueChunk {
  85. get { return false; }
  86. }
  87. #else
  88. internal virtual bool CanReadBinaryContent {
  89. get { return false; }
  90. }
  91. internal virtual bool CanReadValueChunk {
  92. get { return false; }
  93. }
  94. #endif
  95. public virtual bool CanResolveEntity
  96. {
  97. get { return false; }
  98. }
  99. public abstract int Depth { get; }
  100. public abstract bool EOF { get; }
  101. public virtual bool HasAttributes
  102. {
  103. get { return AttributeCount > 0; }
  104. }
  105. public abstract bool HasValue { get; }
  106. #if NET_2_0
  107. public virtual bool IsDefault {
  108. get { return false; }
  109. }
  110. public virtual bool IsEmptyElement {
  111. get { return false; }
  112. }
  113. public virtual string this [int i] {
  114. get { return GetAttribute (i); }
  115. }
  116. public virtual string this [string name] {
  117. get { return GetAttribute (name); }
  118. }
  119. public virtual string this [string name, string namespaceURI] {
  120. get { return GetAttribute (name, namespaceURI); }
  121. }
  122. #else
  123. public abstract bool IsDefault { get; }
  124. public abstract bool IsEmptyElement { get; }
  125. public abstract string this [int i] { get; }
  126. public abstract string this [string name] { get; }
  127. public abstract string this [string localName, string namespaceName] { get; }
  128. #endif
  129. public abstract string LocalName { get; }
  130. public abstract string Name { get; }
  131. public abstract string NamespaceURI { get; }
  132. public abstract XmlNameTable NameTable { get; }
  133. public abstract XmlNodeType NodeType { get; }
  134. public abstract string Prefix { get; }
  135. #if NET_2_0
  136. public virtual char QuoteChar {
  137. get { return '\"'; }
  138. }
  139. #else
  140. public abstract char QuoteChar { get; }
  141. #endif
  142. public abstract ReadState ReadState { get; }
  143. #if NET_2_0
  144. public virtual IXmlSchemaInfo SchemaInfo {
  145. get { return null; }
  146. }
  147. public virtual XmlReaderSettings Settings {
  148. get { return settings; }
  149. }
  150. #endif
  151. public abstract string Value { get; }
  152. #if NET_2_0
  153. public virtual string XmlLang {
  154. get { return String.Empty; }
  155. }
  156. public virtual XmlSpace XmlSpace {
  157. get { return XmlSpace.None; }
  158. }
  159. #else
  160. public abstract string XmlLang { get; }
  161. public abstract XmlSpace XmlSpace { get; }
  162. #endif
  163. #endregion
  164. #region Methods
  165. public abstract void Close ();
  166. #if NET_2_0
  167. private static XmlNameTable PopulateNameTable (
  168. XmlReaderSettings settings)
  169. {
  170. XmlNameTable nameTable = settings.NameTable;
  171. if (nameTable == null)
  172. nameTable = new NameTable ();
  173. return nameTable;
  174. }
  175. private static XmlParserContext PopulateParserContext (
  176. XmlReaderSettings settings, string baseUri)
  177. {
  178. XmlNameTable nt = PopulateNameTable (settings);
  179. return new XmlParserContext (nt,
  180. new XmlNamespaceManager (nt),
  181. baseUri,
  182. XmlSpace.None);
  183. }
  184. private static XmlNodeType GetNodeType (
  185. XmlReaderSettings settings)
  186. {
  187. ConformanceLevel level = settings != null ? settings.ConformanceLevel : ConformanceLevel.Auto;
  188. return
  189. level == ConformanceLevel.Fragment ?
  190. XmlNodeType.Element :
  191. XmlNodeType.Element;
  192. }
  193. public static XmlReader Create (Stream stream)
  194. {
  195. return Create (stream, null);
  196. }
  197. public static XmlReader Create (string url)
  198. {
  199. return Create (url, null);
  200. }
  201. public static XmlReader Create (TextReader reader)
  202. {
  203. return Create (reader, null);
  204. }
  205. public static XmlReader Create (string url, XmlReaderSettings settings)
  206. {
  207. return Create (url, settings, null);
  208. }
  209. public static XmlReader Create (Stream stream, XmlReaderSettings settings)
  210. {
  211. return Create (stream, settings, String.Empty);
  212. }
  213. public static XmlReader Create (TextReader reader, XmlReaderSettings settings)
  214. {
  215. return Create (reader, settings, String.Empty);
  216. }
  217. public static XmlReader Create (Stream stream, XmlReaderSettings settings, string baseUri)
  218. {
  219. if (settings == null)
  220. settings = new XmlReaderSettings ();
  221. return Create (stream, settings,
  222. PopulateParserContext (settings, baseUri));
  223. }
  224. public static XmlReader Create (TextReader reader, XmlReaderSettings settings, string baseUri)
  225. {
  226. if (settings == null)
  227. settings = new XmlReaderSettings ();
  228. return Create (reader, settings,
  229. PopulateParserContext (settings, baseUri));
  230. }
  231. [MonoTODO ("ConformanceLevel")]
  232. public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
  233. {
  234. if (settings == null)
  235. settings = new XmlReaderSettings ();
  236. XmlReader r = CreateFilteredXmlReader (reader, settings);
  237. r.settings = settings;
  238. return r;
  239. }
  240. [MonoTODO ("ConformanceLevel")]
  241. public static XmlReader Create (string url, XmlReaderSettings settings, XmlParserContext context)
  242. {
  243. if (settings == null)
  244. settings = new XmlReaderSettings ();
  245. if (context == null)
  246. context = PopulateParserContext (settings, url);
  247. return CreateCustomizedTextReader (new XmlTextReader (url, GetNodeType (settings), context), settings);
  248. }
  249. [MonoTODO ("ConformanceLevel")]
  250. public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
  251. {
  252. if (settings == null)
  253. settings = new XmlReaderSettings ();
  254. if (context == null)
  255. context = PopulateParserContext (settings, String.Empty);
  256. return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings);
  257. }
  258. [MonoTODO ("ConformanceLevel")]
  259. public static XmlReader Create (TextReader reader, XmlReaderSettings settings, XmlParserContext context)
  260. {
  261. if (settings == null)
  262. settings = new XmlReaderSettings ();
  263. if (context == null)
  264. context = PopulateParserContext (settings, String.Empty);
  265. return CreateCustomizedTextReader (new XmlTextReader (context.BaseURI, reader, GetNodeType (settings), context), settings);
  266. }
  267. private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlReaderSettings settings)
  268. {
  269. reader.XmlResolver = settings.XmlResolver;
  270. // Normalization is set true by default.
  271. reader.Normalization = true;
  272. if (settings.ProhibitDtd)
  273. reader.ProhibitDtd = true;
  274. if (!settings.CheckCharacters)
  275. reader.CharacterChecking = false;
  276. // I guess it might be changed in 2.0 RTM to set true
  277. // as default, or just disappear. It goes against
  278. // XmlTextReader's default usage and users will have
  279. // to close input manually (that's annoying). Moreover,
  280. // MS XmlTextReader consumes text input more than
  281. // actually read and users can acquire those extra
  282. // consumption by GetRemainder() that returns different
  283. // TextReader.
  284. reader.CloseInput = settings.CloseInput;
  285. // I would like to support it in detail later;
  286. // MSDN description looks source of confusion. We don't
  287. // need examples, but precise list of how it works.
  288. reader.Conformance = settings.ConformanceLevel;
  289. reader.AdjustLineInfoOffset (settings.LineNumberOffset,
  290. settings.LinePositionOffset);
  291. if (settings.NameTable != null)
  292. reader.SetNameTable (settings.NameTable);
  293. XmlReader r = CreateFilteredXmlReader (reader, settings);
  294. r.settings = settings;
  295. return r;
  296. }
  297. private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlReaderSettings settings)
  298. {
  299. reader = CreateValidatingXmlReader (reader, settings);
  300. if (reader.Settings != null ||
  301. settings.IgnoreComments ||
  302. settings.IgnoreProcessingInstructions ||
  303. settings.IgnoreWhitespace)
  304. return new XmlFilterReader (reader, settings);
  305. else {
  306. reader.settings = settings;
  307. return reader;
  308. }
  309. }
  310. private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
  311. {
  312. XmlValidatingReader xvr = null;
  313. switch (settings.ValidationType) {
  314. case ValidationType.None:
  315. return reader;
  316. case ValidationType.DTD:
  317. xvr = new XmlValidatingReader (reader);
  318. xvr.XmlResolver = settings.XmlResolver;
  319. xvr.ValidationType = ValidationType.DTD;
  320. break;
  321. case ValidationType.Schema:
  322. // xvr = new XmlValidatingReader (reader);
  323. // xvr.ValidationType = ValidationType.Schema;
  324. return new XmlSchemaValidatingReader (reader, settings);
  325. case ValidationType.Auto:
  326. xvr = new XmlValidatingReader (reader);
  327. xvr.ValidationType = ValidationType.DTD;
  328. reader = xvr;
  329. goto case ValidationType.Schema;
  330. case ValidationType.XDR:
  331. throw new NotSupportedException ();
  332. }
  333. if (xvr != null)
  334. xvr.SetSchemas (settings.Schemas);
  335. // Actually I don't think they are treated in DTD validation though...
  336. if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessIdentityConstraints) == 0)
  337. throw new NotImplementedException ();
  338. if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessInlineSchema) != 0)
  339. throw new NotImplementedException ();
  340. if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessSchemaLocation) != 0)
  341. throw new NotImplementedException ();
  342. if ((settings.ValidationFlags & XmlSchemaValidationFlags.ProcessValidationWarnings) == 0)
  343. throw new NotImplementedException ();
  344. return xvr != null ? xvr : reader;
  345. }
  346. #endif
  347. #if NET_2_0
  348. void IDisposable.Dispose ()
  349. {
  350. Dispose (false);
  351. }
  352. protected virtual void Dispose (bool disposing)
  353. {
  354. if (ReadState != ReadState.Closed)
  355. Close ();
  356. }
  357. #endif
  358. public abstract string GetAttribute (int i);
  359. public abstract string GetAttribute (string name);
  360. public abstract string GetAttribute (
  361. string localName,
  362. string namespaceName);
  363. public static bool IsName (string s)
  364. {
  365. return s != null && XmlChar.IsName (s);
  366. }
  367. public static bool IsNameToken (string s)
  368. {
  369. return s != null && XmlChar.IsNmToken (s);
  370. }
  371. public virtual bool IsStartElement ()
  372. {
  373. return (MoveToContent () == XmlNodeType.Element);
  374. }
  375. public virtual bool IsStartElement (string name)
  376. {
  377. if (!IsStartElement ())
  378. return false;
  379. return (Name == name);
  380. }
  381. public virtual bool IsStartElement (string localName, string namespaceName)
  382. {
  383. if (!IsStartElement ())
  384. return false;
  385. return (LocalName == localName && NamespaceURI == namespaceName);
  386. }
  387. public abstract string LookupNamespace (string prefix);
  388. #if NET_2_0
  389. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  390. #else
  391. internal virtual string LookupNamespace (string prefix, bool atomizedNames)
  392. #endif
  393. {
  394. return LookupNamespace (prefix);
  395. }
  396. public abstract void MoveToAttribute (int i);
  397. public abstract bool MoveToAttribute (string name);
  398. public abstract bool MoveToAttribute (
  399. string localName,
  400. string namespaceName);
  401. private bool IsContent (XmlNodeType nodeType)
  402. {
  403. /* MS doc says:
  404. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  405. */
  406. switch (nodeType) {
  407. case XmlNodeType.Text:
  408. return true;
  409. case XmlNodeType.CDATA:
  410. return true;
  411. case XmlNodeType.Element:
  412. return true;
  413. case XmlNodeType.EndElement:
  414. return true;
  415. case XmlNodeType.EntityReference:
  416. return true;
  417. case XmlNodeType.EndEntity:
  418. return true;
  419. }
  420. return false;
  421. }
  422. public virtual XmlNodeType MoveToContent ()
  423. {
  424. if (NodeType == XmlNodeType.Attribute)
  425. MoveToElement ();
  426. do {
  427. if (IsContent (NodeType))
  428. return NodeType;
  429. Read ();
  430. } while (!EOF);
  431. return XmlNodeType.None;
  432. }
  433. public abstract bool MoveToElement ();
  434. public abstract bool MoveToFirstAttribute ();
  435. public abstract bool MoveToNextAttribute ();
  436. public abstract bool Read ();
  437. public abstract bool ReadAttributeValue ();
  438. public virtual string ReadElementString ()
  439. {
  440. if (MoveToContent () != XmlNodeType.Element) {
  441. string error = String.Format ("'{0}' is an invalid node type.",
  442. NodeType.ToString ());
  443. throw XmlError (error);
  444. }
  445. string result = String.Empty;
  446. if (!IsEmptyElement) {
  447. Read ();
  448. result = ReadString ();
  449. if (NodeType != XmlNodeType.EndElement) {
  450. string error = String.Format ("'{0}' is an invalid node type.",
  451. NodeType.ToString ());
  452. throw XmlError (error);
  453. }
  454. }
  455. Read ();
  456. return result;
  457. }
  458. public virtual string ReadElementString (string name)
  459. {
  460. if (MoveToContent () != XmlNodeType.Element) {
  461. string error = String.Format ("'{0}' is an invalid node type.",
  462. NodeType.ToString ());
  463. throw XmlError (error);
  464. }
  465. if (name != Name) {
  466. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  467. Name, NamespaceURI);
  468. throw XmlError (error);
  469. }
  470. string result = String.Empty;
  471. if (!IsEmptyElement) {
  472. Read ();
  473. result = ReadString ();
  474. if (NodeType != XmlNodeType.EndElement) {
  475. string error = String.Format ("'{0}' is an invalid node type.",
  476. NodeType.ToString ());
  477. throw XmlError (error);
  478. }
  479. }
  480. Read ();
  481. return result;
  482. }
  483. public virtual string ReadElementString (string localName, string namespaceName)
  484. {
  485. if (MoveToContent () != XmlNodeType.Element) {
  486. string error = String.Format ("'{0}' is an invalid node type.",
  487. NodeType.ToString ());
  488. throw XmlError (error);
  489. }
  490. if (localName != LocalName || NamespaceURI != namespaceName) {
  491. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  492. LocalName, NamespaceURI);
  493. throw XmlError (error);
  494. }
  495. string result = String.Empty;
  496. if (!IsEmptyElement) {
  497. Read ();
  498. result = ReadString ();
  499. if (NodeType != XmlNodeType.EndElement) {
  500. string error = String.Format ("'{0}' is an invalid node type.",
  501. NodeType.ToString ());
  502. throw XmlError (error);
  503. }
  504. }
  505. Read ();
  506. return result;
  507. }
  508. public virtual void ReadEndElement ()
  509. {
  510. if (MoveToContent () != XmlNodeType.EndElement) {
  511. string error = String.Format ("'{0}' is an invalid node type.",
  512. NodeType.ToString ());
  513. throw XmlError (error);
  514. }
  515. Read ();
  516. }
  517. #if NET_1_0
  518. public abstract string ReadInnerXml ();
  519. public abstract string ReadOuterXml ();
  520. #else
  521. public virtual string ReadInnerXml ()
  522. {
  523. return ReadInnerXmlInternal ();
  524. }
  525. public virtual string ReadOuterXml ()
  526. {
  527. return ReadOuterXmlInternal ();
  528. }
  529. #endif
  530. internal string ReadInnerXmlInternal ()
  531. {
  532. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  533. return String.Empty;
  534. StringWriter sw = new StringWriter ();
  535. XmlTextWriter xtw = new XmlTextWriter (sw);
  536. if (NodeType == XmlNodeType.Element) {
  537. if (IsEmptyElement) {
  538. Read ();
  539. return String.Empty;
  540. }
  541. int startDepth = Depth;
  542. Read ();
  543. while (startDepth < Depth) {
  544. if (ReadState != ReadState.Interactive)
  545. throw XmlError ("Unexpected end of the XML reader.");
  546. xtw.WriteNode (this, false);
  547. }
  548. // reader is now end element, then proceed once more.
  549. Read ();
  550. }
  551. else
  552. xtw.WriteNode (this, false);
  553. return sw.ToString ();
  554. }
  555. internal string ReadOuterXmlInternal ()
  556. {
  557. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  558. return String.Empty;
  559. StringWriter sw = new StringWriter ();
  560. XmlTextWriter xtw = new XmlTextWriter (sw);
  561. xtw.WriteNode (this, false);
  562. return sw.ToString ();
  563. }
  564. public virtual void ReadStartElement ()
  565. {
  566. if (MoveToContent () != XmlNodeType.Element) {
  567. string error = String.Format ("'{0}' is an invalid node type.",
  568. NodeType.ToString ());
  569. throw XmlError (error);
  570. }
  571. Read ();
  572. }
  573. public virtual void ReadStartElement (string name)
  574. {
  575. if (MoveToContent () != XmlNodeType.Element) {
  576. string error = String.Format ("'{0}' is an invalid node type.",
  577. NodeType.ToString ());
  578. throw XmlError (error);
  579. }
  580. if (name != Name) {
  581. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  582. Name, NamespaceURI);
  583. throw XmlError (error);
  584. }
  585. Read ();
  586. }
  587. public virtual void ReadStartElement (string localName, string namespaceName)
  588. {
  589. if (MoveToContent () != XmlNodeType.Element) {
  590. string error = String.Format ("'{0}' is an invalid node type.",
  591. NodeType.ToString ());
  592. throw XmlError (error);
  593. }
  594. if (localName != LocalName || NamespaceURI != namespaceName) {
  595. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  596. localName, namespaceName,
  597. LocalName, NamespaceURI);
  598. throw XmlError (error);
  599. }
  600. Read ();
  601. }
  602. #if NET_1_0
  603. public abstract string ReadString ();
  604. #else
  605. public virtual string ReadString ()
  606. {
  607. return ReadStringInternal ();
  608. }
  609. #endif
  610. internal string ReadStringInternal ()
  611. {
  612. if (readStringBuffer == null)
  613. readStringBuffer = new StringBuilder ();
  614. readStringBuffer.Length = 0;
  615. MoveToElement ();
  616. switch (NodeType) {
  617. default:
  618. return String.Empty;
  619. case XmlNodeType.Element:
  620. if (IsEmptyElement)
  621. return String.Empty;
  622. do {
  623. Read ();
  624. switch (NodeType) {
  625. case XmlNodeType.Text:
  626. case XmlNodeType.CDATA:
  627. case XmlNodeType.Whitespace:
  628. case XmlNodeType.SignificantWhitespace:
  629. readStringBuffer.Append (Value);
  630. continue;
  631. }
  632. break;
  633. } while (true);
  634. break;
  635. case XmlNodeType.Text:
  636. case XmlNodeType.CDATA:
  637. case XmlNodeType.Whitespace:
  638. case XmlNodeType.SignificantWhitespace:
  639. do {
  640. switch (NodeType) {
  641. case XmlNodeType.Text:
  642. case XmlNodeType.CDATA:
  643. case XmlNodeType.Whitespace:
  644. case XmlNodeType.SignificantWhitespace:
  645. readStringBuffer.Append (Value);
  646. Read ();
  647. continue;
  648. }
  649. break;
  650. } while (true);
  651. break;
  652. }
  653. string ret = readStringBuffer.ToString ();
  654. readStringBuffer.Length = 0;
  655. return ret;
  656. }
  657. #if NET_2_0
  658. public virtual Type ValueType {
  659. get { return typeof (string); }
  660. }
  661. [MonoTODO]
  662. public virtual bool ReadToDescendant (string name)
  663. {
  664. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  665. return false;
  666. int depth = Depth;
  667. for (Read (); depth < Depth; Read ())
  668. if (NodeType == XmlNodeType.Element && name == Name)
  669. return true;
  670. return false;
  671. }
  672. [MonoTODO]
  673. public virtual bool ReadToDescendant (string localName, string namespaceURI)
  674. {
  675. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  676. return false;
  677. int depth = Depth;
  678. for (Read (); depth < Depth; Read ())
  679. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  680. return true;
  681. return false;
  682. }
  683. [MonoTODO]
  684. public virtual bool ReadToFollowing (string name)
  685. {
  686. while (Read ())
  687. if (NodeType == XmlNodeType.Element && name == Name)
  688. return true;
  689. return false;
  690. }
  691. [MonoTODO]
  692. public virtual bool ReadToFollowing (string localName, string namespaceURI)
  693. {
  694. while (Read ())
  695. if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
  696. return true;
  697. return false;
  698. }
  699. [MonoTODO]
  700. public virtual bool ReadToNextSibling (string name)
  701. {
  702. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  703. return false;
  704. int depth = Depth;
  705. for (Skip (); depth < Depth; Skip ())
  706. if (NodeType == XmlNodeType.Element && name == Name)
  707. return true;
  708. return false;
  709. }
  710. [MonoTODO]
  711. public virtual bool ReadToNextSibling (string localName, string namespaceURI)
  712. {
  713. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  714. return false;
  715. int depth = Depth;
  716. for (Skip (); depth < Depth; Skip ())
  717. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  718. return true;
  719. return false;
  720. }
  721. [MonoTODO]
  722. public virtual XmlReader ReadSubtree ()
  723. {
  724. return new SubtreeXmlReader (this);
  725. }
  726. [MonoTODO]
  727. [Obsolete]
  728. public virtual object ReadTypedValue ()
  729. {
  730. if (NodeType == XmlNodeType.Element)
  731. return ReadElementContentAs (ValueType, this as IXmlNamespaceResolver);
  732. else
  733. return ReadContentAs (ValueType, this as IXmlNamespaceResolver);
  734. }
  735. private string ReadContentString ()
  736. {
  737. switch (NodeType) {
  738. case XmlNodeType.Text:
  739. case XmlNodeType.CDATA:
  740. case XmlNodeType.SignificantWhitespace:
  741. case XmlNodeType.Whitespace:
  742. break;
  743. default:
  744. throw new InvalidOperationException (String.Format ("This method does not support node type {0}.", NodeType));
  745. }
  746. return ReadString ();
  747. }
  748. [MonoTODO]
  749. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
  750. {
  751. return ValueAs (ReadElementString (), type, resolver);
  752. }
  753. [MonoTODO]
  754. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
  755. {
  756. return ValueAs (ReadElementString (localName, namespaceURI), type, resolver);
  757. }
  758. [MonoTODO]
  759. public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
  760. {
  761. return ValueAs (ReadContentString (), type, resolver);
  762. }
  763. private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
  764. {
  765. try {
  766. if (type == typeof (XmlQualifiedName))
  767. return XmlQualifiedName.Parse (text, resolver);
  768. switch (Type.GetTypeCode (type)) {
  769. case TypeCode.Boolean:
  770. return ReadContentAsBoolean ();
  771. case TypeCode.DateTime:
  772. return ReadContentAsDateTime ();
  773. case TypeCode.Decimal:
  774. return ReadContentAsDecimal ();
  775. case TypeCode.Double:
  776. return ReadContentAsDouble ();
  777. case TypeCode.Int32:
  778. return ReadContentAsInt ();
  779. case TypeCode.Int64:
  780. return ReadContentAsLong ();
  781. case TypeCode.Single:
  782. return ReadContentAsFloat ();
  783. case TypeCode.String:
  784. return ReadContentAsString ();
  785. }
  786. } catch (Exception ex) {
  787. return new FormatException (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'.", text, type));
  788. }
  789. throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
  790. }
  791. [MonoTODO]
  792. public virtual bool ReadElementContentAsBoolean ()
  793. {
  794. return XQueryConvert.StringToBoolean (ReadElementString ());
  795. }
  796. [MonoTODO]
  797. public virtual DateTime ReadElementContentAsDateTime ()
  798. {
  799. return XQueryConvert.StringToDateTime (ReadElementString ());
  800. }
  801. [MonoTODO]
  802. public virtual decimal ReadElementContentAsDecimal ()
  803. {
  804. return XQueryConvert.StringToDecimal (ReadElementString ());
  805. }
  806. [MonoTODO]
  807. public virtual double ReadElementContentAsDouble ()
  808. {
  809. return XQueryConvert.StringToDouble (ReadElementString ());
  810. }
  811. [MonoTODO]
  812. public virtual float ReadElementContentAsFloat ()
  813. {
  814. return XQueryConvert.StringToFloat (ReadElementString ());
  815. }
  816. [MonoTODO]
  817. public virtual int ReadElementContentAsInt ()
  818. {
  819. return XQueryConvert.StringToInt (ReadElementString ());
  820. }
  821. [MonoTODO]
  822. public virtual long ReadElementContentAsLong ()
  823. {
  824. return XQueryConvert.StringToInteger (ReadElementString ());
  825. }
  826. [MonoTODO]
  827. public virtual string ReadElementContentAsString ()
  828. {
  829. return ReadElementString ();
  830. }
  831. [MonoTODO]
  832. public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
  833. {
  834. return XQueryConvert.StringToBoolean (ReadElementString (localName, namespaceURI));
  835. }
  836. [MonoTODO]
  837. public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
  838. {
  839. return XQueryConvert.StringToDateTime (ReadElementString (localName, namespaceURI));
  840. }
  841. [MonoTODO]
  842. public virtual decimal ReadElementContentAsDecimal (string localName, string namespaceURI)
  843. {
  844. return XQueryConvert.StringToDecimal (ReadElementString (localName, namespaceURI));
  845. }
  846. [MonoTODO]
  847. public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
  848. {
  849. return XQueryConvert.StringToDouble (ReadElementString (localName, namespaceURI));
  850. }
  851. [MonoTODO]
  852. public virtual float ReadElementContentAsFloat (string localName, string namespaceURI)
  853. {
  854. return XQueryConvert.StringToFloat (ReadElementString (localName, namespaceURI));
  855. }
  856. [MonoTODO]
  857. public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
  858. {
  859. return XQueryConvert.StringToInt (ReadElementString (localName, namespaceURI));
  860. }
  861. [MonoTODO]
  862. public virtual long ReadElementContentAsLong (string localName, string namespaceURI)
  863. {
  864. return XQueryConvert.StringToInteger (ReadElementString (localName, namespaceURI));
  865. }
  866. [MonoTODO]
  867. public virtual string ReadElementContentAsString (string localName, string namespaceURI)
  868. {
  869. return ReadElementString (localName, namespaceURI);
  870. }
  871. [MonoTODO]
  872. public virtual bool ReadContentAsBoolean ()
  873. {
  874. return XQueryConvert.StringToBoolean (ReadContentString ());
  875. }
  876. [MonoTODO]
  877. public virtual DateTime ReadContentAsDateTime ()
  878. {
  879. return XQueryConvert.StringToDateTime (ReadContentString ());
  880. }
  881. [MonoTODO]
  882. public virtual decimal ReadContentAsDecimal ()
  883. {
  884. return XQueryConvert.StringToDecimal (ReadContentString ());
  885. }
  886. [MonoTODO]
  887. public virtual double ReadContentAsDouble ()
  888. {
  889. return XQueryConvert.StringToDouble (ReadContentString ());
  890. }
  891. [MonoTODO]
  892. public virtual float ReadContentAsFloat ()
  893. {
  894. return XQueryConvert.StringToFloat (ReadContentString ());
  895. }
  896. [MonoTODO]
  897. public virtual int ReadContentAsInt ()
  898. {
  899. return XQueryConvert.StringToInt (ReadContentString ());
  900. }
  901. [MonoTODO]
  902. public virtual long ReadContentAsLong ()
  903. {
  904. return XQueryConvert.StringToInteger (ReadContentString ());
  905. }
  906. [MonoTODO]
  907. public virtual string ReadContentAsString ()
  908. {
  909. return ReadContentString ();
  910. }
  911. public virtual int ReadContentAsBase64 (
  912. byte [] buffer, int offset, int length)
  913. {
  914. CheckSupport ();
  915. return binary.ReadContentAsBase64 (
  916. buffer, offset, length);
  917. }
  918. public virtual int ReadContentAsBinHex (
  919. byte [] buffer, int offset, int length)
  920. {
  921. CheckSupport ();
  922. return binary.ReadContentAsBinHex (
  923. buffer, offset, length);
  924. }
  925. public virtual int ReadElementContentAsBase64 (
  926. byte [] buffer, int offset, int length)
  927. {
  928. CheckSupport ();
  929. return binary.ReadElementContentAsBase64 (
  930. buffer, offset, length);
  931. }
  932. public virtual int ReadElementContentAsBinHex (
  933. byte [] buffer, int offset, int length)
  934. {
  935. CheckSupport ();
  936. return binary.ReadElementContentAsBinHex (
  937. buffer, offset, length);
  938. }
  939. #endif
  940. #if NET_2_0
  941. public virtual int ReadValueChunk (
  942. char [] buffer, int offset, int length)
  943. #else
  944. internal virtual int ReadValueChunk (
  945. char [] buffer, int offset, int length)
  946. #endif
  947. {
  948. if (!CanReadValueChunk)
  949. throw new NotSupportedException ();
  950. if (binary == null)
  951. binary = new XmlReaderBinarySupport (this);
  952. return binary.ReadValueChunk (buffer, offset, length);
  953. }
  954. private void CheckSupport ()
  955. {
  956. // Default implementation expects both.
  957. if (!CanReadBinaryContent || !CanReadValueChunk)
  958. throw new NotSupportedException ();
  959. if (binary == null)
  960. binary = new XmlReaderBinarySupport (this);
  961. }
  962. public abstract void ResolveEntity ();
  963. public virtual void Skip ()
  964. {
  965. if (ReadState != ReadState.Interactive)
  966. return;
  967. MoveToElement ();
  968. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  969. Read ();
  970. return;
  971. }
  972. int depth = Depth;
  973. while (Read () && depth < Depth)
  974. ;
  975. if (NodeType == XmlNodeType.EndElement)
  976. Read ();
  977. }
  978. private XmlException XmlError (string message)
  979. {
  980. return new XmlException (this as IXmlLineInfo, BaseURI, message);
  981. }
  982. #endregion
  983. }
  984. }