XmlDocument.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. //
  2. // System.Xml.XmlDocument
  3. //
  4. // Author:
  5. // Daniel Weber ([email protected])
  6. //
  7. // (C) 2001 Daniel Weber
  8. using System;
  9. using System.IO;
  10. using System.Xml.XPath;
  11. namespace System.Xml
  12. {
  13. public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
  14. public class XmlDocument : XmlNode
  15. {
  16. #region Fields
  17. private XmlLinkedNode lastLinkedChild;
  18. #endregion
  19. #region Constructors
  20. public XmlDocument () : base (null) { }
  21. [MonoTODO]
  22. protected internal XmlDocument (XmlImplementation imp) : base (null)
  23. {
  24. throw new NotImplementedException ();
  25. }
  26. [MonoTODO]
  27. public XmlDocument (NameTable nt) : base (null)
  28. {
  29. throw new NotImplementedException ();
  30. }
  31. #endregion
  32. #region Events
  33. public event XmlNodeChangedEventHandler NodeChanged;
  34. public event XmlNodeChangedEventHandler NodeChanging;
  35. public event XmlNodeChangedEventHandler NodeInserted;
  36. public event XmlNodeChangedEventHandler NodeInserting;
  37. public event XmlNodeChangedEventHandler NodeRemoved;
  38. public event XmlNodeChangedEventHandler NodeRemoving;
  39. #endregion
  40. #region Properties
  41. [MonoTODO]
  42. public override string BaseURI {
  43. get { throw new NotImplementedException(); }
  44. }
  45. public XmlElement DocumentElement {
  46. get {
  47. XmlNode node = FirstChild;
  48. while (node != null) {
  49. if (node is XmlElement)
  50. break;
  51. node = node.NextSibling;
  52. }
  53. return node != null ? node as XmlElement : null;
  54. }
  55. }
  56. [MonoTODO]
  57. public virtual XmlDocumentType DocumentType {
  58. get { throw new NotImplementedException(); }
  59. }
  60. [MonoTODO]
  61. public XmlImplementation Implementation {
  62. get { throw new NotImplementedException(); }
  63. }
  64. [MonoTODO]
  65. public override string InnerXml {
  66. get { throw new NotImplementedException(); }
  67. set { throw new NotImplementedException(); }
  68. }
  69. public override bool IsReadOnly {
  70. get { return false; }
  71. }
  72. internal override XmlLinkedNode LastLinkedChild {
  73. get {
  74. return lastLinkedChild;
  75. }
  76. set {
  77. lastLinkedChild = value;
  78. }
  79. }
  80. public override string LocalName {
  81. get { return "#document"; }
  82. }
  83. public override string Name {
  84. get { return "#document"; }
  85. }
  86. [MonoTODO]
  87. public XmlNameTable NameTable {
  88. get { throw new NotImplementedException(); }
  89. }
  90. public override XmlNodeType NodeType {
  91. get { return XmlNodeType.Document; }
  92. }
  93. public override XmlDocument OwnerDocument {
  94. get { return null; }
  95. }
  96. [MonoTODO]
  97. public bool PreserveWhitespace {
  98. get { throw new NotImplementedException(); }
  99. set { throw new NotImplementedException(); }
  100. }
  101. [MonoTODO]
  102. public XmlResolver XmlResolver {
  103. set { throw new NotImplementedException(); }
  104. }
  105. #endregion
  106. #region Methods
  107. [MonoTODO]
  108. public override XmlNode CloneNode (bool deep)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. [MonoTODO]
  113. public XmlAttribute CreateAttribute (string name)
  114. {
  115. int indexOfColon = name.IndexOf (':');
  116. if (indexOfColon == -1)
  117. return CreateAttribute (String.Empty, name, String.Empty);
  118. string prefix = name.Substring (0, indexOfColon);
  119. string localName = name.Substring (indexOfColon + 1);
  120. return CreateAttribute (prefix, localName, String.Empty);
  121. }
  122. [MonoTODO]
  123. public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
  124. {
  125. int indexOfColon = qualifiedName.IndexOf (':');
  126. if (indexOfColon == -1)
  127. return CreateAttribute (String.Empty, qualifiedName, String.Empty);
  128. string prefix = qualifiedName.Substring (0, indexOfColon);
  129. string localName = qualifiedName.Substring (indexOfColon + 1);
  130. return CreateAttribute (prefix, localName, String.Empty);
  131. }
  132. public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
  133. {
  134. return new XmlAttribute (prefix, localName, namespaceURI, this);
  135. }
  136. public virtual XmlCDataSection CreateCDataSection (string data)
  137. {
  138. return new XmlCDataSection (data, this);
  139. }
  140. public virtual XmlComment CreateComment (string data)
  141. {
  142. return new XmlComment(data, this);
  143. }
  144. [MonoTODO]
  145. protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. [MonoTODO]
  150. public virtual XmlDocumentFragment CreateDocumentFragment ()
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. [MonoTODO]
  155. public virtual XmlDocumentType CreateDocumentType (
  156. string name,
  157. string publicId,
  158. string systemId,
  159. string internalSubset)
  160. {
  161. throw new NotImplementedException ();
  162. }
  163. public XmlElement CreateElement (string name)
  164. {
  165. int indexOfColon = name.IndexOf (':');
  166. if (indexOfColon == -1)
  167. return CreateElement (String.Empty, name, String.Empty);
  168. string prefix = name.Substring (0, indexOfColon);
  169. string localName = name.Substring (indexOfColon + 1);
  170. return CreateElement (prefix, localName, String.Empty);
  171. }
  172. [MonoTODO]
  173. public XmlElement CreateElement (
  174. string qualifiedName,
  175. string namespaceURI)
  176. {
  177. int indexOfColon = qualifiedName.IndexOf (':');
  178. if (indexOfColon == -1)
  179. return CreateElement (String.Empty, qualifiedName, namespaceURI);
  180. string prefix = qualifiedName.Substring (0, indexOfColon);
  181. string localName = qualifiedName.Substring (indexOfColon + 1);
  182. return CreateElement (prefix, localName, namespaceURI);
  183. }
  184. public virtual XmlElement CreateElement (
  185. string prefix,
  186. string localName,
  187. string namespaceURI)
  188. {
  189. return new XmlElement (prefix, localName, namespaceURI, this);
  190. }
  191. [MonoTODO]
  192. public virtual XmlEntityReference CreateEntityReference (string name)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO]
  197. protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. [MonoTODO]
  202. public virtual XmlNode CreateNode (
  203. string nodeTypeString,
  204. string name,
  205. string namespaceURI)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. [MonoTODO]
  210. public virtual XmlNode CreateNode (
  211. XmlNodeType type,
  212. string name,
  213. string namespaceURI)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. [MonoTODO]
  218. public virtual XmlNode CreateNode (
  219. XmlNodeType type,
  220. string prefix,
  221. string name,
  222. string namespaceURI)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. public virtual XmlProcessingInstruction CreateProcessingInstruction (
  227. string target,
  228. string data)
  229. {
  230. return new XmlProcessingInstruction (target, data, this);
  231. }
  232. [MonoTODO]
  233. public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
  234. {
  235. throw new NotImplementedException ();
  236. }
  237. public virtual XmlText CreateTextNode (string text)
  238. {
  239. return new XmlText (text, this);
  240. }
  241. [MonoTODO]
  242. public virtual XmlWhitespace CreateWhitespace (string text)
  243. {
  244. throw new NotImplementedException ();
  245. }
  246. [MonoTODO]
  247. public virtual XmlDeclaration CreateXmlDeclaration (
  248. string version,
  249. string encoding,
  250. string standalone)
  251. {
  252. throw new NotImplementedException();
  253. }
  254. [MonoTODO]
  255. public virtual XmlElement GetElementById (string elementId)
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. [MonoTODO]
  260. public virtual XmlNodeList GetElementsByTagName (string name)
  261. {
  262. throw new NotImplementedException ();
  263. }
  264. [MonoTODO]
  265. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  266. {
  267. throw new NotImplementedException();
  268. }
  269. [MonoTODO]
  270. public virtual XmlNode ImportNode (XmlNode node, bool deep)
  271. {
  272. throw new NotImplementedException ();
  273. }
  274. [MonoTODO]
  275. public virtual void Load (Stream inStream)
  276. {
  277. throw new NotImplementedException ();
  278. }
  279. public virtual void Load (string filename)
  280. {
  281. XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
  282. Load (xmlReader);
  283. }
  284. [MonoTODO]
  285. public virtual void Load (TextReader txtReader)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. public virtual void Load (XmlReader xmlReader)
  290. {
  291. // Reset our document
  292. // For now this just means removing all our children but later this
  293. // may turn out o need to call a private method that resets other things
  294. // like properties we have, etc.
  295. RemoveAll ();
  296. XmlNode currentNode = this;
  297. while (xmlReader.Read ())
  298. {
  299. switch (xmlReader.NodeType) {
  300. case XmlNodeType.CDATA:
  301. XmlCDataSection cdataSection = CreateCDataSection(xmlReader.Value);
  302. currentNode.AppendChild (cdataSection);
  303. break;
  304. case XmlNodeType.Comment:
  305. XmlComment comment = CreateComment (xmlReader.Value);
  306. currentNode.AppendChild (comment);
  307. break;
  308. case XmlNodeType.Element:
  309. XmlElement element = CreateElement (xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
  310. currentNode.AppendChild (element);
  311. // set the element's attributes.
  312. while (xmlReader.MoveToNextAttribute ())
  313. element.SetAttribute (xmlReader.Name, xmlReader.Value);
  314. // if this element isn't empty, push it onto our "stack".
  315. if (!xmlReader.IsEmptyElement)
  316. currentNode = element;
  317. break;
  318. case XmlNodeType.EndElement:
  319. currentNode = currentNode.ParentNode;
  320. break;
  321. case XmlNodeType.ProcessingInstruction:
  322. XmlProcessingInstruction processingInstruction = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
  323. // Where does a processing instruction go in the doc?
  324. // I think we need to just hold on to them in an internal array in doc.
  325. break;
  326. case XmlNodeType.Text:
  327. XmlText text = CreateTextNode (xmlReader.Value);
  328. currentNode.AppendChild (text);
  329. break;
  330. }
  331. }
  332. }
  333. public virtual void LoadXml (string xml)
  334. {
  335. XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
  336. Load (xmlReader);
  337. }
  338. [MonoTODO]
  339. public virtual XmlNode ReadNode(XmlReader reader)
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. [MonoTODO]
  344. public virtual void Save(Stream outStream)
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. [MonoTODO]
  349. public virtual void Save (string filename)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. [MonoTODO]
  354. public virtual void Save (TextWriter writer)
  355. {
  356. throw new NotImplementedException ();
  357. }
  358. [MonoTODO]
  359. public virtual void Save (XmlWriter writer)
  360. {
  361. throw new NotImplementedException ();
  362. }
  363. [MonoTODO]
  364. public override void WriteContentTo (XmlWriter xw)
  365. {
  366. throw new NotImplementedException ();
  367. }
  368. [MonoTODO]
  369. public override void WriteTo (XmlWriter w)
  370. {
  371. throw new NotImplementedException ();
  372. }
  373. internal void onNodeChanging(XmlNode node, XmlNode Parent)
  374. {
  375. if (NodeInserting != null)
  376. NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
  377. node, Parent, Parent));
  378. }
  379. internal void onNodeChanged(XmlNode node, XmlNode Parent)
  380. {
  381. if (NodeChanged != null)
  382. NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
  383. node, Parent, Parent));
  384. }
  385. internal void onNodeInserting(XmlNode node, XmlNode newParent)
  386. {
  387. if (NodeInserting != null)
  388. NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
  389. node, null, newParent));
  390. }
  391. internal void onNodeInserted(XmlNode node, XmlNode newParent)
  392. {
  393. if (NodeInserted != null)
  394. NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
  395. node, null, newParent));
  396. }
  397. internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
  398. {
  399. if (NodeRemoving != null)
  400. NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
  401. node, oldParent, null));
  402. }
  403. internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
  404. {
  405. if (NodeRemoved != null)
  406. NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
  407. node, oldParent, null));
  408. }
  409. #endregion
  410. }
  411. }