XNodeDocumentOrderComparer.Compare(XNode, XNode) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
比較兩個節點以判斷其相對文件順序。
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
參數
傳回
Int32一個 包含 0 若節點相等;-1 若 x 在 之前y;1 若x在 之後 y。
實作
例外狀況
這兩個節點並不共享共同祖先。
範例
以下範例使用此類別比較兩個節點的文件順序。
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 方法。 這個類別是該方法內部使用的。