XNode.ReplaceWith Method


Replaces this node with the specified content.

Namespace:   System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)


Replaces this node with the specified content.


Replaces this node with the specified content.

For details about the valid content that can be passed to this method, see Valid Content of XElement and XDocument Objects.

This method will raise the Changed and the Changing events.

The XContainer stores its child nodes as a singly-linked list of XNode objects. This means that the ReplaceWith method must traverse the list of direct child nodes under the parent container. Therefore, using this method might affect your performance.

The following example uses this method to replace the contents of a node with different content.

                XElement xmlTree = new XElement("Root",
    new XElement("Child1", "child1 content"),
    new XElement("Child2", "child2 content"),
    new XElement("Child3", "child3 content"),
    new XElement("Child4", "child4 content"),
    new XElement("Child5", "child5 content")
XElement child3 = xmlTree.Element("Child3");
    new XElement("NewChild", "new content")

This example produces the following output:

  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
Return to top