XmlNode.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. //
  2. // System.Xml.XmlNode
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2002 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml.XPath;
  37. #if NET_2_0
  38. using System.Diagnostics;
  39. using System.Xml.Schema;
  40. #endif
  41. namespace System.Xml
  42. {
  43. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  44. {
  45. #region Fields
  46. XmlDocument ownerDocument;
  47. XmlNode parentNode;
  48. XmlLinkedNode lastLinkedChild;
  49. XmlNodeListChildren childNodes;
  50. #endregion
  51. #region Constructors
  52. internal XmlNode (XmlDocument ownerDocument)
  53. {
  54. this.ownerDocument = ownerDocument;
  55. }
  56. #endregion
  57. #region Properties
  58. public virtual XmlAttributeCollection Attributes {
  59. get { return null; }
  60. }
  61. public virtual string BaseURI {
  62. get {
  63. // Isn't it conformant to W3C XML Base Recommendation?
  64. // As far as I tested, there are not...
  65. return (ParentNode != null) ? ParentNode.ChildrenBaseURI : String.Empty;
  66. }
  67. }
  68. internal virtual string ChildrenBaseURI {
  69. get {
  70. return BaseURI;
  71. }
  72. }
  73. public virtual XmlNodeList ChildNodes {
  74. get {
  75. if (childNodes == null)
  76. childNodes = new XmlNodeListChildren (this);
  77. return childNodes;
  78. }
  79. }
  80. public virtual XmlNode FirstChild {
  81. get {
  82. if (LastChild != null) {
  83. return LastLinkedChild.NextLinkedSibling;
  84. }
  85. else {
  86. return null;
  87. }
  88. }
  89. }
  90. public virtual bool HasChildNodes {
  91. get { return LastChild != null; }
  92. }
  93. public virtual string InnerText {
  94. get {
  95. switch (NodeType) {
  96. case XmlNodeType.Text:
  97. case XmlNodeType.CDATA:
  98. case XmlNodeType.SignificantWhitespace:
  99. case XmlNodeType.Whitespace:
  100. return Value;
  101. break;
  102. }
  103. if (FirstChild == null)
  104. return String.Empty;
  105. if (FirstChild == LastChild)
  106. return FirstChild.InnerText;
  107. StringBuilder builder = new StringBuilder ();
  108. AppendChildValues (builder);
  109. return builder.ToString ();
  110. }
  111. set { throw new InvalidOperationException ("This node is read only. Cannot be modified."); }
  112. }
  113. private void AppendChildValues (StringBuilder builder)
  114. {
  115. XmlNode node = FirstChild;
  116. while (node != null) {
  117. switch (node.NodeType) {
  118. case XmlNodeType.Text:
  119. case XmlNodeType.CDATA:
  120. case XmlNodeType.SignificantWhitespace:
  121. case XmlNodeType.Whitespace:
  122. builder.Append (node.Value);
  123. break;
  124. }
  125. node.AppendChildValues (builder);
  126. node = node.NextSibling;
  127. }
  128. }
  129. public virtual string InnerXml {
  130. get {
  131. StringWriter sw = new StringWriter ();
  132. XmlTextWriter xtw = new XmlTextWriter (sw);
  133. WriteContentTo (xtw);
  134. return sw.GetStringBuilder ().ToString ();
  135. }
  136. set {
  137. throw new InvalidOperationException ("This node is readonly or doesn't have any children.");
  138. }
  139. }
  140. public virtual bool IsReadOnly {
  141. get
  142. {
  143. XmlNode curNode = this;
  144. do
  145. {
  146. switch (curNode.NodeType)
  147. {
  148. case XmlNodeType.EntityReference:
  149. case XmlNodeType.Entity:
  150. return true;
  151. case XmlNodeType.Attribute:
  152. curNode = ((XmlAttribute)curNode).OwnerElement;
  153. break;
  154. default:
  155. curNode = curNode.ParentNode;
  156. break;
  157. }
  158. }
  159. while (curNode != null) ;
  160. return false;
  161. }
  162. }
  163. [System.Runtime.CompilerServices.IndexerName("Item")]
  164. public virtual XmlElement this [string name] {
  165. get {
  166. for (int i = 0; i < ChildNodes.Count; i++) {
  167. XmlNode node = ChildNodes [i];
  168. if ((node.NodeType == XmlNodeType.Element) &&
  169. (node.Name == name)) {
  170. return (XmlElement) node;
  171. }
  172. }
  173. return null;
  174. }
  175. }
  176. [System.Runtime.CompilerServices.IndexerName("Item")]
  177. public virtual XmlElement this [string localname, string ns] {
  178. get {
  179. for (int i = 0; i < ChildNodes.Count; i++) {
  180. XmlNode node = ChildNodes [i];
  181. if ((node.NodeType == XmlNodeType.Element) &&
  182. (node.LocalName == localname) &&
  183. (node.NamespaceURI == ns)) {
  184. return (XmlElement) node;
  185. }
  186. }
  187. return null;
  188. }
  189. }
  190. public virtual XmlNode LastChild {
  191. get { return LastLinkedChild; }
  192. }
  193. internal virtual XmlLinkedNode LastLinkedChild {
  194. get { return lastLinkedChild; }
  195. set { lastLinkedChild = value; }
  196. }
  197. public abstract string LocalName { get; }
  198. public abstract string Name { get; }
  199. public virtual string NamespaceURI {
  200. get { return String.Empty; }
  201. }
  202. public virtual XmlNode NextSibling {
  203. get { return null; }
  204. }
  205. public abstract XmlNodeType NodeType { get; }
  206. internal virtual XPathNodeType XPathNodeType {
  207. get {
  208. throw new InvalidOperationException ("Can not get XPath node type from " + this.GetType ().ToString ());
  209. }
  210. }
  211. public virtual string OuterXml {
  212. get {
  213. StringWriter sw = new StringWriter ();
  214. XmlTextWriter xtw = new XmlTextWriter (sw);
  215. WriteTo (xtw);
  216. return sw.ToString ();
  217. }
  218. }
  219. public virtual XmlDocument OwnerDocument {
  220. get { return ownerDocument; }
  221. }
  222. public virtual XmlNode ParentNode {
  223. get { return parentNode; }
  224. }
  225. public virtual string Prefix {
  226. get { return String.Empty; }
  227. set {}
  228. }
  229. public virtual XmlNode PreviousSibling {
  230. get { return null; }
  231. }
  232. public virtual string Value {
  233. get { return null; }
  234. set { throw new InvalidOperationException ("This node does not have a value"); }
  235. }
  236. internal virtual string XmlLang {
  237. get {
  238. if(Attributes != null)
  239. for (int i = 0; i < Attributes.Count; i++) {
  240. XmlAttribute attr = Attributes [i];
  241. if(attr.Name == "xml:lang")
  242. return attr.Value;
  243. }
  244. return (ParentNode != null) ? ParentNode.XmlLang : OwnerDocument.XmlLang;
  245. }
  246. }
  247. internal virtual XmlSpace XmlSpace {
  248. get {
  249. if(Attributes != null) {
  250. for (int i = 0; i < Attributes.Count; i++) {
  251. XmlAttribute attr = Attributes [i];
  252. if(attr.Name == "xml:space") {
  253. switch(attr.Value) {
  254. case "preserve": return XmlSpace.Preserve;
  255. case "default": return XmlSpace.Default;
  256. }
  257. break;
  258. }
  259. }
  260. }
  261. return (ParentNode != null) ? ParentNode.XmlSpace : OwnerDocument.XmlSpace;
  262. }
  263. }
  264. #if NET_2_0
  265. public virtual IXmlSchemaInfo SchemaInfo {
  266. get { return null; }
  267. internal set { }
  268. }
  269. #endif
  270. #endregion
  271. #region Methods
  272. public virtual XmlNode AppendChild (XmlNode newChild)
  273. {
  274. // AppendChild(n) is equivalent to InsertBefore(n, null)
  275. return InsertBefore (newChild, null);
  276. }
  277. internal XmlNode AppendChild (XmlNode newChild, bool checkNodeType)
  278. {
  279. return InsertBefore (newChild, null, checkNodeType, true);
  280. }
  281. public virtual XmlNode Clone ()
  282. {
  283. // By MS document, it is equivalent to CloneNode(true).
  284. return this.CloneNode (true);
  285. }
  286. public abstract XmlNode CloneNode (bool deep);
  287. #if NET_2_0
  288. public virtual XPathNavigator CreateNavigator ()
  289. {
  290. // XmlDocument has overriden definition, so it is safe
  291. // to use OwnerDocument here.
  292. return OwnerDocument.CreateNavigator (this);
  293. }
  294. #else
  295. public XPathNavigator CreateNavigator ()
  296. {
  297. XmlDocument document = this.NodeType == XmlNodeType.Document ?
  298. this as XmlDocument : this.ownerDocument;
  299. return document.CreateNavigator (this);
  300. }
  301. #endif
  302. public IEnumerator GetEnumerator ()
  303. {
  304. return ChildNodes.GetEnumerator ();
  305. }
  306. public virtual string GetNamespaceOfPrefix (string prefix)
  307. {
  308. switch (prefix) {
  309. case null:
  310. throw new ArgumentNullException ("prefix");
  311. #if NET_2_0
  312. case "xml":
  313. return XmlNamespaceManager.XmlnsXml;
  314. case "xmlns":
  315. return XmlNamespaceManager.XmlnsXmlns;
  316. #endif
  317. }
  318. XmlNode node;
  319. switch (NodeType) {
  320. case XmlNodeType.Attribute:
  321. node = ((XmlAttribute) this).OwnerElement;
  322. if (node == null)
  323. return String.Empty;
  324. break;
  325. case XmlNodeType.Element:
  326. node = this;
  327. break;
  328. default:
  329. node = ParentNode;
  330. break;
  331. }
  332. while (node != null) {
  333. if (node.Prefix == prefix)
  334. return node.NamespaceURI;
  335. if (node.Attributes != null) {
  336. int count = node.Attributes.Count;
  337. for (int i = 0; i < count; i++) {
  338. XmlAttribute attr = node.Attributes [i];
  339. if (prefix == attr.LocalName && attr.Prefix == "xmlns"
  340. || attr.Name == "xmlns" && prefix == String.Empty)
  341. return attr.Value;
  342. }
  343. }
  344. node = node.ParentNode;
  345. }
  346. return String.Empty;
  347. }
  348. public virtual string GetPrefixOfNamespace (string namespaceURI)
  349. {
  350. #if NET_2_0
  351. switch (namespaceURI) {
  352. case XmlNamespaceManager.XmlnsXml:
  353. return XmlNamespaceManager.PrefixXml;
  354. case XmlNamespaceManager.XmlnsXmlns:
  355. return XmlNamespaceManager.PrefixXmlns;
  356. }
  357. #endif
  358. XmlNode node;
  359. switch (NodeType) {
  360. case XmlNodeType.Attribute:
  361. node = ((XmlAttribute) this).OwnerElement;
  362. break;
  363. case XmlNodeType.Element:
  364. node = this;
  365. break;
  366. default:
  367. node = ParentNode;
  368. break;
  369. }
  370. while (node != null && node.Attributes != null) {
  371. for (int i = 0; i < Attributes.Count; i++) {
  372. XmlAttribute attr = Attributes [i];
  373. if (attr.Prefix == "xmlns" && attr.Value == namespaceURI)
  374. return attr.LocalName;
  375. else if (attr.Name == "xmlns" && attr.Value == namespaceURI)
  376. return String.Empty;
  377. }
  378. node = node.ParentNode;
  379. }
  380. return String.Empty;
  381. }
  382. object ICloneable.Clone ()
  383. {
  384. return Clone ();
  385. }
  386. IEnumerator IEnumerable.GetEnumerator ()
  387. {
  388. return GetEnumerator ();
  389. }
  390. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  391. {
  392. // InsertAfter(n1, n2) is equivalent to InsertBefore(n1, n2.PreviousSibling).
  393. // I took this way because current implementation
  394. // Calling InsertBefore() in this method is faster than
  395. // the counterpart, since NextSibling is faster than
  396. // PreviousSibling (these children are forward-only list).
  397. XmlNode argNode = null;
  398. if (refChild != null)
  399. argNode = refChild.NextSibling;
  400. else if (FirstChild != null)
  401. argNode = FirstChild;
  402. return InsertBefore (newChild, argNode);
  403. }
  404. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  405. {
  406. return InsertBefore (newChild, refChild, true, true);
  407. }
  408. // check for the node to be one of node ancestors
  409. internal bool IsAncestor (XmlNode newChild)
  410. {
  411. XmlNode currNode = this.ParentNode;
  412. while(currNode != null)
  413. {
  414. if(currNode == newChild)
  415. return true;
  416. currNode = currNode.ParentNode;
  417. }
  418. return false;
  419. }
  420. internal XmlNode InsertBefore (XmlNode newChild, XmlNode refChild, bool checkNodeType, bool raiseEvent)
  421. {
  422. if (checkNodeType)
  423. CheckNodeInsertion (newChild, refChild);
  424. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument) this : OwnerDocument;
  425. if (raiseEvent)
  426. ownerDoc.onNodeInserting (newChild, this);
  427. if (newChild.ParentNode != null)
  428. newChild.ParentNode.RemoveChild (newChild, checkNodeType);
  429. if (newChild.NodeType == XmlNodeType.DocumentFragment) {
  430. // This recursively invokes events. (It is compatible with MS implementation.)
  431. while (newChild.FirstChild != null)
  432. this.InsertBefore (newChild.FirstChild, refChild);
  433. /*
  434. int x = newChild.ChildNodes.Count;
  435. for (int i = 0; i < x; i++) {
  436. XmlNode n = newChild.ChildNodes [0];
  437. this.InsertBefore (n, refChild); }
  438. */
  439. }
  440. else {
  441. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  442. newLinkedChild.parentNode = this;
  443. if (refChild == null) {
  444. // newChild is the last child:
  445. // * set newChild as NextSibling of the existing lastchild
  446. // * set LastChild = newChild
  447. // * set NextSibling of newChild as FirstChild
  448. if (LastLinkedChild != null) {
  449. XmlLinkedNode formerFirst = (XmlLinkedNode) FirstChild;
  450. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  451. LastLinkedChild = newLinkedChild;
  452. newLinkedChild.NextLinkedSibling = formerFirst;
  453. } else {
  454. LastLinkedChild = newLinkedChild;
  455. LastLinkedChild.NextLinkedSibling = newLinkedChild; // FirstChild
  456. }
  457. } else {
  458. // newChild is not the last child:
  459. // * if newchild is first, then set next of lastchild is newChild.
  460. // otherwise, set next of previous sibling to newChild
  461. // * set next of newChild to refChild
  462. XmlLinkedNode prev = refChild.PreviousSibling as XmlLinkedNode;
  463. if (prev == null)
  464. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  465. else
  466. prev.NextLinkedSibling = newLinkedChild;
  467. newLinkedChild.NextLinkedSibling = refChild as XmlLinkedNode;
  468. }
  469. switch (newChild.NodeType) {
  470. case XmlNodeType.EntityReference:
  471. ((XmlEntityReference) newChild).SetReferencedEntityContent ();
  472. break;
  473. case XmlNodeType.Entity:
  474. ((XmlEntity) newChild).SetEntityContent ();
  475. break;
  476. case XmlNodeType.DocumentType:
  477. foreach (XmlEntity ent in ((XmlDocumentType)newChild).Entities)
  478. ent.SetEntityContent ();
  479. break;
  480. }
  481. if (raiseEvent)
  482. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  483. }
  484. return newChild;
  485. }
  486. private void CheckNodeInsertion (XmlNode newChild, XmlNode refChild)
  487. {
  488. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument) this : OwnerDocument;
  489. if (NodeType != XmlNodeType.Element &&
  490. NodeType != XmlNodeType.Attribute &&
  491. NodeType != XmlNodeType.Document &&
  492. NodeType != XmlNodeType.DocumentFragment)
  493. throw new InvalidOperationException (String.Format ("Node cannot be appended to current node {0}.", NodeType));
  494. switch (NodeType) {
  495. case XmlNodeType.Attribute:
  496. switch (newChild.NodeType) {
  497. case XmlNodeType.Text:
  498. case XmlNodeType.EntityReference:
  499. break;
  500. default:
  501. throw new InvalidOperationException (String.Format (
  502. "Cannot insert specified type of node {0} as a child of this node {1}.",
  503. newChild.NodeType, NodeType));
  504. }
  505. break;
  506. case XmlNodeType.Element:
  507. switch (newChild.NodeType) {
  508. case XmlNodeType.Attribute:
  509. case XmlNodeType.Document:
  510. case XmlNodeType.DocumentType:
  511. case XmlNodeType.Entity:
  512. case XmlNodeType.Notation:
  513. case XmlNodeType.XmlDeclaration:
  514. throw new InvalidOperationException ("Cannot insert specified type of node as a child of this node.");
  515. }
  516. break;
  517. }
  518. if (IsReadOnly)
  519. throw new InvalidOperationException ("The node is readonly.");
  520. if (newChild.OwnerDocument != ownerDoc)
  521. throw new ArgumentException ("Can't append a node created by another document.");
  522. if (refChild != null) {
  523. if (refChild.ParentNode != this)
  524. throw new ArgumentException ("The reference node is not a child of this node.");
  525. }
  526. if(this == ownerDoc && ownerDoc.DocumentElement != null && (newChild is XmlElement) && newChild != ownerDoc.DocumentElement)
  527. throw new XmlException ("multiple document element not allowed.");
  528. // checking validity finished. then appending...
  529. if (newChild == this || IsAncestor (newChild))
  530. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  531. }
  532. public virtual void Normalize ()
  533. {
  534. StringBuilder tmpBuilder = new StringBuilder ();
  535. int count = this.ChildNodes.Count;
  536. int start = 0;
  537. for (int i = 0; i < count; i++) {
  538. XmlNode c = ChildNodes [i];
  539. switch (c.NodeType) {
  540. case XmlNodeType.Text:
  541. case XmlNodeType.Whitespace:
  542. case XmlNodeType.SignificantWhitespace:
  543. tmpBuilder.Append (c.Value);
  544. break;
  545. default:
  546. c.Normalize ();
  547. NormalizeRange (start, i, tmpBuilder);
  548. // Continue to normalize from next node.
  549. start = i + 1;
  550. break;
  551. }
  552. }
  553. if (start < count) {
  554. NormalizeRange (start, count, tmpBuilder);
  555. }
  556. }
  557. private void NormalizeRange (int start, int i, StringBuilder tmpBuilder)
  558. {
  559. int keepPos = -1;
  560. // If Texts and Whitespaces are mixed, Text takes precedence to remain.
  561. // i.e. Whitespace should be removed.
  562. for (int j = start; j < i; j++) {
  563. XmlNode keep = ChildNodes [j];
  564. if (keep.NodeType == XmlNodeType.Text) {
  565. keepPos = j;
  566. break;
  567. }
  568. else if (keep.NodeType == XmlNodeType.SignificantWhitespace)
  569. keepPos = j;
  570. // but don't break up to find Text nodes.
  571. }
  572. if (keepPos >= 0) {
  573. for (int del = start; del < keepPos; del++)
  574. RemoveChild (ChildNodes [start]);
  575. int rest = i - keepPos - 1;
  576. for (int del = 0; del < rest; del++) {
  577. RemoveChild (ChildNodes [start + 1]);
  578. }
  579. }
  580. if (keepPos >= 0)
  581. ChildNodes [start].Value = tmpBuilder.ToString ();
  582. // otherwise nothing to be normalized
  583. tmpBuilder.Length = 0;
  584. }
  585. public virtual XmlNode PrependChild (XmlNode newChild)
  586. {
  587. return InsertAfter (newChild, null);
  588. }
  589. public virtual void RemoveAll ()
  590. {
  591. if (Attributes != null)
  592. Attributes.RemoveAll ();
  593. XmlNode next = null;
  594. for (XmlNode node = FirstChild; node != null; node = next) {
  595. next = node.NextSibling;
  596. RemoveChild (node);
  597. }
  598. }
  599. public virtual XmlNode RemoveChild (XmlNode oldChild)
  600. {
  601. return RemoveChild (oldChild, true);
  602. }
  603. private void CheckNodeRemoval ()
  604. {
  605. if (NodeType != XmlNodeType.Attribute &&
  606. NodeType != XmlNodeType.Element &&
  607. NodeType != XmlNodeType.Document &&
  608. NodeType != XmlNodeType.DocumentFragment)
  609. throw new ArgumentException (String.Format ("This {0} node cannot remove its child.", NodeType));
  610. if (IsReadOnly)
  611. throw new ArgumentException (String.Format ("This {0} node is read only.", NodeType));
  612. }
  613. internal XmlNode RemoveChild (XmlNode oldChild, bool checkNodeType)
  614. {
  615. if (oldChild == null)
  616. throw new NullReferenceException ();
  617. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  618. if(oldChild.ParentNode != this)
  619. throw new ArgumentException ("The node to be removed is not a child of this node.");
  620. if (checkNodeType)
  621. ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
  622. if (checkNodeType)
  623. CheckNodeRemoval ();
  624. if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
  625. // If there is only one children, simply clear.
  626. LastLinkedChild = null;
  627. else {
  628. XmlLinkedNode oldLinkedChild = (XmlLinkedNode) oldChild;
  629. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  630. XmlLinkedNode firstChild = (XmlLinkedNode) FirstChild;
  631. while (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) == false &&
  632. Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  633. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  634. if (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  635. throw new ArgumentException ();
  636. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  637. // Each derived class may have its own LastLinkedChild, so we must set it explicitly.
  638. if (oldLinkedChild.NextLinkedSibling == firstChild)
  639. this.LastLinkedChild = beforeLinkedChild;
  640. oldLinkedChild.NextLinkedSibling = null;
  641. }
  642. if (checkNodeType)
  643. ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
  644. oldChild.parentNode = null; // clear parent 'after' above logic.
  645. return oldChild;
  646. }
  647. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  648. {
  649. if(oldChild.ParentNode != this)
  650. throw new ArgumentException ("The node to be removed is not a child of this node.");
  651. if (newChild == this || IsAncestor (newChild))
  652. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  653. XmlNode next = oldChild.NextSibling;
  654. RemoveChild (oldChild);
  655. InsertBefore (newChild, next);
  656. return oldChild;
  657. }
  658. // WARNING: don't use this member outside XmlAttribute nodes.
  659. internal XmlElement AttributeOwnerElement {
  660. get { return (XmlElement) parentNode; }
  661. set { parentNode = value; }
  662. }
  663. internal void SearchDescendantElements (string name, bool matchAll, ArrayList list)
  664. {
  665. for (XmlNode n = FirstChild; n != null; n = n.NextSibling) {
  666. if (n.NodeType != XmlNodeType.Element)
  667. continue;
  668. if (matchAll || n.Name == name)
  669. list.Add (n);
  670. n.SearchDescendantElements (name, matchAll, list);
  671. }
  672. }
  673. internal void SearchDescendantElements (string name, bool matchAllName, string ns, bool matchAllNS, ArrayList list)
  674. {
  675. for (XmlNode n = FirstChild; n != null; n = n.NextSibling) {
  676. if (n.NodeType != XmlNodeType.Element)
  677. continue;
  678. if ((matchAllName || n.LocalName == name)
  679. && (matchAllNS || n.NamespaceURI == ns))
  680. list.Add (n);
  681. n.SearchDescendantElements (name, matchAllName, ns, matchAllNS, list);
  682. }
  683. }
  684. public XmlNodeList SelectNodes (string xpath)
  685. {
  686. return SelectNodes (xpath, null);
  687. }
  688. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  689. {
  690. XPathNavigator nav = CreateNavigator ();
  691. XPathExpression expr = nav.Compile (xpath);
  692. if (nsmgr != null)
  693. expr.SetContext (nsmgr);
  694. XPathNodeIterator iter = nav.Select (expr);
  695. /*
  696. ArrayList rgNodes = new ArrayList ();
  697. while (iter.MoveNext ())
  698. {
  699. rgNodes.Add (((IHasXmlNode) iter.Current).GetNode ());
  700. }
  701. return new XmlNodeArrayList (rgNodes);
  702. */
  703. return new XmlIteratorNodeList (iter);
  704. }
  705. public XmlNode SelectSingleNode (string xpath)
  706. {
  707. return SelectSingleNode (xpath, null);
  708. }
  709. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  710. {
  711. XPathNavigator nav = CreateNavigator ();
  712. XPathExpression expr = nav.Compile (xpath);
  713. if (nsmgr != null)
  714. expr.SetContext (nsmgr);
  715. XPathNodeIterator iter = nav.Select (expr);
  716. if (!iter.MoveNext ())
  717. return null;
  718. return ((IHasXmlNode) iter.Current).GetNode ();
  719. }
  720. public virtual bool Supports (string feature, string version)
  721. {
  722. if (String.Compare (feature, "xml", true, CultureInfo.InvariantCulture) == 0 // not case-sensitive
  723. && (String.Compare (version, "1.0", true, CultureInfo.InvariantCulture) == 0
  724. || String.Compare (version, "2.0", true, CultureInfo.InvariantCulture) == 0))
  725. return true;
  726. else
  727. return false;
  728. }
  729. public abstract void WriteContentTo (XmlWriter w);
  730. public abstract void WriteTo (XmlWriter w);
  731. // It parses this and all the ancestor elements,
  732. // find 'xmlns' declarations, stores and then return them.
  733. internal XmlNamespaceManager ConstructNamespaceManager ()
  734. {
  735. XmlDocument doc = this is XmlDocument ? (XmlDocument)this : this.OwnerDocument;
  736. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  737. XmlElement el = null;
  738. switch(this.NodeType) {
  739. case XmlNodeType.Attribute:
  740. el = ((XmlAttribute)this).OwnerElement;
  741. break;
  742. case XmlNodeType.Element:
  743. el = this as XmlElement;
  744. break;
  745. default:
  746. el = this.ParentNode as XmlElement;
  747. break;
  748. }
  749. while (el != null) {
  750. for (int i = 0; i < el.Attributes.Count; i++) {
  751. XmlAttribute attr = el.Attributes [i];
  752. if(attr.Prefix == "xmlns") {
  753. if (nsmgr.LookupNamespace (attr.LocalName) != attr.Value)
  754. nsmgr.AddNamespace (attr.LocalName, attr.Value);
  755. } else if(attr.Name == "xmlns") {
  756. if(nsmgr.LookupNamespace (String.Empty) != attr.Value)
  757. nsmgr.AddNamespace (String.Empty, attr.Value);
  758. }
  759. }
  760. // When reached to document, then it will set null value :)
  761. el = el.ParentNode as XmlElement;
  762. }
  763. return nsmgr;
  764. }
  765. #endregion
  766. }
  767. }