2
0

XmlNode.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. if(this == ownerDoc && ownerDoc.DocumentElement != null && (newChild is XmlElement))
  338. throw new XmlException ("multiple document element not allowed.");
  339. // checking validity finished. then appending...
  340. if (newChild == this || isAncestorIntern(newChild))
  341. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  342. return insertBeforeIntern (newChild, refChild);
  343. }
  344. // check for the node to be one of node ancestors
  345. internal bool isAncestorIntern(XmlNode newChild)
  346. {
  347. XmlNode currNode = this.ParentNode;
  348. while(currNode != null)
  349. {
  350. if(currNode == newChild)
  351. return true;
  352. currNode = currNode.ParentNode;
  353. }
  354. return false;
  355. }
  356. internal XmlNode insertBeforeIntern (XmlNode newChild, XmlNode refChild)
  357. {
  358. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  359. ownerDoc.onNodeInserting (newChild, this);
  360. if(newChild.ParentNode != null)
  361. newChild.ParentNode.RemoveChild (newChild);
  362. if(newChild.NodeType == XmlNodeType.DocumentFragment) {
  363. int x = newChild.ChildNodes.Count;
  364. for(int i=0; i<x; i++) {
  365. XmlNode n = newChild.ChildNodes [0];
  366. this.InsertBefore (n, refChild); // recursively invokes events. (It is compatible with MS implementation.)
  367. }
  368. }
  369. else {
  370. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  371. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  372. newLinkedChild.parentNode = this;
  373. if(refChild == null) {
  374. // append last, so:
  375. // * set nextSibling of previous lastchild to newChild
  376. // * set lastchild = newChild
  377. // * set next of newChild to firstChild
  378. if(LastLinkedChild != null) {
  379. XmlLinkedNode formerFirst = FirstChild as XmlLinkedNode;
  380. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  381. LastLinkedChild = newLinkedChild;
  382. newLinkedChild.NextLinkedSibling = formerFirst;
  383. }
  384. else {
  385. LastLinkedChild = newLinkedChild;
  386. LastLinkedChild.NextLinkedSibling = newLinkedChild; // FirstChild
  387. }
  388. }
  389. else {
  390. // append not last, so:
  391. // * if newchild is first, then set next of lastchild is newChild.
  392. // otherwise, set next of previous sibling to newChild
  393. // * set next of newChild to refChild
  394. XmlLinkedNode prev = refChild.PreviousSibling as XmlLinkedNode;
  395. if(prev == null)
  396. LastLinkedChild.NextLinkedSibling = newLinkedChild;
  397. else
  398. prev.NextLinkedSibling = newLinkedChild;
  399. newLinkedChild.NextLinkedSibling = refChild as XmlLinkedNode;
  400. }
  401. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  402. }
  403. return newChild;
  404. }
  405. public virtual void Normalize ()
  406. {
  407. // if (tmpBuilder == null)
  408. tmpBuilder = new StringBuilder ();
  409. // tmpBuilder.Length = 0;
  410. int count = this.ChildNodes.Count;
  411. int start = 0;
  412. for (int i = 0; i < count; i++) {
  413. XmlNode c = ChildNodes [i];
  414. switch (c.NodeType) {
  415. case XmlNodeType.Text:
  416. case XmlNodeType.Whitespace:
  417. case XmlNodeType.SignificantWhitespace:
  418. tmpBuilder.Append (c.Value);
  419. break;
  420. default:
  421. c.Normalize ();
  422. NormalizeRange (start, i);
  423. // Continue to normalize from next node.
  424. start = i + 1;
  425. break;
  426. }
  427. }
  428. if (start < count) {
  429. NormalizeRange (start, count);
  430. }
  431. tmpBuilder = null;
  432. }
  433. private void NormalizeRange (int start, int i)
  434. {
  435. int keepPos = -1;
  436. // If Texts and Whitespaces are mixed, Text takes precedence to remain.
  437. // i.e. Whitespace should be removed.
  438. for (int j = start; j < i; j++) {
  439. XmlNode keep = ChildNodes [j];
  440. if (keep.NodeType == XmlNodeType.Text) {
  441. keepPos = j;
  442. break;
  443. }
  444. else if (keep.NodeType == XmlNodeType.SignificantWhitespace)
  445. keepPos = j;
  446. // but don't break up to find Text nodes.
  447. }
  448. // But if no Texts and one or more Whitespaces, then the first
  449. if (keepPos < 0 && i > start)
  450. keepPos = 0;
  451. if (keepPos >= 0) {
  452. for (int del = start; del < keepPos; del++)
  453. RemoveChild (ChildNodes [del]);
  454. for (int del = keepPos + 1; del < i; del++)
  455. RemoveChild (ChildNodes [del]);
  456. }
  457. ChildNodes [keepPos].Value = tmpBuilder.ToString ();
  458. tmpBuilder.Length = 0;
  459. }
  460. public virtual XmlNode PrependChild (XmlNode newChild)
  461. {
  462. return InsertAfter (newChild, null);
  463. }
  464. public virtual void RemoveAll ()
  465. {
  466. if (Attributes != null)
  467. Attributes.RemoveAll ();
  468. XmlNode next = null;
  469. for (XmlNode node = FirstChild; node != null; node = next) {
  470. next = node.NextSibling;
  471. RemoveChild (node);
  472. }
  473. }
  474. public virtual XmlNode RemoveChild (XmlNode oldChild)
  475. {
  476. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  477. if(oldChild.ParentNode != this)
  478. throw new XmlException ("specified child is not child of this node.");
  479. ownerDoc.onNodeRemoving (oldChild, oldChild.ParentNode);
  480. if (NodeType != XmlNodeType.Attribute &&
  481. NodeType != XmlNodeType.Element &&
  482. NodeType != XmlNodeType.Document &&
  483. NodeType != XmlNodeType.DocumentFragment)
  484. throw new ArgumentException (String.Format ("This {0} node cannot remove child.", NodeType));
  485. if (IsReadOnly)
  486. throw new ArgumentException (String.Format ("This {0} node is read only.", NodeType));
  487. if (Object.ReferenceEquals (LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals (LastLinkedChild, oldChild))
  488. // If there is only one children, simply clear.
  489. LastLinkedChild = null;
  490. else {
  491. XmlLinkedNode oldLinkedChild = (XmlLinkedNode) oldChild;
  492. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  493. XmlLinkedNode firstChild = (XmlLinkedNode) FirstChild;
  494. while (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, LastLinkedChild) == false &&
  495. Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  496. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  497. if (Object.ReferenceEquals (beforeLinkedChild.NextLinkedSibling, oldLinkedChild) == false)
  498. throw new ArgumentException ();
  499. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  500. // Each derived class may have its own LastLinkedChild, so we must set it explicitly.
  501. if (oldLinkedChild.NextLinkedSibling == firstChild)
  502. this.LastLinkedChild = beforeLinkedChild;
  503. oldLinkedChild.NextLinkedSibling = null;
  504. }
  505. ownerDoc.onNodeRemoved (oldChild, oldChild.ParentNode);
  506. oldChild.parentNode = null; // clear parent 'after' above logic.
  507. return oldChild;
  508. }
  509. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  510. {
  511. if(oldChild.ParentNode != this)
  512. throw new InvalidOperationException ("oldChild is not a child of this node.");
  513. if (newChild == this || isAncestorIntern(newChild))
  514. throw new ArgumentException("Cannot insert a node or any ancestor of that node as a child of itself.");
  515. foreach(XmlNode n in ChildNodes) {
  516. if(n == oldChild) {
  517. XmlNode prev = oldChild.PreviousSibling;
  518. RemoveChild (oldChild);
  519. InsertAfter (newChild, prev);
  520. break;
  521. }
  522. }
  523. return oldChild;
  524. }
  525. internal void SearchDescendantElements (string name, bool matchAll, ArrayList list)
  526. {
  527. foreach (XmlNode n in ChildNodes){
  528. if (n.NodeType != XmlNodeType.Element)
  529. continue;
  530. if (matchAll || n.Name == name)
  531. list.Add (n);
  532. n.SearchDescendantElements (name, matchAll, list);
  533. }
  534. }
  535. internal void SearchDescendantElements (string name, bool matchAllName, string ns, bool matchAllNS, ArrayList list)
  536. {
  537. foreach (XmlNode n in ChildNodes){
  538. if (n.NodeType != XmlNodeType.Element)
  539. continue;
  540. if ((matchAllName || n.LocalName == name)
  541. && (matchAllNS || n.NamespaceURI == ns))
  542. list.Add (n);
  543. n.SearchDescendantElements (name, matchAllName, ns, matchAllNS, list);
  544. }
  545. }
  546. public XmlNodeList SelectNodes (string xpath)
  547. {
  548. return SelectNodes (xpath, null);
  549. }
  550. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  551. {
  552. XPathNavigator nav = CreateNavigator ();
  553. XPathExpression expr = nav.Compile (xpath);
  554. if (nsmgr != null)
  555. expr.SetContext (nsmgr);
  556. XPathNodeIterator iter = nav.Select (expr);
  557. ArrayList rgNodes = new ArrayList ();
  558. while (iter.MoveNext ())
  559. {
  560. rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
  561. }
  562. return new XmlNodeArrayList (rgNodes);
  563. }
  564. public XmlNode SelectSingleNode (string xpath)
  565. {
  566. return SelectSingleNode (xpath, null);
  567. }
  568. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  569. {
  570. XPathNavigator nav = CreateNavigator ();
  571. XPathExpression expr = nav.Compile (xpath);
  572. if (nsmgr != null)
  573. expr.SetContext (nsmgr);
  574. XPathNodeIterator iter = nav.Select (expr);
  575. if (!iter.MoveNext ())
  576. return null;
  577. return ((XmlDocumentNavigator) iter.Current).Node;
  578. }
  579. public virtual bool Supports (string feature, string version)
  580. {
  581. if (String.Compare (feature, "xml", true) == 0 // not case-sensitive
  582. && (String.Compare (version, "1.0", true) == 0
  583. || String.Compare (version, "2.0", true) == 0))
  584. return true;
  585. else
  586. return false;
  587. }
  588. public abstract void WriteContentTo (XmlWriter w);
  589. public abstract void WriteTo (XmlWriter w);
  590. // It parses this and all the ancestor elements,
  591. // find 'xmlns' declarations, stores and then return them.
  592. internal XmlNamespaceManager ConstructNamespaceManager ()
  593. {
  594. XmlDocument doc = this is XmlDocument ? (XmlDocument)this : this.OwnerDocument;
  595. XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
  596. XmlElement el = null;
  597. switch(this.NodeType) {
  598. case XmlNodeType.Attribute:
  599. el = ((XmlAttribute)this).OwnerElement;
  600. break;
  601. case XmlNodeType.Element:
  602. el = this as XmlElement;
  603. break;
  604. default:
  605. el = this.ParentNode as XmlElement;
  606. break;
  607. }
  608. while(el != null) {
  609. foreach(XmlAttribute attr in el.Attributes) {
  610. if(attr.Prefix == "xmlns") {
  611. if (nsmgr.LookupNamespace (attr.LocalName) != attr.Value)
  612. nsmgr.AddNamespace (attr.LocalName, attr.Value);
  613. } else if(attr.Name == "xmlns") {
  614. if(nsmgr.LookupNamespace (String.Empty) != attr.Value)
  615. nsmgr.AddNamespace (String.Empty, attr.Value);
  616. }
  617. }
  618. // When reached to document, then it will set null value :)
  619. el = el.ParentNode as XmlElement;
  620. }
  621. return nsmgr;
  622. }
  623. #endregion
  624. }
  625. }