XmlNode.cs 23 KB

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