XContainer.ReplaceNodes 方法

定義

以指定的內容取代這個文件或項目的子節點。

多載

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 物件的有效內容

這個方法會引發 ChangedChanging 事件。

這個方法具有快照式語意。 它會先建立新內容的複本。 然後,它會移除此節點的所有子節點。 最後,它會將新內容新增為子節點。 這表示您可以使用子節點本身的查詢來取代子節點。

ReplaceNodes(Object)

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 -> unit
Public 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 物件的有效內容

這個方法會引發 ChangedChanging 事件。

這個方法具有快照式語意。 它會先建立新內容的複本。 然後,它會移除此節點的所有子節點。 最後,它會將新內容新增為子節點。 這表示您可以使用子節點本身的查詢來取代子節點。

另請參閱

適用於

ReplaceNodes(Object[])

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[] -> unit
Public Sub ReplaceNodes (ParamArray content As Object())

參數

content
Object[]

內容物件的參數清單。

範例

下列範例會建立字典和 XML 樹狀結構。 然後它會查詢字典、將結果投影至 IEnumerable<T>XElement ,並將 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 物件的有效內容

這個方法會引發 ChangedChanging 事件。

這個方法具有快照式語意。 它會先建立新內容的複本。 然後,它會移除此節點的所有子節點。 最後,它會將新內容新增為子節點。 這表示您可以使用子節點本身的查詢來取代子節點。

另請參閱

適用於