XContainer.ReplaceNodes 方法

定義

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

多載

ReplaceNodes(Object)

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

ReplaceNodes(Object[])

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

範例

下列範例會建立兩個 XML 樹狀結構,然後使用此方法將其中一個樹狀結構的內容取代為查詢的結果。

C#
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);  

這個範例會產生下列輸出:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

備註

如需可以傳遞至此函式之有效內容的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容

這個方法會引發 ChangedChanging 事件。

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

ReplaceNodes(Object)

來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs

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

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

參數

content
Object

包含簡單內容或內容物件 (取代自節點) 集合的內容物件。

範例

下列範例會建立包含子節點的 XML 樹狀結構。 然後,它會將所有子節點取代為單一元素。

若要查看將子節點取代為 LINQ 查詢結果的範例,請參閱 ReplaceNodes

C#
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);  

這個範例會產生下列輸出:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

備註

如需可以傳遞至此函式之有效內容的詳細資訊,請參閱 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

ReplaceNodes(Object[])

來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs

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

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

參數

content
Object[]

內容物件的參數清單。

範例

下列範例會建立字典和 XML 樹狀結構。 然後它會查詢字典、將結果投影至 IEnumerable<T>XElement ,並將 XML 樹狀結構的內容取代為查詢的結果。

C#
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);  

這個範例會產生下列輸出:

XML
<Root>  
  <Child>3</Child>  
  <Child>4</Child>  
  <Child>5</Child>  
</Root>  

備註

如需可以傳遞至此函式之有效內容的詳細資訊,請參閱 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