Export (0) Print
Expand All

XElement.ReplaceWith Method

Replaces this node with the specified content.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method ReplaceWith(Object) Replaces this node with the specified content. (Inherited from XNode.)
Public method ReplaceWith(Object()) Replaces this node with the specified content. (Inherited from XNode.)
Top

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>
Show:
© 2014 Microsoft