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;
38 Hashtable idTable = new Hashtable ();
44 public XmlDocument () : this (null, null)
48 protected internal XmlDocument (XmlImplementation imp) : this (imp, null)
52 public XmlDocument (XmlNameTable nt) : this (null, nt)
56 XmlDocument (XmlImplementation impl, XmlNameTable nt) : base (null)
58 implementation = (impl != null) ? impl : new XmlImplementation ();
59 nameTable = (nt != null) ? nt : implementation.internalNameTable;
60 AddDefaultNameTableKeys ();
61 resolver = new XmlUrlResolver ();
67 public event XmlNodeChangedEventHandler NodeChanged;
69 public event XmlNodeChangedEventHandler NodeChanging;
71 public event XmlNodeChangedEventHandler NodeInserted;
73 public event XmlNodeChangedEventHandler NodeInserting;
75 public event XmlNodeChangedEventHandler NodeRemoved;
77 public event XmlNodeChangedEventHandler NodeRemoving;
83 public override string BaseURI {
89 public XmlElement DocumentElement {
91 XmlNode node = FirstChild;
93 while (node != null) {
94 if (node is XmlElement)
96 node = node.NextSibling;
99 return node != null ? node as XmlElement : null;
103 public virtual XmlDocumentType DocumentType {
105 foreach(XmlNode n in this.ChildNodes) {
106 if(n.NodeType == XmlNodeType.DocumentType)
107 return (XmlDocumentType)n;
113 public XmlImplementation Implementation {
114 get { return implementation; }
117 public override string InnerXml {
119 return base.InnerXml;
121 set { // reason for overriding
122 this.LoadXml (value);
126 public override bool IsReadOnly {
127 get { return false; }
130 internal bool IsStandalone {
132 return FirstChild != null &&
133 FirstChild.NodeType == XmlNodeType.XmlDeclaration &&
134 ((XmlDeclaration) this.FirstChild).Standalone == "yes";
138 internal override XmlLinkedNode LastLinkedChild {
140 return lastLinkedChild;
144 lastLinkedChild = value;
148 public override string LocalName {
149 get { return "#document"; }
152 public override string Name {
153 get { return "#document"; }
156 public XmlNameTable NameTable {
157 get { return nameTable; }
160 public override XmlNodeType NodeType {
161 get { return XmlNodeType.Document; }
164 internal override XPathNodeType XPathNodeType {
166 return XPathNodeType.Root;
170 public override XmlDocument OwnerDocument {
174 public bool PreserveWhitespace {
175 get { return preserveWhitespace; }
176 set { preserveWhitespace = value; }
179 internal XmlResolver Resolver {
180 get { return resolver; }
183 internal override string XmlLang {
184 get { return String.Empty; }
187 public virtual XmlResolver XmlResolver {
188 set { resolver = value; }
191 internal override XmlSpace XmlSpace {
193 return XmlSpace.None;
197 internal Encoding TextEncoding {
199 XmlDeclaration dec = FirstChild as XmlDeclaration;
201 if (dec == null || dec.Encoding == "")
204 return Encoding.GetEncoding (dec.Encoding);
211 internal void AddIdenticalAttribute (XmlAttribute attr)
213 idTable [attr.Value] = attr;
216 public override XmlNode CloneNode (bool deep)
218 XmlDocument doc = implementation.CreateDocument ();
219 doc.baseURI = baseURI;
223 foreach(XmlNode n in ChildNodes)
224 doc.AppendChild (doc.ImportNode (n, deep));
229 public XmlAttribute CreateAttribute (string name)
233 string namespaceURI = String.Empty;
235 ParseName (name, out prefix, out localName);
237 if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
238 namespaceURI = XmlNamespaceManager.XmlnsXmlns;
239 else if (prefix == "xml")
240 namespaceURI = XmlNamespaceManager.XmlnsXml;
242 return CreateAttribute (prefix, localName, namespaceURI );
245 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
250 ParseName (qualifiedName, out prefix, out localName);
252 return CreateAttribute (prefix, localName, namespaceURI);
255 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
257 if ((localName == null) || (localName == String.Empty))
258 throw new ArgumentException ("The attribute local name cannot be empty.");
260 return new XmlAttribute (prefix, localName, namespaceURI, this);
263 public virtual XmlCDataSection CreateCDataSection (string data)
265 return new XmlCDataSection (data, this);
268 public virtual XmlComment CreateComment (string data)
270 return new XmlComment (data, this);
273 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
275 XmlAttribute attr = CreateAttribute (prefix, localName, namespaceURI);
276 attr.isDefault = true;
280 public virtual XmlDocumentFragment CreateDocumentFragment ()
282 return new XmlDocumentFragment (this);
285 public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
286 string systemId, string internalSubset)
288 return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
291 private XmlDocumentType CreateDocumentType (DTDObjectModel dtd)
293 return new XmlDocumentType (dtd, this);
296 public XmlElement CreateElement (string name)
298 return CreateElement (name, String.Empty);
301 public XmlElement CreateElement (
302 string qualifiedName,
308 ParseName (qualifiedName, out prefix, out localName);
310 return CreateElement (prefix, localName, namespaceURI);
313 public virtual XmlElement CreateElement (
318 if ((localName == null) || (localName == String.Empty))
319 throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
320 // FIXME: MS.NET has a weird behavior that they can Load() from XmlTextReader
321 // whose Namespaces = false, but their CreateElement() never allows qualified name.
322 // I leave it as it is.
323 if (!XmlChar.IsName (localName))
324 throw new ArgumentException ("Invalid name.", "localName");
325 return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
328 public virtual XmlEntityReference CreateEntityReference (string name)
330 return new XmlEntityReference (name, this);
333 protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
335 return new XmlDocumentNavigator (node);
338 public virtual XmlNode CreateNode (
339 string nodeTypeString,
343 return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
346 public virtual XmlNode CreateNode (
351 string prefix = null;
352 string localName = name;
354 if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
355 ParseName (name, out prefix, out localName);
357 return CreateNode (type, prefix, localName, namespaceURI);
360 public virtual XmlNode CreateNode (
367 case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
368 case XmlNodeType.CDATA: return CreateCDataSection (null);
369 case XmlNodeType.Comment: return CreateComment (null);
370 case XmlNodeType.Document: return new XmlDocument ();
371 case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
372 case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
373 case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
374 case XmlNodeType.EntityReference: return CreateEntityReference (null);
375 case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
376 case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
377 case XmlNodeType.Text: return CreateTextNode (null);
378 case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
379 case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
380 default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
381 "Specified argument was out of the range of valid values", type.ToString ()));
385 public virtual XmlProcessingInstruction CreateProcessingInstruction (
389 return new XmlProcessingInstruction (target, data, this);
392 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
394 foreach (char c in text)
395 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
396 throw new ArgumentException ("Invalid whitespace characters.");
398 return new XmlSignificantWhitespace (text, this);
401 public virtual XmlText CreateTextNode (string text)
403 return new XmlText (text, this);
406 public virtual XmlWhitespace CreateWhitespace (string text)
408 foreach (char c in text)
409 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
410 throw new ArgumentException ("Invalid whitespace characters.");
412 return new XmlWhitespace (text, this);
415 public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
418 if (version != "1.0")
419 throw new ArgumentException ("version string is not correct.");
421 if ((standalone != null && standalone != String.Empty) && !((standalone == "yes") || (standalone == "no")))
422 throw new ArgumentException ("standalone string is not correct.");
424 return new XmlDeclaration (version, encoding, standalone, this);
428 // FIXME: Currently XmlAttributeCollection.SetNamedItem() does
429 // add to the identity table, but in fact I delayed identity
430 // check on GetIdenticalAttribute. To make such way complete,
431 // we have to use MultiMap, not Hashtable.
432 public virtual XmlElement GetElementById (string elementId)
434 XmlAttribute attr = GetIdenticalAttribute (elementId);
435 return attr != null ? attr.OwnerElement : null;
438 public virtual XmlNodeList GetElementsByTagName (string name)
440 ArrayList nodeArrayList = new ArrayList ();
441 this.searchNodesRecursively (this, name, nodeArrayList);
442 return new XmlNodeArrayList (nodeArrayList);
445 private void searchNodesRecursively (XmlNode argNode, string argName,
446 ArrayList argArrayList)
448 XmlNodeList xmlNodeList = argNode.ChildNodes;
449 foreach (XmlNode node in xmlNodeList){
450 if (node.Name.Equals (argName))
451 argArrayList.Add (node);
453 this.searchNodesRecursively (node, argName, argArrayList);
457 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
458 ArrayList argArrayList)
460 XmlNodeList xmlNodeList = argNode.ChildNodes;
461 foreach (XmlNode node in xmlNodeList){
462 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
463 argArrayList.Add (node);
465 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
469 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
471 ArrayList nodeArrayList = new ArrayList ();
472 this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
473 return new XmlNodeArrayList (nodeArrayList);
476 private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
478 switch (nodeTypeString) {
479 case "attribute": return XmlNodeType.Attribute;
480 case "cdatasection": return XmlNodeType.CDATA;
481 case "comment": return XmlNodeType.Comment;
482 case "document": return XmlNodeType.Document;
483 case "documentfragment": return XmlNodeType.DocumentFragment;
484 case "documenttype": return XmlNodeType.DocumentType;
485 case "element": return XmlNodeType.Element;
486 case "entityreference": return XmlNodeType.EntityReference;
487 case "processinginstruction": return XmlNodeType.ProcessingInstruction;
488 case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
489 case "text": return XmlNodeType.Text;
490 case "whitespace": return XmlNodeType.Whitespace;
492 throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
496 internal XmlAttribute GetIdenticalAttribute (string id)
498 XmlAttribute attr = this.idTable [id] as XmlAttribute;
501 if (attr.OwnerElement == null || !attr.OwnerElement.IsRooted) {
502 // idTable.Remove (id);
508 public virtual XmlNode ImportNode (XmlNode node, bool deep)
510 switch (node.NodeType) {
511 case XmlNodeType.Attribute:
512 XmlAttribute srcAtt = node as XmlAttribute;
513 XmlAttribute dstAtt = this.CreateAttribute (srcAtt.Prefix, srcAtt.LocalName, srcAtt.NamespaceURI);
514 foreach (XmlNode child in srcAtt.ChildNodes)
515 dstAtt.AppendChild (this.ImportNode (child, deep));
518 case XmlNodeType.CDATA:
519 return this.CreateCDataSection (node.Value);
521 case XmlNodeType.Comment:
522 return this.CreateComment (node.Value);
524 case XmlNodeType.Document:
525 throw new XmlException ("Document cannot be imported.");
527 case XmlNodeType.DocumentFragment:
528 XmlDocumentFragment df = this.CreateDocumentFragment ();
530 foreach(XmlNode n in node.ChildNodes)
531 df.AppendChild (this.ImportNode (n, deep));
534 case XmlNodeType.DocumentType:
535 throw new XmlException ("DocumentType cannot be imported.");
537 case XmlNodeType.Element:
538 XmlElement src = (XmlElement)node;
539 XmlElement dst = this.CreateElement (src.Prefix, src.LocalName, src.NamespaceURI);
540 foreach(XmlAttribute attr in src.Attributes)
541 if(attr.Specified) // copies only specified attributes
542 dst.SetAttributeNode ((XmlAttribute)this.ImportNode (attr, deep));
544 foreach(XmlNode n in src.ChildNodes)
545 dst.AppendChild (this.ImportNode (n, deep));
548 case XmlNodeType.EndElement:
549 throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
550 case XmlNodeType.EndEntity:
551 throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
553 case XmlNodeType.EntityReference:
554 return this.CreateEntityReference (node.Name);
556 case XmlNodeType.None:
557 throw new XmlException ("Illegal ImportNode call for NodeType.None");
559 case XmlNodeType.ProcessingInstruction:
560 XmlProcessingInstruction pi = node as XmlProcessingInstruction;
561 return this.CreateProcessingInstruction (pi.Target, pi.Data);
563 case XmlNodeType.SignificantWhitespace:
564 return this.CreateSignificantWhitespace (node.Value);
566 case XmlNodeType.Text:
567 return this.CreateTextNode (node.Value);
569 case XmlNodeType.Whitespace:
570 return this.CreateWhitespace (node.Value);
572 case XmlNodeType.XmlDeclaration:
573 XmlDeclaration srcDecl = node as XmlDeclaration;
574 return this.CreateXmlDeclaration (srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone);
577 throw new InvalidOperationException ("Cannot import specified node type: " + node.NodeType);
581 public virtual void Load (Stream inStream)
583 Load (new XmlTextReader (inStream));
586 public virtual void Load (string filename)
588 XmlTextReader xr = new XmlTextReader (filename);
589 xr.XmlResolver = resolver;
594 public virtual void Load (TextReader txtReader)
596 XmlTextReader xr = new XmlTextReader (txtReader);
597 xr.XmlResolver = resolver;
601 public virtual void Load (XmlReader xmlReader)
603 // Reset our document
604 // For now this just means removing all our children but later this
605 // may turn out o need to call a private method that resets other things
606 // like properties we have, etc.
609 this.baseURI = xmlReader.BaseURI;
610 // create all contents with use of ReadNode()
612 XmlNode n = ReadNode (xmlReader);
618 public virtual void LoadXml (string xml)
620 XmlTextReader xmlReader = new XmlTextReader (
621 xml, XmlNodeType.Document, null);
622 xmlReader.XmlResolver = resolver;
626 internal void onNodeChanged (XmlNode node, XmlNode Parent)
628 if (NodeChanged != null)
629 NodeChanged (node, new XmlNodeChangedEventArgs
630 (XmlNodeChangedAction.Change,
631 node, Parent, Parent));
634 internal void onNodeChanging(XmlNode node, XmlNode Parent)
636 if (NodeChanging != null)
637 NodeChanging (node, new XmlNodeChangedEventArgs
638 (XmlNodeChangedAction.Change,
639 node, Parent, Parent));
642 internal void onNodeInserted (XmlNode node, XmlNode newParent)
644 if (NodeInserted != null)
645 NodeInserted (node, new XmlNodeChangedEventArgs
646 (XmlNodeChangedAction.Insert,
647 node, null, newParent));
650 internal void onNodeInserting (XmlNode node, XmlNode newParent)
652 if (NodeInserting != null)
653 NodeInserting (node, new XmlNodeChangedEventArgs
654 (XmlNodeChangedAction.Insert,
655 node, null, newParent));
658 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
660 if (NodeRemoved != null)
661 NodeRemoved (node, new XmlNodeChangedEventArgs
662 (XmlNodeChangedAction.Remove,
663 node, oldParent, null));
666 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
668 if (NodeRemoving != null)
669 NodeRemoving (node, new XmlNodeChangedEventArgs
670 (XmlNodeChangedAction.Remove,
671 node, oldParent, null));
674 private void ParseName (string name, out string prefix, out string localName)
676 int indexOfColon = name.IndexOf (':');
678 if (indexOfColon != -1) {
679 prefix = name.Substring (0, indexOfColon);
680 localName = name.Substring (indexOfColon + 1);
687 // Reads XmlReader and creates Attribute Node.
688 private XmlAttribute ReadAttributeNode(XmlReader reader)
690 if(reader.NodeType == XmlNodeType.Element)
691 reader.MoveToFirstAttribute ();
692 else if(reader.NodeType != XmlNodeType.Attribute)
693 throw new InvalidOperationException (MakeReaderErrorMessage ("bad position to read attribute.", reader));
694 XmlAttribute attribute = CreateAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
695 ReadAttributeNodeValue (reader, attribute);
697 // Keep the current reader position
699 if (attribute.NamespaceURI == string.Empty || attribute.NamespaceURI == null)
700 res = reader.MoveToAttribute (attribute.Name);
702 res = reader.MoveToAttribute (attribute.LocalName, attribute.NamespaceURI);
703 if (reader.IsDefault)
704 attribute.SetDefault ();
708 // Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this.
709 internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute)
711 while(reader.ReadAttributeValue ()) {
712 if(reader.NodeType == XmlNodeType.EntityReference)
713 // FIXME: if DocumentType is available, then try to resolve it.
714 attribute.AppendChild (CreateEntityReference (reader.Name));
715 // FIXME: else if(NodeType == EndEntity) -- reset BaseURI and so on -- ;
717 // Children of Attribute is restricted to CharacterData and EntityReference (Comment is not allowed).
718 attribute.AppendChild (CreateTextNode (reader.Value));
722 public virtual XmlNode ReadNode (XmlReader reader)
724 XmlNode resultNode = null;
725 XmlNode newNode = null;
726 XmlNode currentNode = null;
728 switch (reader.ReadState) {
729 case ReadState.Interactive:
731 case ReadState.Initial:
738 int startDepth = reader.Depth;
739 bool ignoredWhitespace;
740 bool reachedEOF = false;
743 ignoredWhitespace = false;
744 if (reader.ReadState != ReadState.Interactive)
746 throw new Exception ("XML Reader reached to end while reading node.");
749 switch (reader.NodeType) {
751 case XmlNodeType.Attribute:
752 newNode = ReadAttributeNode (reader);
755 case XmlNodeType.CDATA:
756 newNode = CreateCDataSection (reader.Value);
757 if(currentNode != null)
758 currentNode.AppendChild (newNode);
761 case XmlNodeType.Comment:
762 newNode = CreateComment (reader.Value);
763 if(currentNode != null)
764 currentNode.AppendChild (newNode);
767 case XmlNodeType.Element:
768 XmlElement element = CreateElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
769 element.IsEmpty = reader.IsEmptyElement;
771 // set the element's attributes.
772 while (reader.MoveToNextAttribute ()) {
773 element.SetAttributeNode (ReadAttributeNode (reader));
776 reader.MoveToElement ();
778 // MS.NET adds element to document after its attributes are filled.
779 if(currentNode != null)
780 currentNode.AppendChild (element);
782 resultNode = element;
784 if (!reader.IsEmptyElement)
785 currentNode = element;
789 case XmlNodeType.EndElement:
790 if (currentNode == null)
791 throw new XmlException ("Unexpected end element.");
792 else if (currentNode.Name != reader.Name)
793 throw new XmlException (reader as IXmlLineInfo, String.Format ("mismatch end tag. Expected {0} but found {1}", currentNode.Name, reader.Name));
794 currentNode = currentNode.ParentNode;
797 case XmlNodeType.EndEntity:
798 break; // no operation
800 case XmlNodeType.ProcessingInstruction:
801 newNode = CreateProcessingInstruction (reader.Name, reader.Value);
802 if(currentNode != null)
803 currentNode.AppendChild (newNode);
806 case XmlNodeType.Text:
807 newNode = CreateTextNode (reader.Value);
808 if(currentNode != null)
809 currentNode.AppendChild (newNode);
812 case XmlNodeType.XmlDeclaration:
813 // empty strings are dummy, then gives over setting value contents to setter.
814 newNode = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
815 ((XmlDeclaration)newNode).Value = reader.Value;
816 if(currentNode != null)
817 throw new XmlException (reader as IXmlLineInfo, "XmlDeclaration at invalid position.");
820 case XmlNodeType.DocumentType:
821 if(currentNode != null)
822 throw new XmlException (reader as IXmlLineInfo, "XmlDocumentType at invalid position.");
824 DTDObjectModel dtd = null;
825 XmlTextReader xtReader = reader as XmlTextReader;
826 if (xtReader != null)
828 XmlNodeReader xnReader = reader as XmlNodeReader;
829 if (xnReader != null)
830 dtd = xnReader.GetInternalParserContext ().Dtd;
831 XmlValidatingReader xvReader = reader as XmlValidatingReader;
832 if (xvReader != null)
833 dtd = xvReader.GetInternalParserContext ().Dtd;
834 IHasXmlParserContext ctxReader = reader as IHasXmlParserContext;
835 if (ctxReader != null)
836 dtd = ctxReader.ParserContext.Dtd;
839 newNode = CreateDocumentType (dtd);
841 newNode = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
845 case XmlNodeType.EntityReference:
846 newNode = CreateEntityReference (reader.Name);
847 if(currentNode != null)
848 currentNode.AppendChild (newNode);
851 case XmlNodeType.SignificantWhitespace:
852 newNode = CreateSignificantWhitespace (reader.Value);
853 if(currentNode != null)
854 currentNode.AppendChild (newNode);
857 case XmlNodeType.Whitespace:
858 if(PreserveWhitespace) {
859 newNode = CreateWhitespace (reader.Value);
860 if(currentNode != null)
861 currentNode.AppendChild (newNode);
864 ignoredWhitespace = true;
867 // Read next, except for reading attribute node.
868 if (!(newNode is XmlAttribute) && !reader.Read ())
870 } while (ignoredWhitespace || reader.Depth > startDepth ||
871 (reader.Depth == startDepth && reader.NodeType == XmlNodeType.EndElement));
872 if (startDepth < reader.Depth && reader.EOF)
873 throw new XmlException ("Unexpected end of xml reader.");
874 return resultNode != null ? resultNode : newNode;
877 private string MakeReaderErrorMessage (string message, XmlReader reader)
879 IXmlLineInfo li = reader as IXmlLineInfo;
881 return String.Format ("{0} Line number = {1}, Inline position = {2}.", message, li.LineNumber, li.LinePosition);
886 internal void RemoveIdenticalAttribute (string id)
891 public virtual void Save(Stream outStream)
893 XmlTextWriter xmlWriter = new XmlTextWriter (outStream, TextEncoding);
894 xmlWriter.Formatting = Formatting.Indented;
895 WriteContentTo (xmlWriter);
899 public virtual void Save (string filename)
901 XmlTextWriter xmlWriter = new XmlTextWriter (filename, TextEncoding);
902 xmlWriter.Formatting = Formatting.Indented;
903 WriteContentTo (xmlWriter);
907 public virtual void Save (TextWriter writer)
909 XmlTextWriter xmlWriter = new XmlTextWriter (writer);
910 xmlWriter.Formatting = Formatting.Indented;
911 WriteContentTo (xmlWriter);
915 public virtual void Save (XmlWriter xmlWriter)
918 // This should preserve white space if PreserveWhiteSpace is true
920 bool autoXmlDecl = FirstChild != null && FirstChild.NodeType != XmlNodeType.XmlDeclaration;
922 xmlWriter.WriteStartDocument ();
923 WriteContentTo (xmlWriter);
925 xmlWriter.WriteEndDocument ();
929 public override void WriteContentTo (XmlWriter w)
931 foreach(XmlNode childNode in ChildNodes) {
932 childNode.WriteTo (w);
936 public override void WriteTo (XmlWriter w)
941 private void AddDefaultNameTableKeys ()
943 // The following keys are default of MS .NET Framework
944 nameTable.Add ("#text");
945 nameTable.Add ("xml");
946 nameTable.Add ("xmlns");
947 nameTable.Add ("#entity");
948 nameTable.Add ("#document-fragment");
949 nameTable.Add ("#comment");
950 nameTable.Add ("space");
951 nameTable.Add ("id");
952 nameTable.Add ("#whitespace");
953 nameTable.Add ("http://www.w3.org/2000/xmlns/");
954 nameTable.Add ("#cdata-section");
955 nameTable.Add ("lang");
956 nameTable.Add ("#document");
957 nameTable.Add ("#significant-whitespace");