更新:2007 年 11 月
页首
有关可以传递到此函数的有效内容的详细信息,请参见 XElement 和 XDocument 对象的有效内容。
此方法将引发 Changed 和 Changing 事件。
下面的示例使用此方法将节点的内容替换为不同内容。
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);
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)
该示例产生下面的输出:
<Root>
<Child1>child1 content</Child1>
<Child2>child2 content</Child2>
<NewChild>new content</NewChild>
<Child4>child4 content</Child4>
<Child5>child5 content</Child5>
</Root>
参考
其他资源