XmlNodeReaderImpl.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. //
  2. // System.Xml.XmlNodeReaderImpl.cs - implements the core part of XmlNodeReader
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2004 Novell Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // This serves the implementation part of XmlNodeReader except for
  31. // ResolveEntity().
  32. //
  33. using System;
  34. #if NET_2_0
  35. using System.Collections.Generic;
  36. #endif
  37. using System.Xml;
  38. using System.Text;
  39. using Mono.Xml;
  40. #if NET_2_0
  41. using System.Xml.Schema;
  42. #endif
  43. namespace System.Xml
  44. {
  45. #if NET_2_0
  46. internal class XmlNodeReaderImpl : XmlReader, IHasXmlParserContext, IXmlNamespaceResolver
  47. #else
  48. internal class XmlNodeReaderImpl : XmlReader, IHasXmlParserContext
  49. #endif
  50. {
  51. XmlDocument document;
  52. XmlNode startNode;
  53. XmlNode current;
  54. ReadState state = ReadState.Initial;
  55. int depth;
  56. bool isEndElement;
  57. bool alreadyRead;
  58. private XmlNode ownerLinkedNode {
  59. get {
  60. if (current.ParentNode != null && current.ParentNode.NodeType == XmlNodeType.Attribute)
  61. return ((XmlAttribute) current.ParentNode).OwnerElement;
  62. else if (current.NodeType == XmlNodeType.Attribute)
  63. return ((XmlAttribute) current).OwnerElement;
  64. else
  65. return current;
  66. }
  67. }
  68. #region Constructor
  69. internal XmlNodeReaderImpl (XmlNodeReaderImpl entityContainer)
  70. : this (entityContainer.current)
  71. {
  72. }
  73. public XmlNodeReaderImpl (XmlNode node)
  74. {
  75. startNode = node;
  76. depth = 0;
  77. document = startNode.NodeType == XmlNodeType.Document ?
  78. startNode as XmlDocument : startNode.OwnerDocument;
  79. switch (node.NodeType) {
  80. case XmlNodeType.Document:
  81. case XmlNodeType.DocumentFragment:
  82. case XmlNodeType.EntityReference:
  83. break;
  84. default:
  85. alreadyRead = true;
  86. break;
  87. }
  88. }
  89. #endregion
  90. #region Properties
  91. public override int AttributeCount {
  92. get {
  93. if (state != ReadState.Interactive)
  94. return 0;
  95. if (isEndElement || current == null)
  96. return 0;
  97. XmlNode n = ownerLinkedNode;
  98. return n.Attributes != null ? n.Attributes.Count : 0;
  99. }
  100. }
  101. public override string BaseURI {
  102. get {
  103. if (current == null)
  104. return startNode.BaseURI;
  105. return current.BaseURI;
  106. }
  107. }
  108. #if NET_2_0
  109. public override bool CanReadBinaryContent {
  110. get { return true; }
  111. }
  112. public override bool CanReadValueChunk {
  113. get { return true; }
  114. }
  115. #else
  116. internal override bool CanReadBinaryContent {
  117. get { return true; }
  118. }
  119. internal override bool CanReadValueChunk {
  120. get { return true; }
  121. }
  122. #endif
  123. public override bool CanResolveEntity {
  124. get { return false; }
  125. }
  126. public override int Depth {
  127. get {
  128. if (current == null)
  129. return 0;
  130. if (current.NodeType == XmlNodeType.Attribute)
  131. return depth + 1;
  132. if (current.ParentNode != null && current.ParentNode.NodeType == XmlNodeType.Attribute)
  133. return depth + 2;
  134. return depth;
  135. }
  136. }
  137. public override bool EOF {
  138. get {
  139. return this.ReadState == ReadState.EndOfFile
  140. || this.ReadState == ReadState.Error;
  141. }
  142. }
  143. public override bool HasAttributes {
  144. get {
  145. if (isEndElement || current == null)
  146. return false;
  147. // MS BUG: inconsistent return value between XmlTextReader and XmlNodeReader.
  148. // As for attribute and its descendants, XmlReader returns element's HasAttributes.
  149. XmlNode n = ownerLinkedNode;
  150. if (n.Attributes == null ||
  151. n.Attributes.Count == 0)
  152. return false;
  153. else
  154. return true;
  155. }
  156. }
  157. public override bool HasValue {
  158. get {
  159. if (current == null)
  160. return false;
  161. switch (current.NodeType) {
  162. case XmlNodeType.Element:
  163. case XmlNodeType.EntityReference:
  164. case XmlNodeType.Document:
  165. case XmlNodeType.DocumentFragment:
  166. case XmlNodeType.Notation:
  167. case XmlNodeType.EndElement:
  168. case XmlNodeType.EndEntity:
  169. return false;
  170. default:
  171. return true;
  172. }
  173. }
  174. }
  175. public override bool IsDefault {
  176. get {
  177. if (current == null)
  178. return false;
  179. if (current.NodeType != XmlNodeType.Attribute)
  180. return false;
  181. else
  182. {
  183. return !((XmlAttribute) current).Specified;
  184. }
  185. }
  186. }
  187. public override bool IsEmptyElement {
  188. get {
  189. if (current == null)
  190. return false;
  191. if(current.NodeType == XmlNodeType.Element)
  192. return ((XmlElement) current).IsEmpty;
  193. else
  194. return false;
  195. }
  196. }
  197. #if NET_2_0
  198. #else
  199. public override string this [int i] {
  200. get { return GetAttribute (i); }
  201. }
  202. public override string this [string name] {
  203. get { return GetAttribute (name); }
  204. }
  205. public override string this [string name, string namespaceURI] {
  206. get { return GetAttribute (name, namespaceURI); }
  207. }
  208. #endif
  209. public override string LocalName {
  210. get {
  211. if (current == null)
  212. return String.Empty;
  213. switch (current.NodeType) {
  214. case XmlNodeType.Attribute:
  215. case XmlNodeType.DocumentType:
  216. case XmlNodeType.Element:
  217. case XmlNodeType.EntityReference:
  218. case XmlNodeType.ProcessingInstruction:
  219. case XmlNodeType.XmlDeclaration:
  220. return current.LocalName;
  221. }
  222. return String.Empty;
  223. }
  224. }
  225. public override string Name {
  226. get {
  227. if (current == null)
  228. return String.Empty;
  229. switch (current.NodeType) {
  230. case XmlNodeType.Attribute:
  231. case XmlNodeType.DocumentType:
  232. case XmlNodeType.Element:
  233. case XmlNodeType.EntityReference:
  234. case XmlNodeType.ProcessingInstruction:
  235. case XmlNodeType.XmlDeclaration:
  236. return current.Name;
  237. }
  238. return String.Empty;
  239. }
  240. }
  241. public override string NamespaceURI {
  242. get {
  243. if (current == null)
  244. return String.Empty;
  245. return current.NamespaceURI;
  246. }
  247. }
  248. public override XmlNameTable NameTable {
  249. get { return document.NameTable; }
  250. }
  251. public override XmlNodeType NodeType {
  252. get {
  253. if (current == null)
  254. return XmlNodeType.None;
  255. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  256. }
  257. }
  258. public override string Prefix {
  259. get {
  260. if (current == null)
  261. return String.Empty;
  262. return current.Prefix;
  263. }
  264. }
  265. #if NET_2_0
  266. #else
  267. public override char QuoteChar {
  268. get {
  269. return '"';
  270. }
  271. }
  272. #endif
  273. public override ReadState ReadState {
  274. get { return state; }
  275. }
  276. #if NET_2_0
  277. public override IXmlSchemaInfo SchemaInfo {
  278. get { return current != null ? current.SchemaInfo : null; }
  279. }
  280. #endif
  281. public override string Value {
  282. get {
  283. if (NodeType == XmlNodeType.DocumentType)
  284. return ((XmlDocumentType) current).InternalSubset;
  285. else
  286. return HasValue ? current.Value : String.Empty;
  287. }
  288. }
  289. public override string XmlLang {
  290. get {
  291. if (current == null)
  292. return startNode.XmlLang;
  293. return current.XmlLang;
  294. }
  295. }
  296. public override XmlSpace XmlSpace {
  297. get {
  298. if (current == null)
  299. return startNode.XmlSpace;
  300. return current.XmlSpace;
  301. }
  302. }
  303. #endregion
  304. #region Methods
  305. public override void Close ()
  306. {
  307. current = null;
  308. state = ReadState.Closed;
  309. }
  310. public override string GetAttribute (int attributeIndex)
  311. {
  312. if (NodeType == XmlNodeType.XmlDeclaration) {
  313. XmlDeclaration decl = current as XmlDeclaration;
  314. if (attributeIndex == 0)
  315. return decl.Version;
  316. else if (attributeIndex == 1) {
  317. if (decl.Encoding != String.Empty)
  318. return decl.Encoding;
  319. else if (decl.Standalone != String.Empty)
  320. return decl.Standalone;
  321. }
  322. else if (attributeIndex == 2 &&
  323. decl.Encoding != String.Empty && decl.Standalone != null)
  324. return decl.Standalone;
  325. throw new ArgumentOutOfRangeException ("Index out of range.");
  326. } else if (NodeType == XmlNodeType.DocumentType) {
  327. XmlDocumentType doctype = current as XmlDocumentType;
  328. if (attributeIndex == 0) {
  329. if (doctype.PublicId != "")
  330. return doctype.PublicId;
  331. else if (doctype.SystemId != "")
  332. return doctype.SystemId;
  333. } else if (attributeIndex == 1)
  334. if (doctype.PublicId == "" && doctype.SystemId != "")
  335. return doctype.SystemId;
  336. throw new ArgumentOutOfRangeException ("Index out of range.");
  337. }
  338. // This is MS.NET bug which returns attributes in spite of EndElement.
  339. if (isEndElement || current == null)
  340. return null;
  341. if (attributeIndex < 0 || attributeIndex > AttributeCount)
  342. throw new ArgumentOutOfRangeException ("Index out of range.");
  343. return ownerLinkedNode.Attributes [attributeIndex].Value;
  344. }
  345. public override string GetAttribute (string name)
  346. {
  347. // This is MS.NET bug which returns attributes in spite of EndElement.
  348. if (isEndElement || current == null)
  349. return null;
  350. if (NodeType == XmlNodeType.XmlDeclaration)
  351. return GetXmlDeclarationAttribute (name);
  352. else if (NodeType == XmlNodeType.DocumentType)
  353. return GetDocumentTypeAttribute (name);
  354. if (ownerLinkedNode.Attributes == null)
  355. return null;
  356. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  357. if (attr == null)
  358. return null;
  359. else
  360. return attr.Value;
  361. }
  362. public override string GetAttribute (string name, string namespaceURI)
  363. {
  364. // This is MS.NET bug which returns attributes in spite of EndElement.
  365. if (isEndElement || current == null)
  366. return null;
  367. if (NodeType == XmlNodeType.XmlDeclaration)
  368. return GetXmlDeclarationAttribute (name);
  369. else if (NodeType == XmlNodeType.DocumentType)
  370. return GetDocumentTypeAttribute (name);
  371. if (ownerLinkedNode.Attributes == null)
  372. return null;
  373. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  374. if (attr == null)
  375. return null; // In fact MS.NET returns null instead of String.Empty.
  376. else
  377. return attr.Value;
  378. }
  379. private string GetXmlDeclarationAttribute (string name)
  380. {
  381. XmlDeclaration decl = current as XmlDeclaration;
  382. switch (name) {
  383. case "version":
  384. return decl.Version;
  385. case "encoding":
  386. // This is MS.NET bug that XmlNodeReturns in case of string.empty.
  387. return decl.Encoding != String.Empty ? decl.Encoding : null;
  388. case "standalone":
  389. return decl.Standalone;
  390. }
  391. return null;
  392. }
  393. private string GetDocumentTypeAttribute (string name)
  394. {
  395. XmlDocumentType doctype = current as XmlDocumentType;
  396. switch (name) {
  397. case "PUBLIC":
  398. return doctype.PublicId;
  399. case "SYSTEM":
  400. return doctype.SystemId;
  401. }
  402. return null;
  403. }
  404. XmlParserContext IHasXmlParserContext.ParserContext {
  405. get {
  406. return new XmlParserContext (document.NameTable,
  407. current.ConstructNamespaceManager (),
  408. document.DocumentType != null ? document.DocumentType.DTD : null,
  409. current.BaseURI, XmlLang, XmlSpace, Encoding.Unicode);
  410. }
  411. }
  412. #if NET_2_0
  413. public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  414. {
  415. IDictionary<string, string> table = new Dictionary<string, string> ();
  416. XmlNode n = current;
  417. do {
  418. if (n.NodeType == XmlNodeType.Document)
  419. break;
  420. for (int i = 0; i < current.Attributes.Count; i++) {
  421. XmlAttribute a = current.Attributes [i];
  422. if (a.NamespaceURI == XmlNamespaceManager.XmlnsXmlns)
  423. table.Add (a.Prefix == XmlNamespaceManager.PrefixXmlns ? a.LocalName : String.Empty, a.Value);
  424. }
  425. if (scope == XmlNamespaceScope.Local)
  426. return table;
  427. n = n.ParentNode;
  428. } while (n != null);
  429. if (scope == XmlNamespaceScope.All)
  430. table.Add (XmlNamespaceManager.PrefixXml, XmlNamespaceManager.XmlnsXml);
  431. return table;
  432. }
  433. #endif
  434. private XmlElement GetCurrentElement ()
  435. {
  436. XmlElement el = null;
  437. switch (current.NodeType) {
  438. case XmlNodeType.Attribute:
  439. el = ((XmlAttribute) current).OwnerElement;
  440. break;
  441. case XmlNodeType.Element:
  442. el = (XmlElement) current;
  443. break;
  444. case XmlNodeType.Text:
  445. case XmlNodeType.CDATA:
  446. case XmlNodeType.EntityReference:
  447. case XmlNodeType.Comment:
  448. case XmlNodeType.SignificantWhitespace:
  449. case XmlNodeType.Whitespace:
  450. case XmlNodeType.ProcessingInstruction:
  451. el = current.ParentNode as XmlElement;
  452. break;
  453. }
  454. return el;
  455. }
  456. public override string LookupNamespace (string prefix)
  457. {
  458. if (current == null)
  459. return null;
  460. XmlElement el = GetCurrentElement ();
  461. for (; el != null; el = el.ParentNode as XmlElement) {
  462. for (int i = 0; i < el.Attributes.Count; i++) {
  463. XmlAttribute attr = el.Attributes [i];
  464. if (attr.NamespaceURI != XmlNamespaceManager.XmlnsXmlns)
  465. continue;
  466. if (prefix == "") {
  467. if (attr.Prefix == "")
  468. return attr.Value;
  469. }
  470. else if (attr.LocalName == prefix)
  471. return attr.Value;
  472. continue;
  473. }
  474. }
  475. switch (prefix) {
  476. case XmlNamespaceManager.PrefixXml:
  477. return XmlNamespaceManager.XmlnsXml;
  478. case XmlNamespaceManager.PrefixXmlns:
  479. return XmlNamespaceManager.XmlnsXmlns;
  480. }
  481. return null;
  482. }
  483. #if NET_2_0
  484. public string LookupPrefix (string ns)
  485. {
  486. return LookupPrefix (ns, false);
  487. }
  488. public string LookupPrefix (string ns, bool atomizedNames)
  489. {
  490. if (current == null)
  491. return null;
  492. XmlElement el = GetCurrentElement ();
  493. for (; el != null; el = el.ParentNode as XmlElement) {
  494. for (int i = 0; i < el.Attributes.Count; i++) {
  495. XmlAttribute attr = el.Attributes [i];
  496. if (atomizedNames) {
  497. if (!Object.ReferenceEquals (attr.NamespaceURI, XmlNamespaceManager.XmlnsXmlns))
  498. continue;
  499. if (Object.ReferenceEquals (attr.Value, ns))
  500. // xmlns:blah="..." -> LocalName, xmlns="..." -> String.Empty
  501. return attr.Prefix != String.Empty ? attr.LocalName : String.Empty;
  502. } else {
  503. if (attr.NamespaceURI != XmlNamespaceManager.XmlnsXmlns)
  504. continue;
  505. if (attr.Value == ns)
  506. // xmlns:blah="..." -> LocalName, xmlns="..." -> String.Empty
  507. return attr.Prefix != String.Empty ? attr.LocalName : String.Empty;
  508. }
  509. }
  510. }
  511. switch (ns) {
  512. case XmlNamespaceManager.XmlnsXml:
  513. return XmlNamespaceManager.PrefixXml;
  514. case XmlNamespaceManager.XmlnsXmlns:
  515. return XmlNamespaceManager.PrefixXmlns;
  516. }
  517. return null;
  518. }
  519. #endif
  520. public override void MoveToAttribute (int attributeIndex)
  521. {
  522. if (isEndElement || attributeIndex < 0 || attributeIndex > AttributeCount)
  523. throw new ArgumentOutOfRangeException ();
  524. state = ReadState.Interactive;
  525. current = ownerLinkedNode.Attributes [attributeIndex];
  526. }
  527. public override bool MoveToAttribute (string name)
  528. {
  529. if (isEndElement || current == null)
  530. return false;
  531. XmlNode tmpCurrent = current;
  532. if (current.ParentNode.NodeType == XmlNodeType.Attribute)
  533. current = current.ParentNode;
  534. if (ownerLinkedNode.Attributes == null)
  535. return false;
  536. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  537. if (attr == null) {
  538. current = tmpCurrent;
  539. return false;
  540. }
  541. else {
  542. current = attr;
  543. return true;
  544. }
  545. }
  546. public override bool MoveToAttribute (string name, string namespaceURI)
  547. {
  548. if (isEndElement || current == null)
  549. return false;
  550. if (ownerLinkedNode.Attributes == null)
  551. return false;
  552. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  553. if (attr == null)
  554. return false;
  555. else {
  556. current = attr;
  557. return true;
  558. }
  559. }
  560. private void MoveToParentElement ()
  561. {
  562. // This is buggy. It is not only the case when EndElement = true.
  563. isEndElement = true;
  564. depth--;
  565. current = current.ParentNode;
  566. }
  567. public override bool MoveToElement ()
  568. {
  569. if (current == null)
  570. return false;
  571. XmlNode n = ownerLinkedNode;
  572. if (current != n) {
  573. current = n;
  574. return true;
  575. } else
  576. return false;
  577. }
  578. public override bool MoveToFirstAttribute ()
  579. {
  580. if (current == null)
  581. return false;
  582. if (ownerLinkedNode.Attributes == null)
  583. return false;
  584. if(ownerLinkedNode.Attributes.Count > 0)
  585. {
  586. current = ownerLinkedNode.Attributes [0];
  587. return true;
  588. }
  589. else
  590. return false;
  591. }
  592. public override bool MoveToNextAttribute ()
  593. {
  594. if (current == null)
  595. return false;
  596. if (current.NodeType != XmlNodeType.Attribute)
  597. return MoveToFirstAttribute ();
  598. else
  599. {
  600. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  601. for (int i=0; i<ac.Count-1; i++)
  602. {
  603. XmlAttribute attr = ac [i];
  604. if (attr == current)
  605. {
  606. i++;
  607. if (i == ac.Count)
  608. return false;
  609. current = ac [i];
  610. return true;
  611. }
  612. }
  613. return false;
  614. }
  615. }
  616. private bool MoveToNextSibling ()
  617. {
  618. if (alreadyRead) {
  619. alreadyRead = false;
  620. return current != null;
  621. }
  622. if (current.NextSibling != null) {
  623. isEndElement = false;
  624. current = current.NextSibling;
  625. } else {
  626. MoveToParentElement ();
  627. }
  628. if (current == null) {
  629. state = ReadState.EndOfFile;
  630. return false;
  631. }
  632. else
  633. return true;
  634. }
  635. public override bool Read ()
  636. {
  637. if (EOF)
  638. return false;
  639. #if NET_2_0
  640. if (Binary != null)
  641. Binary.Reset ();
  642. #endif
  643. if (ReadState == ReadState.Initial) {
  644. current = startNode;
  645. state = ReadState.Interactive;
  646. // when startNode is document or fragment
  647. if (!alreadyRead)
  648. current = startNode.FirstChild;
  649. else
  650. alreadyRead = false;
  651. if (current == null) {
  652. state = ReadState.Error;
  653. return false;
  654. } else
  655. return true;
  656. }
  657. MoveToElement ();
  658. if (alreadyRead) {
  659. alreadyRead = false;
  660. return current != null;
  661. }
  662. bool isEnd = false;
  663. if (IsEmptyElement || isEndElement) {
  664. // Then go up and move to next.
  665. // If no more nodes, then set EOF.
  666. isEndElement = false;
  667. if (current.ParentNode == null
  668. || current.ParentNode.NodeType == XmlNodeType.Document
  669. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment
  670. || current == startNode) {
  671. isEnd = true;
  672. } else if (current.NextSibling == null) {
  673. depth--;
  674. current = current.ParentNode;
  675. isEndElement = true;
  676. return true;
  677. } else {
  678. current = current.NextSibling;
  679. return true;
  680. }
  681. }
  682. if (current.NextSibling == null
  683. && current.ParentNode is XmlEntityReference)
  684. isEnd = true;
  685. if (isEnd) {
  686. current = null;
  687. state = ReadState.EndOfFile;
  688. return false;
  689. }
  690. if (!isEndElement && current.FirstChild != null && current.NodeType != XmlNodeType.EntityReference) {
  691. isEndElement = false;
  692. current = current.FirstChild;
  693. depth++;
  694. } else if (current.NodeType == XmlNodeType.Element) {
  695. isEndElement = true;
  696. if (current.FirstChild != null)
  697. depth--;
  698. } else
  699. MoveToNextSibling ();
  700. return current != null;
  701. }
  702. public override bool ReadAttributeValue ()
  703. {
  704. if (current.NodeType == XmlNodeType.Attribute) {
  705. if (current.FirstChild == null)
  706. return false;
  707. current = current.FirstChild;
  708. return true;
  709. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  710. if (current.NextSibling == null)
  711. return false;
  712. current = current.NextSibling;
  713. return true;
  714. } else
  715. return false;
  716. }
  717. public override string ReadString ()
  718. {
  719. return base.ReadString ();
  720. }
  721. public override void ResolveEntity ()
  722. {
  723. throw new NotSupportedException ("Should not happen.");
  724. }
  725. public override void Skip ()
  726. {
  727. // Why is this overriden? Such skipping might raise
  728. // (or ignore) unexpected validation error.
  729. base.Skip ();
  730. }
  731. #endregion
  732. }
  733. }