2 // System.Xml.XmlDocument
5 // Daniel Weber (daniel-weber@austin.rr.com)
6 // Kral Ferch <kral_ferch@hotmail.com>
7 // Jason Diamond <jason@injektilo.org>
8 // Miguel de Icaza (miguel@ximian.com)
9 // Duncan Mak (duncan@ximian.com)
10 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
12 // (C) 2001 Daniel Weber
13 // (C) 2002 Kral Ferch, Jason Diamond, Miguel de Icaza, Duncan Mak,
20 using System.Xml.XPath;
21 using System.Diagnostics;
22 using System.Collections;
24 using Mono.Xml.Native;
28 public class XmlDocument : XmlNode
32 XmlLinkedNode lastLinkedChild;
33 XmlNameTable nameTable;
34 string baseURI = String.Empty;
35 XmlImplementation implementation;
36 bool preserveWhitespace = false;
37 WeakReference reusableXmlTextReader;
43 public XmlDocument () : this (null, null)
47 protected internal XmlDocument (XmlImplementation imp) : this (imp, null)
51 public XmlDocument (XmlNameTable nt) : this (null, nt)
55 XmlDocument (XmlImplementation impl, XmlNameTable nt) : base (null)
57 implementation = (impl != null) ? impl : new XmlImplementation ();
58 nameTable = (nt != null) ? nt : implementation.internalNameTable;
59 AddDefaultNameTableKeys ();
65 public event XmlNodeChangedEventHandler NodeChanged;
67 public event XmlNodeChangedEventHandler NodeChanging;
69 public event XmlNodeChangedEventHandler NodeInserted;
71 public event XmlNodeChangedEventHandler NodeInserting;
73 public event XmlNodeChangedEventHandler NodeRemoved;
75 public event XmlNodeChangedEventHandler NodeRemoving;
81 public override string BaseURI {
87 // Used to read 'InnerXml's for its descendants at any place.
88 internal XmlTextReader ReusableReader {
90 if(reusableXmlTextReader == null)
91 reusableXmlTextReader = new WeakReference (null);
92 if(!reusableXmlTextReader.IsAlive) {
93 XmlTextReader reader = new XmlTextReader ((TextReader)null);
94 reusableXmlTextReader.Target = reader;
96 return (XmlTextReader)reusableXmlTextReader.Target;
100 public XmlElement DocumentElement {
102 XmlNode node = FirstChild;
104 while (node != null) {
105 if (node is XmlElement)
107 node = node.NextSibling;
110 return node != null ? node as XmlElement : null;
114 [MonoTODO("It doesn't have internal subset object model.")]
115 public virtual XmlDocumentType DocumentType {
117 foreach(XmlNode n in this.ChildNodes) {
118 if(n.NodeType == XmlNodeType.DocumentType)
119 return (XmlDocumentType)n;
125 public XmlImplementation Implementation {
126 get { return implementation; }
129 public override string InnerXml {
131 return base.InnerXml;
133 set { // reason for overriding
134 this.LoadXml (value);
138 public override bool IsReadOnly {
139 get { return false; }
142 internal override XmlLinkedNode LastLinkedChild {
144 return lastLinkedChild;
148 lastLinkedChild = value;
152 public override string LocalName {
153 get { return "#document"; }
156 public override string Name {
157 get { return "#document"; }
160 public XmlNameTable NameTable {
161 get { return nameTable; }
164 public override XmlNodeType NodeType {
165 get { return XmlNodeType.Document; }
168 internal override XPathNodeType XPathNodeType {
170 return XPathNodeType.Root;
174 public override XmlDocument OwnerDocument {
178 public bool PreserveWhitespace {
179 get { return preserveWhitespace; }
180 set { preserveWhitespace = value; }
183 internal override string XmlLang {
184 get { return String.Empty; }
188 public virtual XmlResolver XmlResolver {
189 set { throw new NotImplementedException (); }
192 internal override XmlSpace XmlSpace {
194 return XmlSpace.None;
202 [MonoTODO("Should BaseURI be cloned?")]
203 public override XmlNode CloneNode (bool deep)
205 XmlDocument doc = implementation.CreateDocument ();
206 doc.PreserveWhitespace = PreserveWhitespace; // required?
209 foreach(XmlNode n in ChildNodes)
210 doc.AppendChild (doc.ImportNode (n, deep));
215 public XmlAttribute CreateAttribute (string name)
217 return CreateAttribute (name, String.Empty);
220 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
225 ParseName (qualifiedName, out prefix, out localName);
227 return CreateAttribute (prefix, localName, namespaceURI);
230 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
232 if ((localName == null) || (localName == String.Empty))
233 throw new ArgumentException ("The attribute local name cannot be empty.");
235 return new XmlAttribute (prefix, localName, namespaceURI, this);
238 public virtual XmlCDataSection CreateCDataSection (string data)
240 return new XmlCDataSection (data, this);
243 public virtual XmlComment CreateComment (string data)
245 return new XmlComment (data, this);
249 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
251 throw new NotImplementedException ();
254 public virtual XmlDocumentFragment CreateDocumentFragment ()
256 return new XmlDocumentFragment (this);
259 public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
260 string systemId, string internalSubset)
262 return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
265 public XmlElement CreateElement (string name)
267 return CreateElement (name, String.Empty);
270 public XmlElement CreateElement (
271 string qualifiedName,
277 ParseName (qualifiedName, out prefix, out localName);
279 return CreateElement (prefix, localName, namespaceURI);
282 public virtual XmlElement CreateElement (
287 if ((localName == null) || (localName == String.Empty))
288 throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
289 CheckName (localName);
290 return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
293 public virtual XmlEntityReference CreateEntityReference (string name)
295 return new XmlEntityReference (name, this);
299 internal protected virtual XPathNavigator CreateNavigator (XmlNode node)
301 throw new NotImplementedException ();
304 public virtual XmlNode CreateNode (
305 string nodeTypeString,
309 return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
312 public virtual XmlNode CreateNode (
317 string prefix = null;
318 string localName = name;
320 if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
321 ParseName (name, out prefix, out localName);
323 return CreateNode (type, prefix, localName, namespaceURI);
326 public virtual XmlNode CreateNode (
333 case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
334 case XmlNodeType.CDATA: return CreateCDataSection (null);
335 case XmlNodeType.Comment: return CreateComment (null);
336 case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
337 case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
338 case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
339 case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
340 case XmlNodeType.EntityReference: return CreateEntityReference (null);
341 case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
342 case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
343 case XmlNodeType.Text: return CreateTextNode (null);
344 case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
345 case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
346 default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
347 "Specified argument was out of the range of valid values", type.ToString ()));
351 public virtual XmlProcessingInstruction CreateProcessingInstruction (
355 return new XmlProcessingInstruction (target, data, this);
358 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
360 foreach (char c in text)
361 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
362 throw new ArgumentException ("Invalid whitespace characters.");
364 return new XmlSignificantWhitespace (text, this);
367 public virtual XmlText CreateTextNode (string text)
369 return new XmlText (text, this);
372 public virtual XmlWhitespace CreateWhitespace (string text)
374 foreach (char c in text)
375 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
376 throw new ArgumentException ("Invalid whitespace characters.");
378 return new XmlWhitespace (text, this);
381 public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
384 if (version != "1.0")
385 throw new ArgumentException ("version string is not correct.");
387 if ((standalone != null && standalone != String.Empty) && !((standalone == "yes") || (standalone == "no")))
388 throw new ArgumentException ("standalone string is not correct.");
390 return new XmlDeclaration (version, encoding, standalone, this);
394 public virtual XmlElement GetElementById (string elementId)
396 throw new NotImplementedException ();
399 public virtual XmlNodeList GetElementsByTagName (string name)
401 ArrayList nodeArrayList = new ArrayList ();
402 this.searchNodesRecursively (this, name, nodeArrayList);
403 return new XmlNodeArrayList (nodeArrayList);
406 private void searchNodesRecursively (XmlNode argNode, string argName,
407 ArrayList argArrayList)
409 XmlNodeList xmlNodeList = argNode.ChildNodes;
410 foreach (XmlNode node in xmlNodeList){
411 if (node.Name.Equals (argName))
412 argArrayList.Add (node);
414 this.searchNodesRecursively (node, argName, argArrayList);
418 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
419 ArrayList argArrayList)
421 XmlNodeList xmlNodeList = argNode.ChildNodes;
422 foreach (XmlNode node in xmlNodeList){
423 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
424 argArrayList.Add (node);
426 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
430 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
432 ArrayList nodeArrayList = new ArrayList ();
433 this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
434 return new XmlNodeArrayList (nodeArrayList);
437 private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
439 switch (nodeTypeString) {
440 case "attribute": return XmlNodeType.Attribute;
441 case "cdatasection": return XmlNodeType.CDATA;
442 case "comment": return XmlNodeType.Comment;
443 case "document": return XmlNodeType.Document;
444 case "documentfragment": return XmlNodeType.DocumentFragment;
445 case "documenttype": return XmlNodeType.DocumentType;
446 case "element": return XmlNodeType.Element;
447 case "entityreference": return XmlNodeType.EntityReference;
448 case "processinginstruction": return XmlNodeType.ProcessingInstruction;
449 case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
450 case "text": return XmlNodeType.Text;
451 case "whitespace": return XmlNodeType.Whitespace;
453 throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
457 [MonoTODO("default attributes (of imported doc); Entity; Notation")]
458 public virtual XmlNode ImportNode (XmlNode node, bool deep)
460 switch(node.NodeType)
462 case XmlNodeType.Attribute:
464 XmlAttribute src_att = node as XmlAttribute;
465 XmlAttribute dst_att = this.CreateAttribute (src_att.Prefix, src_att.LocalName, src_att.NamespaceURI);
466 dst_att.Value = src_att.Value; // always explicitly specified (whether source is specified or not)
470 case XmlNodeType.CDATA:
471 return this.CreateCDataSection (node.Value);
473 case XmlNodeType.Comment:
474 return this.CreateComment (node.Value);
476 case XmlNodeType.Document:
477 throw new XmlException ("Document cannot be imported.");
479 case XmlNodeType.DocumentFragment:
481 XmlDocumentFragment df = this.CreateDocumentFragment ();
484 foreach(XmlNode n in node.ChildNodes)
486 df.AppendChild (this.ImportNode (n, deep));
492 case XmlNodeType.DocumentType:
493 throw new XmlException ("DocumentType cannot be imported.");
495 case XmlNodeType.Element:
497 XmlElement src = (XmlElement)node;
498 XmlElement dst = this.CreateElement (src.Prefix, src.LocalName, src.NamespaceURI);
499 foreach(XmlAttribute attr in src.Attributes)
501 if(attr.Specified) // copies only specified attributes
502 dst.SetAttributeNode ((XmlAttribute)this.ImportNode (attr, deep));
503 if(DocumentType != null)
505 // TODO: create default attribute values
510 foreach(XmlNode n in src.ChildNodes)
511 dst.AppendChild (this.ImportNode (n, deep));
516 case XmlNodeType.EndElement:
517 throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
518 case XmlNodeType.EndEntity:
519 throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
521 case XmlNodeType.Entity:
522 throw new NotImplementedException (); // TODO
524 case XmlNodeType.EntityReference:
525 return this.CreateEntityReference (node.Name);
527 case XmlNodeType.None:
528 throw new XmlException ("Illegal ImportNode call for NodeType.None");
530 case XmlNodeType.Notation:
531 throw new NotImplementedException (); // TODO
533 case XmlNodeType.ProcessingInstruction:
534 XmlProcessingInstruction pi = node as XmlProcessingInstruction;
535 return this.CreateProcessingInstruction (pi.Target, pi.Data);
537 case XmlNodeType.SignificantWhitespace:
538 return this.CreateSignificantWhitespace (node.Value);
540 case XmlNodeType.Text:
541 return this.CreateTextNode (node.Value);
543 case XmlNodeType.Whitespace:
544 return this.CreateWhitespace (node.Value);
546 case XmlNodeType.XmlDeclaration:
547 XmlDeclaration srcDecl = node as XmlDeclaration;
548 return this.CreateXmlDeclaration (srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone);
551 throw new NotImplementedException ();
555 public virtual void Load (Stream inStream)
557 Load (new XmlTextReader (new XmlStreamReader (inStream)));
560 public virtual void Load (string filename)
562 Load (new XmlTextReader (filename));
565 public virtual void Load (TextReader txtReader)
567 Load (new XmlTextReader (txtReader));
570 public virtual void Load (XmlReader xmlReader)
572 // Reset our document
573 // For now this just means removing all our children but later this
574 // may turn out o need to call a private method that resets other things
575 // like properties we have, etc.
578 this.baseURI = xmlReader.BaseURI;
579 // create all contents with use of ReadNode()
581 XmlNode n = ReadNode (xmlReader);
588 public virtual void LoadXml (string xml)
590 XmlReader xmlReader = new XmlTextReader (new StringReader (xml));
594 internal void onNodeChanged (XmlNode node, XmlNode Parent)
596 if (NodeChanged != null)
597 NodeChanged (node, new XmlNodeChangedEventArgs
598 (XmlNodeChangedAction.Change,
599 node, Parent, Parent));
602 internal void onNodeChanging(XmlNode node, XmlNode Parent)
604 if (NodeChanging != null)
605 NodeChanging (node, new XmlNodeChangedEventArgs
606 (XmlNodeChangedAction.Change,
607 node, Parent, Parent));
610 internal void onNodeInserted (XmlNode node, XmlNode newParent)
612 if (NodeInserted != null)
613 NodeInserted (node, new XmlNodeChangedEventArgs
614 (XmlNodeChangedAction.Insert,
615 node, null, newParent));
618 internal void onNodeInserting (XmlNode node, XmlNode newParent)
620 if (NodeInserting != null)
621 NodeInserting (node, new XmlNodeChangedEventArgs
622 (XmlNodeChangedAction.Insert,
623 node, null, newParent));
626 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
628 if (NodeRemoved != null)
629 NodeRemoved (node, new XmlNodeChangedEventArgs
630 (XmlNodeChangedAction.Remove,
631 node, oldParent, null));
634 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
636 if (NodeRemoving != null)
637 NodeRemoving (node, new XmlNodeChangedEventArgs
638 (XmlNodeChangedAction.Remove,
639 node, oldParent, null));
642 private void ParseName (string name, out string prefix, out string localName)
644 int indexOfColon = name.IndexOf (':');
646 if (indexOfColon != -1) {
647 prefix = name.Substring (0, indexOfColon);
648 localName = name.Substring (indexOfColon + 1);
655 // Checks that Element's name is valid
656 private void CheckName (String name)
658 // TODO: others validations?
659 if (name.IndexOf (" ") >= 0)
660 throw new XmlException ("The ' ' characted cannot be included in a name");
663 // Reads XmlReader and creates Attribute Node.
664 private XmlAttribute ReadAttributeNode(XmlReader reader)
666 if(reader.NodeType == XmlNodeType.Element)
667 reader.MoveToFirstAttribute ();
668 else if(reader.NodeType != XmlNodeType.Attribute)
669 throw new InvalidOperationException (MakeReaderErrorMessage ("bad position to read attribute.", reader));
670 XmlAttribute attribute = CreateAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
671 ReadAttributeNodeValue (reader, attribute);
675 // Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this.
676 internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute)
678 while(reader.ReadAttributeValue ()) {
679 if(reader.NodeType == XmlNodeType.EntityReference)
680 // FIXME: if DocumentType is available, then try to resolve it.
681 attribute.AppendChild (CreateEntityReference (reader.Name));
682 // FIXME: else if(NodeType == EndEntity) -- reset BaseURI and so on -- ;
684 // (IMHO) Children of Attribute is likely restricted to Text and EntityReference.
685 attribute.AppendChild (CreateTextNode (reader.Value));
689 [MonoTODO ("Child of entity is not simple Value string;Get prefix of NotationDecl")]
690 public virtual XmlNode ReadNode(XmlReader reader)
692 XmlNode resultNode = null;
693 XmlNode newNode = null;
694 XmlNode currentNode = null;
696 if (reader.ReadState == ReadState.Initial)
699 int startDepth = reader.Depth;
700 bool ignoredWhitespace;
701 bool reachedEOF = false;
704 ignoredWhitespace = false;
705 if (reader.NodeType == XmlNodeType.None)
707 throw new Exception ("XML Reader reached to end while reading node.");
710 switch (reader.NodeType) {
712 case XmlNodeType.Attribute:
713 newNode = ReadAttributeNode (reader);
716 case XmlNodeType.CDATA:
717 newNode = CreateCDataSection (reader.Value);
718 if(currentNode != null)
719 currentNode.AppendChild (newNode);
722 case XmlNodeType.Comment:
723 newNode = CreateComment (reader.Value);
724 if(currentNode != null)
725 currentNode.AppendChild (newNode);
728 case XmlNodeType.Element:
729 XmlElement element = CreateElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
730 element.IsEmpty = reader.IsEmptyElement;
731 if(currentNode != null)
732 currentNode.AppendChild (element);
734 resultNode = element;
736 // set the element's attributes.
737 while (reader.MoveToNextAttribute ()) {
738 element.SetAttributeNode (ReadAttributeNode (reader));
741 reader.MoveToElement ();
743 if (!reader.IsEmptyElement)
744 currentNode = element;
748 case XmlNodeType.EndElement:
749 if (currentNode == null)
750 throw new XmlException ("Unexpected end element.");
751 else if (currentNode.Name != reader.Name)
752 throw new XmlException (MakeReaderErrorMessage (String.Format ("mismatch end tag. Expected {0} but found {1}", currentNode.Name, reader.Name), reader));
753 currentNode = currentNode.ParentNode;
756 case XmlNodeType.EndEntity:
757 break; // no operation
759 case XmlNodeType.ProcessingInstruction:
760 newNode = CreateProcessingInstruction (reader.Name, reader.Value);
761 if(currentNode != null)
762 currentNode.AppendChild (newNode);
765 case XmlNodeType.Text:
766 newNode = CreateTextNode (reader.Value);
767 if(currentNode != null)
768 currentNode.AppendChild (newNode);
771 case XmlNodeType.XmlDeclaration:
772 // empty strings are dummy, then gives over setting value contents to setter.
773 newNode = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
774 ((XmlDeclaration)newNode).Value = reader.Value;
775 if(currentNode != null)
776 throw new XmlException (MakeReaderErrorMessage ("XmlDeclaration at invalid position.", reader));
779 case XmlNodeType.DocumentType:
781 XmlTextReader xtReader = reader as XmlTextReader;
782 if(xtReader == null) {
783 // XmlTextReader doesn't allow such creation that starts reading from Doctype.
785 xtReader = new XmlTextReader (reader.ReadOuterXml (),
786 XmlNodeType.DocumentType,
787 new XmlParserContext (NameTable, ConstructNamespaceManager(), XmlLang, XmlSpace));
790 newNode = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
792 newNode = ReadDoctypeNode (xtReader);
794 if(currentNode != null)
795 throw new XmlException (this.MakeReaderErrorMessage ("XmlDocumentType at invalid position.", reader));
798 case XmlNodeType.EntityReference:
799 newNode = CreateEntityReference (reader.Name);
800 if(currentNode != null)
801 currentNode.AppendChild (newNode);
804 case XmlNodeType.SignificantWhitespace:
805 newNode = CreateSignificantWhitespace (reader.Value);
806 if(currentNode != null)
807 currentNode.AppendChild (newNode);
810 case XmlNodeType.Whitespace:
811 if(PreserveWhitespace) {
812 newNode = CreateWhitespace (reader.Value);
813 if(currentNode != null)
814 currentNode.AppendChild (newNode);
817 ignoredWhitespace = true;
822 } while (ignoredWhitespace || reader.Depth > startDepth ||
823 (reader.Depth == startDepth && reader.NodeType == XmlNodeType.EndElement));
824 if (startDepth != reader.Depth && reader.EOF)
825 throw new XmlException ("Unexpected end of xml reader.");
826 return resultNode != null ? resultNode : newNode;
829 private string MakeReaderErrorMessage (string message, XmlReader reader)
831 IXmlLineInfo li = reader as IXmlLineInfo;
833 return String.Format ("{0} Line number = {1}, Inline position = {2}.", li.LineNumber, li.LinePosition);
838 private XmlDocumentType ReadDoctypeNode (XmlTextReader xtReader)
840 XmlDocumentType doctype = CreateDocumentType (xtReader.Name,
841 xtReader.GetAttribute ("PUBLIC"),
842 xtReader.GetAttribute ("SYSTEM"),
844 DTDObjectModel subset = xtReader.currentSubset;
845 foreach (DTDEntityDeclaration decl in subset.EntityDecls.Values) {
846 XmlNode n = new XmlEntity (decl.Name, decl.NotationName,
847 decl.PublicId, decl.SystemId, this);
848 // FIXME: Value is more complex, similar to Attribute.
849 n.insertBeforeIntern (this.CreateTextNode (decl.EntityValue), null);
850 doctype.entities.Nodes.Add (n);
852 foreach (DTDNotationDeclaration decl in subset.NotationDecls.Values) {
853 XmlNode n = new XmlNotation (decl.LocalName, decl.Prefix,
854 decl.PublicId, decl.SystemId, this);
855 doctype.notations.Nodes.Add (n);
857 foreach (DTDElementDeclaration decl in subset.ElementDecls.Values) {
858 doctype.elementDecls.Add (decl.Name, decl.Clone ());
860 foreach (DTDAttListDeclaration decl in subset.AttListDecls.Values) {
861 doctype.attListDecls.Add (decl.Name, decl.Clone ());
866 public virtual void Save(Stream outStream)
868 XmlTextWriter xmlWriter = new XmlTextWriter (outStream, Encoding.UTF8);
869 xmlWriter.Formatting = Formatting.Indented;
870 WriteContentTo (xmlWriter);
874 public virtual void Save (string filename)
876 XmlTextWriter xmlWriter = new XmlTextWriter (filename, Encoding.UTF8);
877 xmlWriter.Formatting = Formatting.Indented;
878 WriteContentTo (xmlWriter);
883 public virtual void Save (TextWriter writer)
885 XmlTextWriter xmlWriter = new XmlTextWriter (writer);
886 xmlWriter.Formatting = Formatting.Indented;
887 WriteContentTo (xmlWriter);
891 public virtual void Save (XmlWriter xmlWriter)
894 // This should preserve white space if PreserveWhiteSpace is true
896 WriteContentTo (xmlWriter);
900 public override void WriteContentTo (XmlWriter w)
902 foreach(XmlNode childNode in ChildNodes) {
903 childNode.WriteTo (w);
907 public override void WriteTo (XmlWriter w)
912 private void AddDefaultNameTableKeys ()
914 // The following keys are default of MS .NET Framework
915 nameTable.Add ("#text");
916 nameTable.Add ("xml");
917 nameTable.Add ("xmlns");
918 nameTable.Add ("#entity");
919 nameTable.Add ("#document-fragment");
920 nameTable.Add ("#comment");
921 nameTable.Add ("space");
922 nameTable.Add ("id");
923 nameTable.Add ("#whitespace");
924 nameTable.Add ("http://www.w3.org/2000/xmlns/");
925 nameTable.Add ("#cdata-section");
926 nameTable.Add ("lang");
927 nameTable.Add ("#document");
928 nameTable.Add ("#significant-whitespace");