XmlNode.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  157. ownerDoc.onNodeInserting (newChild, this);
  158. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute) {
  159. if (newChild.OwnerDocument != ownerDoc)
  160. throw new ArgumentException ("Can't append a node created by another document.");
  161. XmlLinkedNode newLinkedChild = (XmlLinkedNode) newChild;
  162. XmlLinkedNode lastLinkedChild = LastLinkedChild;
  163. newLinkedChild.parentNode = this;
  164. if (lastLinkedChild != null) {
  165. newLinkedChild.NextLinkedSibling = lastLinkedChild.NextLinkedSibling;
  166. lastLinkedChild.NextLinkedSibling = newLinkedChild;
  167. } else
  168. newLinkedChild.NextLinkedSibling = newLinkedChild;
  169. LastLinkedChild = newLinkedChild;
  170. ownerDoc.onNodeInserted (newChild, newChild.ParentNode);
  171. return newChild;
  172. } else
  173. throw new InvalidOperationException();
  174. }
  175. [MonoTODO]
  176. public virtual XmlNode Clone ()
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. public abstract XmlNode CloneNode (bool deep);
  181. [MonoTODO]
  182. public virtual XPathNavigator CreateNavigator ()
  183. {
  184. return new XmlDocumentNavigator(this);
  185. }
  186. public IEnumerator GetEnumerator ()
  187. {
  188. return new XmlNodeListChildren(this).GetEnumerator();
  189. }
  190. [MonoTODO]
  191. public virtual string GetNamespaceOfPrefix (string prefix)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. public virtual string GetPrefixOfNamespace (string namespaceURI)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. object ICloneable.Clone ()
  201. {
  202. return Clone ();
  203. }
  204. IEnumerator IEnumerable.GetEnumerator ()
  205. {
  206. return GetEnumerator ();
  207. }
  208. [MonoTODO]
  209. public virtual XmlNode InsertAfter (XmlNode newChild, XmlNode refChild)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. [MonoTODO]
  214. public virtual XmlNode InsertBefore (XmlNode newChild, XmlNode refChild)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. [MonoTODO]
  219. public virtual void Normalize ()
  220. {
  221. throw new NotImplementedException ();
  222. }
  223. [MonoTODO]
  224. public virtual XmlNode PrependChild (XmlNode newChild)
  225. {
  226. throw new NotImplementedException ();
  227. }
  228. public virtual void RemoveAll ()
  229. {
  230. XmlDocument ownerDoc = (NodeType == XmlNodeType.Document) ? (XmlDocument)this : OwnerDocument;
  231. ownerDoc.onNodeRemoving (this, this.ParentNode);
  232. LastLinkedChild = null;
  233. ownerDoc.onNodeRemoved (this, this.ParentNode);
  234. }
  235. public virtual XmlNode RemoveChild (XmlNode oldChild)
  236. {
  237. OwnerDocument.onNodeRemoving (oldChild, oldChild.ParentNode);
  238. if (NodeType == XmlNodeType.Document || NodeType == XmlNodeType.Element || NodeType == XmlNodeType.Attribute)
  239. {
  240. if (IsReadOnly)
  241. throw new ArgumentException();
  242. if (Object.ReferenceEquals(LastLinkedChild, LastLinkedChild.NextLinkedSibling) && Object.ReferenceEquals(LastLinkedChild, oldChild))
  243. LastLinkedChild = null;
  244. else {
  245. XmlLinkedNode oldLinkedChild = (XmlLinkedNode)oldChild;
  246. XmlLinkedNode beforeLinkedChild = LastLinkedChild;
  247. while (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, LastLinkedChild) && !Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  248. beforeLinkedChild = beforeLinkedChild.NextLinkedSibling;
  249. if (!Object.ReferenceEquals(beforeLinkedChild.NextLinkedSibling, oldLinkedChild))
  250. throw new ArgumentException();
  251. beforeLinkedChild.NextLinkedSibling = oldLinkedChild.NextLinkedSibling;
  252. oldLinkedChild.NextLinkedSibling = null;
  253. }
  254. OwnerDocument.onNodeRemoved (oldChild, oldChild.ParentNode);
  255. return oldChild;
  256. }
  257. else
  258. throw new ArgumentException();
  259. }
  260. [MonoTODO]
  261. public virtual XmlNode ReplaceChild (XmlNode newChild, XmlNode oldChild)
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. public XmlNodeList SelectNodes (string xpath)
  266. {
  267. return SelectNodes (xpath, null);
  268. }
  269. [MonoTODO]
  270. public XmlNodeList SelectNodes (string xpath, XmlNamespaceManager nsmgr)
  271. {
  272. XPathNavigator nav = CreateNavigator ();
  273. XPathExpression expr = nav.Compile (xpath);
  274. if (nsmgr != null)
  275. expr.SetContext (nsmgr);
  276. XPathNodeIterator iter = nav.Select (expr);
  277. ArrayList rgNodes = new ArrayList ();
  278. while (iter.MoveNext ())
  279. {
  280. rgNodes.Add (((XmlDocumentNavigator) iter.Current).Node);
  281. }
  282. return new XmlNodeArrayList (rgNodes);
  283. }
  284. public XmlNode SelectSingleNode (string xpath)
  285. {
  286. return SelectSingleNode (xpath, null);
  287. }
  288. [MonoTODO]
  289. public XmlNode SelectSingleNode (string xpath, XmlNamespaceManager nsmgr)
  290. {
  291. XPathNavigator nav = CreateNavigator ();
  292. XPathExpression expr = nav.Compile (xpath);
  293. if (nsmgr != null)
  294. expr.SetContext (nsmgr);
  295. XPathNodeIterator iter = nav.Select (expr);
  296. if (!iter.MoveNext ())
  297. return null;
  298. return ((XmlDocumentNavigator) iter.Current).Node;
  299. }
  300. internal void SetParentNode (XmlNode parent)
  301. {
  302. parentNode = parent;
  303. }
  304. [MonoTODO]
  305. public virtual bool Supports (string feature, string version)
  306. {
  307. throw new NotImplementedException ();
  308. }
  309. public abstract void WriteContentTo (XmlWriter w);
  310. public abstract void WriteTo (XmlWriter w);
  311. #endregion
  312. }
  313. }