XmlNode.cs 23 KB

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