2 // System.Xml.XmlDocument
5 // Daniel Weber (daniel-weber@austin.rr.com)
7 // (C) 2001 Daniel Weber
11 using System.Xml.XPath;
12 using System.Diagnostics;
16 public class XmlDocument : XmlNode
20 XmlLinkedNode lastLinkedChild;
21 XmlNameTable nameTable;
27 public XmlDocument () : base (null) { }
30 protected internal XmlDocument (XmlImplementation imp) : base (null)
32 throw new NotImplementedException ();
35 public XmlDocument (NameTable nt) : base (null)
44 public event XmlNodeChangedEventHandler NodeChanged;
46 public event XmlNodeChangedEventHandler NodeChanging;
48 public event XmlNodeChangedEventHandler NodeInserted;
50 public event XmlNodeChangedEventHandler NodeInserting;
52 public event XmlNodeChangedEventHandler NodeRemoved;
54 public event XmlNodeChangedEventHandler NodeRemoving;
61 public override string BaseURI {
62 get { throw new NotImplementedException(); }
65 public XmlElement DocumentElement {
67 XmlNode node = FirstChild;
69 while (node != null) {
70 if (node is XmlElement)
72 node = node.NextSibling;
75 return node != null ? node as XmlElement : null;
80 public virtual XmlDocumentType DocumentType {
81 get { throw new NotImplementedException(); }
85 public XmlImplementation Implementation {
86 get { throw new NotImplementedException(); }
90 public override string InnerXml {
91 get { throw new NotImplementedException(); }
92 set { throw new NotImplementedException(); }
95 public override bool IsReadOnly {
99 internal override XmlLinkedNode LastLinkedChild {
101 return lastLinkedChild;
105 lastLinkedChild = value;
109 public override string LocalName {
110 get { return "#document"; }
113 public override string Name {
114 get { return "#document"; }
117 public XmlNameTable NameTable {
118 get { return nameTable; }
121 public override XmlNodeType NodeType {
122 get { return XmlNodeType.Document; }
125 public override XmlDocument OwnerDocument {
130 public bool PreserveWhitespace {
131 get { throw new NotImplementedException(); }
132 set { throw new NotImplementedException(); }
136 public XmlResolver XmlResolver {
137 set { throw new NotImplementedException(); }
145 public override XmlNode CloneNode (bool deep)
147 throw new NotImplementedException ();
151 public XmlAttribute CreateAttribute (string name)
153 int indexOfColon = name.IndexOf (':');
155 if (indexOfColon == -1)
156 return CreateAttribute (String.Empty, name, String.Empty);
158 string prefix = name.Substring (0, indexOfColon);
159 string localName = name.Substring (indexOfColon + 1);
161 return CreateAttribute (prefix, localName, String.Empty);
165 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
167 int indexOfColon = qualifiedName.IndexOf (':');
169 if (indexOfColon == -1)
170 return CreateAttribute (String.Empty, qualifiedName, String.Empty);
172 string prefix = qualifiedName.Substring (0, indexOfColon);
173 string localName = qualifiedName.Substring (indexOfColon + 1);
175 return CreateAttribute (prefix, localName, String.Empty);
178 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
180 return new XmlAttribute (prefix, localName, namespaceURI, this);
183 public virtual XmlCDataSection CreateCDataSection (string data)
185 return new XmlCDataSection (data, this);
188 public virtual XmlComment CreateComment (string data)
190 return new XmlComment(data, this);
194 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
196 throw new NotImplementedException ();
200 public virtual XmlDocumentFragment CreateDocumentFragment ()
202 throw new NotImplementedException ();
205 public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
206 string systemId, string internalSubset)
208 return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
211 public XmlElement CreateElement (string name)
213 int indexOfColon = name.IndexOf (':');
215 if (indexOfColon == -1)
216 return CreateElement (String.Empty, name, String.Empty);
218 string prefix = name.Substring (0, indexOfColon);
219 string localName = name.Substring (indexOfColon + 1);
221 return CreateElement (prefix, localName, String.Empty);
225 public XmlElement CreateElement (
226 string qualifiedName,
229 int indexOfColon = qualifiedName.IndexOf (':');
231 if (indexOfColon == -1)
232 return CreateElement (String.Empty, qualifiedName, namespaceURI);
234 string prefix = qualifiedName.Substring (0, indexOfColon);
235 string localName = qualifiedName.Substring (indexOfColon + 1);
237 return CreateElement (prefix, localName, namespaceURI);
240 public virtual XmlElement CreateElement (
245 return new XmlElement (prefix, localName, namespaceURI, this);
249 public virtual XmlEntityReference CreateEntityReference (string name)
251 throw new NotImplementedException ();
255 protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
257 throw new NotImplementedException ();
261 public virtual XmlNode CreateNode (
262 string nodeTypeString,
266 throw new NotImplementedException ();
270 public virtual XmlNode CreateNode (
275 throw new NotImplementedException ();
279 public virtual XmlNode CreateNode (
285 throw new NotImplementedException ();
288 public virtual XmlProcessingInstruction CreateProcessingInstruction (
292 return new XmlProcessingInstruction (target, data, this);
295 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
297 foreach (char c in text)
298 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
299 throw new ArgumentException ("Invalid whitespace characters.");
301 return new XmlSignificantWhitespace (text, this);
304 public virtual XmlText CreateTextNode (string text)
306 return new XmlText (text, this);
309 public virtual XmlWhitespace CreateWhitespace (string text)
311 foreach (char c in text)
312 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
313 throw new ArgumentException ("Invalid whitespace characters.");
315 return new XmlWhitespace (text, this);
318 public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
321 if (version != "1.0")
322 throw new ArgumentException ("version string is not correct.");
324 if ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
325 throw new ArgumentException ("standalone string is not correct.");
327 return new XmlDeclaration (version, encoding, standalone, this);
331 public virtual XmlElement GetElementById (string elementId)
333 throw new NotImplementedException ();
337 public virtual XmlNodeList GetElementsByTagName (string name)
339 throw new NotImplementedException ();
343 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
345 throw new NotImplementedException();
349 public virtual XmlNode ImportNode (XmlNode node, bool deep)
351 throw new NotImplementedException ();
355 public virtual void Load (Stream inStream)
357 throw new NotImplementedException ();
360 public virtual void Load (string filename)
362 XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
367 public virtual void Load (TextReader txtReader)
369 throw new NotImplementedException ();
372 public virtual void Load (XmlReader xmlReader)
374 // Reset our document
375 // For now this just means removing all our children but later this
376 // may turn out o need to call a private method that resets other things
377 // like properties we have, etc.
380 XmlNode currentNode = this;
383 while (xmlReader.Read ())
385 switch (xmlReader.NodeType) {
387 case XmlNodeType.CDATA:
388 newNode = CreateCDataSection(xmlReader.Value);
389 currentNode.AppendChild (newNode);
392 case XmlNodeType.Comment:
393 newNode = CreateComment (xmlReader.Value);
394 currentNode.AppendChild (newNode);
397 case XmlNodeType.Element:
398 XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
399 currentNode.AppendChild (element);
401 // set the element's attributes.
402 while (xmlReader.MoveToNextAttribute ())
403 element.SetAttribute (xmlReader.Name, xmlReader.Value);
405 xmlReader.MoveToElement ();
407 // if this element isn't empty, push it onto our "stack".
408 if (!xmlReader.IsEmptyElement)
409 currentNode = element;
413 case XmlNodeType.EndElement:
414 currentNode = currentNode.ParentNode;
417 case XmlNodeType.ProcessingInstruction:
418 newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
419 currentNode.AppendChild (newNode);
422 case XmlNodeType.Text:
423 newNode = CreateTextNode (xmlReader.Value);
424 currentNode.AppendChild (newNode);
430 public virtual void LoadXml (string xml)
432 XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
436 internal void onNodeChanged (XmlNode node, XmlNode Parent)
438 if (NodeChanged != null)
439 NodeInserted (node, new XmlNodeChangedEventArgs
440 (XmlNodeChangedAction.Change,
441 node, Parent, Parent));
444 internal void onNodeChanging(XmlNode node, XmlNode Parent)
446 if (NodeInserting != null)
447 NodeChanging (node, new XmlNodeChangedEventArgs
448 (XmlNodeChangedAction.Change,
449 node, Parent, Parent));
452 internal void onNodeInserted (XmlNode node, XmlNode newParent)
454 if (NodeInserted != null)
455 NodeInserted (node, new XmlNodeChangedEventArgs
456 (XmlNodeChangedAction.Insert,
457 node, null, newParent));
460 internal void onNodeInserting (XmlNode node, XmlNode newParent)
462 if (NodeInserting != null)
463 NodeInserting (node, new XmlNodeChangedEventArgs
464 (XmlNodeChangedAction.Insert,
465 node, null, newParent));
468 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
470 if (NodeRemoved != null)
471 NodeRemoved (node, new XmlNodeChangedEventArgs
472 (XmlNodeChangedAction.Remove,
473 node, oldParent, null));
476 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
478 if (NodeRemoving != null)
479 NodeRemoving (node, new XmlNodeChangedEventArgs
480 (XmlNodeChangedAction.Remove,
481 node, oldParent, null));
485 public virtual XmlNode ReadNode(XmlReader reader)
487 throw new NotImplementedException ();
491 public virtual void Save(Stream outStream)
493 throw new NotImplementedException ();
497 public virtual void Save (string filename)
499 throw new NotImplementedException ();
503 public virtual void Save (TextWriter writer)
505 throw new NotImplementedException ();
509 public virtual void Save (XmlWriter writer)
511 throw new NotImplementedException ();
514 public override void WriteContentTo (XmlWriter w)
516 foreach(XmlNode childNode in ChildNodes)
517 childNode.WriteTo(w);
520 public override void WriteTo (XmlWriter w)