Share via


DocumentBase.SelectSingleNode 方法

傳回 Microsoft.Office.Interop.Word.XMLNode 物件,表示在文件中第一個符合 XPath 參數的節點。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word.v4.0.Utilities (在 Microsoft.Office.Tools.Word.v4.0.Utilities.dll 中)

語法

'宣告
Public Function SelectSingleNode ( _
    xPath As String, _
    prefixMapping As String, _
    fastSearchSkippingTextNodes As Boolean _
) As XMLNode
public XMLNode SelectSingleNode(
    string xPath,
    string prefixMapping,
    bool fastSearchSkippingTextNodes
)

參數

  • xPath
    型別:System.String
    有效的 XPath 字串。如需關於 XPath 的詳細資訊,請參閱 XPath 參考。
  • prefixMapping
    型別:System.String
    提供要在結構描述中執行搜尋的前置詞。如果 XPath 參數是以名稱來搜尋項目,請使用 PrefixMapping 參數。
  • fastSearchSkippingTextNodes
    型別:System.Boolean
    true 表示在搜尋指定節點時,要略過所有文字節點。false 則表示要在搜尋中包含文字節點。預設值為 False。

傳回值

型別:Microsoft.Office.Interop.Word.XMLNode
Microsoft.Office.Interop.Word.XMLNode 物件,代表文件中符合 XPath 參數的第一個節點。如果找不到符合 XPath 參數的節點,則為 nullnull 參考 (即 Visual Basic 中的 Nothing)。

備註

將 FastSearchSkippingTextNodes 參數設定為 true 會減低效能,因為 Microsoft Office Word 會在文件的所有節點中搜尋包含在節點內的文字。

選擇性參數

如需選擇性參數的詳細資訊,請參閱Office 方案中的選擇性參數

範例

下列程式碼範例示範當呼叫 SelectSingleNode 方法時,如何指定 XPath 和 PrefixMapping 參數。 若要使用這個範例,請在文件層級專案中的 ThisDocument 類別執行。

Private Sub DocumentSelectSingleNode()
    If Me.XMLSchemaReferences.Count > 0 Then
        Dim XPath As String = "/x:catalog/x:book/x:title"
        Dim PrefixMapping As String = "xmlns:x=""" + _
            Me.XMLSchemaReferences(1).NamespaceURI + """"
        Dim node As Word.XMLNode = Me.SelectSingleNode(XPath, _
            PrefixMapping, True)
    Else
        MessageBox.Show("The document does not contain a " + _
            "schema reference.")
    End If
End Sub
private void DocumentSelectSingleNode()
{
    if (this.XMLSchemaReferences.Count > 0)
    {
        string XPath = "/x:catalog/x:book/x:title";
        object key = 1;

        string PrefixMapping = "xmlns:x=\"" +
            this.XMLSchemaReferences.get_Item(ref key).NamespaceURI +
            "\"";

        Word.XMLNode node = this.SelectSingleNode(XPath, 
            PrefixMapping, true);
    }
    else
    {
        MessageBox.Show("The document does not contain a " +
            "schema reference.");
    }
}

.NET Framework 安全性

請參閱

參考

DocumentBase 類別

Microsoft.Office.Tools.Word 命名空間