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 ();
206 public virtual XmlDocumentType CreateDocumentType (
210 string internalSubset)
212 throw new NotImplementedException ();
215 public XmlElement CreateElement (string name)
217 int indexOfColon = name.IndexOf (':');
219 if (indexOfColon == -1)
220 return CreateElement (String.Empty, name, String.Empty);
222 string prefix = name.Substring (0, indexOfColon);
223 string localName = name.Substring (indexOfColon + 1);
225 return CreateElement (prefix, localName, String.Empty);
229 public XmlElement CreateElement (
230 string qualifiedName,
233 int indexOfColon = qualifiedName.IndexOf (':');
235 if (indexOfColon == -1)
236 return CreateElement (String.Empty, qualifiedName, namespaceURI);
238 string prefix = qualifiedName.Substring (0, indexOfColon);
239 string localName = qualifiedName.Substring (indexOfColon + 1);
241 return CreateElement (prefix, localName, namespaceURI);
244 public virtual XmlElement CreateElement (
249 return new XmlElement (prefix, localName, namespaceURI, this);
253 public virtual XmlEntityReference CreateEntityReference (string name)
255 throw new NotImplementedException ();
259 protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
261 throw new NotImplementedException ();
265 public virtual XmlNode CreateNode (
266 string nodeTypeString,
270 throw new NotImplementedException ();
274 public virtual XmlNode CreateNode (
279 throw new NotImplementedException ();
283 public virtual XmlNode CreateNode (
289 throw new NotImplementedException ();
292 public virtual XmlProcessingInstruction CreateProcessingInstruction (
296 return new XmlProcessingInstruction (target, data, this);
299 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
301 foreach (char c in text.ToCharArray ())
302 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
303 throw new ArgumentException ("Invalid whitespace characters.");
305 return new XmlSignificantWhitespace (text, this);
308 public virtual XmlText CreateTextNode (string text)
310 return new XmlText (text, this);
313 public virtual XmlWhitespace CreateWhitespace (string text)
315 foreach (char c in text.ToCharArray ())
316 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
317 throw new ArgumentException ("Invalid whitespace characters.");
319 return new XmlWhitespace (text, this);
323 public virtual XmlDeclaration CreateXmlDeclaration (
328 throw new NotImplementedException();
332 public virtual XmlElement GetElementById (string elementId)
334 throw new NotImplementedException ();
338 public virtual XmlNodeList GetElementsByTagName (string name)
340 throw new NotImplementedException ();
344 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
346 throw new NotImplementedException();
350 public virtual XmlNode ImportNode (XmlNode node, bool deep)
352 throw new NotImplementedException ();
356 public virtual void Load (Stream inStream)
358 throw new NotImplementedException ();
361 public virtual void Load (string filename)
363 XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
368 public virtual void Load (TextReader txtReader)
370 throw new NotImplementedException ();
373 public virtual void Load (XmlReader xmlReader)
375 // Reset our document
376 // For now this just means removing all our children but later this
377 // may turn out o need to call a private method that resets other things
378 // like properties we have, etc.
381 XmlNode currentNode = this;
384 while (xmlReader.Read ())
386 switch (xmlReader.NodeType) {
388 case XmlNodeType.CDATA:
389 newNode = CreateCDataSection(xmlReader.Value);
390 currentNode.AppendChild (newNode);
393 case XmlNodeType.Comment:
394 newNode = CreateComment (xmlReader.Value);
395 currentNode.AppendChild (newNode);
398 case XmlNodeType.Element:
399 XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
400 currentNode.AppendChild (element);
402 // set the element's attributes.
403 while (xmlReader.MoveToNextAttribute ())
404 element.SetAttribute (xmlReader.Name, xmlReader.Value);
406 xmlReader.MoveToElement ();
408 // if this element isn't empty, push it onto our "stack".
409 if (!xmlReader.IsEmptyElement)
410 currentNode = element;
414 case XmlNodeType.EndElement:
415 currentNode = currentNode.ParentNode;
418 case XmlNodeType.ProcessingInstruction:
419 newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
420 currentNode.AppendChild (newNode);
423 case XmlNodeType.Text:
424 newNode = CreateTextNode (xmlReader.Value);
425 currentNode.AppendChild (newNode);
431 public virtual void LoadXml (string xml)
433 XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
437 internal void onNodeChanged (XmlNode node, XmlNode Parent)
439 if (NodeChanged != null)
440 NodeInserted (node, new XmlNodeChangedEventArgs
441 (XmlNodeChangedAction.Change,
442 node, Parent, Parent));
445 internal void onNodeChanging(XmlNode node, XmlNode Parent)
447 if (NodeInserting != null)
448 NodeChanging (node, new XmlNodeChangedEventArgs
449 (XmlNodeChangedAction.Change,
450 node, Parent, Parent));
453 internal void onNodeInserted (XmlNode node, XmlNode newParent)
455 if (NodeInserted != null)
456 NodeInserted (node, new XmlNodeChangedEventArgs
457 (XmlNodeChangedAction.Insert,
458 node, null, newParent));
461 internal void onNodeInserting (XmlNode node, XmlNode newParent)
463 if (NodeInserting != null)
464 NodeInserting (node, new XmlNodeChangedEventArgs
465 (XmlNodeChangedAction.Insert,
466 node, null, newParent));
469 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
471 if (NodeRemoved != null)
472 NodeRemoved (node, new XmlNodeChangedEventArgs
473 (XmlNodeChangedAction.Remove,
474 node, oldParent, null));
477 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
479 if (NodeRemoving != null)
480 NodeRemoving (node, new XmlNodeChangedEventArgs
481 (XmlNodeChangedAction.Remove,
482 node, oldParent, null));
486 public virtual XmlNode ReadNode(XmlReader reader)
488 throw new NotImplementedException ();
492 public virtual void Save(Stream outStream)
494 throw new NotImplementedException ();
498 public virtual void Save (string filename)
500 throw new NotImplementedException ();
504 public virtual void Save (TextWriter writer)
506 throw new NotImplementedException ();
510 public virtual void Save (XmlWriter writer)
512 throw new NotImplementedException ();
515 public override void WriteContentTo (XmlWriter w)
517 foreach(XmlNode childNode in ChildNodes)
518 childNode.WriteTo(w);
521 public override void WriteTo (XmlWriter w)