XmlNode.cs 23 KB

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