Extensions.Remove 方法

定義

多載

Remove(IEnumerable<XAttribute>)

在來源集合中,從每一個屬性的父項目移除這些屬性。

Remove<T>(IEnumerable<T>)

在來源集合中,從每一個節點的父節點移除這些節點。

Remove(IEnumerable<XAttribute>)

在來源集合中,從每一個屬性的父項目移除這些屬性。

C#
public static void Remove (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> source);
C#
public static void Remove (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute?> source);

參數

source
IEnumerable<XAttribute>

IEnumerable<T>XAttribute,其中包含來源集合。

範例

下列範例會擷取屬性集合,然後呼叫這個方法,從其父元素中移除它們。

C#
XElement root = new XElement("Root",  
    new XAttribute("Att1", 1),  
    new XAttribute("Att2", 2),  
    new XAttribute("Att3", 3),  
    new XAttribute("Att4", 4),  
    new XAttribute("Att5", 5)  
);  

IEnumerable<XAttribute> atList =  
    from at in root.Attributes()  
    where (int)at >= 3  
    select at;  

atList.Remove();  

Console.WriteLine(root);  

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

<Root Att1="1" Att2="2" />  

備註

這個方法會使用快照式語意 ,也就是說,它會先將來源集合中的屬性複製到 , System.Collections.Generic.List<T> 然後再將其與父系中斷連線。 這是避免混合命令式/宣告式程式碼的問題的必要專案。 如需詳細資訊,請參閱混合宣告式程式碼/命令式程式碼 Bug (LINQ to XML)

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Remove<T>(IEnumerable<T>)

在來源集合中,從每一個節點的父節點移除這些節點。

C#
public static void Remove<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XNode;
C#
public static void Remove<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XNode;

類型參數

T

source 中物件的型別,限制為 XNode

參數

source
IEnumerable<T>

IEnumerable<T>XNode,其中包含來源集合。

範例

下列範例會擷取專案的集合。 然後它會呼叫這個方法,以從其父元素中移除專案。

C#
XElement root = new XElement("Root",  
    new XElement("Data", 1),  
    new XElement("Data", 2),  
    new XElement("Data", 3),  
    new XElement("Data", 4),  
    new XElement("Data", 5)  
);  

IEnumerable<XElement> elList =  
    from el in root.Elements()  
    where (int)el >= 3  
    select el;  

elList.Remove();  

Console.WriteLine(root);  

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

<Root>  
  <Data>1</Data>  
  <Data>2</Data>  
</Root>  

備註

這個方法會使用快照式語意 ,也就是說,它會先將來源集合中的屬性複製到 , List<T> 然後再將其與父系中斷連線。 這是避免混合命令式/宣告式程式碼的問題的必要專案。 如需詳細資訊,請參閱混合宣告式程式碼/命令式程式碼 Bug (LINQ to XML)

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0