XmlNode.cs 22 KB

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