XElement.ReplaceAll Method

Definition

Replaces the child nodes and the attributes of this element with the specified content.

Overloads

ReplaceAll(Object)

Replaces the child nodes and the attributes of this element with the specified content.

ReplaceAll(Object[])

Replaces the child nodes and the attributes of this element with the specified content.

Examples

The following example passes the results of a LINQ query to this method, replacing the contents of an element with the query results. It queries the element that is having its contents replaced.

C#
XElement xmlTree = new XElement("Root",
    new XElement("Data", 1),
    new XElement("Data", 2),
    new XElement("Data", 3),
    new XElement("Data", 4),
    new XElement("Data", 5)
);

Console.WriteLine(xmlTree);
Console.WriteLine("-----");

xmlTree.ReplaceAll(
    from el in xmlTree.Elements()
    where (int)el >= 3
    select new XElement("NewData", (int)el)
);

Console.WriteLine(xmlTree);

This example produces the following output:

<Root>
  <Data>1</Data>
  <Data>2</Data>
  <Data>3</Data>
  <Data>4</Data>
  <Data>5</Data>
</Root>
-----
<Root>
  <NewData>3</NewData>
  <NewData>4</NewData>
  <NewData>5</NewData>
</Root>

Remarks

This method uses snapshot semantics - that is, it creates a separate copy of the new content before replacing the contents of the current element with the new content. This means that you can query the contents of the current element and use the results of the query as the specified new content.

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.

ReplaceAll(Object)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Replaces the child nodes and the attributes of this element with the specified content.

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

Parameters

content
Object

The content that will replace the child nodes and attributes of this element.

Examples

The following example uses this method.

C#
XElement root = new XElement("Root",
    new XElement("Child", "child content")
);

// ReplaceAll with an XElement object.
root.ReplaceAll(new XElement("NewChild", "n"));
Console.WriteLine(root);

// ReplaceAll with an XAttribute object.
root.ReplaceAll(new XAttribute("NewAttribute", "n"));
Console.WriteLine(root);

// ReplaceAll with a string.
root.ReplaceAll("Some text");
Console.WriteLine(root);

// ReplaceAll with a double.
double dbl = 12.345;
root.ReplaceAll(dbl);
Console.WriteLine(root);

// ReplaceAll with a DateTime object.
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);
root.ReplaceAll(dt);
Console.WriteLine(root);

// ReplaceAll with a string array.
// Any collection other than a collection of XElement or XAttribute objects
// are converted to strings. The strings are concatenated and added.
string[] stringArray = {
    "abc",
    "def",
    "ghi"
};
root.ReplaceAll(stringArray);
Console.WriteLine(root);

// ReplaceAll with an array of XElement objects.
XElement[] ellArray = {
    new XElement("NewChild1", 1),
    new XElement("NewChild2", 2),
    new XElement("NewChild3", 3)
};
root.ReplaceAll(ellArray);
Console.WriteLine(root);

// ReplaceAll with an array of XAttribute objects.
XAttribute[] attArray = {
    new XAttribute("NewAtt1", 1),
    new XAttribute("NewAtt2", 2),
    new XAttribute("NewAtt3", 3)
};
root.ReplaceAll(attArray);
Console.WriteLine(root);

This example produces the following output:

XML
<Root>
  <NewChild>n</NewChild>
</Root>
<Root NewAttribute="n" />
<Root>Some text</Root>
<Root>12.345</Root>
<Root>2006-10-06T12:30:00</Root>
<Root>abcdefghi</Root>
<Root>
  <NewChild1>1</NewChild1>
  <NewChild2>2</NewChild2>
  <NewChild3>3</NewChild3>
</Root>
<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />

Remarks

This method first removes existing content and attributes. It then adds the specified content.

This method uses snapshot semantics - that is, it creates a separate copy of the new content before replacing the contents of the current element with the new content. This means that you can query the contents of the current element and use the results of the query as the specified new content.

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.

See also

Applies to

.NET 10 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, 10
.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

ReplaceAll(Object[])

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Replaces the child nodes and the attributes of this element with the specified content.

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

Parameters

content
Object[]

A parameter list of content objects.

Examples

The following example passes the results of a LINQ query to this method, replacing the contents of an element with the query results.

C#
XElement xmlTree1 = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
    new XElement("Child6", 6)
);

XElement root = new XElement("Root",
    new XElement("Child", "child content")
);

root.ReplaceAll(
    from el in xmlTree1.Elements()
    where((int)el >= 3 && (int)el <= 5)
    select el
);
Console.WriteLine(root);

This example produces the following output:

XML
<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

Remarks

This method first removes existing content and attributes. It then adds the specified content.

This method uses snapshot semantics - that is, it creates a separate copy of the new content before replacing the contents of the current element with the new content. This means that you can query the contents of the current element and use the results of the query as the specified new content.

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.

See also

Applies to

.NET 10 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, 10
.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