XNode.IsBefore(XNode) 方法

定義

根據文件順序,判斷目前的節點是否出現在指定的節點之前。

C#
public bool IsBefore (System.Xml.Linq.XNode node);
C#
public bool IsBefore (System.Xml.Linq.XNode? node);

參數

node
XNode

要針對文件順序比較的 XNode

傳回

Boolean

如果這個節點出現在指定的節點之前則為 true,否則為 false

範例

下列範例會使用這個方法。

C#
XElement xmlTree = new XElement("Root",  
    new XText("Text content."),  
    new XElement("Child1", "child1 content"),  
    new XElement("Child2", "child2 content"),  
    new XElement("Child3", "child3 content"),  
    new XText("More text content."),  
    new XElement("Child4", "child4 content"),  
    new XElement("Child5", "child5 content")  
);  
XElement child3 = xmlTree.Element("Child3");  
XElement child5 = xmlTree.Element("Child5");  
if (child5.IsBefore(child3))  
    Console.WriteLine("Child5 is before Child3");  
else  
    Console.WriteLine("Child5 is not before Child3");  

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

Child5 is not before Child3  

備註

會將 XContainer 其子節點儲存為物件的單一連結清單 XNode 。 這表示 IsBefore 方法必須周遊要比較之兩個節點的上階,直到找到通用父系為止。 然後,它必須周遊一般父節點的子節點清單,以判斷要比較之兩個節點的順序。 因此,使用此方法可能會影響您的效能。

適用於

產品 版本
.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

另請參閱