XContainer.ReplaceNodes Method

Definition

Replaces the children nodes of this document or element with the specified content.

Overloads

ReplaceNodes(Object)

Replaces the children nodes of this document or element with the specified content.

ReplaceNodes(Object[])

Replaces the children nodes of this document or element with the specified content.

Examples

The following example creates two XML trees, and then uses this method to replace the contents of one of them with the results of a query.

C#
XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  

This example produces the following output:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

Remarks

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

This method will raise the Changed and the Changing events.

This method has snapshot semantics. It first creates a copy of the new content. It then removes all children nodes of this node. Finally, it adds the new content as children nodes. This means that you can replace children nodes using a query on the children nodes themselves.

ReplaceNodes(Object)

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Replaces the children nodes of this document or element with the specified content.

C#
public void ReplaceNodes(object content);
C#
public void ReplaceNodes(object? content);

Parameters

content
Object

A content object containing simple content or a collection of content objects that replace the children nodes.

Examples

The following example creates an XML tree that contains children nodes. It then replaces all of the children nodes with a single element.

To see an example of replacing the children nodes with the results of a LINQ query, see ReplaceNodes.

C#
XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  

This example produces the following output:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

Remarks

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

This method will raise the Changed and the Changing events.

This method has snapshot semantics. It first creates a copy of the new content. It then removes all children nodes of this node. Finally, it adds the new content as children nodes. This means that you can replace children nodes using a query on the children nodes themselves.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ReplaceNodes(Object[])

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

Replaces the children nodes of this document or element with the specified content.

C#
public void ReplaceNodes(params object[] content);
C#
public void ReplaceNodes(params object?[] content);

Parameters

content
Object[]

A parameter list of content objects.

Examples

The following example creates a dictionary and an XML tree. It then queries the dictionary, projects the results to an IEnumerable<T> of XElement, and replaces the contents of the XML tree with the results of the query.

C#
XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  

This example produces the following output:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

Remarks

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

This method will raise the Changed and the Changing events.

This method has snapshot semantics. It first creates a copy of the new content. It then removes all children nodes of this node. Finally, it adds the new content as children nodes. This means that you can replace children nodes using a query on the children nodes themselves.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0