XmlNode.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.Xml.XPath;
  13. namespace System.Xml
  14. {
  15. public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
  16. {
  17. #region Fields
  18. XmlDocument ownerDocument;
  19. XmlNode parentNode;
  20. #endregion
  21. #region Constructors
  22. internal XmlNode (XmlDocument ownerDocument)
  23. {
  24. this.ownerDocument = ownerDocument;
  25. }
  26. #endregion
  27. #region Properties
  28. public virtual XmlAttributeCollection Attributes
  29. {
  30. get { return null; }
  31. }
  32. [MonoTODO]
  33. public virtual string BaseURI
  34. {
  35. get { throw new NotImplementedException (); }
  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 { throw new NotImplementedException (); }
  58. set { throw new NotImplementedException (); }
  59. }
  60. [MonoTODO("Setter.")]
  61. public virtual string InnerXml {
  62. get {
  63. StringWriter sw = new StringWriter ();
  64. XmlTextWriter xtw = new XmlTextWriter (sw);
  65. WriteContentTo(xtw);
  66. return sw.GetStringBuilder().ToString();
  67. }
  68. set { throw new NotImplementedException (); }
  69. }
  70. public virtual bool IsReadOnly {
  71. get { return false; }
  72. }
  73. [System.Runtime.CompilerServices.IndexerName("Item")]
  74. public virtual XmlElement this [string name] {
  75. get {
  76. foreach (XmlNode node in ChildNodes) {
  77. if ((node.NodeType == XmlNodeType.Element) &&
  78. (node.Name == name)) {
  79. return (XmlElement) node;
  80. }
  81. }
  82. return null;
  83. }
  84. }
  85. [System.Runtime.CompilerServices.IndexerName("Item")]
  86. public virtual XmlElement this [string localname, string ns] {
  87. get {
  88. foreach (XmlNode node in ChildNodes) {
  89. if ((node.NodeType == XmlNodeType.Element) &&
  90. (node.LocalName == localname) &&
  91. (node.NamespaceURI == ns)) {
  92. return (XmlElement) node;
  93. }
  94. }
  95. return null;
  96. }
  97. }
  98. public virtual XmlNode LastChild {
  99. get { return LastLinkedChild; }
  100. }
  101. internal virtual XmlLinkedNode LastLinkedChild {
  102. get { return null; }
  103. set { }
  104. }
  105. public abstract string LocalName { get; }
  106. public abstract string Name { get; }
  107. [MonoTODO]
  108. public virtual string NamespaceURI {
  109. get { throw new NotImplementedException (); }
  110. }
  111. public virtual XmlNode NextSibling {
  112. get { return null; }
  113. }
  114. public abstract XmlNodeType NodeType { get; }
  115. public virtual string OuterXml {
  116. get {
  117. StringWriter sw = new StringWriter ();
  118. XmlTextWriter xtw = new XmlTextWriter (sw);
  119. WriteTo(xtw);
  120. return sw.GetStringBuilder().ToString();
  121. }
  122. }
  123. public virtual XmlDocument OwnerDocument {
  124. get { return ownerDocument; }
  125. }
  126. public virtual XmlNode ParentNode {
  127. get { return parentNode; }
  128. }
  129. [MonoTODO]
  130. public virtual string Prefix {
  131. get { throw new NotImplementedException (); }
  132. set { throw new NotImplementedException (); }
  133. }
  134. public virtual XmlNode PreviousSibling {
  135. get { return null; }
  136. }
  137. public virtual string Value {
  138. get { return null; }
  139. set { throw new InvalidOperationException ("This node does not have a value"); }
  140. }
  141. #endregion
  142. #region Methods
  143. public virtual XmlNode AppendChild (XmlNode newChild)
  144. {
  145. if (NodeType == XmlNodeType.Document
  146. || NodeType == XmlNodeType.Element
  147. || NodeType == XmlNodeType.Attribute) {
  148. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  149. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  150. newLinkedChild.parentNode = this;
  151. if (lastLinkedChild != null) {
  152. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  153. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  154. } else
  155. newLinkedChild.NextLinkedSibling = newLinkedChild;
  156. LastLinkedChild = newLinkedChild;
  157. return newChild;
  158. } else
  159. throw new InvalidOperationException();
  160. }
  161. [MonoTODO]
  162. public virtual XmlNode Clone ()
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. public abstract XmlNode CloneNode (bool deep);
  167. [MonoTODO]
  168. public XPathNavigator CreateNavigator ()
  169. {
  170. throw new NotImplementedException ();
  171. }
  172. public IEnumerator GetEnumerator ()
  173. {
  174. return new XmlNodeListChildren(this).GetEnumerator();
  175. }
  176. [MonoTODO]
  177. public virtual string GetNamespaceOfPrefix (string prefix)
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. [MonoTODO]
  182. public virtual string GetPrefixOfNamespace (string namespaceURI)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. object ICloneable.Clone ()
  187. {
  188. return Clone ();
  189. }
  190. IEnumerator IEnumerable.GetEnumerator ()
  191. {
  192. return GetEnumerator ();
  193. }
  194. [MonoTODO]
  195. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  196. {
  197. throw new NotImplementedException ();
  198. }
  199. [MonoTODO]
  200. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  201. {
  202. throw new NotImplementedException ();
  203. }
  204. [MonoTODO]
  205. public virtual void Normalize ()
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. [MonoTODO]
  210. public virtual XmlNode PrependChild (XmlNode newChild)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. public virtual void RemoveAll ()
  215. {
  216. LastLinkedChild = null;
  217. }
  218. public virtual XmlNode RemoveChild (XmlNode oldChild)
  219. {
  220. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
  221. {
  222. if (IsReadOnly)
  223. throw new ArgumentException();
  224. if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
  225. LastLinkedChild = null;
  226. else {
  227. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  228. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  229. while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  230. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  231. if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  232. throw new ArgumentException();
  233. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  234. oldLinkedChild.NextLinkedSibling = null;
  235. }
  236. return oldChild;
  237. }
  238. else
  239. throw new ArgumentException();
  240. }
  241. [MonoTODO]
  242. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  243. {
  244. throw new NotImplementedException ();
  245. }
  246. [MonoTODO]
  247. public XmlNodeList SelectNodes (string xpath)
  248. {
  249. throw new NotImplementedException ();
  250. }
  251. [MonoTODO]
  252. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  253. {
  254. throw new NotImplementedException ();
  255. }
  256. [MonoTODO]
  257. public XmlNode SelectSingleNode (string xpath)
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. [MonoTODO]
  262. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. internal void SetParentNode (XmlNode parent)
  267. {
  268. parentNode = parent;
  269. }
  270. [MonoTODO]
  271. public virtual bool Supports (string feature, string version)
  272. {
  273. throw new NotImplementedException ();
  274. }
  275. public abstract void WriteContentTo (XmlWriter w);
  276. public abstract void WriteTo (XmlWriter w);
  277. #endregion
  278. }
  279. }