XmlDocument.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
  154. string systemId, string internalSubset)
  155. {
  156. return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
  157. }
  158. public XmlElement CreateElement (string name)
  159. {
  160. int indexOfColon = name.IndexOf (':');
  161. if (indexOfColon == -1)
  162. return CreateElement (String.Empty, name, String.Empty);
  163. string prefix = name.Substring (0, indexOfColon);
  164. string localName = name.Substring (indexOfColon + 1);
  165. return CreateElement (prefix, localName, String.Empty);
  166. }
  167. [MonoTODO]
  168. public XmlElement CreateElement (
  169. string qualifiedName,
  170. string namespaceURI)
  171. {
  172. int indexOfColon = qualifiedName.IndexOf (':');
  173. if (indexOfColon == -1)
  174. return CreateElement (String.Empty, qualifiedName, namespaceURI);
  175. string prefix = qualifiedName.Substring (0, indexOfColon);
  176. string localName = qualifiedName.Substring (indexOfColon + 1);
  177. return CreateElement (prefix, localName, namespaceURI);
  178. }
  179. public virtual XmlElement CreateElement (
  180. string prefix,
  181. string localName,
  182. string namespaceURI)
  183. {
  184. return new XmlElement (prefix, localName, namespaceURI, this);
  185. }
  186. [MonoTODO]
  187. public virtual XmlEntityReference CreateEntityReference (string name)
  188. {
  189. throw new NotImplementedException ();
  190. }
  191. [MonoTODO]
  192. protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO]
  197. public virtual XmlNode CreateNode (
  198. string nodeTypeString,
  199. string name,
  200. string namespaceURI)
  201. {
  202. throw new NotImplementedException ();
  203. }
  204. [MonoTODO]
  205. public virtual XmlNode CreateNode (
  206. XmlNodeType type,
  207. string name,
  208. string namespaceURI)
  209. {
  210. throw new NotImplementedException ();
  211. }
  212. [MonoTODO]
  213. public virtual XmlNode CreateNode (
  214. XmlNodeType type,
  215. string prefix,
  216. string name,
  217. string namespaceURI)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. public virtual XmlProcessingInstruction CreateProcessingInstruction (
  222. string target,
  223. string data)
  224. {
  225. return new XmlProcessingInstruction (target, data, this);
  226. }
  227. public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
  228. {
  229. foreach (char c in text)
  230. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  231. throw new ArgumentException ("Invalid whitespace characters.");
  232. return new XmlSignificantWhitespace (text, this);
  233. }
  234. public virtual XmlText CreateTextNode (string text)
  235. {
  236. return new XmlText (text, this);
  237. }
  238. public virtual XmlWhitespace CreateWhitespace (string text)
  239. {
  240. foreach (char c in text)
  241. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  242. throw new ArgumentException ("Invalid whitespace characters.");
  243. return new XmlWhitespace (text, this);
  244. }
  245. public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
  246. string standalone)
  247. {
  248. if (version != "1.0")
  249. throw new ArgumentException ("version string is not correct.");
  250. if ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
  251. throw new ArgumentException ("standalone string is not correct.");
  252. return new XmlDeclaration (version, encoding, standalone, this);
  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. XmlNode newNode;
  298. while (xmlReader.Read ())
  299. {
  300. switch (xmlReader.NodeType) {
  301. case XmlNodeType.CDATA:
  302. newNode = CreateCDataSection(xmlReader.Value);
  303. currentNode.AppendChild (newNode);
  304. break;
  305. case XmlNodeType.Comment:
  306. newNode = CreateComment (xmlReader.Value);
  307. currentNode.AppendChild (newNode);
  308. break;
  309. case XmlNodeType.Element:
  310. XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
  311. currentNode.AppendChild (element);
  312. // set the element's attributes.
  313. while (xmlReader.MoveToNextAttribute ())
  314. element.SetAttribute (xmlReader.Name, xmlReader.Value);
  315. xmlReader.MoveToElement ();
  316. // if this element isn't empty, push it onto our "stack".
  317. if (!xmlReader.IsEmptyElement)
  318. currentNode = element;
  319. break;
  320. case XmlNodeType.EndElement:
  321. currentNode = currentNode.ParentNode;
  322. break;
  323. case XmlNodeType.ProcessingInstruction:
  324. newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
  325. currentNode.AppendChild (newNode);
  326. break;
  327. case XmlNodeType.Text:
  328. newNode = CreateTextNode (xmlReader.Value);
  329. currentNode.AppendChild (newNode);
  330. break;
  331. }
  332. }
  333. }
  334. public virtual void LoadXml (string xml)
  335. {
  336. XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
  337. Load (xmlReader);
  338. }
  339. internal void onNodeChanged (XmlNode node, XmlNode Parent)
  340. {
  341. if (NodeChanged != null)
  342. NodeInserted (node, new XmlNodeChangedEventArgs
  343. (XmlNodeChangedAction.Change,
  344. node, Parent, Parent));
  345. }
  346. internal void onNodeChanging(XmlNode node, XmlNode Parent)
  347. {
  348. if (NodeInserting != null)
  349. NodeChanging (node, new XmlNodeChangedEventArgs
  350. (XmlNodeChangedAction.Change,
  351. node, Parent, Parent));
  352. }
  353. internal void onNodeInserted (XmlNode node, XmlNode newParent)
  354. {
  355. if (NodeInserted != null)
  356. NodeInserted (node, new XmlNodeChangedEventArgs
  357. (XmlNodeChangedAction.Insert,
  358. node, null, newParent));
  359. }
  360. internal void onNodeInserting (XmlNode node, XmlNode newParent)
  361. {
  362. if (NodeInserting != null)
  363. NodeInserting (node, new XmlNodeChangedEventArgs
  364. (XmlNodeChangedAction.Insert,
  365. node, null, newParent));
  366. }
  367. internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
  368. {
  369. if (NodeRemoved != null)
  370. NodeRemoved (node, new XmlNodeChangedEventArgs
  371. (XmlNodeChangedAction.Remove,
  372. node, oldParent, null));
  373. }
  374. internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
  375. {
  376. if (NodeRemoving != null)
  377. NodeRemoving (node, new XmlNodeChangedEventArgs
  378. (XmlNodeChangedAction.Remove,
  379. node, oldParent, null));
  380. }
  381. [MonoTODO]
  382. public virtual XmlNode ReadNode(XmlReader reader)
  383. {
  384. throw new NotImplementedException ();
  385. }
  386. [MonoTODO]
  387. public virtual void Save(Stream outStream)
  388. {
  389. throw new NotImplementedException ();
  390. }
  391. [MonoTODO]
  392. public virtual void Save (string filename)
  393. {
  394. throw new NotImplementedException ();
  395. }
  396. [MonoTODO]
  397. public virtual void Save (TextWriter writer)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. [MonoTODO]
  402. public virtual void Save (XmlWriter writer)
  403. {
  404. throw new NotImplementedException ();
  405. }
  406. public override void WriteContentTo (XmlWriter w)
  407. {
  408. foreach(XmlNode childNode in ChildNodes)
  409. childNode.WriteTo(w);
  410. }
  411. public override void WriteTo (XmlWriter w)
  412. {
  413. WriteContentTo(w);
  414. }
  415. #endregion
  416. }
  417. }