XmlNode.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 ("current node {0} is not allowed to have any children.", 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 ArgumentException (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 ArgumentException ("Cannot insert specified type of node as a child of this node.");
  416. }
  417. break;
  418. }
  419. if (IsReadOnly)
  420. throw new ArgumentException ("The specified 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 && newChild.OwnerDocument != refChild.OwnerDocument)
  424. throw new ArgumentException ("argument nodes are on the different documents.");
  425. if(this == ownerDoc && ownerDoc.DocumentElement != null && (newChild is XmlElement))
  426. throw new XmlException ("multiple document element not allowed.");
  427. // checking validity finished. then appending...
  428. if (newChild == this || IsAncestor (newChild))
  429. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  430. }
  431. public virtual void Normalize ()
  432. {
  433. // if (tmpBuilder == null)
  434. tmpBuilder = new StringBuilder ();
  435. // tmpBuilder.Length = 0;
  436. int count = this.ChildNodes.Count;
  437. int start = 0;
  438. for (int i = 0; i < count; i++) {
  439. XmlNode c = ChildNodes [i];
  440. switch (c.NodeType) {
  441. case XmlNodeType.Text:
  442. case XmlNodeType.Whitespace:
  443. case XmlNodeType.SignificantWhitespace:
  444. tmpBuilder.Append (c.Value);
  445. break;
  446. default:
  447. c.Normalize ();
  448. NormalizeRange (start, i);
  449. // Continue to normalize from next node.
  450. start = i + 1;
  451. break;
  452. }
  453. }
  454. if (start < count) {
  455. NormalizeRange (start, count);
  456. }
  457. tmpBuilder = null;
  458. }
  459. private void NormalizeRange (int start, int i)
  460. {
  461. int keepPos = -1;
  462. // If Texts and Whitespaces are mixed, Text takes precedence to remain.
  463. // i.e. Whitespace should be removed.
  464. for (int j = start; j < i; j++) {
  465. XmlNode keep = ChildNodes [j];
  466. if (keep.NodeType == XmlNodeType.Text) {
  467. keepPos = j;
  468. break;
  469. }
  470. else if (keep.NodeType == XmlNodeType.SignificantWhitespace)
  471. keepPos = j;
  472. // but don't break up to find Text nodes.
  473. }
  474. // But if no Texts and one or more Whitespaces, then the first
  475. if (keepPos < 0 && i > start)
  476. keepPos = 0;
  477. if (keepPos >= 0) {
  478. for (int del = start; del < keepPos; del++)
  479. RemoveChild (ChildNodes [del]);
  480. for (int del = keepPos + 1; del < i; del++)
  481. RemoveChild (ChildNodes [del]);
  482. }
  483. ChildNodes [keepPos].Value = tmpBuilder.ToString ();
  484. tmpBuilder.Length = 0;
  485. }
  486. public virtual XmlNode PrependChild (XmlNode newChild)
  487. {
  488. return InsertAfter (newChild, null);
  489. }
  490. public virtual void RemoveAll ()
  491. {
  492. if (Attributes != null)
  493. Attributes.RemoveAll ();
  494. XmlNode next = null;
  495. for (XmlNode node = FirstChild; node != null; node = next) {
  496. next = node.NextSibling;
  497. RemoveChild (node);
  498. }
  499. }
  500. public virtual XmlNode RemoveChild (XmlNode oldChild)
  501. {
  502. return RemoveChild (oldChild, true);
  503. }
  504. private void CheckNodeRemoval ()
  505. {
  506. if (NodeType != XmlNodeType.Attribute &&
  507. NodeType != XmlNodeType.Element &&
  508. NodeType != XmlNodeType.Document &&
  509. NodeType != XmlNodeType.DocumentFragment)
  510. throw new ArgumentException (String.Format ("This {0} node cannot remove child.", NodeType));
  511. if (IsReadOnly)
  512. throw new ArgumentException (String.Format ("This {0} node is read only.", NodeType));
  513. }
  514. internal XmlNode RemoveChild (XmlNode oldChild, bool checkNodeType)
  515. {
  516. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  517. if(oldChild.ParentNode != this)
  518. throw new XmlException ("specified child is not child of this node.");
  519. if (checkNodeType)
  520. ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
  521. if (checkNodeType)
  522. CheckNodeRemoval ();
  523. if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
  524. // If there is only one children, simply clear.
  525. LastLinkedChild = null;
  526. else {
  527. XmlLinkedNode oldLinkedChild = (XmlLinkedNode) oldChild;
  528. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  529. XmlLinkedNode firstChild = (XmlLinkedNode) FirstChild;
  530. while (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) == false &&
  531. Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  532. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  533. if (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  534. throw new ArgumentException ();
  535. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  536. // Each derived class may have its own LastLinkedChild, so we must set it explicitly.
  537. if (oldLinkedChild.NextLinkedSibling == firstChild)
  538. this.LastLinkedChild = beforeLinkedChild;
  539. oldLinkedChild.NextLinkedSibling = null;
  540. }
  541. if (checkNodeType)
  542. ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
  543. oldChild.parentNode = null; // clear parent 'after' above logic.
  544. return oldChild;
  545. }
  546. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  547. {
  548. if(oldChild.ParentNode != this)
  549. throw new InvalidOperationException ("oldChild is not a child of this node.");
  550. if (newChild == this || IsAncestor (newChild))
  551. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  552. for (int i = 0; i < ChildNodes.Count; i++) {
  553. XmlNode n = ChildNodes [i];
  554. if(n == oldChild) {
  555. XmlNode prev = oldChild.PreviousSibling;
  556. RemoveChild (oldChild);
  557. InsertAfter (newChild, prev);
  558. break;
  559. }
  560. }
  561. return oldChild;
  562. }
  563. internal void SearchDescendantElements (string name, bool matchAll, ArrayList list)
  564. {
  565. for (int i = 0; i < ChildNodes.Count; i++) {
  566. XmlNode n = ChildNodes [i];
  567. if (n.NodeType != XmlNodeType.Element)
  568. continue;
  569. if (matchAll || n.Name == name)
  570. list.Add (n);
  571. n.SearchDescendantElements (name, matchAll, list);
  572. }
  573. }
  574. internal void SearchDescendantElements (string name, bool matchAllName, string ns, bool matchAllNS, ArrayList list)
  575. {
  576. for (int i = 0; i < ChildNodes.Count; i++) {
  577. XmlNode n = ChildNodes [i];
  578. if (n.NodeType != XmlNodeType.Element)
  579. continue;
  580. if ((matchAllName || n.LocalName == name)
  581. && (matchAllNS || n.NamespaceURI == ns))
  582. list.Add (n);
  583. n.SearchDescendantElements (name, matchAllName, ns, matchAllNS, list);
  584. }
  585. }
  586. public XmlNodeList SelectNodes (string xpath)
  587. {
  588. return SelectNodes (xpath, null);
  589. }
  590. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  591. {
  592. XPathNavigator nav = CreateNavigator ();
  593. XPathExpression expr = nav.Compile (xpath);
  594. if (nsmgr != null)
  595. expr.SetContext (nsmgr);
  596. XPathNodeIterator iter = nav.Select (expr);
  597. ArrayList rgNodes = new ArrayList ();
  598. while (iter.MoveNext ())
  599. {
  600. rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
  601. }
  602. return new XmlNodeArrayList (rgNodes);
  603. }
  604. public XmlNode SelectSingleNode (string xpath)
  605. {
  606. return SelectSingleNode (xpath, null);
  607. }
  608. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  609. {
  610. XPathNavigator nav = CreateNavigator ();
  611. XPathExpression expr = nav.Compile (xpath);
  612. if (nsmgr != null)
  613. expr.SetContext (nsmgr);
  614. XPathNodeIterator iter = nav.Select (expr);
  615. if (!iter.MoveNext ())
  616. return null;
  617. return ((XmlDocumentNavigator) iter.Current).Node;
  618. }
  619. public virtual bool Supports (string feature, string version)
  620. {
  621. if (String.Compare (feature, "xml", true) == 0 // not case-sensitive
  622. && (String.Compare (version, "1.0", true) == 0
  623. || String.Compare (version, "2.0", true) == 0))
  624. return true;
  625. else
  626. return false;
  627. }
  628. public abstract void WriteContentTo (XmlWriter w);
  629. public abstract void WriteTo (XmlWriter w);
  630. // It parses this and all the ancestor elements,
  631. // find 'xmlns' declarations, stores and then return them.
  632. internal XmlNamespaceManager ConstructNamespaceManager ()
  633. {
  634. XmlDocument doc = this is XmlDocument ? (XmlDocument)this : this.OwnerDocument;
  635. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  636. XmlElement el = null;
  637. switch(this.NodeType) {
  638. case XmlNodeType.Attribute:
  639. el = ((XmlAttribute)this).OwnerElement;
  640. break;
  641. case XmlNodeType.Element:
  642. el = this as XmlElement;
  643. break;
  644. default:
  645. el = this.ParentNode as XmlElement;
  646. break;
  647. }
  648. while (el != null) {
  649. for (int i = 0; i < el.Attributes.Count; i++) {
  650. XmlAttribute attr = el.Attributes [i];
  651. if(attr.Prefix == "xmlns") {
  652. if (nsmgr.LookupNamespace (attr.LocalName) != attr.Value)
  653. nsmgr.AddNamespace (attr.LocalName, attr.Value);
  654. } else if(attr.Name == "xmlns") {
  655. if(nsmgr.LookupNamespace (String.Empty) != attr.Value)
  656. nsmgr.AddNamespace (String.Empty, attr.Value);
  657. }
  658. }
  659. // When reached to document, then it will set null value :)
  660. el = el.ParentNode as XmlElement;
  661. }
  662. return nsmgr;
  663. }
  664. #endregion
  665. }
  666. }