XElement.ReplaceAll 方法

定义

将此元素的子节点和属性替换为指定的内容。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.

ReplaceAll(Object[])

将此元素的子节点和属性替换为指定的内容。Replaces the child nodes and the attributes of this element with the specified content.

示例

下面的示例将 LINQ 查询的结果传递给此方法,并将元素的内容替换为查询结果。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>  

注解

此方法使用快照语义,也就是说,它会在将当前元素的内容替换为新内容之前创建新内容的单独副本。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.

有关可传递到此函数的有效内容的详细信息,请参阅system.xml.linq.xelement> 和 XDocument 对象的有效内容For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

此方法将引发 ChangedChanging 事件。This method will raise the Changed and the Changing events.

ReplaceAll(Object)

将此元素的子节点和属性替换为指定的内容。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);
member this.ReplaceAll : obj -> unit
Public Sub ReplaceAll (content As Object)

参数

content
Object

替换此元素的子节点和属性时将使用的内容。The content that will replace the child nodes and attributes of this element.

示例

下面的示例使用此方法。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" />  

注解

此方法首先删除现有内容和属性。This method first removes existing content and attributes. 然后添加指定的 contentIt 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.

有关可传递到此函数的有效内容的详细信息,请参阅system.xml.linq.xelement> 和 XDocument 对象的有效内容For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

此方法将引发 ChangedChanging 事件。This method will raise the Changed and the Changing events.

另请参阅

ReplaceAll(Object[])

将此元素的子节点和属性替换为指定的内容。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);
member this.ReplaceAll : obj[] -> unit
Public Sub ReplaceAll (ParamArray content As Object())

参数

content
Object[]

内容对象的参数列表。A parameter list of content objects.

示例

下面的示例将 LINQ 查询的结果传递给此方法,并将元素的内容替换为查询结果。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>  

注解

此方法首先删除现有内容和属性。This method first removes existing content and attributes. 然后添加指定的 contentIt 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.

有关可传递到此函数的有效内容的详细信息,请参阅system.xml.linq.xelement> 和 XDocument 对象的有效内容For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

此方法将引发 ChangedChanging 事件。This method will raise the Changed and the Changing events.

另请参阅

适用于