XmlNode.cs 19 KB

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