XContainer.ReplaceNodes 方法

定义

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

重载

ReplaceNodes(Object)

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

ReplaceNodes(Object[])

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

示例

下面的示例创建两个 XML 树,然后使用此方法将其中一个树的内容替换为查询结果。The following example creates two XML trees, and then uses this method to replace the contents of one of them with the results of a query.

XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

注解

有关可传递到此函数的有效内容的详细信息,请参阅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.

此方法具有快照语义。This method has snapshot semantics. 它首先创建新内容的副本。It first creates a copy of the new content. 然后删除此节点的所有子节点。It then removes all children nodes of this node. 最后,它会将新内容添加为子节点。Finally, it adds the new content as children nodes. 这意味着您可以使用子节点本身的查询来替换子节点。This means that you can replace children nodes using a query on the children nodes themselves.

ReplaceNodes(Object)

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

public:
 void ReplaceNodes(System::Object ^ content);
public void ReplaceNodes (object content);
member this.ReplaceNodes : obj -> unit
Public Sub ReplaceNodes (content As Object)

参数

content
Object

用于替换子节点的包含简单内容的内容对象或内容对象集合。A content object containing simple content or a collection of content objects that replace the children nodes.

示例

下面的示例创建一个包含子节点的 XML 树。The following example creates an XML tree that contains children nodes. 然后,它将所有子节点替换为单个元素。It then replaces all of the children nodes with a single element.

若要查看将子节点替换 LINQLINQ 查询结果的示例,请参阅 ReplaceNodesTo see an example of replacing the children nodes with the results of a LINQLINQ query, see ReplaceNodes.

XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

注解

有关可传递到此函数的有效内容的详细信息,请参阅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.

此方法具有快照语义。This method has snapshot semantics. 它首先创建新内容的副本。It first creates a copy of the new content. 然后删除此节点的所有子节点。It then removes all children nodes of this node. 最后,它会将新内容添加为子节点。Finally, it adds the new content as children nodes. 这意味着您可以使用子节点本身的查询来替换子节点。This means that you can replace children nodes using a query on the children nodes themselves.

另请参阅

ReplaceNodes(Object[])

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

public:
 void ReplaceNodes(... cli::array <System::Object ^> ^ content);
public void ReplaceNodes (params object[] content);
member this.ReplaceNodes : obj[] -> unit
Public Sub ReplaceNodes (ParamArray content As Object())

参数

content
Object[]

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

示例

下面的示例创建一个字典和一个 XML 树。The following example creates a dictionary and an XML tree. 然后,它会查询字典,将结果投影到 IEnumerable<T>XElement中,并将 XML 树的内容替换为查询结果。It then queries the dictionary, projects the results to an IEnumerable<T> of XElement, and replaces the contents of the XML tree with the results of the query.

XElement root = new XElement("Root",  
    new XElement("Child", 1),  
    new XElement("Child", 2),  
    new XElement("Child", 3),  
    new XElement("Child", 4),  
    new XElement("Child", 5)  
);  
root.ReplaceNodes(  
    from el in root.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(root);  
Dim root As XElement = _   
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  
root.ReplaceNodes( _  
    From el In root.Elements _  
    Where el.Value >= 3 _  
    Select el)  
Console.WriteLine(root)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

注解

有关可传递到此函数的有效内容的详细信息,请参阅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.

此方法具有快照语义。This method has snapshot semantics. 它首先创建新内容的副本。It first creates a copy of the new content. 然后删除此节点的所有子节点。It then removes all children nodes of this node. 最后,它会将新内容添加为子节点。Finally, it adds the new content as children nodes. 这意味着您可以使用子节点本身的查询来替换子节点。This means that you can replace children nodes using a query on the children nodes themselves.

另请参阅

适用于