XNode.DocumentOrderComparer 屬性

定義

取得可比較兩個節點相對位置的比較子 (Comparer)。Gets a comparer that can compare the relative position of two nodes.

public:
 static property System::Xml::Linq::XNodeDocumentOrderComparer ^ DocumentOrderComparer { System::Xml::Linq::XNodeDocumentOrderComparer ^ get(); };
public static System.Xml.Linq.XNodeDocumentOrderComparer DocumentOrderComparer { get; }
member this.DocumentOrderComparer : System.Xml.Linq.XNodeDocumentOrderComparer
Public Shared ReadOnly Property DocumentOrderComparer As XNodeDocumentOrderComparer

屬性值

XNodeDocumentOrderComparer

可比較兩個節點相對位置的 XNodeDocumentOrderComparerAn XNodeDocumentOrderComparer that can compare the relative position of two nodes.

範例

下列範例會建立具有部分元素的 XML 樹狀結構。The following example creates an XML tree with some elements. 然後,它會建立 XNodeList<T>,其中包含以隨機方式從 XML 樹狀結構中的某些元素。It then creates a List<T> of XNode that contains some elements from the XML tree at random. 它會排序清單,使用這個屬性來抓取會執行 System.Collections.IComparerSystem.Collections.Generic.IComparer<T> 介面的 XNodeDocumentOrderComparerIt sorts the list, using this property to retrieve a XNodeDocumentOrderComparer, which implements the System.Collections.IComparer and System.Collections.Generic.IComparer<T> interfaces.

XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5)  
);  
  
List<XNode> nodeList = new List<XNode>();  
nodeList.Add(xmlTree.Element("Child5"));  
nodeList.Add(xmlTree.Element("Child3"));  
nodeList.Add(xmlTree.Element("Child1"));  
  
// Sort nodes in document order.  
nodeList.Sort(XNode.DocumentOrderComparer);  
  
foreach (XElement el in nodeList)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _   
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
        </Root>  
  
Dim nodeList As List(Of XNode) = New List(Of XNode)()  
nodeList.Add(xmlTree.Element("Child5"))  
nodeList.Add(xmlTree.Element("Child3"))  
nodeList.Add(xmlTree.Element("Child1"))  
  
' Sort nodes in document order.  
nodeList.Sort(XNode.DocumentOrderComparer)  
  
For Each el In nodeList  
    Console.WriteLine(el)  
Next  

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

<Child1>1</Child1>  
<Child3>3</Child3>  
<Child5>5</Child5>  

備註

這個屬性主要是在內部用來執行 InDocumentOrder 擴充方法。This property is primarily used internally for implementing the InDocumentOrder extension method. 建議的方法是使用該擴充方法,而不是直接使用此屬性。The recommended approach is to use that extension method instead of using this property directly.

適用於

另請參閱