XPathNavigator.NavigatorComparer 屬性

定義

取得 IEqualityComparer,其用於 XPathNavigator 物件的相等比較。

public:
 static property System::Collections::IEqualityComparer ^ NavigatorComparer { System::Collections::IEqualityComparer ^ get(); };
public static System.Collections.IEqualityComparer NavigatorComparer { get; }
static member NavigatorComparer : System.Collections.IEqualityComparer
Public Shared ReadOnly Property NavigatorComparer As IEqualityComparer

屬性值

IEqualityComparer 用於 XPathNavigator 物件的相等比較。

範例

下列範例說明 屬性的使用 NavigatorComparer

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();
Hashtable^ table = gcnew Hashtable(XPathNavigator::NavigatorComparer);

// Add nodes to the Hashtable.
for each (XPathNavigator^ navigator2 in navigator->Select("//book"))
{
    Object^ value = navigator2->Evaluate("string(./title)");
    table->Add(navigator2->Clone(), value);
    Console::WriteLine("Added book with title {0}", value);
}

Console::WriteLine(table->Count);
Console::WriteLine("Does the Hashtable have the book \"The Confidence Man\"?");
Console::WriteLine(table->Contains(navigator->SelectSingleNode("//book[title='The Confidence Man']")));
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();
Hashtable table = new Hashtable(XPathNavigator.NavigatorComparer);

// Add nodes to the Hashtable.
foreach (XPathNavigator navigator2 in navigator.Select("//book"))
{
    object value = navigator2.Evaluate("string(./title)");
    table.Add(navigator2.Clone(), value);
    Console.WriteLine("Added book with title {0}", value);
}

Console.WriteLine(table.Count);
Console.WriteLine("Does the Hashtable have the book \"The Confidence Man\"?");
Console.WriteLine(table.Contains(navigator.SelectSingleNode("//book[title='The Confidence Man']")));
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim table As Hashtable = New Hashtable(XPathNavigator.NavigatorComparer)

' Add nodes to the Hashtable.
For Each navigator2 As XPathNavigator In navigator.Select("//book")
    Dim value As Object = navigator2.Evaluate("string(./title)")
    table.Add(navigator2.Clone(), value)
    Console.WriteLine("Added book with title {0}", value)
Next

Console.WriteLine(table.Count)
Console.WriteLine("Does the Hashtable have the book 'The Confidence Man'?")
Console.WriteLine(table.Contains(navigator.SelectSingleNode("//book[title='The Confidence Man']")))

範例將 books.xml 檔案做為輸入。

<?xml version="1.0" encoding="utf-8" ?>   
<bookstore>  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

備註

屬性 NavigatorComparer 允許 XPathNavigator 根據位置來比較物件,而不是參考物件。 ComparePosition這個類別所實作的 和 Equals 方法是以傳遞為參數的物件 XPathNavigator 位置為基礎。 GetHashCode這個類別實作的方法會使用節點的屬性,例如其本機名稱和命名空間 URI,來建立節點的雜湊碼。

適用於