XmlNode.cs 21 KB

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