XmlNode.cs 24 KB

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