ReplaceWith Method

XNode.ReplaceWith Method

 

Replaces this node with the specified content.

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

NameDescription
System_CAPS_pubmethodReplaceWith(Object)

Replaces this node with the specified content.

System_CAPS_pubmethodReplaceWith(Object())

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.


                Dim xmlTree As XElement = _ 
        <Root>
            <Child1>child1 content</Child1>
            <Child2>child2 content</Child2>
            <Child3>child3 content</Child3>
            <Child4>child4 content</Child4>
            <Child5>child5 content</Child5>
        </Root>

Dim child3 As XElement = xmlTree.<Child3>(0)
child3.ReplaceWith(<NewChild>new content</NewChild>)
Console.WriteLine(xmlTree)

This example produces the following output:


                <Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>
Return to top
Show:
© 2016 Microsoft