XmlNodeReaderImpl.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. XmlNode ownerLinkedNode;
  55. ReadState state = ReadState.Initial;
  56. int depth;
  57. bool isEndElement;
  58. bool ignoreStartNode;
  59. #region Constructor
  60. internal XmlNodeReaderImpl (XmlNodeReaderImpl entityContainer)
  61. : this (entityContainer.current)
  62. {
  63. }
  64. public XmlNodeReaderImpl (XmlNode node)
  65. {
  66. startNode = node;
  67. depth = 0;
  68. document = startNode.NodeType == XmlNodeType.Document ?
  69. startNode as XmlDocument : startNode.OwnerDocument;
  70. switch (node.NodeType) {
  71. case XmlNodeType.Document:
  72. case XmlNodeType.DocumentFragment:
  73. case XmlNodeType.EntityReference:
  74. ignoreStartNode = true;
  75. break;
  76. }
  77. }
  78. #endregion
  79. #region Properties
  80. public override int AttributeCount {
  81. get {
  82. if (state != ReadState.Interactive)
  83. return 0;
  84. if (isEndElement || current == null)
  85. return 0;
  86. XmlNode n = ownerLinkedNode;
  87. return n.Attributes != null ? n.Attributes.Count : 0;
  88. }
  89. }
  90. public override string BaseURI {
  91. get {
  92. if (current == null)
  93. return startNode.BaseURI;
  94. return current.BaseURI;
  95. }
  96. }
  97. #if NET_2_0
  98. public override bool CanReadBinaryContent {
  99. get { return true; }
  100. }
  101. public override bool CanReadValueChunk {
  102. get { return true; }
  103. }
  104. #else
  105. internal override bool CanReadBinaryContent {
  106. get { return true; }
  107. }
  108. internal override bool CanReadValueChunk {
  109. get { return true; }
  110. }
  111. #endif
  112. public override bool CanResolveEntity {
  113. get { return false; }
  114. }
  115. public override int Depth {
  116. get {
  117. return current == null ? 0 :
  118. current == ownerLinkedNode ? depth :
  119. current.NodeType == XmlNodeType.Attribute ? depth + 1 :
  120. depth + 2;
  121. }
  122. }
  123. public override bool EOF {
  124. get { return state == ReadState.EndOfFile || state == ReadState.Error; }
  125. }
  126. public override bool HasAttributes {
  127. get {
  128. if (isEndElement || current == null)
  129. return false;
  130. // MS BUG: inconsistent return value between XmlTextReader and XmlNodeReader.
  131. // As for attribute and its descendants, XmlReader returns element's HasAttributes.
  132. XmlNode n = ownerLinkedNode;
  133. if (n.Attributes == null ||
  134. n.Attributes.Count == 0)
  135. return false;
  136. else
  137. return true;
  138. }
  139. }
  140. #if !NET_2_1
  141. public override bool HasValue {
  142. get {
  143. if (current == null)
  144. return false;
  145. switch (current.NodeType) {
  146. case XmlNodeType.Element:
  147. case XmlNodeType.EntityReference:
  148. case XmlNodeType.Document:
  149. case XmlNodeType.DocumentFragment:
  150. case XmlNodeType.Notation:
  151. case XmlNodeType.EndElement:
  152. case XmlNodeType.EndEntity:
  153. return false;
  154. default:
  155. return true;
  156. }
  157. }
  158. }
  159. #endif
  160. public override bool IsDefault {
  161. get {
  162. if (current == null)
  163. return false;
  164. if (current.NodeType != XmlNodeType.Attribute)
  165. return false;
  166. else
  167. {
  168. return !((XmlAttribute) current).Specified;
  169. }
  170. }
  171. }
  172. public override bool IsEmptyElement {
  173. get {
  174. if (current == null)
  175. return false;
  176. if(current.NodeType == XmlNodeType.Element)
  177. return ((XmlElement) current).IsEmpty;
  178. else
  179. return false;
  180. }
  181. }
  182. #if NET_2_0
  183. #else
  184. public override string this [int i] {
  185. get { return GetAttribute (i); }
  186. }
  187. public override string this [string name] {
  188. get { return GetAttribute (name); }
  189. }
  190. public override string this [string name, string namespaceURI] {
  191. get { return GetAttribute (name, namespaceURI); }
  192. }
  193. #endif
  194. public override string LocalName {
  195. get {
  196. if (current == null)
  197. return String.Empty;
  198. switch (current.NodeType) {
  199. case XmlNodeType.Attribute:
  200. case XmlNodeType.DocumentType:
  201. case XmlNodeType.Element:
  202. case XmlNodeType.EntityReference:
  203. case XmlNodeType.ProcessingInstruction:
  204. case XmlNodeType.XmlDeclaration:
  205. return current.LocalName;
  206. }
  207. return String.Empty;
  208. }
  209. }
  210. public override string Name {
  211. get {
  212. if (current == null)
  213. return String.Empty;
  214. switch (current.NodeType) {
  215. case XmlNodeType.Attribute:
  216. case XmlNodeType.DocumentType:
  217. case XmlNodeType.Element:
  218. case XmlNodeType.EntityReference:
  219. case XmlNodeType.ProcessingInstruction:
  220. case XmlNodeType.XmlDeclaration:
  221. return current.Name;
  222. }
  223. return String.Empty;
  224. }
  225. }
  226. public override string NamespaceURI {
  227. get {
  228. if (current == null)
  229. return String.Empty;
  230. return current.NamespaceURI;
  231. }
  232. }
  233. public override XmlNameTable NameTable {
  234. get { return document.NameTable; }
  235. }
  236. public override XmlNodeType NodeType {
  237. get {
  238. if (current == null)
  239. return XmlNodeType.None;
  240. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  241. }
  242. }
  243. public override string Prefix {
  244. get {
  245. if (current == null)
  246. return String.Empty;
  247. return current.Prefix;
  248. }
  249. }
  250. #if NET_2_0
  251. #else
  252. public override char QuoteChar {
  253. get {
  254. return '"';
  255. }
  256. }
  257. #endif
  258. public override ReadState ReadState {
  259. get { return state; }
  260. }
  261. #if NET_2_0
  262. public override IXmlSchemaInfo SchemaInfo {
  263. get { return current != null ? current.SchemaInfo : null; }
  264. }
  265. #endif
  266. public override string Value {
  267. get {
  268. if (NodeType == XmlNodeType.DocumentType)
  269. return ((XmlDocumentType) current).InternalSubset;
  270. else
  271. return HasValue ? current.Value : String.Empty;
  272. }
  273. }
  274. public override string XmlLang {
  275. get {
  276. if (current == null)
  277. return startNode.XmlLang;
  278. return current.XmlLang;
  279. }
  280. }
  281. public override XmlSpace XmlSpace {
  282. get {
  283. if (current == null)
  284. return startNode.XmlSpace;
  285. return current.XmlSpace;
  286. }
  287. }
  288. #endregion
  289. #region Methods
  290. public override void Close ()
  291. {
  292. current = null;
  293. state = ReadState.Closed;
  294. }
  295. public override string GetAttribute (int attributeIndex)
  296. {
  297. if (NodeType == XmlNodeType.XmlDeclaration) {
  298. XmlDeclaration decl = current as XmlDeclaration;
  299. if (attributeIndex == 0)
  300. return decl.Version;
  301. else if (attributeIndex == 1) {
  302. if (decl.Encoding != String.Empty)
  303. return decl.Encoding;
  304. else if (decl.Standalone != String.Empty)
  305. return decl.Standalone;
  306. }
  307. else if (attributeIndex == 2 &&
  308. decl.Encoding != String.Empty && decl.Standalone != null)
  309. return decl.Standalone;
  310. throw new ArgumentOutOfRangeException ("Index out of range.");
  311. } else if (NodeType == XmlNodeType.DocumentType) {
  312. XmlDocumentType doctype = current as XmlDocumentType;
  313. if (attributeIndex == 0) {
  314. if (doctype.PublicId != "")
  315. return doctype.PublicId;
  316. else if (doctype.SystemId != "")
  317. return doctype.SystemId;
  318. } else if (attributeIndex == 1)
  319. if (doctype.PublicId == "" && doctype.SystemId != "")
  320. return doctype.SystemId;
  321. throw new ArgumentOutOfRangeException ("Index out of range.");
  322. }
  323. // This is MS.NET bug which returns attributes in spite of EndElement.
  324. if (isEndElement || current == null)
  325. return null;
  326. if (attributeIndex < 0 || attributeIndex > AttributeCount)
  327. throw new ArgumentOutOfRangeException ("Index out of range.");
  328. return ownerLinkedNode.Attributes [attributeIndex].Value;
  329. }
  330. public override string GetAttribute (string name)
  331. {
  332. // This is MS.NET bug which returns attributes in spite of EndElement.
  333. if (isEndElement || current == null)
  334. return null;
  335. if (NodeType == XmlNodeType.XmlDeclaration)
  336. return GetXmlDeclarationAttribute (name);
  337. else if (NodeType == XmlNodeType.DocumentType)
  338. return GetDocumentTypeAttribute (name);
  339. if (ownerLinkedNode.Attributes == null)
  340. return null;
  341. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  342. if (attr == null)
  343. return null;
  344. else
  345. return attr.Value;
  346. }
  347. public override string GetAttribute (string name, string namespaceURI)
  348. {
  349. // This is MS.NET bug which returns attributes in spite of EndElement.
  350. if (isEndElement || current == null)
  351. return null;
  352. if (NodeType == XmlNodeType.XmlDeclaration)
  353. return GetXmlDeclarationAttribute (name);
  354. else if (NodeType == XmlNodeType.DocumentType)
  355. return GetDocumentTypeAttribute (name);
  356. if (ownerLinkedNode.Attributes == null)
  357. return null;
  358. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  359. if (attr == null)
  360. return null; // In fact MS.NET returns null instead of String.Empty.
  361. else
  362. return attr.Value;
  363. }
  364. private string GetXmlDeclarationAttribute (string name)
  365. {
  366. XmlDeclaration decl = current as XmlDeclaration;
  367. switch (name) {
  368. case "version":
  369. return decl.Version;
  370. case "encoding":
  371. // This is MS.NET bug that XmlNodeReturns in case of string.empty.
  372. return decl.Encoding != String.Empty ? decl.Encoding : null;
  373. case "standalone":
  374. return decl.Standalone;
  375. }
  376. return null;
  377. }
  378. private string GetDocumentTypeAttribute (string name)
  379. {
  380. XmlDocumentType doctype = current as XmlDocumentType;
  381. switch (name) {
  382. case "PUBLIC":
  383. return doctype.PublicId;
  384. case "SYSTEM":
  385. return doctype.SystemId;
  386. }
  387. return null;
  388. }
  389. XmlParserContext IHasXmlParserContext.ParserContext {
  390. get {
  391. return new XmlParserContext (document.NameTable,
  392. current == null ?
  393. new XmlNamespaceManager (document.NameTable) :
  394. current.ConstructNamespaceManager (),
  395. document.DocumentType != null ? document.DocumentType.DTD : null,
  396. current == null ? document.BaseURI : current.BaseURI,
  397. XmlLang, XmlSpace, Encoding.Unicode);
  398. }
  399. }
  400. #if NET_2_0
  401. public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  402. {
  403. IDictionary<string, string> table = new Dictionary<string, string> ();
  404. XmlNode n = current;
  405. do {
  406. if (n.NodeType == XmlNodeType.Document)
  407. break;
  408. for (int i = 0; i < current.Attributes.Count; i++) {
  409. XmlAttribute a = current.Attributes [i];
  410. if (a.NamespaceURI == XmlNamespaceManager.XmlnsXmlns)
  411. table.Add (a.Prefix == XmlNamespaceManager.PrefixXmlns ? a.LocalName : String.Empty, a.Value);
  412. }
  413. if (scope == XmlNamespaceScope.Local)
  414. return table;
  415. n = n.ParentNode;
  416. } while (n != null);
  417. if (scope == XmlNamespaceScope.All)
  418. table.Add (XmlNamespaceManager.PrefixXml, XmlNamespaceManager.XmlnsXml);
  419. return table;
  420. }
  421. #endif
  422. private XmlElement GetCurrentElement ()
  423. {
  424. XmlElement el = null;
  425. switch (current.NodeType) {
  426. case XmlNodeType.Attribute:
  427. el = ((XmlAttribute) current).OwnerElement;
  428. break;
  429. case XmlNodeType.Element:
  430. el = (XmlElement) current;
  431. break;
  432. case XmlNodeType.Text:
  433. case XmlNodeType.CDATA:
  434. case XmlNodeType.EntityReference:
  435. case XmlNodeType.Comment:
  436. case XmlNodeType.SignificantWhitespace:
  437. case XmlNodeType.Whitespace:
  438. case XmlNodeType.ProcessingInstruction:
  439. el = current.ParentNode as XmlElement;
  440. break;
  441. }
  442. return el;
  443. }
  444. public override string LookupNamespace (string prefix)
  445. {
  446. if (current == null)
  447. return null;
  448. XmlElement el = GetCurrentElement ();
  449. for (; el != null; el = el.ParentNode as XmlElement) {
  450. for (int i = 0; i < el.Attributes.Count; i++) {
  451. XmlAttribute attr = el.Attributes [i];
  452. if (attr.NamespaceURI != XmlNamespaceManager.XmlnsXmlns)
  453. continue;
  454. if (prefix == "") {
  455. if (attr.Prefix == "")
  456. return attr.Value;
  457. }
  458. else if (attr.LocalName == prefix)
  459. return attr.Value;
  460. continue;
  461. }
  462. }
  463. switch (prefix) {
  464. case XmlNamespaceManager.PrefixXml:
  465. return XmlNamespaceManager.XmlnsXml;
  466. case XmlNamespaceManager.PrefixXmlns:
  467. return XmlNamespaceManager.XmlnsXmlns;
  468. }
  469. return null;
  470. }
  471. #if NET_2_0
  472. public string LookupPrefix (string ns)
  473. {
  474. return LookupPrefix (ns, false);
  475. }
  476. public string LookupPrefix (string ns, bool atomizedNames)
  477. {
  478. if (current == null)
  479. return null;
  480. XmlElement el = GetCurrentElement ();
  481. for (; el != null; el = el.ParentNode as XmlElement) {
  482. for (int i = 0; i < el.Attributes.Count; i++) {
  483. XmlAttribute attr = el.Attributes [i];
  484. if (atomizedNames) {
  485. if (!Object.ReferenceEquals (attr.NamespaceURI, XmlNamespaceManager.XmlnsXmlns))
  486. continue;
  487. if (Object.ReferenceEquals (attr.Value, ns))
  488. // xmlns:blah="..." -> LocalName, xmlns="..." -> String.Empty
  489. return attr.Prefix != String.Empty ? attr.LocalName : String.Empty;
  490. } else {
  491. if (attr.NamespaceURI != XmlNamespaceManager.XmlnsXmlns)
  492. continue;
  493. if (attr.Value == ns)
  494. // xmlns:blah="..." -> LocalName, xmlns="..." -> String.Empty
  495. return attr.Prefix != String.Empty ? attr.LocalName : String.Empty;
  496. }
  497. }
  498. }
  499. switch (ns) {
  500. case XmlNamespaceManager.XmlnsXml:
  501. return XmlNamespaceManager.PrefixXml;
  502. case XmlNamespaceManager.XmlnsXmlns:
  503. return XmlNamespaceManager.PrefixXmlns;
  504. }
  505. return null;
  506. }
  507. #endif
  508. public override void MoveToAttribute (int attributeIndex)
  509. {
  510. if (isEndElement || attributeIndex < 0 || attributeIndex > AttributeCount)
  511. throw new ArgumentOutOfRangeException ();
  512. state = ReadState.Interactive;
  513. current = ownerLinkedNode.Attributes [attributeIndex];
  514. }
  515. public override bool MoveToAttribute (string name)
  516. {
  517. if (isEndElement || current == null)
  518. return false;
  519. XmlNode tmpCurrent = current;
  520. if (current.ParentNode.NodeType == XmlNodeType.Attribute)
  521. current = current.ParentNode;
  522. if (ownerLinkedNode.Attributes == null)
  523. return false;
  524. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  525. if (attr == null) {
  526. current = tmpCurrent;
  527. return false;
  528. }
  529. else {
  530. current = attr;
  531. return true;
  532. }
  533. }
  534. public override bool MoveToAttribute (string name, string namespaceURI)
  535. {
  536. if (isEndElement || current == null)
  537. return false;
  538. if (ownerLinkedNode.Attributes == null)
  539. return false;
  540. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  541. if (attr == null)
  542. return false;
  543. else {
  544. current = attr;
  545. return true;
  546. }
  547. }
  548. public override bool MoveToElement ()
  549. {
  550. if (current == null)
  551. return false;
  552. XmlNode n = ownerLinkedNode;
  553. if (current != n) {
  554. current = n;
  555. return true;
  556. } else
  557. return false;
  558. }
  559. public override bool MoveToFirstAttribute ()
  560. {
  561. if (current == null)
  562. return false;
  563. if (ownerLinkedNode.Attributes == null)
  564. return false;
  565. if(ownerLinkedNode.Attributes.Count > 0)
  566. {
  567. current = ownerLinkedNode.Attributes [0];
  568. return true;
  569. }
  570. else
  571. return false;
  572. }
  573. public override bool MoveToNextAttribute ()
  574. {
  575. if (current == null)
  576. return false;
  577. if (current.NodeType != XmlNodeType.Attribute)
  578. return MoveToFirstAttribute ();
  579. else
  580. {
  581. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  582. for (int i=0; i<ac.Count-1; i++)
  583. {
  584. XmlAttribute attr = ac [i];
  585. if (attr == current)
  586. {
  587. i++;
  588. if (i == ac.Count)
  589. return false;
  590. current = ac [i];
  591. return true;
  592. }
  593. }
  594. return false;
  595. }
  596. }
  597. public override bool Read ()
  598. {
  599. // FIXME: at some stage inlining might work effectively.
  600. // if (EOF)
  601. switch (state) {
  602. case ReadState.EndOfFile:
  603. case ReadState.Error:
  604. case ReadState.Closed:
  605. return false;
  606. }
  607. #if NET_2_0
  608. if (Binary != null)
  609. Binary.Reset ();
  610. #endif
  611. bool ret = ReadContent ();
  612. ownerLinkedNode = current;
  613. return ret;
  614. }
  615. bool ReadContent ()
  616. {
  617. if (ReadState == ReadState.Initial) {
  618. current = startNode;
  619. state = ReadState.Interactive;
  620. // when startNode is document or fragment
  621. if (ignoreStartNode)
  622. current = startNode.FirstChild;
  623. if (current == null) {
  624. state = ReadState.Error;
  625. return false;
  626. } else
  627. return true;
  628. }
  629. MoveToElement ();
  630. // don't step into EntityReference's children. Also
  631. // avoid re-entering children of already-consumed
  632. // element (i.e. when it is regarded as EndElement).
  633. XmlNode firstChild =
  634. !isEndElement && current.NodeType != XmlNodeType.EntityReference ?
  635. current.FirstChild : null;
  636. if (firstChild != null) {
  637. isEndElement = false;
  638. current = firstChild;
  639. depth++;
  640. return true;
  641. }
  642. if (current == startNode) { // Currently it is on the start node.
  643. if (IsEmptyElement || isEndElement) {
  644. // The start node is already consumed.
  645. isEndElement = false;
  646. current = null;
  647. state = ReadState.EndOfFile;
  648. return false;
  649. } else {
  650. // The start node is the only element
  651. // which should be processed. Now it
  652. // is set as EndElement.
  653. isEndElement = true;
  654. return true;
  655. }
  656. }
  657. if (!isEndElement && !IsEmptyElement &&
  658. current.NodeType == XmlNodeType.Element) {
  659. // element, currently not EndElement, and has
  660. // no child. (such as <foo></foo>, which
  661. // should become EndElement).
  662. isEndElement = true;
  663. return true;
  664. }
  665. // If NextSibling is available, move to there.
  666. XmlNode next = current.NextSibling;
  667. if (next != null) {
  668. isEndElement = false;
  669. current = next;
  670. return true;
  671. }
  672. // Otherwise, parent.
  673. XmlNode parent = current.ParentNode;
  674. if (parent == null || parent == startNode && ignoreStartNode) {
  675. // Parent is not available, or reached to
  676. // the start node. This reader never sets
  677. // startNode as current if it was originally
  678. // ignored (e.g. startNode is XmlDocument).
  679. isEndElement = false;
  680. current = null;
  681. state = ReadState.EndOfFile;
  682. return false;
  683. } else {
  684. // Parent was available, so return it as
  685. // EndElement.
  686. current = parent;
  687. depth--;
  688. isEndElement = true;
  689. return true;
  690. }
  691. }
  692. #if !NET_2_1
  693. public override bool ReadAttributeValue ()
  694. {
  695. if (current.NodeType == XmlNodeType.Attribute) {
  696. if (current.FirstChild == null)
  697. return false;
  698. current = current.FirstChild;
  699. return true;
  700. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  701. if (current.NextSibling == null)
  702. return false;
  703. current = current.NextSibling;
  704. return true;
  705. } else
  706. return false;
  707. }
  708. #endif
  709. public override string ReadString ()
  710. {
  711. return base.ReadString ();
  712. }
  713. #if !NET_2_1
  714. public override void ResolveEntity ()
  715. {
  716. throw new NotSupportedException ("Should not happen.");
  717. }
  718. #endif
  719. public override void Skip ()
  720. {
  721. // Why is this overriden? Such skipping might raise
  722. // (or ignore) unexpected validation error.
  723. base.Skip ();
  724. }
  725. #endregion
  726. }
  727. }