XmlNode.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //
  2. // System.Xml.XmlNode
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml.XPath;
  14. namespace System.Xml
  15. {
  16. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  17. {
  18. #region Fields
  19. XmlDocument ownerDocument;
  20. XmlNode parentNode;
  21. #endregion
  22. #region Constructors
  23. internal XmlNode (XmlDocument ownerDocument)
  24. {
  25. this.ownerDocument = ownerDocument;
  26. }
  27. #endregion
  28. #region Properties
  29. public virtual XmlAttributeCollection Attributes
  30. {
  31. get { return null; }
  32. }
  33. public virtual string BaseURI
  34. {
  35. get { return ParentNode.BaseURI; }
  36. }
  37. public virtual XmlNodeList ChildNodes {
  38. get {
  39. return new XmlNodeListChildren(this);
  40. }
  41. }
  42. public virtual XmlNode FirstChild {
  43. get {
  44. if (LastChild != null) {
  45. return LastLinkedChild.NextLinkedSibling;
  46. }
  47. else {
  48. return null;
  49. }
  50. }
  51. }
  52. public virtual bool HasChildNodes {
  53. get { return LastChild != null; }
  54. }
  55. [MonoTODO]
  56. public virtual string InnerText {
  57. get {
  58. StringBuilder builder = new StringBuilder ();
  59. AppendChildValues (this, builder);
  60. return builder.ToString ();
  61. }
  62. set { throw new NotImplementedException (); }
  63. }
  64. private void AppendChildValues (XmlNode parent, StringBuilder builder)
  65. {
  66. XmlNode node = parent.FirstChild;
  67. while (node != null) {
  68. if (node.NodeType == XmlNodeType.Text)
  69. builder.Append (node.Value);
  70. AppendChildValues (node, builder);
  71. node = node.NextSibling;
  72. }
  73. }
  74. [MonoTODO("Setter.")]
  75. public virtual string InnerXml {
  76. get {
  77. StringWriter sw = new StringWriter ();
  78. XmlTextWriter xtw = new XmlTextWriter (sw);
  79. WriteContentTo(xtw);
  80. return sw.GetStringBuilder().ToString();
  81. }
  82. set { throw new NotImplementedException (); }
  83. }
  84. public virtual bool IsReadOnly {
  85. get { return false; }
  86. }
  87. [System.Runtime.CompilerServices.IndexerName("Item")]
  88. public virtual XmlElement this [string name] {
  89. get {
  90. foreach (XmlNode node in ChildNodes) {
  91. if ((node.NodeType == XmlNodeType.Element) &&
  92. (node.Name == name)) {
  93. return (XmlElement) node;
  94. }
  95. }
  96. return null;
  97. }
  98. }
  99. [System.Runtime.CompilerServices.IndexerName("Item")]
  100. public virtual XmlElement this [string localname, string ns] {
  101. get {
  102. foreach (XmlNode node in ChildNodes) {
  103. if ((node.NodeType == XmlNodeType.Element) &&
  104. (node.LocalName == localname) &&
  105. (node.NamespaceURI == ns)) {
  106. return (XmlElement) node;
  107. }
  108. }
  109. return null;
  110. }
  111. }
  112. public virtual XmlNode LastChild {
  113. get { return LastLinkedChild; }
  114. }
  115. internal virtual XmlLinkedNode LastLinkedChild {
  116. get { return null; }
  117. set { }
  118. }
  119. public abstract string LocalName { get; }
  120. public abstract string Name { get; }
  121. public virtual string NamespaceURI {
  122. get { return String.Empty; }
  123. }
  124. public virtual XmlNode NextSibling {
  125. get { return null; }
  126. }
  127. public abstract XmlNodeType NodeType { get; }
  128. public virtual string OuterXml {
  129. get {
  130. StringWriter sw = new StringWriter ();
  131. XmlTextWriter xtw = new XmlTextWriter (sw);
  132. WriteTo(xtw);
  133. return sw.GetStringBuilder().ToString();
  134. }
  135. }
  136. public virtual XmlDocument OwnerDocument {
  137. get { return ownerDocument; }
  138. }
  139. public virtual XmlNode ParentNode {
  140. get { return parentNode; }
  141. }
  142. public virtual string Prefix {
  143. get { return String.Empty; }
  144. set {}
  145. }
  146. public virtual XmlNode PreviousSibling {
  147. get { return null; }
  148. }
  149. public virtual string Value {
  150. get { return null; }
  151. set { throw new InvalidOperationException ("This node does not have a value"); }
  152. }
  153. #endregion
  154. #region Methods
  155. public virtual XmlNode AppendChild (XmlNode newChild)
  156. {
  157. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  158. ownerDoc.onNodeInserting (newChild, this);
  159. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
  160. if (newChild.OwnerDocument != ownerDoc)
  161. throw new ArgumentException ("Can't append a node created by another document.");
  162. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  163. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  164. newLinkedChild.parentNode = this;
  165. if (lastLinkedChild != null) {
  166. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  167. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  168. } else
  169. newLinkedChild.NextLinkedSibling = newLinkedChild;
  170. LastLinkedChild = newLinkedChild;
  171. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  172. return newChild;
  173. } else
  174. throw new InvalidOperationException();
  175. }
  176. [MonoTODO]
  177. public virtual XmlNode Clone ()
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. public abstract XmlNode CloneNode (bool deep);
  182. [MonoTODO]
  183. public XPathNavigator CreateNavigator ()
  184. {
  185. return new XmlDocumentNavigator(this);
  186. }
  187. public IEnumerator GetEnumerator ()
  188. {
  189. return new XmlNodeListChildren(this).GetEnumerator();
  190. }
  191. [MonoTODO]
  192. public virtual string GetNamespaceOfPrefix (string prefix)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO]
  197. public virtual string GetPrefixOfNamespace (string namespaceURI)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. object ICloneable.Clone ()
  202. {
  203. return Clone ();
  204. }
  205. IEnumerator IEnumerable.GetEnumerator ()
  206. {
  207. return GetEnumerator ();
  208. }
  209. [MonoTODO]
  210. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. [MonoTODO]
  220. public virtual void Normalize ()
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. [MonoTODO]
  225. public virtual XmlNode PrependChild (XmlNode newChild)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. public virtual void RemoveAll ()
  230. {
  231. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  232. ownerDoc.onNodeRemoving (this, this.ParentNode);
  233. LastLinkedChild = null;
  234. ownerDoc.onNodeRemoved (this, this.ParentNode);
  235. }
  236. public virtual XmlNode RemoveChild (XmlNode oldChild)
  237. {
  238. OwnerDocument.onNodeRemoving (oldChild, oldChild.ParentNode);
  239. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
  240. {
  241. if (IsReadOnly)
  242. throw new ArgumentException();
  243. if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
  244. LastLinkedChild = null;
  245. else {
  246. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  247. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  248. while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  249. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  250. if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  251. throw new ArgumentException();
  252. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  253. oldLinkedChild.NextLinkedSibling = null;
  254. }
  255. OwnerDocument.onNodeRemoved (oldChild, oldChild.ParentNode);
  256. return oldChild;
  257. }
  258. else
  259. throw new ArgumentException();
  260. }
  261. [MonoTODO]
  262. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. public XmlNodeList SelectNodes (string xpath)
  267. {
  268. return SelectNodes (xpath, null);
  269. }
  270. [MonoTODO]
  271. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  272. {
  273. XPathNavigator nav = CreateNavigator ();
  274. XPathExpression expr = nav.Compile (xpath);
  275. if (nsmgr != null)
  276. expr.SetContext (nsmgr);
  277. XPathNodeIterator iter = nav.Select (expr);
  278. ArrayList rgNodes = new ArrayList ();
  279. while (iter.MoveNext ())
  280. {
  281. rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
  282. }
  283. return new XmlNodeArrayList (rgNodes);
  284. }
  285. public XmlNode SelectSingleNode (string xpath)
  286. {
  287. return SelectSingleNode (xpath, null);
  288. }
  289. [MonoTODO]
  290. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  291. {
  292. XPathNavigator nav = CreateNavigator ();
  293. XPathExpression expr = nav.Compile (xpath);
  294. if (nsmgr != null)
  295. expr.SetContext (nsmgr);
  296. XPathNodeIterator iter = nav.Select (expr);
  297. if (!iter.MoveNext ())
  298. return null;
  299. return ((XmlDocumentNavigator) iter.Current).Node;
  300. }
  301. internal void SetParentNode (XmlNode parent)
  302. {
  303. parentNode = parent;
  304. }
  305. [MonoTODO]
  306. public virtual bool Supports (string feature, string version)
  307. {
  308. throw new NotImplementedException ();
  309. }
  310. public abstract void WriteContentTo (XmlWriter w);
  311. public abstract void WriteTo (XmlWriter w);
  312. #endregion
  313. }
  314. }