* XmlDeclaration.cs : Fixed ParseInput() more parse strictly.
[mono.git] / mcs / class / System.XML / System.Xml / XmlDocument.cs
index 0a7267dabcd86cf46cb7ea67074c9fd305a163f5..1ba2d42e2d11d39f160a675cbb3dacc67a26e3cb 100644 (file)
@@ -1,30 +1,73 @@
-// -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 //
 // System.Xml.XmlDocument
 //
-// Author:
+// Authors:
 //   Daniel Weber (daniel-weber@austin.rr.com)
+//   Kral Ferch <kral_ferch@hotmail.com>
+//   Jason Diamond <jason@injektilo.org>
+//   Miguel de Icaza (miguel@ximian.com)
+//   Duncan Mak (duncan@ximian.com)
 //
 // (C) 2001 Daniel Weber
+// (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza, Duncan Mak
+//
 
 using System;
 using System.IO;
+using System.Text;
+using System.Xml.XPath;
+using System.Diagnostics;
+using System.Collections;
 
 namespace System.Xml
 {
-
-       public delegate void XmlNodeChangedEventHandler (XmlNodeChangedEventArgs args);
-
-       /// <summary>
-       /// Abstract class XmlNodeList.
-       /// </summary>
        public class XmlDocument : XmlNode
        {
-               // Private data members
-               XmlResolver _resolver = null;
+               #region Fields
+
+               XmlLinkedNode lastLinkedChild;
+               XmlNameTable nameTable;
+               string baseURI = String.Empty;
+
+               #endregion
+
+               #region Constructors
+
+               public XmlDocument () : base (null)
+               {
+                       System.Xml.NameTable nt = new NameTable();
+                       // keys below are default of MS .NET Framework
+                       nt.Add("#text");
+                       nt.Add("xml");
+                       nt.Add("xmlns");
+                       nt.Add("#entity");
+                       nt.Add("#document-fragment");
+                       nt.Add("#comment");
+                       nt.Add("space");
+                       nt.Add("id");
+                       nt.Add("#whitespace");
+                       nt.Add("http://www.w3.org/2000/xmlns/");
+                       nt.Add("#cdata-section");
+                       nt.Add("lang");
+
+                       nameTable = nt;
+               }
+
+               [MonoTODO]
+               protected internal XmlDocument (XmlImplementation imp) : base (null)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public XmlDocument (XmlNameTable nt) : base (null)
+               {
+                       nameTable = nt;
+               }
+
+               #endregion
+
+               #region Events
 
-               // Public events
-               //===========================================================================
                public event XmlNodeChangedEventHandler NodeChanged;
 
                public event XmlNodeChangedEventHandler NodeChanging;
@@ -37,29 +80,18 @@ namespace System.Xml
 
                public event XmlNodeChangedEventHandler NodeRemoving;
 
-               // public properties
+               #endregion
 
-               /// <summary>
-               /// Get the base URI for this document (the location from where the document was loaded)
-               /// </summary>
-               /// <example>If a document was loaded with doc.Load("c:\tmp\mydoc.xml"),
-               /// then BaseURI would hold "c:\tmp\mydoc.xml"</example>
-               public override string BaseURI
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.BaseURI {get;}
-                               throw new NotImplementedException("BaseURI.get not implemented");
+               #region Properties
+
+               public override string BaseURI {
+                       get {
+                               return baseURI;
                        }
                }
 
-               /// <summary>
-               /// Get the root element for the document.  If no root exists, null is returned.
-               /// </summary>
-               public XmlElement DocumentElement
-               {
-                       get
-                       {
+               public XmlElement DocumentElement {
+                       get {
                                XmlNode node = FirstChild;
 
                                while (node != null) {
@@ -72,479 +104,623 @@ namespace System.Xml
                        }
                }
 
-               /// <summary>
-               /// Gets the node containing the DOCTYPE declaration.
-               /// </summary>
-               public virtual XmlDocumentType DocumentType
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.DocumentType
-                               throw new NotImplementedException("XmlDocument.DocumentType not implemented");
-                       }
+               [MonoTODO]
+               public virtual XmlDocumentType DocumentType {
+                       get { throw new NotImplementedException(); }
                }
 
+               [MonoTODO]
+               public XmlImplementation Implementation {
+                       get { throw new NotImplementedException(); }
+               }
 
-               /// <summary>
-               /// Get the XmlImplemenation for the current document.
-               /// </summary>
-               public XmlImplementation Implementation
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.Implementation
-                               throw new NotImplementedException("Implementation not implemented");
+               [MonoTODO ("Setter.")]
+               public override string InnerXml {
+                       get {
+                               // Not sure why this is an override.  Passing through for now.
+                               return base.InnerXml;
                        }
+                       set { throw new NotImplementedException(); }
                }
 
+               public override bool IsReadOnly {
+                       get { return false; }
+               }
 
-               /// <summary>
-               /// Get/Set the markup representing the children of the document.
-               /// </summary>
-               public override string InnerXml
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.InnerXml {get;}
-                               throw new NotImplementedException("InnerXml get not implemented");
+               internal override XmlLinkedNode LastLinkedChild {
+                       get     {
+                               return lastLinkedChild;
                        }
-                       set
-                       {
-                               // TODO - implement XmlDocument.InnerXml {set;}
-                               throw new NotImplementedException("InnerXml set not implemented");
-                       }
-               }
 
-               /// <summary>
-               /// Get a value indicating if the document is read-only.
-               /// </summary>
-               public override bool IsReadOnly
-               {
-                       get
-                       {
-                               return false;
+                       set {
+                               lastLinkedChild = value;
                        }
                }
-
-               /// <summary>
-               /// Get the local name of the node.  For documents, returns "#document"
-               /// </summary>
+               
                public override string LocalName {
-                       get
-                       {
-                               return "#document";
-                       }
+                       get { return "#document"; }
                }
 
-               /// <summary>
-               /// Get the qualified name of the node.  For documents, returns "#document"
-               /// </summary>
-               public override string Name
-               {
-                       get
-                       {
-                               return "#document";
-                       }
+               public override string Name {
+                       get { return "#document"; }
                }
 
-               public XmlNameTable NameTable
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.NameTable {get;}
-                               throw new NotImplementedException("NameTable get not implemented");
-                       }
+               public XmlNameTable NameTable {
+                       get { return nameTable; }
                }
 
+               public override XmlNodeType NodeType {
+                       get { return XmlNodeType.Document; }
+               }
 
-               public override XmlNodeType NodeType
-               {
-                       get
-                       {
-                               return XmlNodeType.Document;
-                       }
+               public override XmlDocument OwnerDocument {
+                       get { return null; }
                }
 
-               /// <summary>
-               /// Returns OwnerDocument.  For an XmlDocument, this property is always null.
-               /// </summary>
-               public override XmlDocument OwnerDocument
-               {
-                       get
-                       {
-                               return null;
-                       }
+               [MonoTODO]
+               public bool PreserveWhitespace {
+                       get { throw new NotImplementedException(); }
+                       set { throw new NotImplementedException(); }
                }
 
-               public bool PreserveWhitespace
-               {
-                       get
-                       {
-                               // TODO - implement XmlDocument.PreserveWhitespace {get;}
-                               throw new NotImplementedException("PreserveWhitespace get not implemented");
-                       }
-                       set
-                       {
-                               // TODO - implement XmlDocument.PreserveWhitespace {set;}
-                               throw new NotImplementedException("PreserveWhitespace set not implemented");
-                       }
+               [MonoTODO]
+               public virtual XmlResolver XmlResolver {
+                       set { throw new NotImplementedException(); }
                }
 
-               public XmlResolver XmlResolver
+               #endregion
+
+               #region Methods
+
+               [MonoTODO]
+               public override XmlNode CloneNode (bool deep)
                {
-                       set
-                       {
-                               // TODO - Finish/test XmlDocument.XmlResolver {set;}
-                               _resolver = value;
-                       }
+                       throw new NotImplementedException ();
                }
 
-               // Public Methods
-               //===========================================================================
-               public override XmlNode CloneNode(bool deep)
+               public XmlAttribute CreateAttribute (string name)
                {
-                       // TODO - implement XmlDocument.CloneNode(bool)
-                       throw new NotImplementedException("CloneNode(bool) not implemented");
+                       return CreateAttribute (name, String.Empty);
                }
 
-               public XmlAttribute CreateAttribute(string name)
+               public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
                {
-                       return new XmlAttribute(this, name, "");
+                       string prefix;
+                       string localName;
+
+                       ParseName (qualifiedName, out prefix, out localName);
+
+                       return CreateAttribute (prefix, localName, namespaceURI);
                }
 
-               public XmlAttribute CreateAttribute(string qualifiedName,string namespaceURI)
+               public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
                {
-                       // TODO - implement XmlDocument.CreateAttribute(string, string)
-                       throw new NotImplementedException("CreateAttribute(string, string) not implemented");
+                       if ((localName == null) || (localName == String.Empty))
+                               throw new ArgumentException ("The attribute local name cannot be empty.");
+
+                       return new XmlAttribute (prefix, localName, namespaceURI, this);
                }
 
-               public virtual XmlAttribute CreateAttribute(
-                       string prefix,
-                       string localName,
-                       string namespaceURI
-                       )
+               public virtual XmlCDataSection CreateCDataSection (string data)
                {
-                       // TODO - implement XmlDocument.CreateAttribute(prefix, localName, namespaceURI)
-                       throw new NotImplementedException("CreateAttribute(prefix, localName, namespaceURI) not implemented");
+                       return new XmlCDataSection (data, this);
                }
 
-               public virtual XmlCDataSection CreateCDataSection(string data)
+               public virtual XmlComment CreateComment (string data)
                {
-                       // TODO - implement XmlDocument.CreateCDataSection(string data)
-                       throw new NotImplementedException("CreateCDataSection(string data) not implemented");
+                       return new XmlComment(data, this);
                }
 
-
-               public virtual XmlComment CreateComment(string data)
+               [MonoTODO]
+               protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
                {
-                       // TODO - implement XmlDocument.CreateComment(string data)
-                       throw new NotImplementedException("CreateComment(string data) not implemented");
+                       throw new NotImplementedException ();
                }
 
-               public virtual XmlDocumentFragment CreateDocumentFragment()
+               [MonoTODO]
+               public virtual XmlDocumentFragment CreateDocumentFragment ()
                {
-                       // TODO - implement XmlDocument.CreateDocumentFragment
-                       throw new NotImplementedException("CreateDocumentFragment not implemented");
+                       throw new NotImplementedException ();
                }
 
-               public virtual XmlDocumentType CreateDocumentType(
-                       string name,
-                       string publicId,
-                       string systemId,
-                       string internalSubset
-                       )
+               public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
+                                                                  string systemId, string internalSubset)
                {
-                       // TODO - implement XmlDocument.CreateDocumentType
-                       throw new NotImplementedException("CreateDocumentType not implemented");
+                       return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
                }
 
-               public XmlElement CreateElement(string name)
+               public XmlElement CreateElement (string name)
                {
-                       // TODO - implement XmlDocument.CreateElement(string name)
-                       throw new NotImplementedException("CreateElement(string name) not implemented");
+                       return CreateElement (name, String.Empty);
                }
 
-               public XmlElement CreateElement(
-                       string qualifiedName,
-                       string namespaceURI
-                       )
+               public XmlElement CreateElement (
+                       string qualifiedName, 
+                       string namespaceURI)
                {
-                       // TODO - implement XmlDocument.CreateElement(string qualifiedName,     string namespaceURI)
-                       throw new NotImplementedException("CreateElement(string qualifiedName,  string namespaceURI) not implemented");
+                       string prefix;
+                       string localName;
+
+                       ParseName (qualifiedName, out prefix, out localName);
+
+                       return CreateElement (prefix, localName, namespaceURI);
                }
 
-               public virtual XmlElement CreateElement(
+               public virtual XmlElement CreateElement (
                        string prefix,
                        string localName,
-                       string namespaceURI
-                       )
+                       string namespaceURI)
                {
-                       return new XmlElement(prefix, localName, namespaceURI, this);
+                       if ((localName == null) || (localName == String.Empty))
+                               throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
+
+                       return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
                }
 
+               [MonoTODO]
+               public virtual XmlEntityReference CreateEntityReference (string name)
+               {
+                       throw new NotImplementedException ();
+               }
 
-               public virtual XmlEntityReference CreateEntityReference(string name)
+               [MonoTODO]
+               protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
                {
-                       // TODO - implement XmlDocument.CreateEntityReference
-                       throw new NotImplementedException("XmlDocument.CreateEntityReference not implemented.");
+                       throw new NotImplementedException ();
                }
 
-               public virtual XmlNode CreateNode(
+               public virtual XmlNode CreateNode (
                        string nodeTypeString,
                        string name,
-                       string namespaceURI
-                       )
+                       string namespaceURI)
                {
-                       // TODO - implement XmlDocument.CreateNode(string, string, string)
-                       throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
+                       return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
                }
 
-               public virtual XmlNode CreateNode(
+               public virtual XmlNode CreateNode (
                        XmlNodeType type,
                        string name,
-                       string namespaceURI
-                       )
+                       string namespaceURI)
                {
-                       // TODO - implement XmlDocument.CreateNode(XmlNodeType, string, string)
-                       throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
+                       string prefix = null;
+                       string localName = name;
+
+                       if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
+                               ParseName (name, out prefix, out localName);
+                       
+                       return CreateNode (type, prefix, localName, namespaceURI);
                }
 
-               public virtual XmlNode CreateNode(
+               public virtual XmlNode CreateNode (
                        XmlNodeType type,
                        string prefix,
                        string name,
-                       string namespaceURI
-                       )
-               {
-                       // TODO - implement XmlDocument.CreateNode(XmlNodeType, string, string, string)
-                       throw new NotImplementedException("XmlDocument.CreateNode not implemented.");
+                       string namespaceURI)
+               {
+                       switch (type) {
+                               case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
+                               case XmlNodeType.CDATA: return CreateCDataSection (null);
+                               case XmlNodeType.Comment: return CreateComment (null);
+                               case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
+                               case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
+                               case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
+                               case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
+                               case XmlNodeType.EntityReference: return CreateEntityReference (null);
+                               case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
+                               case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
+                               case XmlNodeType.Text: return CreateTextNode (null);
+                               case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
+                               case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
+                               default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
+                                                        "Specified argument was out of the range of valid values", type.ToString ()));
+                       }
                }
 
-               public virtual XmlProcessingInstruction CreateProcessingInstruction(
+               public virtual XmlProcessingInstruction CreateProcessingInstruction (
                        string target,
-                       string data
-                       )
+                       string data)
                {
-                       // TODO - implement XmlDocument.CreateProcessingInstruction
-                       throw new NotImplementedException("XmlDocument.CreateProcessingInstruction not implemented.");
+                       return new XmlProcessingInstruction (target, data, this);
                }
 
-               public virtual XmlSignificantWhitespace CreateSignificantWhitespace(string text )
+               public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
                {
-                       // TODO - implement XmlDocument.CreateSignificantWhitespace
-                       throw new NotImplementedException("XmlDocument.CreateSignificantWhitespace not implemented.");
+                       foreach (char c in text)
+                               if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+                                   throw new ArgumentException ("Invalid whitespace characters.");
+                        
+                       return new XmlSignificantWhitespace (text, this);
                }
 
-               public virtual XmlText CreateTextNode(string text)
+               public virtual XmlText CreateTextNode (string text)
                {
-                       // TODO - implement XmlDocument.CreateTextNode
-                       throw new NotImplementedException("XmlDocument.CreateTextNode not implemented.");
+                       return new XmlText (text, this);
                }
 
-               public virtual XmlWhitespace CreateWhitespace(string text)
+               public virtual XmlWhitespace CreateWhitespace (string text)
                {
-                       // TODO - implement XmlDocument.CreateWhitespace
-                       throw new NotImplementedException("XmlDocument.CreateWhitespace not implemented.");
+                       foreach (char c in text)
+                               if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
+                                   throw new ArgumentException ("Invalid whitespace characters.");
+                        
+                       return new XmlWhitespace (text, this);
                }
 
-               public virtual XmlDeclaration CreateXmlDeclaration(
-                       string version,
-                       string encoding,
-                       string standalone
-                       )
+               public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
+                                                                   string standalone)
                {
-                       // TODO - implement XmlDocument.CreateXmlDeclaration
-                       throw new NotImplementedException("XmlDocument.CreateXmlDeclaration not implemented.");
+                       if (version != "1.0")
+                               throw new ArgumentException ("version string is not correct.");
+
+                       if  ((standalone != null) && !((standalone == "yes") || (standalone == "no")))
+                               throw new ArgumentException ("standalone string is not correct.");
+                       
+                       return new XmlDeclaration (version, encoding, standalone, this);
                }
 
-               public virtual XmlElement GetElementById(string elementId)
+               [MonoTODO]
+               public virtual XmlElement GetElementById (string elementId)
                {
-                       // TODO - implement XmlDocument.GetElementById
-                       throw new NotImplementedException("XmlDocument.GetElementById not implemented.");
+                       throw new NotImplementedException ();
                }
 
-               public virtual XmlNodeList GetElementsByTagName(string name)
+               public virtual XmlNodeList GetElementsByTagName (string name)
                {
-                       // TODO - implement XmlDocument.GetElementsByTagName(name)
-                       throw new NotImplementedException("XmlDocument.GetElementsByTagName not implemented.");
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, name, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
                }
 
-               public virtual XmlNodeList GetElementsByTagName(
-                       string localName,
-                       string namespaceURI
-                       )
+               private void searchNodesRecursively (XmlNode argNode, string argName, 
+                       ArrayList argArrayList)
                {
-                       // TODO - implement XmlDocument.GetElementsByTagName(localName, namespaceURI)
-                       throw new NotImplementedException("XmlDocument.GetElementsByTagName not implemented.");
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList){
+                               if (node.Name.Equals (argName))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argArrayList);
+                       }
                }
 
-               public virtual XmlNode ImportNode(
-                       XmlNode node,
-                       bool deep
-                       )
+               private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI, 
+                       ArrayList argArrayList)
                {
-                       // TODO - implement XmlDocument.ImportNode
-                       throw new NotImplementedException("XmlDocument.ImportNode not implemented.");
+                       XmlNodeList xmlNodeList = argNode.ChildNodes;
+                       foreach (XmlNode node in xmlNodeList){
+                               if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
+                                       argArrayList.Add (node);
+                               else    
+                                       this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
+                       }
                }
 
-               public virtual void Load(Stream inStream)
+               public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
                {
-                       // TODO - implement XmlDocument.Load(Stream)
-                       throw new NotImplementedException("XmlDocument.Load(Stream) not implemented.");
+                       ArrayList nodeArrayList = new ArrayList ();
+                       this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
+                       return new XmlNodeArrayList (nodeArrayList);
                }
 
-               public virtual void Load(string filename)
+               private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
                {
-                       // TODO - implement XmlDocument.Load(string)
-                       throw new NotImplementedException("XmlDocument.Load(string) not implemented.");
+                       switch (nodeTypeString) {
+                               case "attribute": return XmlNodeType.Attribute;
+                               case "cdatasection": return XmlNodeType.CDATA;
+                               case "comment": return XmlNodeType.Comment;
+                               case "document": return XmlNodeType.Document;
+                               case "documentfragment": return XmlNodeType.DocumentFragment;
+                               case "documenttype": return XmlNodeType.DocumentType;
+                               case "element": return XmlNodeType.Element;
+                               case "entityreference": return XmlNodeType.EntityReference;
+                               case "processinginstruction": return XmlNodeType.ProcessingInstruction;
+                               case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
+                               case "text": return XmlNodeType.Text;
+                               case "whitespace": return XmlNodeType.Whitespace;
+                               default:
+                                       throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
+                       }
                }
 
-               public virtual void Load(TextReader txtReader)
+               [MonoTODO]
+               public virtual XmlNode ImportNode (XmlNode node, bool deep)
                {
-                       // TODO - implement XmlDocument.Load(TextReader)
-                       throw new NotImplementedException("XmlDocument.Load(TextReader) not implemented.");
+                       // How to resolve default attribute values?
+                       switch(node.NodeType)
+                       {
+                               case XmlNodeType.Attribute:
+                                       {
+                                               XmlAttribute src_att = node as XmlAttribute;
+                                               XmlAttribute dst_att = this.CreateAttribute(src_att.Prefix, src_att.LocalName, src_att.NamespaceURI);
+                                               // TODO: resolve default attribute values
+                                               dst_att.Value = src_att.Value;
+                                               return dst_att;
+                                       }
+
+                               case XmlNodeType.CDATA:
+                                       return this.CreateCDataSection(node.Value);
+
+                               case XmlNodeType.Comment:
+                                       return this.CreateComment(node.Value);
+
+                               case XmlNodeType.Document:
+                                       throw new XmlException("Document cannot be imported.");
+
+                               case XmlNodeType.DocumentFragment:
+                                       {
+                                               XmlDocumentFragment df = this.CreateDocumentFragment();
+                                               if(deep)
+                                               {
+                                                       foreach(XmlNode n in node.ChildNodes)
+                                                       {
+                                                               df.AppendChild(this.ImportNode(n, deep));
+                                                       }
+                                               }
+                                               return df;
+                                       }
+
+                               case XmlNodeType.DocumentType:
+                                       throw new XmlException("DocumentType cannot be imported.");
+
+                               case XmlNodeType.Element:
+                                       {
+                                               XmlElement src = (XmlElement)node;
+                                               XmlElement dst = this.CreateElement(src.Prefix, src.LocalName, src.NamespaceURI);
+                                               foreach(XmlAttribute attr in src.Attributes)
+                                               {
+                                                       // TODO: create default attribute values
+                                                       dst.SetAttributeNode((XmlAttribute)this.ImportNode(attr, deep));
+                                               }
+                                               if(deep)
+                                               {
+                                                       foreach(XmlNode n in src.ChildNodes)
+                                                               dst.AppendChild(this.ImportNode(n, deep));
+                                               }
+                                               return dst;
+                                       }
+
+                               case XmlNodeType.EndElement:
+                                       throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
+                               case XmlNodeType.EndEntity:
+                                       throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
+                               case XmlNodeType.Entity:
+                                       throw new NotImplementedException ();
+
+                               // [2002.10.14] CreateEntityReference not implemented.
+                               case XmlNodeType.EntityReference:
+                                       throw new NotImplementedException("ImportNode of EntityReference not implemented mainly because CreateEntityReference was implemented in the meantime.");
+//                                     return this.CreateEntityReference(node.Name);
+
+                               case XmlNodeType.None:
+                                       throw new XmlException ("Illegal ImportNode call for NodeType.None");
+                               case XmlNodeType.Notation:
+                                       throw new NotImplementedException ();
+
+                               case XmlNodeType.ProcessingInstruction:
+                                       XmlProcessingInstruction pi = node as XmlProcessingInstruction;
+                                       return this.CreateProcessingInstruction(pi.Target, pi.Data);
+
+                               case XmlNodeType.SignificantWhitespace:
+                                       return this.CreateSignificantWhitespace(node.Value);
+
+                               case XmlNodeType.Text:
+                                       return this.CreateTextNode(node.Value);
+
+                               case XmlNodeType.Whitespace:
+                                       return this.CreateWhitespace(node.Value);
+
+                               // I don't know how to test it...
+                               case XmlNodeType.XmlDeclaration:
+                               //      return this.CreateNode(XmlNodeType.XmlDeclaration, String.Empty, node.Value);
+                                       throw new NotImplementedException ();
+
+                               default:
+                                       throw new NotImplementedException ();
+                       }
                }
 
-               public virtual void Load(XmlReader reader)
+               public virtual void Load (Stream inStream)
                {
-                       // TODO - implement XmlDocument.Load(XmlReader)
-                       throw new NotImplementedException("XmlDocument.Load(XmlReader) not implemented.");
+                       XmlReader xmlReader = new XmlTextReader (inStream);
+                       Load (xmlReader);
                }
 
-               public virtual void LoadXml(string xml)
+               public virtual void Load (string filename)
                {
-                       XmlReader       xmlReader = new XmlTextReader(new StringReader(xml));
-                       XmlNode         currentNode = this;
-                       XmlNode         newNode;
+                       baseURI = filename;
+                       XmlReader xmlReader = new XmlTextReader (new StreamReader (filename));
+                       Load (xmlReader);
+               }
 
+               public virtual void Load (TextReader txtReader)
+               {
+                       Load (new XmlTextReader (txtReader));
+               }
+
+               public virtual void Load (XmlReader xmlReader)
+               {
                        // Reset our document
                        // For now this just means removing all our children but later this
                        // may turn out o need to call a private method that resets other things
                        // like properties we have, etc.
-                       RemoveAll();
+                       RemoveAll ();
 
-                       // Wrapping in try/catch for now until XmlTextReader starts throwing XmlException
-                       try 
+                       XmlNode currentNode = this;
+                       XmlNode newNode;
+
+#if true
+                       this.ConstructDOM(xmlReader, currentNode);
+#else
+                       // Below are copied to XmlNode.Construct(currentNode, xmlReader)
+                       while (xmlReader.Read ()) 
                        {
-                               while (xmlReader.Read())
-                               {
-                                       switch(xmlReader.NodeType)
-                                       {
-                                               case XmlNodeType.Element:
-                                                       newNode = CreateElement(xmlReader.Name, xmlReader.LocalName, xmlReader.NamespaceURI);
-                                                       currentNode.AppendChild(newNode);
-                                                       if (!xmlReader.IsEmptyElement)
-                                                       {
-                                                               currentNode = newNode;
-                                                       }
-                                                       break;
-                                               
-                                               case XmlNodeType.Text:
-                                                       newNode = CreateTextNode(xmlReader.Value);
-                                                       currentNode.AppendChild(newNode);
-                                                       break;
-
-                                               case XmlNodeType.EndElement:
-                                                       currentNode = currentNode.ParentNode;
-                                                       break;
+                               switch (xmlReader.NodeType) {
+
+                               case XmlNodeType.CDATA:
+                                       newNode = CreateCDataSection(xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
+                               case XmlNodeType.Comment:
+                                       newNode = CreateComment (xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
+                               case XmlNodeType.Element:
+                                       XmlElement element = CreateElement (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
+                                       currentNode.AppendChild (element);
+
+                                       // set the element's attributes.
+                                       while (xmlReader.MoveToNextAttribute ()) {
+                                               XmlAttribute attribute = CreateAttribute (xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
+                                               attribute.Value = xmlReader.Value;
+                                               element.SetAttributeNode (attribute);
                                        }
+
+                                       xmlReader.MoveToElement ();
+
+                                       // if this element isn't empty, push it onto our "stack".
+                                       if (!xmlReader.IsEmptyElement)
+                                               currentNode = element;
+
+                                       break;
+
+                               case XmlNodeType.EndElement:
+                                       currentNode = currentNode.ParentNode;
+                                       break;
+
+                               case XmlNodeType.ProcessingInstruction:
+                                       newNode = CreateProcessingInstruction (xmlReader.Name, xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
+
+                               case XmlNodeType.Text:
+                                       newNode = CreateTextNode (xmlReader.Value);
+                                       currentNode.AppendChild (newNode);
+                                       break;
                                }
                        }
-                       catch(Exception e)
-                       {
-                               throw new XmlException(e.Message, e);
-                       }
+#endif
                }
 
-               public virtual void Save(Stream outStream)
+               public virtual void LoadXml (string xml)
                {
-                       // TODO - implement XmlDocument.Save(Stream)
-                       throw new NotImplementedException("XmlDocument.Save(Stream) not implemented.");
+                       XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
+                       Load (xmlReader);
                }
 
-               public virtual void Save(string filename)
+               internal void onNodeChanged (XmlNode node, XmlNode Parent)
                {
-                       // TODO - implement XmlDocument.Save(string)
-                       throw new NotImplementedException("XmlDocument.Save(string) not implemented.");
+                       if (NodeChanged != null)
+                               NodeChanged (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Change,
+                                       node, Parent, Parent));
                }
 
-               public virtual void Save(TextWriter writer)
+               internal void onNodeChanging(XmlNode node, XmlNode Parent)
                {
-                       // TODO - implement XmlDocument.Save(TextWriter)
-                       throw new NotImplementedException("XmlDocument.Save(TextWriter) not implemented.");
+                       if (NodeChanging != null)
+                               NodeChanging (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Change,
+                                       node, Parent, Parent));
                }
 
-               public virtual void Save(XmlWriter writer)
+               internal void onNodeInserted (XmlNode node, XmlNode newParent)
                {
-                       // TODO - implement XmlDocument.Save(XmlWriter)
-                       throw new NotImplementedException("XmlDocument.Save(XmlWriter) not implemented.");
+                       if (NodeInserted != null)
+                               NodeInserted (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Insert,
+                                       node, null, newParent));
                }
 
-               public override void WriteContentTo(XmlWriter w)
+               internal void onNodeInserting (XmlNode node, XmlNode newParent)
                {
-                       // TODO - implement XmlDocument.WriteContentTo
-                       throw new NotImplementedException("XmlDocument.WriteContentTo not implemented.");
+                       if (NodeInserting != null)
+                               NodeInserting (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Insert,
+                                       node, null, newParent));
                }
 
-               public override void WriteTo(XmlWriter w)
+               internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
                {
-                       // TODO - implement XmlDocument.WriteTo
-                       throw new NotImplementedException("XmlDocument.WriteTo not implemented.");
+                       if (NodeRemoved != null)
+                               NodeRemoved (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Remove,
+                                       node, oldParent, null));
                }
 
-
-               // Internal functions
-               //===========================================================================
-               internal void onNodeChanging(XmlNode node, XmlNode Parent)
+               internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
                {
-                       if (NodeInserting != null)
-                               NodeChanging( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
-                                       node, Parent, Parent));
+                       if (NodeRemoving != null)
+                               NodeRemoving (node, new XmlNodeChangedEventArgs
+                                       (XmlNodeChangedAction.Remove,
+                                       node, oldParent, null));
                }
 
-               internal void onNodeChanged(XmlNode node, XmlNode Parent)
+               private void ParseName (string name, out string prefix, out string localName)
                {
-                       if (NodeChanged != null)
-                               NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Change,
-                                       node, Parent, Parent));
+                       int indexOfColon = name.IndexOf (':');
+                       
+                       if (indexOfColon != -1) {
+                               prefix = name.Substring (0, indexOfColon);
+                               localName = name.Substring (indexOfColon + 1);
+                       } else {
+                               prefix = "";
+                               localName = name;
+                       }
                }
 
-               internal void onNodeInserting(XmlNode node, XmlNode newParent)
+               [MonoTODO]
+               public virtual XmlNode ReadNode(XmlReader reader)
                {
-                       if (NodeInserting != null)
-                               NodeInserting( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
-                                       node, null, newParent));
+                       throw new NotImplementedException ();
                }
 
-               internal void onNodeInserted(XmlNode node, XmlNode newParent)
+               [MonoTODO ("Verify what encoding is used by default;  Should use PreserveWhiteSpace")]
+               public virtual void Save(Stream outStream)
                {
-                       if (NodeInserted != null)
-                               NodeInserted( new XmlNodeChangedEventArgs(XmlNodeChangedAction.Insert,
-                                       node, null, newParent));
+                       XmlTextWriter xmlWriter = new XmlTextWriter (outStream, Encoding.UTF8);
+                       WriteContentTo (xmlWriter);
+                       xmlWriter.Close ();
                }
 
-               internal void onNodeRemoving(XmlNode node, XmlNode oldParent)
+               [MonoTODO ("Verify what encoding is used by default; Should use PreseveWhiteSpace")]
+               public virtual void Save (string filename)
                {
-                       if (NodeRemoving != null)
-                               NodeRemoving(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
-                                       node, oldParent, null));
+                       XmlTextWriter xmlWriter = new XmlTextWriter (filename, Encoding.UTF8);
+                       WriteContentTo (xmlWriter);
+                       xmlWriter.Close ();
                }
 
-               internal void onNodeRemoved(XmlNode node, XmlNode oldParent)
+               [MonoTODO]
+               public virtual void Save (TextWriter writer)
                {
-                       if (NodeRemoved != null)
-                               NodeRemoved(new XmlNodeChangedEventArgs(XmlNodeChangedAction.Remove,
-                                       node, oldParent, null));
+                       XmlTextWriter xmlWriter = new XmlTextWriter (writer);
+                       WriteContentTo (xmlWriter);
+                       xmlWriter.Flush ();
+               }
 
+               [MonoTODO ("Should preserve white space if PreserveWhisspace is set")]
+               public virtual void Save (XmlWriter xmlWriter)
+               {
+                       //
+                       // This should preserve white space if PreserveWhiteSpace is true
+                       //
+                       WriteContentTo (xmlWriter);
+                       xmlWriter.Flush ();
                }
 
-               // Constructors
-               //===========================================================================
-               public XmlDocument() : base(null)
+               public override void WriteContentTo (XmlWriter w)
                {
-                       FOwnerDocument = this;
+                       foreach(XmlNode childNode in ChildNodes)
+                               childNode.WriteTo(w);
                }
 
+               public override void WriteTo (XmlWriter w)
+               {
+                       WriteContentTo(w);
+               }
 
+               #endregion
        }
 }