XElement.ReplaceAll Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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.
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);
Dim xmlTree As XElement = _
<Root>
<Data>1</Data>
<Data>2</Data>
<Data>3</Data>
<Data>4</Data>
<Data>5</Data>
</Root>
Console.WriteLine(xmlTree)
Console.WriteLine("-----")
xmlTree.ReplaceAll( _
From el In xmlTree.Elements _
Where el.Value >= 3 _
Select <NewData><%= el.Value %></NewData> _
)
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.
public:
void ReplaceAll(System::Object ^ content);
public void ReplaceAll (object content);
public void ReplaceAll (object? content);
member this.ReplaceAll : obj -> unit
Public Sub ReplaceAll (content As Object)
Parameters
- content
- Object
The content that will replace the child nodes and attributes of this element.
Examples
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);
Dim root As XElement = _
<Root>
<Child>child content</Child>
</Root>
' ReplaceAll with an XElement object.
root.ReplaceAll(<NewChild>n</NewChild>)
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.
Dim dbl As Double = 12.345
root.ReplaceAll(dbl)
Console.WriteLine(root)
' ReplaceAll with a DateTime object.
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)
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.
Dim stringArray As String() = { _
"abc", _
"def", _
"ghi" _
}
root.ReplaceAll(stringArray)
Console.WriteLine(root)
' ReplaceAll with an array of XElement objects.
Dim ellArray As XElement() = { _
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.
Dim attArray As XAttribute() = { _
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" />
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
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.
public:
void ReplaceAll(... cli::array <System::Object ^> ^ content);
public void ReplaceAll (params object[] content);
public void ReplaceAll (params object?[] content);
member this.ReplaceAll : obj[] -> unit
Public Sub ReplaceAll (ParamArray content As Object())
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.
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);
Dim xmlTree1 As XElement = _
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
<Child4>4</Child4>
<Child5>5</Child5>
<Child6>6</Child6>
</Root>
Dim root As XElement = <Root>
<Child>child content</Child>
</Root>
root.ReplaceAll( _
From el In xmlTree1.Elements() _
Where el.Value >= 3 And el.Value <= 5 _
Select el _
)
Console.WriteLine(root)
This example produces the following output:
<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.