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;
221 doc.PreserveWhitespace = PreserveWhitespace; // required?
224 foreach(XmlNode n in ChildNodes)
225 doc.AppendChild (doc.ImportNode (n, deep));
230 public XmlAttribute CreateAttribute (string name)
232 return CreateAttribute (name,
233 name == "xmlns" ? "http://www.w3.org/2000/xmlns/" : String.Empty);
236 public XmlAttribute CreateAttribute (string qualifiedName, string namespaceURI)
241 ParseName (qualifiedName, out prefix, out localName);
243 return CreateAttribute (prefix, localName, namespaceURI);
246 public virtual XmlAttribute CreateAttribute (string prefix, string localName, string namespaceURI)
248 if ((localName == null) || (localName == String.Empty))
249 throw new ArgumentException ("The attribute local name cannot be empty.");
251 return new XmlAttribute (prefix, localName, namespaceURI, this);
254 public virtual XmlCDataSection CreateCDataSection (string data)
256 return new XmlCDataSection (data, this);
259 public virtual XmlComment CreateComment (string data)
261 return new XmlComment (data, this);
264 protected internal virtual XmlAttribute CreateDefaultAttribute (string prefix, string localName, string namespaceURI)
266 XmlAttribute attr = CreateAttribute (prefix, localName, namespaceURI);
267 attr.isDefault = true;
271 public virtual XmlDocumentFragment CreateDocumentFragment ()
273 return new XmlDocumentFragment (this);
276 public virtual XmlDocumentType CreateDocumentType (string name, string publicId,
277 string systemId, string internalSubset)
279 return new XmlDocumentType (name, publicId, systemId, internalSubset, this);
282 private XmlDocumentType CreateDocumentType (DTDObjectModel dtd)
284 return new XmlDocumentType (dtd, this);
287 public XmlElement CreateElement (string name)
289 return CreateElement (name, String.Empty);
292 public XmlElement CreateElement (
293 string qualifiedName,
299 ParseName (qualifiedName, out prefix, out localName);
301 return CreateElement (prefix, localName, namespaceURI);
304 public virtual XmlElement CreateElement (
309 if ((localName == null) || (localName == String.Empty))
310 throw new ArgumentException ("The local name for elements or attributes cannot be null or an empty string.");
311 CheckName (localName);
312 return new XmlElement (prefix != null ? prefix : String.Empty, localName, namespaceURI != null ? namespaceURI : String.Empty, this);
315 public virtual XmlEntityReference CreateEntityReference (string name)
317 return new XmlEntityReference (name, this);
320 protected internal virtual XPathNavigator CreateNavigator (XmlNode node)
322 return new XmlDocumentNavigator (node);
325 public virtual XmlNode CreateNode (
326 string nodeTypeString,
330 return CreateNode (GetNodeTypeFromString (nodeTypeString), name, namespaceURI);
333 public virtual XmlNode CreateNode (
338 string prefix = null;
339 string localName = name;
341 if ((type == XmlNodeType.Attribute) || (type == XmlNodeType.Element) || (type == XmlNodeType.EntityReference))
342 ParseName (name, out prefix, out localName);
344 return CreateNode (type, prefix, localName, namespaceURI);
347 public virtual XmlNode CreateNode (
354 case XmlNodeType.Attribute: return CreateAttribute (prefix, name, namespaceURI);
355 case XmlNodeType.CDATA: return CreateCDataSection (null);
356 case XmlNodeType.Comment: return CreateComment (null);
357 case XmlNodeType.Document: return new XmlDocument (); // TODO - test to see which constructor to use, i.e. use existing NameTable or not.
358 case XmlNodeType.DocumentFragment: return CreateDocumentFragment ();
359 case XmlNodeType.DocumentType: return CreateDocumentType (null, null, null, null);
360 case XmlNodeType.Element: return CreateElement (prefix, name, namespaceURI);
361 case XmlNodeType.EntityReference: return CreateEntityReference (null);
362 case XmlNodeType.ProcessingInstruction: return CreateProcessingInstruction (null, null);
363 case XmlNodeType.SignificantWhitespace: return CreateSignificantWhitespace (String.Empty);
364 case XmlNodeType.Text: return CreateTextNode (null);
365 case XmlNodeType.Whitespace: return CreateWhitespace (String.Empty);
366 case XmlNodeType.XmlDeclaration: return CreateXmlDeclaration ("1.0", null, null);
367 default: throw new ArgumentOutOfRangeException(String.Format("{0}\nParameter name: {1}",
368 "Specified argument was out of the range of valid values", type.ToString ()));
372 public virtual XmlProcessingInstruction CreateProcessingInstruction (
376 return new XmlProcessingInstruction (target, data, this);
379 public virtual XmlSignificantWhitespace CreateSignificantWhitespace (string text)
381 foreach (char c in text)
382 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
383 throw new ArgumentException ("Invalid whitespace characters.");
385 return new XmlSignificantWhitespace (text, this);
388 public virtual XmlText CreateTextNode (string text)
390 return new XmlText (text, this);
393 public virtual XmlWhitespace CreateWhitespace (string text)
395 foreach (char c in text)
396 if ((c != ' ') && (c != '\r') && (c != '\n') && (c != '\t'))
397 throw new ArgumentException ("Invalid whitespace characters.");
399 return new XmlWhitespace (text, this);
402 public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding,
405 if (version != "1.0")
406 throw new ArgumentException ("version string is not correct.");
408 if ((standalone != null && standalone != String.Empty) && !((standalone == "yes") || (standalone == "no")))
409 throw new ArgumentException ("standalone string is not correct.");
411 return new XmlDeclaration (version, encoding, standalone, this);
415 // FIXME: Currently XmlAttributeCollection.SetNamedItem() does
416 // add to the identity table, but in fact I delayed identity
417 // check on GetIdenticalAttribute. To make such way complete,
418 // we have to use MultiMap, not Hashtable.
419 public virtual XmlElement GetElementById (string elementId)
421 XmlAttribute attr = GetIdenticalAttribute (elementId);
422 return attr != null ? attr.OwnerElement : null;
425 public virtual XmlNodeList GetElementsByTagName (string name)
427 ArrayList nodeArrayList = new ArrayList ();
428 this.searchNodesRecursively (this, name, nodeArrayList);
429 return new XmlNodeArrayList (nodeArrayList);
432 private void searchNodesRecursively (XmlNode argNode, string argName,
433 ArrayList argArrayList)
435 XmlNodeList xmlNodeList = argNode.ChildNodes;
436 foreach (XmlNode node in xmlNodeList){
437 if (node.Name.Equals (argName))
438 argArrayList.Add (node);
440 this.searchNodesRecursively (node, argName, argArrayList);
444 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
445 ArrayList argArrayList)
447 XmlNodeList xmlNodeList = argNode.ChildNodes;
448 foreach (XmlNode node in xmlNodeList){
449 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
450 argArrayList.Add (node);
452 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
456 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
458 ArrayList nodeArrayList = new ArrayList ();
459 this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
460 return new XmlNodeArrayList (nodeArrayList);
463 private XmlNodeType GetNodeTypeFromString (string nodeTypeString)
465 switch (nodeTypeString) {
466 case "attribute": return XmlNodeType.Attribute;
467 case "cdatasection": return XmlNodeType.CDATA;
468 case "comment": return XmlNodeType.Comment;
469 case "document": return XmlNodeType.Document;
470 case "documentfragment": return XmlNodeType.DocumentFragment;
471 case "documenttype": return XmlNodeType.DocumentType;
472 case "element": return XmlNodeType.Element;
473 case "entityreference": return XmlNodeType.EntityReference;
474 case "processinginstruction": return XmlNodeType.ProcessingInstruction;
475 case "significantwhitespace": return XmlNodeType.SignificantWhitespace;
476 case "text": return XmlNodeType.Text;
477 case "whitespace": return XmlNodeType.Whitespace;
479 throw new ArgumentException(String.Format("The string doesn't represent any node type : {0}.", nodeTypeString));
483 internal XmlAttribute GetIdenticalAttribute (string id)
485 XmlAttribute attr = this.idTable [id] as XmlAttribute;
488 if (attr.OwnerElement == null || !attr.OwnerElement.IsRooted) {
489 // idTable.Remove (id);
495 public virtual XmlNode ImportNode (XmlNode node, bool deep)
497 switch (node.NodeType) {
498 case XmlNodeType.Attribute:
499 XmlAttribute srcAtt = node as XmlAttribute;
500 XmlAttribute dstAtt = this.CreateAttribute (srcAtt.Prefix, srcAtt.LocalName, srcAtt.NamespaceURI);
501 foreach (XmlNode child in srcAtt.ChildNodes)
502 dstAtt.AppendChild (this.ImportNode (child, deep));
505 case XmlNodeType.CDATA:
506 return this.CreateCDataSection (node.Value);
508 case XmlNodeType.Comment:
509 return this.CreateComment (node.Value);
511 case XmlNodeType.Document:
512 throw new XmlException ("Document cannot be imported.");
514 case XmlNodeType.DocumentFragment:
515 XmlDocumentFragment df = this.CreateDocumentFragment ();
517 foreach(XmlNode n in node.ChildNodes)
518 df.AppendChild (this.ImportNode (n, deep));
521 case XmlNodeType.DocumentType:
522 throw new XmlException ("DocumentType cannot be imported.");
524 case XmlNodeType.Element:
525 XmlElement src = (XmlElement)node;
526 XmlElement dst = this.CreateElement (src.Prefix, src.LocalName, src.NamespaceURI);
527 foreach(XmlAttribute attr in src.Attributes)
528 if(attr.Specified) // copies only specified attributes
529 dst.SetAttributeNode ((XmlAttribute)this.ImportNode (attr, deep));
531 foreach(XmlNode n in src.ChildNodes)
532 dst.AppendChild (this.ImportNode (n, deep));
535 case XmlNodeType.EndElement:
536 throw new XmlException ("Illegal ImportNode call for NodeType.EndElement");
537 case XmlNodeType.EndEntity:
538 throw new XmlException ("Illegal ImportNode call for NodeType.EndEntity");
540 case XmlNodeType.EntityReference:
541 return this.CreateEntityReference (node.Name);
543 case XmlNodeType.None:
544 throw new XmlException ("Illegal ImportNode call for NodeType.None");
546 case XmlNodeType.ProcessingInstruction:
547 XmlProcessingInstruction pi = node as XmlProcessingInstruction;
548 return this.CreateProcessingInstruction (pi.Target, pi.Data);
550 case XmlNodeType.SignificantWhitespace:
551 return this.CreateSignificantWhitespace (node.Value);
553 case XmlNodeType.Text:
554 return this.CreateTextNode (node.Value);
556 case XmlNodeType.Whitespace:
557 return this.CreateWhitespace (node.Value);
559 case XmlNodeType.XmlDeclaration:
560 XmlDeclaration srcDecl = node as XmlDeclaration;
561 return this.CreateXmlDeclaration (srcDecl.Version, srcDecl.Encoding, srcDecl.Standalone);
564 throw new InvalidOperationException ("Cannot import specified node type: " + node.NodeType);
568 public virtual void Load (Stream inStream)
570 Load (new XmlTextReader (inStream));
573 public virtual void Load (string filename)
575 XmlTextReader xr = new XmlTextReader (filename);
576 xr.XmlResolver = resolver;
581 public virtual void Load (TextReader txtReader)
583 XmlTextReader xr = new XmlTextReader (txtReader);
584 xr.XmlResolver = resolver;
588 public virtual void Load (XmlReader xmlReader)
590 // Reset our document
591 // For now this just means removing all our children but later this
592 // may turn out o need to call a private method that resets other things
593 // like properties we have, etc.
596 this.baseURI = xmlReader.BaseURI;
597 // create all contents with use of ReadNode()
599 XmlNode n = ReadNode (xmlReader);
605 public virtual void LoadXml (string xml)
607 XmlTextReader xmlReader = new XmlTextReader (
608 xml, XmlNodeType.Document, null);
609 xmlReader.XmlResolver = resolver;
613 internal void onNodeChanged (XmlNode node, XmlNode Parent)
615 if (NodeChanged != null)
616 NodeChanged (node, new XmlNodeChangedEventArgs
617 (XmlNodeChangedAction.Change,
618 node, Parent, Parent));
621 internal void onNodeChanging(XmlNode node, XmlNode Parent)
623 if (NodeChanging != null)
624 NodeChanging (node, new XmlNodeChangedEventArgs
625 (XmlNodeChangedAction.Change,
626 node, Parent, Parent));
629 internal void onNodeInserted (XmlNode node, XmlNode newParent)
631 if (NodeInserted != null)
632 NodeInserted (node, new XmlNodeChangedEventArgs
633 (XmlNodeChangedAction.Insert,
634 node, null, newParent));
637 internal void onNodeInserting (XmlNode node, XmlNode newParent)
639 if (NodeInserting != null)
640 NodeInserting (node, new XmlNodeChangedEventArgs
641 (XmlNodeChangedAction.Insert,
642 node, null, newParent));
645 internal void onNodeRemoved (XmlNode node, XmlNode oldParent)
647 if (NodeRemoved != null)
648 NodeRemoved (node, new XmlNodeChangedEventArgs
649 (XmlNodeChangedAction.Remove,
650 node, oldParent, null));
653 internal void onNodeRemoving (XmlNode node, XmlNode oldParent)
655 if (NodeRemoving != null)
656 NodeRemoving (node, new XmlNodeChangedEventArgs
657 (XmlNodeChangedAction.Remove,
658 node, oldParent, null));
661 private void ParseName (string name, out string prefix, out string localName)
663 int indexOfColon = name.IndexOf (':');
665 if (indexOfColon != -1) {
666 prefix = name.Substring (0, indexOfColon);
667 localName = name.Substring (indexOfColon + 1);
674 // Checks that Element's name is valid
675 private void CheckName (String name)
677 // TODO: others validations?
678 if (name.IndexOf (" ") >= 0)
679 throw new XmlException ("The ' ' characted cannot be included in a name");
682 // Reads XmlReader and creates Attribute Node.
683 private XmlAttribute ReadAttributeNode(XmlReader reader)
685 if(reader.NodeType == XmlNodeType.Element)
686 reader.MoveToFirstAttribute ();
687 else if(reader.NodeType != XmlNodeType.Attribute)
688 throw new InvalidOperationException (MakeReaderErrorMessage ("bad position to read attribute.", reader));
689 XmlAttribute attribute = CreateAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
690 ReadAttributeNodeValue (reader, attribute);
692 // Keep the current reader position
694 if (attribute.NamespaceURI == string.Empty || attribute.NamespaceURI == null)
695 res = reader.MoveToAttribute (attribute.Name);
697 res = reader.MoveToAttribute (attribute.LocalName, attribute.NamespaceURI);
698 if (reader.IsDefault)
699 attribute.SetDefault ();
703 // Reads attribute from XmlReader and then creates attribute value children. XmlAttribute also uses this.
704 internal void ReadAttributeNodeValue(XmlReader reader, XmlAttribute attribute)
706 while(reader.ReadAttributeValue ()) {
707 if(reader.NodeType == XmlNodeType.EntityReference)
708 // FIXME: if DocumentType is available, then try to resolve it.
709 attribute.AppendChild (CreateEntityReference (reader.Name));
710 // FIXME: else if(NodeType == EndEntity) -- reset BaseURI and so on -- ;
712 // Children of Attribute is restricted to CharacterData and EntityReference (Comment is not allowed).
713 attribute.AppendChild (CreateTextNode (reader.Value));
717 public virtual XmlNode ReadNode (XmlReader reader)
719 XmlNode resultNode = null;
720 XmlNode newNode = null;
721 XmlNode currentNode = null;
723 switch (reader.ReadState) {
724 case ReadState.Interactive:
726 case ReadState.Initial:
733 int startDepth = reader.Depth;
734 bool ignoredWhitespace;
735 bool reachedEOF = false;
738 ignoredWhitespace = false;
739 if (reader.ReadState != ReadState.Interactive)
741 throw new Exception ("XML Reader reached to end while reading node.");
744 switch (reader.NodeType) {
746 case XmlNodeType.Attribute:
747 newNode = ReadAttributeNode (reader);
750 case XmlNodeType.CDATA:
751 newNode = CreateCDataSection (reader.Value);
752 if(currentNode != null)
753 currentNode.AppendChild (newNode);
756 case XmlNodeType.Comment:
757 newNode = CreateComment (reader.Value);
758 if(currentNode != null)
759 currentNode.AppendChild (newNode);
762 case XmlNodeType.Element:
763 XmlElement element = CreateElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
764 element.IsEmpty = reader.IsEmptyElement;
765 if(currentNode != null)
766 currentNode.AppendChild (element);
768 resultNode = element;
770 // set the element's attributes.
771 while (reader.MoveToNextAttribute ()) {
772 element.SetAttributeNode (ReadAttributeNode (reader));
775 reader.MoveToElement ();
777 if (!reader.IsEmptyElement)
778 currentNode = element;
782 case XmlNodeType.EndElement:
783 if (currentNode == null)
784 throw new XmlException ("Unexpected end element.");
785 else if (currentNode.Name != reader.Name)
786 throw new XmlException (reader as IXmlLineInfo, String.Format ("mismatch end tag. Expected {0} but found {1}", currentNode.Name, reader.Name));
787 currentNode = currentNode.ParentNode;
790 case XmlNodeType.EndEntity:
791 break; // no operation
793 case XmlNodeType.ProcessingInstruction:
794 newNode = CreateProcessingInstruction (reader.Name, reader.Value);
795 if(currentNode != null)
796 currentNode.AppendChild (newNode);
799 case XmlNodeType.Text:
800 newNode = CreateTextNode (reader.Value);
801 if(currentNode != null)
802 currentNode.AppendChild (newNode);
805 case XmlNodeType.XmlDeclaration:
806 // empty strings are dummy, then gives over setting value contents to setter.
807 newNode = CreateXmlDeclaration ("1.0" , String.Empty, String.Empty);
808 ((XmlDeclaration)newNode).Value = reader.Value;
809 if(currentNode != null)
810 throw new XmlException (reader as IXmlLineInfo, "XmlDeclaration at invalid position.");
813 case XmlNodeType.DocumentType:
814 if(currentNode != null)
815 throw new XmlException (reader as IXmlLineInfo, "XmlDocumentType at invalid position.");
817 DTDObjectModel dtd = null;
818 XmlTextReader xtReader = reader as XmlTextReader;
819 if (xtReader != null)
821 XmlNodeReader xnReader = reader as XmlNodeReader;
822 if (xnReader != null)
823 dtd = xnReader.GetInternalParserContext ().Dtd;
824 XmlValidatingReader xvReader = reader as XmlValidatingReader;
825 if (xvReader != null)
826 dtd = xvReader.GetInternalParserContext ().Dtd;
827 IHasXmlParserContext ctxReader = reader as IHasXmlParserContext;
828 if (ctxReader != null)
829 dtd = ctxReader.ParserContext.Dtd;
832 newNode = CreateDocumentType (dtd);
834 newNode = CreateDocumentType (reader.Name, reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
838 case XmlNodeType.EntityReference:
839 newNode = CreateEntityReference (reader.Name);
840 if(currentNode != null)
841 currentNode.AppendChild (newNode);
844 case XmlNodeType.SignificantWhitespace:
845 newNode = CreateSignificantWhitespace (reader.Value);
846 if(currentNode != null)
847 currentNode.AppendChild (newNode);
850 case XmlNodeType.Whitespace:
851 if(PreserveWhitespace) {
852 newNode = CreateWhitespace (reader.Value);
853 if(currentNode != null)
854 currentNode.AppendChild (newNode);
857 ignoredWhitespace = true;
860 // Read next, except for reading attribute node.
861 if (!(newNode is XmlAttribute) && !reader.Read ())
863 } while (ignoredWhitespace || reader.Depth > startDepth ||
864 (reader.Depth == startDepth && reader.NodeType == XmlNodeType.EndElement));
865 if (startDepth != reader.Depth && reader.EOF)
866 throw new XmlException ("Unexpected end of xml reader.");
867 return resultNode != null ? resultNode : newNode;
870 private string MakeReaderErrorMessage (string message, XmlReader reader)
872 IXmlLineInfo li = reader as IXmlLineInfo;
874 return String.Format ("{0} Line number = {1}, Inline position = {2}.", message, li.LineNumber, li.LinePosition);
879 internal void RemoveIdenticalAttribute (string id)
884 public virtual void Save(Stream outStream)
886 XmlTextWriter xmlWriter = new XmlTextWriter (outStream, TextEncoding);
887 xmlWriter.Formatting = Formatting.Indented;
888 WriteContentTo (xmlWriter);
892 public virtual void Save (string filename)
894 XmlTextWriter xmlWriter = new XmlTextWriter (filename, TextEncoding);
895 xmlWriter.Formatting = Formatting.Indented;
896 WriteContentTo (xmlWriter);
900 public virtual void Save (TextWriter writer)
902 XmlTextWriter xmlWriter = new XmlTextWriter (writer);
903 xmlWriter.Formatting = Formatting.Indented;
904 WriteContentTo (xmlWriter);
908 public virtual void Save (XmlWriter xmlWriter)
911 // This should preserve white space if PreserveWhiteSpace is true
913 WriteContentTo (xmlWriter);
917 public override void WriteContentTo (XmlWriter w)
919 foreach(XmlNode childNode in ChildNodes) {
920 childNode.WriteTo (w);
924 public override void WriteTo (XmlWriter w)
929 private void AddDefaultNameTableKeys ()
931 // The following keys are default of MS .NET Framework
932 nameTable.Add ("#text");
933 nameTable.Add ("xml");
934 nameTable.Add ("xmlns");
935 nameTable.Add ("#entity");
936 nameTable.Add ("#document-fragment");
937 nameTable.Add ("#comment");
938 nameTable.Add ("space");
939 nameTable.Add ("id");
940 nameTable.Add ("#whitespace");
941 nameTable.Add ("http://www.w3.org/2000/xmlns/");
942 nameTable.Add ("#cdata-section");
943 nameTable.Add ("lang");
944 nameTable.Add ("#document");
945 nameTable.Add ("#significant-whitespace");