XmlNode.cs 24 KB

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