XNode.ReplaceWith 方法

定義

以指定的內容取代這個節點。Replaces this node with the specified content.

多載

ReplaceWith(Object)

以指定的內容取代這個節點。Replaces this node with the specified content.

ReplaceWith(Object[])

以指定的內容取代這個節點。Replaces this node with the specified content.

範例

下列範例會使用這個方法來取代具有不同內容的節點內容。The following example uses this method to replace the contents of a node with different content.

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)  

這個範例會產生下列輸出:This example produces the following output:

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

備註

如需可傳遞給這個方法之有效內容的詳細資訊,請參閱system.xml.linq.xelement> 和 XDocument 物件的有效內容For details about the valid content that can be passed to this method, see Valid Content of XElement and XDocument Objects.

這個方法會引發 ChangedChanging 的事件。This method will raise the Changed and the Changing events.

XContainer 會將其子節點儲存為 XNode 物件的單一連結清單。The XContainer stores its child nodes as a singly-linked list of XNode objects. 這表示 ReplaceWith 方法必須在父容器下的直接子節點清單中進行。This means that the ReplaceWith method must traverse the list of direct child nodes under the parent container. 因此,使用此方法可能會影響您的效能。Therefore, using this method might affect your performance.

ReplaceWith(Object)

以指定的內容取代這個節點。Replaces this node with the specified content.

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

參數

content
Object

取代這個節點的內容。Content that replaces this node.

範例

下列範例會使用這個方法來取代具有不同內容的節點內容。The following example uses this method to replace the contents of a node with different content.

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)  

這個範例會產生下列輸出:This example produces the following output:

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

備註

這個方法會先將此節點從其父代移除,然後將指定的內容加入此節點的父系,以取代此節點。This method first removes this node from its parent, and then adds the specified content to this node's parent, in the place of this node.

XContainer 會將其子節點儲存為 XNode 物件的單一連結清單。The XContainer stores its child nodes as a singly-linked list of XNode objects. 這表示 ReplaceWith 方法必須在父容器下的直接子節點清單中進行。This means that the ReplaceWith method must traverse the list of direct child nodes under the parent container. 因此,使用此方法可能會影響您的效能。Therefore, using this method might affect your performance.

如需可傳遞給這個方法之有效內容的詳細資訊,請參閱system.xml.linq.xelement> 和 XDocument 物件的有效內容For details about the valid content that can be passed to this method, see Valid Content of XElement and XDocument Objects.

這個方法會引發 ChangedChanging 的事件。This method will raise the Changed and the Changing events.

另請參閱

ReplaceWith(Object[])

以指定的內容取代這個節點。Replaces this node with the specified content.

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

參數

content
Object[]

新內容的參數清單。A parameter list of the new content.

範例

下列範例會示範如何使用 LINQ to XMLLINQ to XML 查詢的結果做為此方法的輸入。The following example shows using the results of a LINQ to XMLLINQ to XML query as the input to this method.

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);  
Dim srcTree As XElement = _   
        <Root>  
            <Element1>1</Element1>  
            <Element2>2</Element2>  
            <Element3>3</Element3>  
            <Element4>4</Element4>  
            <Element5>5</Element5>  
        </Root>  
  
Dim xmlTree As XElement = _   
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
        </Root>  
  
Dim child3 As XElement = xmlTree.<Child3>(0)  
child3.ReplaceWith( _  
    From el In srcTree.Elements() _  
    Where (CInt(el) > 3) _  
    Select el)  
  
Console.WriteLine(xmlTree)  

這個範例會產生下列輸出:This example produces the following output:

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

備註

這個方法會先將此節點從其父代移除,然後將指定的內容加入此節點的父系,以取代此節點。This method first removes this node from its parent, and then adds the specified content to this node's parent, in the place of this node.

XContainer 會將其子節點儲存為 XNode 物件的單一連結清單。The XContainer stores its child nodes as a singly-linked list of XNode objects. 這表示 ReplaceWith 方法必須在父容器下的直接子節點清單中進行。This means that the ReplaceWith method must traverse the list of direct child nodes under the parent container. 因此,使用此方法可能會影響您的效能。Therefore, using this method might affect your performance.

如需可傳遞給這個方法之有效內容的詳細資訊,請參閱system.xml.linq.xelement> 和 XDocument 物件的有效內容For details about the valid content that can be passed to this method, see Valid Content of XElement and XDocument Objects.

這個方法會引發 ChangedChanging 的事件。This method will raise the Changed and the Changing events.

另請參閱

適用於