/// <include file='doc\XmlNode.uex' path='docs/doc[@for="XmlNode.RemoveChild"]/*' /> /// <devdoc> /// <para>Removes specified child node.</para> /// </devdoc> public virtual XmlNode RemoveChild(XmlNode oldChild) { if (!IsContainer) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Remove_Contain)); } if (oldChild.ParentNode != this) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Remove_Child)); } XmlLinkedNode oldNode = (XmlLinkedNode)oldChild; XmlNodeChangedEventArgs args = GetEventArgs(oldNode, this, null, XmlNodeChangedAction.Remove); if (args != null) { BeforeEvent(args); } if (oldNode == FirstChild) { if (LastNode.next == LastNode) { LastNode = null; } else { LastNode.next = (XmlLinkedNode)FirstChild.NextSibling; } } else { XmlLinkedNode prev = (XmlLinkedNode)oldNode.PreviousSibling; prev.next = oldNode.next; if (oldNode == LastNode) { LastNode = prev; } } oldNode.next = null; oldNode.SetParent(null); if (args != null) { AfterEvent(args); } return(oldChild); }
/// <include file='doc\XmlNode.uex' path='docs/doc[@for="XmlNode.AppendChild"]/*' /> /// <devdoc> /// <para>Adds the specified node to the end of the list of children of /// this node.</para> /// </devdoc> public virtual XmlNode AppendChild(XmlNode newChild) { XmlDocument thisDoc = OwnerDocument; if (thisDoc == null) { thisDoc = this as XmlDocument; } if (!IsContainer) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_Contain)); } if (this == newChild || AncesterNode(newChild)) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Insert_Child)); } if (newChild.ParentNode != null) { newChild.ParentNode.RemoveChild(newChild); } XmlDocument childDoc = newChild.OwnerDocument; if (childDoc != null && childDoc != thisDoc && childDoc != this) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Insert_Context)); } // special case for doc-fragment. if (newChild.NodeType == XmlNodeType.DocumentFragment) { XmlNode first = newChild.FirstChild; XmlNode node = first; while (node != null) { XmlNode next = node.NextSibling; newChild.RemoveChild(node); AppendChild(node); node = next; } return(first); } if (!(newChild is XmlLinkedNode) || !IsValidChildType(newChild.NodeType)) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_TypeConflict)); } if (!CanInsertAfter(newChild, LastChild)) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_Location)); } XmlNodeChangedEventArgs args = GetEventArgs(newChild, newChild.ParentNode, this, XmlNodeChangedAction.Insert); if (args != null) { BeforeEvent(args); } XmlLinkedNode newNode = (XmlLinkedNode)newChild; if (LastNode == null) { newNode.next = newNode; } else { newNode.next = LastNode.next; LastNode.next = newNode; } LastNode = newNode; newNode.SetParent(this); if (args != null) { AfterEvent(args); } return(newNode); }
/// <include file='doc\XmlNode.uex' path='docs/doc[@for="XmlNode.InsertBefore"]/*' /> /// <devdoc> /// <para>Inserts the specified node immediately before the specified reference node.</para> /// </devdoc> public virtual XmlNode InsertBefore(XmlNode newChild, XmlNode refChild) { if (this == newChild || AncesterNode(newChild)) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Insert_Child)); } if (refChild == null) { return(AppendChild(newChild)); } if (!IsContainer) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_Contain)); } if (refChild.ParentNode != this) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Insert_Path)); } if (newChild == refChild) { return(newChild); } XmlDocument childDoc = newChild.OwnerDocument; XmlDocument thisDoc = OwnerDocument; if (childDoc != null && childDoc != thisDoc && childDoc != this) { throw new ArgumentException(Res.GetString(Res.Xdom_Node_Insert_Context)); } if (!CanInsertBefore(newChild, refChild)) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_Location)); } if (newChild.ParentNode != null) { newChild.ParentNode.RemoveChild(newChild); } // special case for doc-fragment. if (newChild.NodeType == XmlNodeType.DocumentFragment) { XmlNode first = newChild.FirstChild; XmlNode node = first; if (node != null) { newChild.RemoveChild(node); InsertBefore(node, refChild); // insert the rest of the children after this one. InsertAfter(newChild, node); } return(first); } if (!(newChild is XmlLinkedNode) || !IsValidChildType(newChild.NodeType)) { throw new InvalidOperationException(Res.GetString(Res.Xdom_Node_Insert_TypeConflict)); } XmlLinkedNode newNode = (XmlLinkedNode)newChild; XmlLinkedNode refNode = (XmlLinkedNode)refChild; XmlNodeChangedEventArgs args = GetEventArgs(newChild, newChild.ParentNode, this, XmlNodeChangedAction.Insert); if (args != null) { BeforeEvent(args); } if (refNode == FirstChild) { newNode.next = (XmlLinkedNode)FirstChild; LastNode.next = newNode; } else { XmlLinkedNode prev = (XmlLinkedNode)refNode.PreviousSibling; newNode.next = refNode; prev.next = newNode; } newNode.SetParent(this); if (args != null) { AfterEvent(args); } return(newNode); }
// Removes specified child node. public virtual XmlNode RemoveChild(XmlNode oldChild) { if (!IsContainer) { throw new InvalidOperationException(SR.Xdom_Node_Remove_Contain); } if (oldChild.ParentNode != this) { throw new ArgumentException(SR.Xdom_Node_Remove_Child); } XmlLinkedNode oldNode = (XmlLinkedNode)oldChild; string oldNodeValue = oldNode.Value; XmlNodeChangedEventArgs args = GetEventArgs(oldNode, this, null, oldNodeValue, oldNodeValue, XmlNodeChangedAction.Remove); if (args != null) { BeforeEvent(args); } XmlLinkedNode lastNode = LastNode; if (oldNode == FirstChild) { if (oldNode == lastNode) { LastNode = null; oldNode.next = null; oldNode.SetParent(null); } else { XmlLinkedNode nextNode = oldNode.next; if (nextNode.IsText) { if (oldNode.IsText) { UnnestTextNodes(oldNode, nextNode); } } lastNode.next = nextNode; oldNode.next = null; oldNode.SetParent(null); } } else { if (oldNode == lastNode) { XmlLinkedNode prevNode = (XmlLinkedNode)oldNode.PreviousSibling; prevNode.next = oldNode.next; LastNode = prevNode; oldNode.next = null; oldNode.SetParent(null); } else { XmlLinkedNode prevNode = (XmlLinkedNode)oldNode.PreviousSibling; XmlLinkedNode nextNode = oldNode.next; if (nextNode.IsText) { if (prevNode.IsText) { NestTextNodes(prevNode, nextNode); } else { if (oldNode.IsText) { UnnestTextNodes(oldNode, nextNode); } } } prevNode.next = nextNode; oldNode.next = null; oldNode.SetParent(null); } } if (args != null) { AfterEvent(args); } return(oldChild); }
// Inserts the specified node immediately after the specified reference node. public virtual XmlNode InsertAfter(XmlNode newChild, XmlNode refChild) { if (this == newChild || AncestorNode(newChild)) { throw new ArgumentException(SR.Xdom_Node_Insert_Child); } if (refChild == null) { return(PrependChild(newChild)); } if (!IsContainer) { throw new InvalidOperationException(SR.Xdom_Node_Insert_Contain); } if (refChild.ParentNode != this) { throw new ArgumentException(SR.Xdom_Node_Insert_Path); } if (newChild == refChild) { return(newChild); } XmlDocument childDoc = newChild.OwnerDocument; XmlDocument thisDoc = OwnerDocument; if (childDoc != null && childDoc != thisDoc && childDoc != this) { throw new ArgumentException(SR.Xdom_Node_Insert_Context); } if (!CanInsertAfter(newChild, refChild)) { throw new InvalidOperationException(SR.Xdom_Node_Insert_Location); } if (newChild.ParentNode != null) { newChild.ParentNode.RemoveChild(newChild); } // special case for doc-fragment. if (newChild.NodeType == XmlNodeType.DocumentFragment) { XmlNode last = refChild; XmlNode first = newChild.FirstChild; XmlNode node = first; while (node != null) { XmlNode next = node.NextSibling; newChild.RemoveChild(node); InsertAfter(node, last); last = node; node = next; } return(first); } if (!(newChild is XmlLinkedNode) || !IsValidChildType(newChild.NodeType)) { throw new InvalidOperationException(SR.Xdom_Node_Insert_TypeConflict); } XmlLinkedNode newNode = (XmlLinkedNode)newChild; XmlLinkedNode refNode = (XmlLinkedNode)refChild; string newChildValue = newChild.Value; XmlNodeChangedEventArgs args = GetEventArgs(newChild, newChild.ParentNode, this, newChildValue, newChildValue, XmlNodeChangedAction.Insert); if (args != null) { BeforeEvent(args); } if (refNode == LastNode) { newNode.next = refNode.next; refNode.next = newNode; LastNode = newNode; newNode.SetParent(this); if (refNode.IsText) { if (newNode.IsText) { NestTextNodes(refNode, newNode); } } } else { XmlLinkedNode nextNode = refNode.next; newNode.next = nextNode; refNode.next = newNode; newNode.SetParent(this); if (refNode.IsText) { if (newNode.IsText) { NestTextNodes(refNode, newNode); if (nextNode.IsText) { NestTextNodes(newNode, nextNode); } } else { if (nextNode.IsText) { UnnestTextNodes(refNode, nextNode); } } } else { if (newNode.IsText) { if (nextNode.IsText) { NestTextNodes(newNode, nextNode); } } } } if (args != null) { AfterEvent(args); } return(newNode); }
public virtual XmlNode RemoveChild(XmlNode oldChild) { if (!this.IsContainer) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Remove_Contain")); } if (oldChild.ParentNode != this) { throw new ArgumentException(Res.GetString("Xdom_Node_Remove_Child")); } XmlLinkedNode node = (XmlLinkedNode)oldChild; string oldValue = node.Value; XmlNodeChangedEventArgs args = this.GetEventArgs(node, this, null, oldValue, oldValue, XmlNodeChangedAction.Remove); if (args != null) { this.BeforeEvent(args); } XmlLinkedNode lastNode = this.LastNode; if (node == this.FirstChild) { if (node == lastNode) { this.LastNode = null; node.next = null; node.SetParent(null); } else { XmlLinkedNode next = node.next; if (next.IsText && node.IsText) { UnnestTextNodes(node, next); } lastNode.next = next; node.next = null; node.SetParent(null); } } else if (node == lastNode) { XmlLinkedNode previousSibling = (XmlLinkedNode)node.PreviousSibling; previousSibling.next = node.next; this.LastNode = previousSibling; node.next = null; node.SetParent(null); } else { XmlLinkedNode prevNode = (XmlLinkedNode)node.PreviousSibling; XmlLinkedNode nextNode = node.next; if (nextNode.IsText) { if (prevNode.IsText) { NestTextNodes(prevNode, nextNode); } else if (node.IsText) { UnnestTextNodes(node, nextNode); } } prevNode.next = nextNode; node.next = null; node.SetParent(null); } if (args != null) { this.AfterEvent(args); } return(oldChild); }
public virtual XmlNode InsertBefore(XmlNode newChild, XmlNode refChild) { if ((this == newChild) || this.AncestorNode(newChild)) { throw new ArgumentException(Res.GetString("Xdom_Node_Insert_Child")); } if (refChild == null) { return(this.AppendChild(newChild)); } if (!this.IsContainer) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_Contain")); } if (refChild.ParentNode != this) { throw new ArgumentException(Res.GetString("Xdom_Node_Insert_Path")); } if (newChild == refChild) { return(newChild); } XmlDocument ownerDocument = newChild.OwnerDocument; XmlDocument document2 = this.OwnerDocument; if (((ownerDocument != null) && (ownerDocument != document2)) && (ownerDocument != this)) { throw new ArgumentException(Res.GetString("Xdom_Node_Insert_Context")); } if (!this.CanInsertBefore(newChild, refChild)) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_Location")); } if (newChild.ParentNode != null) { newChild.ParentNode.RemoveChild(newChild); } if (newChild.NodeType == XmlNodeType.DocumentFragment) { XmlNode firstChild = newChild.FirstChild; XmlNode oldChild = firstChild; if (oldChild != null) { newChild.RemoveChild(oldChild); this.InsertBefore(oldChild, refChild); this.InsertAfter(newChild, oldChild); } return(firstChild); } if (!(newChild is XmlLinkedNode) || !this.IsValidChildType(newChild.NodeType)) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_TypeConflict")); } XmlLinkedNode prevNode = (XmlLinkedNode)newChild; XmlLinkedNode nextNode = (XmlLinkedNode)refChild; string oldValue = newChild.Value; XmlNodeChangedEventArgs args = this.GetEventArgs(newChild, newChild.ParentNode, this, oldValue, oldValue, XmlNodeChangedAction.Insert); if (args != null) { this.BeforeEvent(args); } if (nextNode == this.FirstChild) { prevNode.next = nextNode; this.LastNode.next = prevNode; prevNode.SetParent(this); if (prevNode.IsText && nextNode.IsText) { NestTextNodes(prevNode, nextNode); } } else { XmlLinkedNode previousSibling = (XmlLinkedNode)nextNode.PreviousSibling; prevNode.next = nextNode; previousSibling.next = prevNode; prevNode.SetParent(this); if (previousSibling.IsText) { if (prevNode.IsText) { NestTextNodes(previousSibling, prevNode); if (nextNode.IsText) { NestTextNodes(prevNode, nextNode); } } else if (nextNode.IsText) { UnnestTextNodes(previousSibling, nextNode); } } else if (prevNode.IsText && nextNode.IsText) { NestTextNodes(prevNode, nextNode); } } if (args != null) { this.AfterEvent(args); } return(prevNode); }
public virtual XmlNode AppendChild(XmlNode newChild) { XmlDocument ownerDocument = this.OwnerDocument; if (ownerDocument == null) { ownerDocument = this as XmlDocument; } if (!this.IsContainer) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_Contain")); } if ((this == newChild) || this.AncestorNode(newChild)) { throw new ArgumentException(Res.GetString("Xdom_Node_Insert_Child")); } if (newChild.ParentNode != null) { newChild.ParentNode.RemoveChild(newChild); } XmlDocument document2 = newChild.OwnerDocument; if (((document2 != null) && (document2 != ownerDocument)) && (document2 != this)) { throw new ArgumentException(Res.GetString("Xdom_Node_Insert_Context")); } if (newChild.NodeType == XmlNodeType.DocumentFragment) { XmlNode nextSibling; XmlNode firstChild = newChild.FirstChild; for (XmlNode node2 = firstChild; node2 != null; node2 = nextSibling) { nextSibling = node2.NextSibling; newChild.RemoveChild(node2); this.AppendChild(node2); } return(firstChild); } if (!(newChild is XmlLinkedNode) || !this.IsValidChildType(newChild.NodeType)) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_TypeConflict")); } if (!this.CanInsertAfter(newChild, this.LastChild)) { throw new InvalidOperationException(Res.GetString("Xdom_Node_Insert_Location")); } string oldValue = newChild.Value; XmlNodeChangedEventArgs args = this.GetEventArgs(newChild, newChild.ParentNode, this, oldValue, oldValue, XmlNodeChangedAction.Insert); if (args != null) { this.BeforeEvent(args); } XmlLinkedNode lastNode = this.LastNode; XmlLinkedNode nextNode = (XmlLinkedNode)newChild; if (lastNode == null) { nextNode.next = nextNode; this.LastNode = nextNode; nextNode.SetParent(this); } else { nextNode.next = lastNode.next; lastNode.next = nextNode; this.LastNode = nextNode; nextNode.SetParent(this); if (lastNode.IsText && nextNode.IsText) { NestTextNodes(lastNode, nextNode); } } if (args != null) { this.AfterEvent(args); } return(nextNode); }