XNodeDocumentOrderComparer.Compare(XNode, XNode) 方法

定义

比较两个节点以确定其相对文档顺序。

public:
 virtual int Compare(System::Xml::Linq::XNode ^ x, System::Xml::Linq::XNode ^ y);
public int Compare(System.Xml.Linq.XNode x, System.Xml.Linq.XNode y);
public int Compare(System.Xml.Linq.XNode? x, System.Xml.Linq.XNode? y);
abstract member Compare : System.Xml.Linq.XNode * System.Xml.Linq.XNode -> int
override this.Compare : System.Xml.Linq.XNode * System.Xml.Linq.XNode -> int
Public Function Compare (x As XNode, y As XNode) As Integer

参数

x
XNode

第一个 XNode 要比较的。

y
XNode

要比较的第二个 XNode 值。

返回

Int32如果节点相等,则包含 0;如果之前,则为 x -1 1;如果y之后xy,则为 1。

实现

例外

这两个节点不共享公共祖先。

示例

以下示例使用此类比较两个节点的文档顺序。

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");
XNodeDocumentOrderComparer documentOrderComparer = new XNodeDocumentOrderComparer();
int val = documentOrderComparer.Compare(child3, child5);
if (val == 0)
    Console.WriteLine("Child3 is same as Child5");
else if (val < 0)
    Console.WriteLine("Child3 is before Child5");
else
    Console.WriteLine("Child3 is after Child5");

此示例生成以下输出:

Child3 is before Child5

注解

建议不要直接使用此类,而是使用 InDocumentOrder 该方法。 此方法在内部使用此类。

适用于

另请参阅