XmlNode.cs 23 KB

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