XmlNode.cs 23 KB

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