XmlNode.cs 23 KB

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