XmlNode.cs 7.7 KB

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