XmlNode.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. protected 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. [MonoTODO]
  116. public virtual string OuterXml {
  117. get {
  118. StringWriter sw = new StringWriter ();
  119. XmlTextWriter xtw = new XmlTextWriter (sw);
  120. WriteTo(xtw);
  121. return sw.GetStringBuilder().ToString();
  122. }
  123. }
  124. public virtual XmlDocument OwnerDocument {
  125. get { return ownerDocument; }
  126. }
  127. public virtual XmlNode ParentNode {
  128. get { return parentNode; }
  129. }
  130. [MonoTODO]
  131. public virtual string Prefix {
  132. get { throw new NotImplementedException (); }
  133. set { throw new NotImplementedException (); }
  134. }
  135. public virtual XmlNode PreviousSibling {
  136. get { return null; }
  137. }
  138. public virtual string Value {
  139. get { return null; }
  140. set { throw new InvalidOperationException ("This node does not have a value"); }
  141. }
  142. #endregion
  143. #region Methods
  144. public virtual XmlNode AppendChild (XmlNode newChild)
  145. {
  146. if (NodeType == XmlNodeType.Document
  147. || NodeType == XmlNodeType.Element
  148. || NodeType == XmlNodeType.Attribute) {
  149. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  150. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  151. newLinkedChild.parentNode = this;
  152. if (lastLinkedChild != null) {
  153. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  154. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  155. } else
  156. newLinkedChild.NextLinkedSibling = newLinkedChild;
  157. LastLinkedChild = newLinkedChild;
  158. return newChild;
  159. } else
  160. throw new InvalidOperationException();
  161. }
  162. [MonoTODO]
  163. public virtual XmlNode Clone ()
  164. {
  165. throw new NotImplementedException ();
  166. }
  167. public abstract XmlNode CloneNode (bool deep);
  168. [MonoTODO]
  169. public XPathNavigator CreateNavigator ()
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. public IEnumerator GetEnumerator ()
  174. {
  175. return new XmlNodeListChildren(this).GetEnumerator();
  176. }
  177. [MonoTODO]
  178. public virtual string GetNamespaceOfPrefix (string prefix)
  179. {
  180. throw new NotImplementedException ();
  181. }
  182. [MonoTODO]
  183. public virtual string GetPrefixOfNamespace (string namespaceURI)
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. object ICloneable.Clone ()
  188. {
  189. return Clone ();
  190. }
  191. IEnumerator IEnumerable.GetEnumerator ()
  192. {
  193. return GetEnumerator ();
  194. }
  195. [MonoTODO]
  196. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. [MonoTODO]
  201. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  202. {
  203. throw new NotImplementedException ();
  204. }
  205. [MonoTODO]
  206. public virtual void Normalize ()
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. [MonoTODO]
  211. public virtual XmlNode PrependChild (XmlNode newChild)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. public virtual void RemoveAll ()
  216. {
  217. LastLinkedChild = null;
  218. }
  219. public virtual XmlNode RemoveChild (XmlNode oldChild)
  220. {
  221. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
  222. {
  223. if (IsReadOnly)
  224. throw new ArgumentException();
  225. if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
  226. LastLinkedChild = null;
  227. else {
  228. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  229. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  230. while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  231. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  232. if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  233. throw new ArgumentException();
  234. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  235. oldLinkedChild.NextLinkedSibling = null;
  236. }
  237. return oldChild;
  238. }
  239. else
  240. throw new ArgumentException();
  241. }
  242. [MonoTODO]
  243. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. [MonoTODO]
  248. public virtual XmlNodeList SelectNodes (string xpath)
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. [MonoTODO]
  253. public virtual XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  254. {
  255. throw new NotImplementedException ();
  256. }
  257. [MonoTODO]
  258. public virtual XmlNode SelectSingleNode (string xpath)
  259. {
  260. throw new NotImplementedException ();
  261. }
  262. [MonoTODO]
  263. public virtual XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  264. {
  265. throw new NotImplementedException ();
  266. }
  267. internal void SetParentNode (XmlNode parent)
  268. {
  269. parentNode = parent;
  270. }
  271. [MonoTODO]
  272. public virtual bool Supports (string feature, string version)
  273. {
  274. throw new NotImplementedException ();
  275. }
  276. public abstract void WriteContentTo (XmlWriter w);
  277. public abstract void WriteTo (XmlWriter w);
  278. #endregion
  279. }
  280. }