XmlNode.cs 25 KB

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