XmlNode.cs 23 KB

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