XmlNode.cs 23 KB

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