XmlNode.cs 21 KB

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