XmlNode.cs 25 KB

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