XNode.ReplaceWith 方法

定义

将此节点替换为指定的内容。

重载

ReplaceWith(Object)

将此节点替换为指定的内容。

ReplaceWith(Object[])

将此节点替换为指定的内容。

示例

以下示例使用此方法将节点的内容替换为不同的内容。

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Child1", "child1 content"),  
    new XElement("Child2", "child2 content"),  
    new XElement("Child3", "child3 content"),  
    new XElement("Child4", "child4 content"),  
    new XElement("Child5", "child5 content")  
);  
XElement child3 = xmlTree.Element("Child3");  
child3.ReplaceWith(  
    new XElement("NewChild", "new content")  
);  
Console.WriteLine(xmlTree);  

该示例产生下面的输出:

XML
<Root>  
  <Child1>child1 content</Child1>  
  <Child2>child2 content</Child2>  
  <NewChild>new content</NewChild>  
  <Child4>child4 content</Child4>  
  <Child5>child5 content</Child5>  
</Root>  

注解

有关可传递给此方法的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容

此方法将引发 ChangedChanging 事件。

将其 XContainer 子节点存储为对象的单独链接列表 XNode 。 这意味着 方法 ReplaceWith 必须遍历父容器下的直接子节点列表。 因此,使用此方法可能会影响性能。

ReplaceWith(Object)

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

将此节点替换为指定的内容。

C#
public void ReplaceWith(object content);
C#
public void ReplaceWith(object? content);

参数

content
Object

替换此节点时所用的内容。

示例

以下示例使用此方法将节点的内容替换为不同的内容。

C#
XElement xmlTree = new XElement("Root",  
    new XElement("Child1", "child1 content"),  
    new XElement("Child2", "child2 content"),  
    new XElement("Child3", "child3 content"),  
    new XElement("Child4", "child4 content"),  
    new XElement("Child5", "child5 content")  
);  
XElement child3 = xmlTree.Element("Child3");  
child3.ReplaceWith(  
    new XElement("NewChild", "new content")  
);  
Console.WriteLine(xmlTree);  

该示例产生下面的输出:

XML
<Root>  
  <Child1>child1 content</Child1>  
  <Child2>child2 content</Child2>  
  <NewChild>new content</NewChild>  
  <Child4>child4 content</Child4>  
  <Child5>child5 content</Child5>  
</Root>  

注解

此方法首先从其父节点中删除此节点,然后将指定内容添加到此节点的父节点中,以取代此节点。

将其 XContainer 子节点存储为对象的单独链接列表 XNode 。 这意味着 方法 ReplaceWith 必须遍历父容器下的直接子节点列表。 因此,使用此方法可能会影响性能。

有关可传递给此方法的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容

此方法将引发 ChangedChanging 事件。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ReplaceWith(Object[])

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

将此节点替换为指定的内容。

C#
public void ReplaceWith(params object[] content);
C#
public void ReplaceWith(params object?[] content);

参数

content
Object[]

新内容的参数列表。

示例

以下示例演示如何使用 LINQ to XML 查询的结果作为此方法的输入。

C#
XElement srcTree = new XElement("Root",  
    new XElement("Element1", 1),  
    new XElement("Element2", 2),  
    new XElement("Element3", 3),  
    new XElement("Element4", 4),  
    new XElement("Element5", 5)  
);  
XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5)  
);  
XElement child3 = xmlTree.Element("Child3");  
child3.ReplaceWith(  
    from el in srcTree.Elements()  
    where (int)el > 3  
    select el  
);  
Console.WriteLine(xmlTree);  

该示例产生下面的输出:

XML
<Root>  
  <Child1>1</Child1>  
  <Child2>2</Child2>  
  <Element4>4</Element4>  
  <Element5>5</Element5>  
  <Child4>4</Child4>  
  <Child5>5</Child5>  
</Root>  

注解

此方法首先从其父节点中删除此节点,然后将指定内容添加到此节点的父节点中,以取代此节点。

将其 XContainer 子节点存储为对象的单独链接列表 XNode 。 这意味着 方法 ReplaceWith 必须遍历父容器下的直接子节点列表。 因此,使用此方法可能会影响性能。

有关可传递给此方法的有效内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容

此方法将引发 ChangedChanging 事件。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0