Extensions.Remove Method

Definition

Overloads

Remove(IEnumerable<XAttribute>)

Removes every attribute in the source collection from its parent element.

Remove<T>(IEnumerable<T>)

Removes every node in the source collection from its parent node.

Remove(IEnumerable<XAttribute>)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Removes every attribute in the source collection from its parent element.

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

Parameters

source
IEnumerable<XAttribute>

An IEnumerable<T> of XAttribute that contains the source collection.

Examples

The following example retrieves a collection of attributes, and then calls this method to remove them from their parent elements.

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

This example produces the following output:

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

Remarks

This method uses snapshot semantics - that is, it copies the attributes in the source collection to a System.Collections.Generic.List<T> before disconnecting them from their parents. This is required to avoid issues with mixed imperative/declarative code. For more information, see Mixed Declarative Code/Imperative Code Bugs (LINQ to XML).

See also

Applies to

.NET 10 and other versions
Product Versions
.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

Remove<T>(IEnumerable<T>)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Removes every node in the source collection from its parent node.

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;

Type Parameters

T

The type of the objects in source, constrained to XNode.

Parameters

source
IEnumerable<T>

An IEnumerable<T> of XNode that contains the source collection.

Examples

The following example retrieves a collection of elements. It then calls this method to remove the elements from their parent element.

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

This example produces the following output:

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

Remarks

This method uses snapshot semantics - that is, it copies the attributes in the source collection to a List<T> before disconnecting them from their parents. This is required to avoid issues with mixed imperative/declarative code. For more information, see Mixed Declarative Code/Imperative Code Bugs (LINQ to XML).

See also

Applies to

.NET 10 and other versions
Product Versions
.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