2 // System.Xml.XmlDocument
5 // Daniel Weber (daniel-weber@austin.rr.com)
7 // (C) 2001 Daniel Weber
11 using System.Xml.XPath;
15 public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
17 public class XmlDocument : XmlNode
21 private XmlLinkedNode lastLinkedChild;
27 public XmlDocument () : base (null) { }
30 protected internal XmlDocument (XmlImplementation imp) : base (null)
32 throw new NotImplementedException ();
36 public XmlDocument (NameTable nt) : base (null)
38 throw new NotImplementedException ();
45 public event XmlNodeChangedEventHandler NodeChanged;
47 public event XmlNodeChangedEventHandler NodeChanging;
49 public event XmlNodeChangedEventHandler NodeInserted;
51 public event XmlNodeChangedEventHandler NodeInserting;
53 public event XmlNodeChangedEventHandler NodeRemoved;
55 public event XmlNodeChangedEventHandler NodeRemoving;
62 public override string BaseURI {
63 get { throw new NotImplementedException(); }
66 public XmlElement DocumentElement {
68 XmlNode node = FirstChild;
70 while (node != null) {
71 if (node is XmlElement)
73 node = node.NextSibling;
76 return node != null ? node as XmlElement : null;
81 public virtual XmlDocumentType DocumentType {
82 get { throw new NotImplementedException(); }
86 public XmlImplementation Implementation {
87 get { throw new NotImplementedException(); }
91 public override string InnerXml {
92 get { throw new NotImplementedException(); }
93 set { throw new NotImplementedException(); }
96 public override bool IsReadOnly {
100 internal override XmlLinkedNode LastLinkedChild {
102 return lastLinkedChild;
106 lastLinkedChild = value;
110 public override string LocalName {
111 get { return "#document"; }
114 public override string Name {
115 get { return "#document"; }
119 public XmlNameTable NameTable {
120 get { throw new NotImplementedException(); }
123 public override XmlNodeType NodeType {
124 get { return XmlNodeType.Document; }
127 public override XmlDocument OwnerDocument {
132 public bool PreserveWhitespace {
133 get { throw new NotImplementedException(); }
134 set { throw new NotImplementedException(); }
138 public XmlResolver XmlResolver {
139 set { throw new NotImplementedException(); }
147 public override XmlNode CloneNode (bool deep)
149 throw new NotImplementedException ();
153 public XmlAttribute CreateAttribute (string name)
155 int indexOfColon = name.IndexOf (':');
157 if (indexOfColon == -1)
158 return CreateAttribute (String.Empty, name, String.Empty);
160 string prefix = name.Substring (0, indexOfColon);
161 string localName = name.Substring (indexOfColon + 1);
163 return CreateAttribute (prefix, localName, String.Empty);
167 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
169 int indexOfColon = qualifiedName.IndexOf (':');
171 if (indexOfColon == -1)
172 return CreateAttribute (String.Empty, qualifiedName, String.Empty);
174 string prefix = qualifiedName.Substring (0, indexOfColon);
175 string localName = qualifiedName.Substring (indexOfColon + 1);
177 return CreateAttribute (prefix, localName, String.Empty);
180 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
182 return new XmlAttribute (prefix, localName, namespaceURI, this);
185 public virtual XmlCDataSection CreateCDataSection (string data)
187 return new XmlCDataSection (data, this);
190 public virtual XmlComment CreateComment (string data)
192 return new XmlComment(data, this);
196 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
198 throw new NotImplementedException ();
202 public virtual XmlDocumentFragment CreateDocumentFragment ()
204 throw new NotImplementedException ();
208 public virtual XmlDocumentType CreateDocumentType (
212 string internalSubset)
214 throw new NotImplementedException ();
217 public XmlElement CreateElement (string name)
219 int indexOfColon = name.IndexOf (':');
221 if (indexOfColon == -1)
222 return CreateElement (String.Empty, name, String.Empty);
224 string prefix = name.Substring (0, indexOfColon);
225 string localName = name.Substring (indexOfColon + 1);
227 return CreateElement (prefix, localName, String.Empty);
231 public XmlElement CreateElement (
232 string qualifiedName,
235 int indexOfColon = qualifiedName.IndexOf (':');
237 if (indexOfColon == -1)
238 return CreateElement (String.Empty, qualifiedName, namespaceURI);
240 string prefix = qualifiedName.Substring (0, indexOfColon);
241 string localName = qualifiedName.Substring (indexOfColon + 1);
243 return CreateElement (prefix, localName, namespaceURI);
246 public virtual XmlElement CreateElement (
251 return new XmlElement (prefix, localName, namespaceURI, this);
255 public virtual XmlEntityReference CreateEntityReference (string name)
257 throw new NotImplementedException ();
261 protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
263 throw new NotImplementedException ();
267 public virtual XmlNode CreateNode (
268 string nodeTypeString,
272 throw new NotImplementedException ();
276 public virtual XmlNode CreateNode (
281 throw new NotImplementedException ();
285 public virtual XmlNode CreateNode (
291 throw new NotImplementedException ();
294 public virtual XmlProcessingInstruction CreateProcessingInstruction (
298 return new XmlProcessingInstruction (target, data, this);
302 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
304 throw new NotImplementedException ();
307 public virtual XmlText CreateTextNode (string text)
309 return new XmlText (text, this);
313 public virtual XmlWhitespace CreateWhitespace (string text)
315 throw new NotImplementedException ();
319 public virtual XmlDeclaration CreateXmlDeclaration (
324 throw new NotImplementedException();
328 public virtual XmlElement GetElementById (string elementId)
330 throw new NotImplementedException ();
334 public virtual XmlNodeList GetElementsByTagName (string name)
336 throw new NotImplementedException ();
340 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
342 throw new NotImplementedException();
346 public virtual XmlNode ImportNode (XmlNode node, bool deep)
348 throw new NotImplementedException ();
352 public virtual void Load (Stream inStream)
354 throw new NotImplementedException ();
357 public virtual void Load (string filename)
359 XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
364 public virtual void Load (TextReader txtReader)
366 throw new NotImplementedException ();
369 public virtual void Load (XmlReader xmlReader)
371 // Reset our document
372 // For now this just means removing all our children but later this
373 // may turn out o need to call a private method that resets other things
374 // like properties we have, etc.
377 XmlNode currentNode = this;
379 while (xmlReader.Read ())
381 switch (xmlReader.NodeType) {
383 case XmlNodeType.CDATA:
384 XmlCDataSection cdataSection = CreateCDataSection(xmlReader.Value);
385 currentNode.AppendChild (cdataSection);
388 case XmlNodeType.Comment:
389 XmlComment comment = CreateComment (xmlReader.Value);
390 currentNode.AppendChild (comment);
393 case XmlNodeType.Element:
394 XmlElement element = CreateElement (xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
395 currentNode.AppendChild (element);
397 // set the element's attributes.
398 while (xmlReader.MoveToNextAttribute ())
399 element.SetAttribute (xmlReader.Name, xmlReader.Value);
401 // if this element isn't empty, push it onto our "stack".
402 if (!xmlReader.IsEmptyElement)
403 currentNode = element;
407 case XmlNodeType.EndElement:
408 currentNode = currentNode.ParentNode;
411 case XmlNodeType.ProcessingInstruction:
412 XmlProcessingInstruction processingInstruction = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
413 // Where does a processing instruction go in the doc?
414 // I think we need to just hold on to them in an internal array in doc.
417 case XmlNodeType.Text:
418 XmlText text = CreateTextNode (xmlReader.Value);
419 currentNode.AppendChild (text);
425 public virtual void LoadXml (string xml)
427 XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
432 public virtual XmlNode ReadNode(XmlReader reader)
434 throw new NotImplementedException ();
438 public virtual void Save(Stream outStream)
440 throw new NotImplementedException ();
444 public virtual void Save (string filename)
446 throw new NotImplementedException ();
450 public virtual void Save (TextWriter writer)
452 throw new NotImplementedException ();
456 public virtual void Save (XmlWriter writer)
458 throw new NotImplementedException ();
462 public override void WriteContentTo (XmlWriter xw)
464 throw new NotImplementedException ();
468 public override void WriteTo (XmlWriter w)
470 throw new NotImplementedException ();
473 internal void onNodeChanging(XmlNode node, XmlNode Parent)
475 if (NodeInserting != null)
476 NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
477 node, Parent, Parent));
480 internal void onNodeChanged(XmlNode node, XmlNode Parent)
482 if (NodeChanged != null)
483 NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
484 node, Parent, Parent));
487 internal void onNodeInserting(XmlNode node, XmlNode newParent)
489 if (NodeInserting != null)
490 NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
491 node, null, newParent));
494 internal void onNodeInserted(XmlNode node, XmlNode newParent)
496 if (NodeInserted != null)
497 NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
498 node, null, newParent));
501 internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
503 if (NodeRemoving != null)
504 NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
505 node, oldParent, null));
508 internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
510 if (NodeRemoved != null)
511 NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
512 node, oldParent, null));