XmlDocument.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. using System.Diagnostics;
  12. namespace System.Xml
  13. {
  14. public class XmlDocument : XmlNode
  15. {
  16. #region Fields
  17. XmlLinkedNode lastLinkedChild;
  18. XmlNameTable nameTable;
  19. #endregion
  20. #region Constructors
  21. public XmlDocument () : base (null) { }
  22. [MonoTODO]
  23. protected internal XmlDocument (XmlImplementation imp) : base (null)
  24. {
  25. throw new NotImplementedException ();
  26. }
  27. public XmlDocument (NameTable nt) : base (null)
  28. {
  29. nameTable = nt;
  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. public XmlNameTable NameTable {
  87. get { return nameTable; }
  88. }
  89. public override XmlNodeType NodeType {
  90. get { return XmlNodeType.Document; }
  91. }
  92. public override XmlDocument OwnerDocument {
  93. get { return null; }
  94. }
  95. [MonoTODO]
  96. public bool PreserveWhitespace {
  97. get { throw new NotImplementedException(); }
  98. set { throw new NotImplementedException(); }
  99. }
  100. [MonoTODO]
  101. public XmlResolver XmlResolver {
  102. set { throw new NotImplementedException(); }
  103. }
  104. #endregion
  105. #region Methods
  106. [MonoTODO]
  107. public override XmlNode CloneNode (bool deep)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. [MonoTODO]
  112. public XmlAttribute CreateAttribute (string name)
  113. {
  114. int indexOfColon = name.IndexOf (':');
  115. if (indexOfColon == -1)
  116. return CreateAttribute (String.Empty, name, String.Empty);
  117. string prefix = name.Substring (0, indexOfColon);
  118. string localName = name.Substring (indexOfColon + 1);
  119. return CreateAttribute (prefix, localName, String.Empty);
  120. }
  121. [MonoTODO]
  122. public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
  123. {
  124. int indexOfColon = qualifiedName.IndexOf (':');
  125. if (indexOfColon == -1)
  126. return CreateAttribute (String.Empty, qualifiedName, String.Empty);
  127. string prefix = qualifiedName.Substring (0, indexOfColon);
  128. string localName = qualifiedName.Substring (indexOfColon + 1);
  129. return CreateAttribute (prefix, localName, String.Empty);
  130. }
  131. public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
  132. {
  133. return new XmlAttribute (prefix, localName, namespaceURI, this);
  134. }
  135. public virtual XmlCDataSection CreateCDataSection (string data)
  136. {
  137. return new XmlCDataSection (data, this);
  138. }
  139. public virtual XmlComment CreateComment (string data)
  140. {
  141. return new XmlComment(data, this);
  142. }
  143. [MonoTODO]
  144. protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
  145. {
  146. throw new NotImplementedException ();
  147. }
  148. [MonoTODO]
  149. public virtual XmlDocumentFragment CreateDocumentFragment ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. [MonoTODO]
  154. public virtual XmlDocumentType CreateDocumentType (
  155. string name,
  156. string publicId,
  157. string systemId,
  158. string internalSubset)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. public XmlElement CreateElement (string name)
  163. {
  164. int indexOfColon = name.IndexOf (':');
  165. if (indexOfColon == -1)
  166. return CreateElement (String.Empty, name, String.Empty);
  167. string prefix = name.Substring (0, indexOfColon);
  168. string localName = name.Substring (indexOfColon + 1);
  169. return CreateElement (prefix, localName, String.Empty);
  170. }
  171. [MonoTODO]
  172. public XmlElement CreateElement (
  173. string qualifiedName,
  174. string namespaceURI)
  175. {
  176. int indexOfColon = qualifiedName.IndexOf (':');
  177. if (indexOfColon == -1)
  178. return CreateElement (String.Empty, qualifiedName, namespaceURI);
  179. string prefix = qualifiedName.Substring (0, indexOfColon);
  180. string localName = qualifiedName.Substring (indexOfColon + 1);
  181. return CreateElement (prefix, localName, namespaceURI);
  182. }
  183. public virtual XmlElement CreateElement (
  184. string prefix,
  185. string localName,
  186. string namespaceURI)
  187. {
  188. return new XmlElement (prefix, localName, namespaceURI, this);
  189. }
  190. [MonoTODO]
  191. public virtual XmlEntityReference CreateEntityReference (string name)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. [MonoTODO]
  201. public virtual XmlNode CreateNode (
  202. string nodeTypeString,
  203. string name,
  204. string namespaceURI)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. [MonoTODO]
  209. public virtual XmlNode CreateNode (
  210. XmlNodeType type,
  211. string name,
  212. string namespaceURI)
  213. {
  214. throw new NotImplementedException ();
  215. }
  216. [MonoTODO]
  217. public virtual XmlNode CreateNode (
  218. XmlNodeType type,
  219. string prefix,
  220. string name,
  221. string namespaceURI)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. public virtual XmlProcessingInstruction CreateProcessingInstruction (
  226. string target,
  227. string data)
  228. {
  229. return new XmlProcessingInstruction (target, data, this);
  230. }
  231. [MonoTODO]
  232. public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. public virtual XmlText CreateTextNode (string text)
  237. {
  238. return new XmlText (text, this);
  239. }
  240. [MonoTODO]
  241. public virtual XmlWhitespace CreateWhitespace (string text)
  242. {
  243. throw new NotImplementedException ();
  244. }
  245. [MonoTODO]
  246. public virtual XmlDeclaration CreateXmlDeclaration (
  247. string version,
  248. string encoding,
  249. string standalone)
  250. {
  251. throw new NotImplementedException();
  252. }
  253. [MonoTODO]
  254. public virtual XmlElement GetElementById (string elementId)
  255. {
  256. throw new NotImplementedException ();
  257. }
  258. [MonoTODO]
  259. public virtual XmlNodeList GetElementsByTagName (string name)
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. [MonoTODO]
  264. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  265. {
  266. throw new NotImplementedException();
  267. }
  268. [MonoTODO]
  269. public virtual XmlNode ImportNode (XmlNode node, bool deep)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. [MonoTODO]
  274. public virtual void Load (Stream inStream)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. public virtual void Load (string filename)
  279. {
  280. XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
  281. Load (xmlReader);
  282. }
  283. [MonoTODO]
  284. public virtual void Load (TextReader txtReader)
  285. {
  286. throw new NotImplementedException ();
  287. }
  288. public virtual void Load (XmlReader xmlReader)
  289. {
  290. // Reset our document
  291. // For now this just means removing all our children but later this
  292. // may turn out o need to call a private method that resets other things
  293. // like properties we have, etc.
  294. RemoveAll ();
  295. XmlNode currentNode = this;
  296. XmlNode newNode;
  297. while (xmlReader.Read ())
  298. {
  299. switch (xmlReader.NodeType) {
  300. case XmlNodeType.CDATA:
  301. newNode = CreateCDataSection(xmlReader.Value);
  302. currentNode.AppendChild (newNode);
  303. break;
  304. case XmlNodeType.Comment:
  305. newNode = CreateComment (xmlReader.Value);
  306. currentNode.AppendChild (newNode);
  307. break;
  308. case XmlNodeType.Element:
  309. XmlElement element = CreateElement (xmlReader.Prefix, 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. xmlReader.MoveToElement ();
  315. // if this element isn't empty, push it onto our "stack".
  316. if (!xmlReader.IsEmptyElement)
  317. currentNode = element;
  318. break;
  319. case XmlNodeType.EndElement:
  320. currentNode = currentNode.ParentNode;
  321. break;
  322. case XmlNodeType.ProcessingInstruction:
  323. newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
  324. currentNode.AppendChild (newNode);
  325. break;
  326. case XmlNodeType.Text:
  327. newNode = CreateTextNode (xmlReader.Value);
  328. currentNode.AppendChild (newNode);
  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. internal void onNodeChanged (XmlNode node, XmlNode Parent)
  339. {
  340. if (NodeChanged != null)
  341. NodeInserted (node, new XmlNodeChangedEventArgs
  342. (XmlNodeChangedAction.Change,
  343. node, Parent, Parent));
  344. }
  345. internal void onNodeChanging(XmlNode node, XmlNode Parent)
  346. {
  347. if (NodeInserting != null)
  348. NodeChanging (node, new XmlNodeChangedEventArgs
  349. (XmlNodeChangedAction.Change,
  350. node, Parent, Parent));
  351. }
  352. internal void onNodeInserted (XmlNode node, XmlNode newParent)
  353. {
  354. if (NodeInserted != null)
  355. NodeInserted (node, new XmlNodeChangedEventArgs
  356. (XmlNodeChangedAction.Insert,
  357. node, null, newParent));
  358. }
  359. internal void onNodeInserting (XmlNode node, XmlNode newParent)
  360. {
  361. if (NodeInserting != null)
  362. NodeInserting (node, new XmlNodeChangedEventArgs
  363. (XmlNodeChangedAction.Insert,
  364. node, null, newParent));
  365. }
  366. internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
  367. {
  368. if (NodeRemoved != null)
  369. NodeRemoved (node, new XmlNodeChangedEventArgs
  370. (XmlNodeChangedAction.Remove,
  371. node, oldParent, null));
  372. }
  373. internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
  374. {
  375. if (NodeRemoving != null)
  376. NodeRemoving (node, new XmlNodeChangedEventArgs
  377. (XmlNodeChangedAction.Remove,
  378. node, oldParent, null));
  379. }
  380. [MonoTODO]
  381. public virtual XmlNode ReadNode(XmlReader reader)
  382. {
  383. throw new NotImplementedException ();
  384. }
  385. [MonoTODO]
  386. public virtual void Save(Stream outStream)
  387. {
  388. throw new NotImplementedException ();
  389. }
  390. [MonoTODO]
  391. public virtual void Save (string filename)
  392. {
  393. throw new NotImplementedException ();
  394. }
  395. [MonoTODO]
  396. public virtual void Save (TextWriter writer)
  397. {
  398. throw new NotImplementedException ();
  399. }
  400. [MonoTODO]
  401. public virtual void Save (XmlWriter writer)
  402. {
  403. throw new NotImplementedException ();
  404. }
  405. public override void WriteContentTo (XmlWriter w)
  406. {
  407. foreach(XmlNode childNode in ChildNodes)
  408. childNode.WriteTo(w);
  409. }
  410. public override void WriteTo (XmlWriter w)
  411. {
  412. WriteContentTo(w);
  413. }
  414. #endregion
  415. }
  416. }