Extensions.Remove 方法

定義

多載

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

在來源集合中,從每一個屬性的父項目移除這些屬性。Removes every attribute in the source collection from its parent element.

public:
[System::Runtime::CompilerServices::Extension]
 static void Remove(System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ source);
public static void Remove (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> source);
static member Remove : seq<System.Xml.Linq.XAttribute> -> unit
<Extension()>
Public Sub Remove (source As IEnumerable(Of XAttribute))

參數

source
IEnumerable<XAttribute>

IEnumerable<T>XAttribute,其中包含來源集合。An IEnumerable<T> of XAttribute that contains the source collection.

範例

下列範例會抓取屬性的集合,然後呼叫這個方法,將它們從其父元素中移除。The following example retrieves a collection of attributes, and then calls this method to remove them from their parent elements.

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);  
Dim root As XElement = <Root Att1="1" Att2="2" Att3="3" Att4="4" Att5="5"/>  
  
Dim atList = From at In root.Attributes _  
             Where at.Value >= 3 _  
             Select at  
  
atList.Remove()  
  
Console.WriteLine(root)  
  

這個範例會產生下列輸出:This example produces the following output:

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

備註

這個方法會使用快照式語義,也就是說,它會將來源集合中的屬性複製System.Collections.Generic.List<T>到,然後再將其從父系中斷連接。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. 如需詳細資訊,請參閱混合的宣告式程式代碼/命令式程式碼 bug (LINQ to XML)For more information, see Mixed Declarative Code/Imperative Code Bugs (LINQ to XML).

另請參閱

Remove<T>(IEnumerable<T>)

在來源集合中,從每一個節點的父節點移除這些節點。Removes every node in the source collection from its parent node.

public:
generic <typename T>
 where T : System::Xml::Linq::XNode[System::Runtime::CompilerServices::Extension]
 static void Remove(System::Collections::Generic::IEnumerable<T> ^ source);
public static void Remove<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XNode;
static member Remove : seq<'T (requires 'T :> System.Xml.Linq.XNode)> -> unit (requires 'T :> System.Xml.Linq.XNode)
<Extension()>
Public Sub Remove(Of T As XNode) (source As IEnumerable(Of T))

類型參數

T

source 中物件的型別,限制為 XNodeThe type of the objects in source, constrained to XNode.

參數

source
IEnumerable<T>

IEnumerable<T>XNode,其中包含來源集合。An IEnumerable<T> of XNode that contains the source collection.

範例

下列範例會抓取元素的集合。The following example retrieves a collection of elements. 然後,它會呼叫這個方法來移除其父項目中的元素。It then calls this method to remove the elements from their parent element.

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);  
Dim root As XElement = _  
    <Root>  
        <Data>1</Data>  
        <Data>2</Data>  
        <Data>3</Data>  
        <Data>4</Data>  
        <Data>5</Data>  
    </Root>  
  
Dim elList = From el In root.Elements _  
             Where el.Value >= 3 _  
             Select el  
  
elList.Remove()  
  
Console.WriteLine(root)  
  

這個範例會產生下列輸出:This example produces the following output:

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

備註

這個方法會使用快照式語義,也就是說,它會將來源集合中的屬性複製List<T>到,然後再將其從父系中斷連接。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. 如需詳細資訊,請參閱混合的宣告式程式代碼/命令式程式碼 bug (LINQ to XML)For more information, see Mixed Declarative Code/Imperative Code Bugs (LINQ to XML).

另請參閱

適用於