XContainer.ReplaceNodes 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定内容替换此文档或元素的子节点。
重载
| ReplaceNodes(Object) | 使用指定内容替换此文档或元素的子节点。 | 
| ReplaceNodes(Object[]) | 使用指定内容替换此文档或元素的子节点。 | 
示例
以下示例创建两个 XML 树,然后使用此方法将其中一个树的内容替换为查询结果。
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)  
该示例产生下面的输出:
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  
注解
有关可传递给此函数的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容。
此方法具有快照语义。 它首先创建新内容的副本。 然后,它会删除此节点的所有子节点。 最后,它将新内容添加为子节点。 这意味着可以使用子节点本身的查询来替换子节点。
ReplaceNodes(Object)
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
使用指定内容替换此文档或元素的子节点。
public:
 void ReplaceNodes(System::Object ^ content);public void ReplaceNodes(object content);public void ReplaceNodes(object? content);member this.ReplaceNodes : obj -> unitPublic Sub ReplaceNodes (content As Object)参数
- content
- Object
用于替换子节点的包含简单内容的内容对象或内容对象集合。
示例
以下示例创建一个包含子节点的 XML 树。 然后,它将所有子节点替换为单个元素。
若要查看将子节点替换为 LINQ 查询结果的示例,请参阅 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)  
该示例产生下面的输出:
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  
注解
有关可传递给此函数的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容。
此方法具有快照语义。 它首先创建新内容的副本。 然后,它会删除此节点的所有子节点。 最后,它将新内容添加为子节点。 这意味着可以使用子节点本身的查询来替换子节点。
另请参阅
适用于
ReplaceNodes(Object[])
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
使用指定内容替换此文档或元素的子节点。
public:
 void ReplaceNodes(... cli::array <System::Object ^> ^ content);public void ReplaceNodes(params object[] content);public void ReplaceNodes(params object?[] content);member this.ReplaceNodes : obj[] -> unitPublic Sub ReplaceNodes (ParamArray content As Object())参数
- content
- Object[]
内容对象的参数列表。
示例
以下示例创建字典和 XML 树。 然后,它会查询字典,将结果投影到 的 XElement,IEnumerable<T>并将 XML 树的内容替换为查询结果。
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)  
该示例产生下面的输出:
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  
注解
有关可传递给此函数的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容。
此方法具有快照语义。 它首先创建新内容的副本。 然后,它会删除此节点的所有子节点。 最后,它将新内容添加为子节点。 这意味着可以使用子节点本身的查询来替换子节点。