XmlNode.cs 23 KB

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