XmlDocument.cs 12 KB

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