XmlNode.cs 21 KB

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