.NET Framework 类库
XNode..::.ReplaceWith 方法

更新:2007 年 11 月

使用指定的内容替换此节点。

重载列表

  名称说明
ReplaceWith(Object) 使用指定的内容替换此节点。
ReplaceWith(array<Object>[]()[]) 使用指定的内容替换此节点。
页首
备注

有关可以传递到此函数的有效内容的详细信息,请参见 XElement 和 XDocument 对象的有效内容

此方法将引发 ChangedChanging 事件。

示例

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

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);
Visual Basic
Dim xmlTree As XElement = _ 
        <Root>
            <Child1>child1 content</Child1>
            <Child2>child2 content</Child2>
            <Child3>child3 content</Child3>
            <Child4>child4 content</Child4>
            <Child5>child5 content</Child5>
        </Root>

Dim child3 As XElement = xmlTree.<Child3>(0)
child3.ReplaceWith(<NewChild>new content</NewChild>)
Console.WriteLine(xmlTree)

该示例产生下面的输出:

xmlLang
<Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>
另请参见

参考

其他资源

标记 :


Page view tracker