Bewerken

Share via


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.

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);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

This example produces the following output:

<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.

public:
 void ReplaceNodes(System::Object ^ content);
public void ReplaceNodes (object content);
public void ReplaceNodes (object? content);
member this.ReplaceNodes : obj -> unit
Public Sub ReplaceNodes (content As Object)

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.

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);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

This example produces the following output:

<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

ReplaceNodes(Object[])

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

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

public:
 void ReplaceNodes(... cli::array <System::Object ^> ^ content);
public void ReplaceNodes (params object[] content);
public void ReplaceNodes (params object?[] content);
member this.ReplaceNodes : obj[] -> unit
Public Sub ReplaceNodes (ParamArray content As Object())

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.

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);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

This example produces the following output:

<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