Export (0) Print
Expand All

XElement.ReplaceAll Method (Object)

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

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public void ReplaceAll(
	Object content
)

Parameters

content
Type: System.Object

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

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.

The following example uses this method.

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:

<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" />

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft