XmlDocument.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. //
  2. // System.Xml.XmlDocument
  3. //
  4. // Authors:
  5. // Daniel Weber ([email protected])
  6. // Kral Ferch <[email protected]>
  7. // Jason Diamond <[email protected]>
  8. // Miguel de Icaza ([email protected])
  9. //
  10. // (C) 2001 Daniel Weber
  11. // (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza
  12. //
  13. using System;
  14. using System.IO;
  15. using System.Text;
  16. using System.Xml.XPath;
  17. using System.Diagnostics;
  18. namespace System.Xml
  19. {
  20. public class XmlDocument : XmlNode
  21. {
  22. #region Fields
  23. XmlLinkedNode lastLinkedChild;
  24. XmlNameTable nameTable;
  25. #endregion
  26. #region Constructors
  27. public XmlDocument () : base (null) { }
  28. [MonoTODO]
  29. protected internal XmlDocument (XmlImplementation imp) : base (null)
  30. {
  31. throw new NotImplementedException ();
  32. }
  33. public XmlDocument (XmlNameTable nt) : base (null)
  34. {
  35. nameTable = nt;
  36. }
  37. #endregion
  38. #region Events
  39. public event XmlNodeChangedEventHandler NodeChanged;
  40. public event XmlNodeChangedEventHandler NodeChanging;
  41. public event XmlNodeChangedEventHandler NodeInserted;
  42. public event XmlNodeChangedEventHandler NodeInserting;
  43. public event XmlNodeChangedEventHandler NodeRemoved;
  44. public event XmlNodeChangedEventHandler NodeRemoving;
  45. #endregion
  46. #region Properties
  47. [MonoTODO]
  48. public override string BaseURI {
  49. get { throw new NotImplementedException(); }
  50. }
  51. public XmlElement DocumentElement {
  52. get {
  53. XmlNode node = FirstChild;
  54. while (node != null) {
  55. if (node is XmlElement)
  56. break;
  57. node = node.NextSibling;
  58. }
  59. return node != null ? node as XmlElement : null;
  60. }
  61. }
  62. [MonoTODO]
  63. public virtual XmlDocumentType DocumentType {
  64. get { throw new NotImplementedException(); }
  65. }
  66. [MonoTODO]
  67. public XmlImplementation Implementation {
  68. get { throw new NotImplementedException(); }
  69. }
  70. [MonoTODO ("Setter.")]
  71. public override string InnerXml {
  72. get {
  73. // Not sure why this is an override. Passing through for now.
  74. return base.InnerXml;
  75. }
  76. set { throw new NotImplementedException(); }
  77. }
  78. public override bool IsReadOnly {
  79. get { return false; }
  80. }
  81. internal override XmlLinkedNode LastLinkedChild {
  82. get {
  83. return lastLinkedChild;
  84. }
  85. set {
  86. lastLinkedChild = value;
  87. }
  88. }
  89. public override string LocalName {
  90. get { return "#document"; }
  91. }
  92. public override string Name {
  93. get { return "#document"; }
  94. }
  95. public XmlNameTable NameTable {
  96. get { return nameTable; }
  97. }
  98. public override XmlNodeType NodeType {
  99. get { return XmlNodeType.Document; }
  100. }
  101. public override XmlDocument OwnerDocument {
  102. get { return null; }
  103. }
  104. [MonoTODO]
  105. public bool PreserveWhitespace {
  106. get { throw new NotImplementedException(); }
  107. set { throw new NotImplementedException(); }
  108. }
  109. [MonoTODO]
  110. public virtual XmlResolver XmlResolver {
  111. set { throw new NotImplementedException(); }
  112. }
  113. #endregion
  114. #region Methods
  115. [MonoTODO]
  116. public override XmlNode CloneNode (bool deep)
  117. {
  118. throw new NotImplementedException ();
  119. }
  120. public XmlAttribute CreateAttribute (string name)
  121. {
  122. return CreateAttribute (name, String.Empty);
  123. }
  124. public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
  125. {
  126. string prefix;
  127. string localName;
  128. ParseName (qualifiedName, out prefix, out localName);
  129. return CreateAttribute (prefix, localName, namespaceURI);
  130. }
  131. public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
  132. {
  133. if ((localName == null) || (localName == String.Empty))
  134. throw new ArgumentException ("The attribute local name cannot be empty.");
  135. return new XmlAttribute (prefix, localName, namespaceURI, this);
  136. }
  137. public virtual XmlCDataSection CreateCDataSection (string data)
  138. {
  139. return new XmlCDataSection (data, this);
  140. }
  141. public virtual XmlComment CreateComment (string data)
  142. {
  143. return new XmlComment(data, this);
  144. }
  145. [MonoTODO]
  146. protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. [MonoTODO]
  151. public virtual XmlDocumentFragment CreateDocumentFragment ()
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
  156. string systemId, string internalSubset)
  157. {
  158. return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
  159. }
  160. public XmlElement CreateElement (string name)
  161. {
  162. return CreateElement (name, String.Empty);
  163. }
  164. public XmlElement CreateElement (
  165. string qualifiedName,
  166. string namespaceURI)
  167. {
  168. string prefix;
  169. string localName;
  170. ParseName (qualifiedName, out prefix, out localName);
  171. return CreateElement (prefix, localName, namespaceURI);
  172. }
  173. public virtual XmlElement CreateElement (
  174. string prefix,
  175. string localName,
  176. string namespaceURI)
  177. {
  178. if ((localName == null) || (localName == String.Empty))
  179. throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
  180. return new XmlElement (prefix, localName, namespaceURI, this);
  181. }
  182. [MonoTODO]
  183. public virtual XmlEntityReference CreateEntityReference (string name)
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. [MonoTODO]
  188. protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
  189. {
  190. throw new NotImplementedException ();
  191. }
  192. public virtual XmlNode CreateNode (
  193. string nodeTypeString,
  194. string name,
  195. string namespaceURI)
  196. {
  197. return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
  198. }
  199. public virtual XmlNode CreateNode (
  200. XmlNodeType type,
  201. string name,
  202. string namespaceURI)
  203. {
  204. string prefix = null;
  205. string localName = name;
  206. if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
  207. ParseName (name, out prefix, out localName);
  208. return CreateNode (type, prefix, localName, namespaceURI);
  209. }
  210. public virtual XmlNode CreateNode (
  211. XmlNodeType type,
  212. string prefix,
  213. string name,
  214. string namespaceURI)
  215. {
  216. switch (type) {
  217. case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
  218. case XmlNodeType.CDATA: return CreateCDataSection (null);
  219. case XmlNodeType.Comment: return CreateComment (null);
  220. case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
  221. case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
  222. case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
  223. case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
  224. case XmlNodeType.EntityReference: return CreateEntityReference (null);
  225. case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
  226. case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
  227. case XmlNodeType.Text: return CreateTextNode (null);
  228. case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
  229. case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
  230. default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
  231. "Specified argument was out of the range of valid values", type.ToString ()));
  232. }
  233. }
  234. public virtual XmlProcessingInstruction CreateProcessingInstruction (
  235. string target,
  236. string data)
  237. {
  238. return new XmlProcessingInstruction (target, data, this);
  239. }
  240. public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
  241. {
  242. foreach (char c in text)
  243. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  244. throw new ArgumentException ("Invalid whitespace characters.");
  245. return new XmlSignificantWhitespace (text, this);
  246. }
  247. public virtual XmlText CreateTextNode (string text)
  248. {
  249. return new XmlText (text, this);
  250. }
  251. public virtual XmlWhitespace CreateWhitespace (string text)
  252. {
  253. foreach (char c in text)
  254. if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
  255. throw new ArgumentException ("Invalid whitespace characters.");
  256. return new XmlWhitespace (text, this);
  257. }
  258. public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
  259. string standalone)
  260. {
  261. if (version != "1.0")
  262. throw new ArgumentException ("version string is not correct.");
  263. if ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
  264. throw new ArgumentException ("standalone string is not correct.");
  265. return new XmlDeclaration (version, encoding, standalone, this);
  266. }
  267. [MonoTODO]
  268. public virtual XmlElement GetElementById (string elementId)
  269. {
  270. throw new NotImplementedException ();
  271. }
  272. [MonoTODO]
  273. public virtual XmlNodeList GetElementsByTagName (string name)
  274. {
  275. throw new NotImplementedException ();
  276. }
  277. [MonoTODO]
  278. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  279. {
  280. throw new NotImplementedException();
  281. }
  282. private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
  283. {
  284. switch (nodeTypeString) {
  285. case "attribute": return XmlNodeType.Attribute;
  286. case "cdatasection": return XmlNodeType.CDATA;
  287. case "comment": return XmlNodeType.Comment;
  288. case "document": return XmlNodeType.Document;
  289. case "documentfragment": return XmlNodeType.DocumentFragment;
  290. case "documenttype": return XmlNodeType.DocumentType;
  291. case "element": return XmlNodeType.Element;
  292. case "entityreference": return XmlNodeType.EntityReference;
  293. case "processinginstruction": return XmlNodeType.ProcessingInstruction;
  294. case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
  295. case "text": return XmlNodeType.Text;
  296. case "whitespace": return XmlNodeType.Whitespace;
  297. default:
  298. throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
  299. }
  300. }
  301. [MonoTODO]
  302. public virtual XmlNode ImportNode (XmlNode node, bool deep)
  303. {
  304. throw new NotImplementedException ();
  305. }
  306. [MonoTODO]
  307. public virtual void Load (Stream inStream)
  308. {
  309. throw new NotImplementedException ();
  310. }
  311. public virtual void Load (string filename)
  312. {
  313. XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
  314. Load (xmlReader);
  315. }
  316. [MonoTODO]
  317. public virtual void Load (TextReader txtReader)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. public virtual void Load (XmlReader xmlReader)
  322. {
  323. // Reset our document
  324. // For now this just means removing all our children but later this
  325. // may turn out o need to call a private method that resets other things
  326. // like properties we have, etc.
  327. RemoveAll ();
  328. XmlNode currentNode = this;
  329. XmlNode newNode;
  330. while (xmlReader.Read ())
  331. {
  332. switch (xmlReader.NodeType) {
  333. case XmlNodeType.CDATA:
  334. newNode = CreateCDataSection(xmlReader.Value);
  335. currentNode.AppendChild (newNode);
  336. break;
  337. case XmlNodeType.Comment:
  338. newNode = CreateComment (xmlReader.Value);
  339. currentNode.AppendChild (newNode);
  340. break;
  341. case XmlNodeType.Element:
  342. XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
  343. currentNode.AppendChild (element);
  344. // set the element's attributes.
  345. while (xmlReader.MoveToNextAttribute ())
  346. element.SetAttribute (xmlReader.Name, xmlReader.Value);
  347. xmlReader.MoveToElement ();
  348. // if this element isn't empty, push it onto our "stack".
  349. if (!xmlReader.IsEmptyElement)
  350. currentNode = element;
  351. break;
  352. case XmlNodeType.EndElement:
  353. currentNode = currentNode.ParentNode;
  354. break;
  355. case XmlNodeType.ProcessingInstruction:
  356. newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
  357. currentNode.AppendChild (newNode);
  358. break;
  359. case XmlNodeType.Text:
  360. newNode = CreateTextNode (xmlReader.Value);
  361. currentNode.AppendChild (newNode);
  362. break;
  363. }
  364. }
  365. }
  366. public virtual void LoadXml (string xml)
  367. {
  368. XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
  369. Load (xmlReader);
  370. }
  371. internal void onNodeChanged (XmlNode node, XmlNode Parent)
  372. {
  373. if (NodeChanged != null)
  374. NodeInserted (node, new XmlNodeChangedEventArgs
  375. (XmlNodeChangedAction.Change,
  376. node, Parent, Parent));
  377. }
  378. internal void onNodeChanging(XmlNode node, XmlNode Parent)
  379. {
  380. if (NodeInserting != null)
  381. NodeChanging (node, new XmlNodeChangedEventArgs
  382. (XmlNodeChangedAction.Change,
  383. node, Parent, Parent));
  384. }
  385. internal void onNodeInserted (XmlNode node, XmlNode newParent)
  386. {
  387. if (NodeInserted != null)
  388. NodeInserted (node, new XmlNodeChangedEventArgs
  389. (XmlNodeChangedAction.Insert,
  390. node, null, newParent));
  391. }
  392. internal void onNodeInserting (XmlNode node, XmlNode newParent)
  393. {
  394. if (NodeInserting != null)
  395. NodeInserting (node, new XmlNodeChangedEventArgs
  396. (XmlNodeChangedAction.Insert,
  397. node, null, newParent));
  398. }
  399. internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
  400. {
  401. if (NodeRemoved != null)
  402. NodeRemoved (node, new XmlNodeChangedEventArgs
  403. (XmlNodeChangedAction.Remove,
  404. node, oldParent, null));
  405. }
  406. internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
  407. {
  408. if (NodeRemoving != null)
  409. NodeRemoving (node, new XmlNodeChangedEventArgs
  410. (XmlNodeChangedAction.Remove,
  411. node, oldParent, null));
  412. }
  413. private void ParseName (string name, out string prefix, out string localName)
  414. {
  415. int indexOfColon = name.IndexOf (':');
  416. if (indexOfColon != -1) {
  417. prefix = name.Substring (0, indexOfColon);
  418. localName = name.Substring (indexOfColon + 1);
  419. } else {
  420. prefix = "";
  421. localName = name;
  422. }
  423. }
  424. [MonoTODO]
  425. public virtual XmlNode ReadNode(XmlReader reader)
  426. {
  427. throw new NotImplementedException ();
  428. }
  429. [MonoTODO ("Verify what encoding is used by default; Should use PreserveWhiteSpace")]
  430. public virtual void Save(Stream outStream)
  431. {
  432. XmlTextWriter xmlWriter = new XmlTextWriter (outStream, Encoding.UTF8);
  433. WriteContentTo (xmlWriter);
  434. xmlWriter.Close ();
  435. }
  436. [MonoTODO ("Verify what encoding is used by default; Should use PreseveWhiteSpace")]
  437. public virtual void Save (string filename)
  438. {
  439. XmlTextWriter xmlWriter = new XmlTextWriter (filename, Encoding.UTF8);
  440. WriteContentTo (xmlWriter);
  441. xmlWriter.Close ();
  442. }
  443. [MonoTODO]
  444. public virtual void Save (TextWriter writer)
  445. {
  446. XmlTextWriter xmlWriter = new XmlTextWriter (writer);
  447. WriteContentTo (xmlWriter);
  448. xmlWriter.Flush ();
  449. }
  450. [MonoTODO ("Should preserve white space if PreserveWhisspace is set")]
  451. public virtual void Save (XmlWriter xmlWriter)
  452. {
  453. //
  454. // This should preserve white space if PreserveWhiteSpace is true
  455. //
  456. WriteContentTo (xmlWriter);
  457. xmlWriter.Flush ();
  458. }
  459. public override void WriteContentTo (XmlWriter w)
  460. {
  461. foreach(XmlNode childNode in ChildNodes)
  462. childNode.WriteTo(w);
  463. }
  464. public override void WriteTo (XmlWriter w)
  465. {
  466. WriteContentTo(w);
  467. }
  468. #endregion
  469. }
  470. }