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