Extensions.Remove 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Remove(IEnumerable<XAttribute>) |
移除來源集合中所有的屬性,從其父元素中移除。 |
| Remove<T>(IEnumerable<T>) |
從來源集合的父節點中移除每個節點。 |
Remove(IEnumerable<XAttribute>)
移除來源集合中所有的屬性,從其父元素中移除。
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);
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>
其中 A IEnumerable<T>XAttribute 包含了原始集合。
範例
以下範例會取得一組屬性,然後呼叫此方法將它們從父元素中移除。
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)
此範例會產生下列輸出:
<Root Att1="1" Att2="2" />
備註
此方法使用快照語意——也就是說,在斷開與父集合斷開連接前,先將來源集合中的屬性複製到 a System.Collections.Generic.List<T> 。 這是為了避免命令式與宣告式混合程式碼的問題。 欲了解更多資訊,請參閱混合宣告程式碼/命令式程式碼錯誤(LINQ 轉 XML)。
另請參閱
- RemoveAttributes()
- Attributes
- System.Xml.Linq.XAttribute.Remove
- System.Xml.Linq.XNode.Remove
- LINQ to XML 概觀
適用於
Remove<T>(IEnumerable<T>)
從來源集合的父節點中移除每個節點。
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;
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物件類型 ,受限於 XNode。
參數
- source
- IEnumerable<T>
其中 A IEnumerable<T>XNode 包含了原始集合。
範例
以下範例檢索一組元素。 接著它會呼叫此方法,將元素從父元素中移除。
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)
此範例會產生下列輸出:
<Root>
<Data>1</Data>
<Data>2</Data>
</Root>
備註
此方法使用快照語意——也就是說,在斷開與父集合斷開連接前,先將來源集合中的屬性複製到 a List<T> 。 這是為了避免命令式與宣告式混合程式碼的問題。 欲了解更多資訊,請參閱混合宣告程式碼/命令式程式碼錯誤(LINQ 轉 XML)。
另請參閱
- RemoveAll()
- Attributes
- System.Xml.Linq.XAttribute.Remove
- System.Xml.Linq.XNode.Remove
- LINQ to XML 概觀