XNode.CompareDocumentOrder(XNode, XNode) 메서드

정의

두 노드를 비교하여 상대적인 XML 문서 순서를 확인합니다.

public:
 static int CompareDocumentOrder(System::Xml::Linq::XNode ^ n1, System::Xml::Linq::XNode ^ n2);
public static int CompareDocumentOrder (System.Xml.Linq.XNode n1, System.Xml.Linq.XNode n2);
public static int CompareDocumentOrder (System.Xml.Linq.XNode? n1, System.Xml.Linq.XNode? n2);
static member CompareDocumentOrder : System.Xml.Linq.XNode * System.Xml.Linq.XNode -> int
Public Shared Function CompareDocumentOrder (n1 As XNode, n2 As XNode) As Integer

매개 변수

n1
XNode

비교할 첫 번째 XNode입니다.

n2
XNode

비교할 두 번째 XNode입니다.

반환

Int32

노드가 서로 같으면 0, n1n2보다 앞에 나오면 -1, n1n2보다 뒤에 나오면 1이 들어 있는 int입니다.

예외

두 노드가 공통 상위 노드를 공유하지 않는 경우

예제

다음 예제에서는 이 메서드를 사용합니다.

XElement xmlTree = new XElement("Root",  
    new XElement("Child1",  
        new XElement("GrandChild1", 1),  
        new XElement("GrandChild2", 2),  
        new XElement("GrandChild3", 3)  
    ),  
    new XElement("Child2",  
        new XElement("GrandChild4", 4),  
        new XElement("GrandChild5", 5),  
        new XElement("GrandChild6", 6)  
    )  
);  
XElement el1 = xmlTree.Descendants("GrandChild2").First();  
XElement el2 = xmlTree.Descendants("GrandChild6").First();  
if (XElement.CompareDocumentOrder(el1, el2) == 0)  
    Console.WriteLine("Compared elements are the same element");  
else if (XElement.CompareDocumentOrder(el1, el2) > 0)  
    Console.WriteLine("el1 is after el2");  
else  
    Console.WriteLine("el1 is before el2");  
Dim xmlTree As XElement = _   
        <Root>  
            <Child1>  
                <GrandChild1>1</GrandChild1>  
                <GrandChild2>2</GrandChild2>  
                <GrandChild3>3</GrandChild3>  
            </Child1>  
            <Child2>  
                <GrandChild4>4</GrandChild4>  
                <GrandChild5>5</GrandChild5>  
                <GrandChild6>6</GrandChild6>  
            </Child2>  
        </Root>  

Dim el1 As XElement = xmlTree...<GrandChild2>(0)  
Dim el2 As XElement = xmlTree...<GrandChild6>(0)  

If (XElement.CompareDocumentOrder(el1, el2) = 0) Then  
    Console.WriteLine("Compared elements are the same element")  
ElseIf (XElement.CompareDocumentOrder(el1, el2) > 0) Then  
    Console.WriteLine("el1 is after el2")  
Else  
    Console.WriteLine("el1 is before el2")  
End If  

이 예제는 다음과 같은 출력을 생성합니다.

el1 is before el2  

설명

XContainer 식 노드는 자동으로 연결된 개체 목록 XNode 으로 저장됩니다. 즉, 메서드는 CompareDocumentOrder 공통 부모를 발견할 때까지 비교할 두 노드의 상위 항목을 트래버스해야 합니다. 그런 다음 공통 부모의 자식 노드 목록을 트래버스하여 비교할 두 노드의 순서를 결정해야 합니다. 따라서 이 방법을 사용하면 성능에 영향을 줄 수 있습니다.

적용 대상

추가 정보