XDocument.ReplaceWith Method (System.Xml.Linq)

Switch View :
ScriptFree
.NET Framework Class Library
XDocument.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.

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
Remarks

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.

Examples

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

C#
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");
child3.ReplaceWith(
    new XElement("NewChild", "new content")
);
Console.WriteLine(xmlTree);
Visual Basic
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:

xmlLang
<Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>
See Also

Reference

Other Resources

Community Content

ScottWelker
Note: ReplaceWith() doesn't seem to have any effect..

http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/164f8029-b4a7-4c6d-a570-f5cad291a32c/?prof=required