XmlNode.cs 23 KB

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