XElement.ReplaceAll Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Include Protected Members
Include Inherited Members

Include Silverlight Members
Include Silverlight for Windows Phone Members
Include XNA Framework Members

Replaces the child nodes and the attributes of this element 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ReplaceAll(Object) Replaces the child nodes and the attributes of this element with the specified content.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ReplaceAll(array<Object[]) Replaces the child nodes and the attributes of this element with the specified content.

Top

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 more information about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects in the .NET Framework documentation.

This method will raise the Changed and the Changing events.

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.

Dim output As New StringBuilder
Dim xmlTree As XElement = _
    <Root>
        <Data>1</Data>
        <Data>2</Data>
        <Data>3</Data>
        <Data>4</Data>
        <Data>5</Data>
    </Root>

output.Append(xmlTree)
output.Append(Environment.NewLine)
output.Append("-----")
output.Append(Environment.NewLine)

xmlTree.ReplaceAll( _
    From el In xmlTree.Elements _
    Where el.Value >= 3 _
    Select <NewData><%= el.Value %></NewData> _
)

output.Append(xmlTree)
output.Append(Environment.NewLine)


OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
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)
);

output.Append(xmlTree + Environment.NewLine);
output.Append("-----" + Environment.NewLine);

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

output.Append(xmlTree + Environment.NewLine);

OutputTextBlock.Text = output.ToString();