DocumentBase.SelectSingleNode(String, String, Boolean) 方法

定义

返回一个 XMLNode 对象,该对象表示文档中与 XPath 参数匹配的第一个节点。

public Microsoft.Office.Interop.Word.XMLNode SelectSingleNode (string xPath, string prefixMapping, bool fastSearchSkippingTextNodes);
member this.SelectSingleNode : string * string * bool -> Microsoft.Office.Interop.Word.XMLNode
Public Function SelectSingleNode (xPath As String, Optional prefixMapping As String, Optional fastSearchSkippingTextNodes As Boolean) As XMLNode

参数

xPath
String

有效的 XPath 字符串。 关于 XPath 的更多信息,请参见 XPath 参考。

prefixMapping
String

提供架构中用作搜索依据的前缀。 PrefixMapping如果XPath参数使用名称来搜索元素,请使用 参数。

fastSearchSkippingTextNodes
Boolean

如果为 true,则在搜索指定的节点时忽略所有文本节点。 如果为 false,则在搜索中包含文本节点。 默认值为 false。

返回

一个 XMLNode 对象,该对象表示文档中与 XPath 参数匹配的第一个节点;如果未找到与 XPath 参数匹配的节点,则为 null

示例

下面的代码示例演示如何在调用 SelectSingleNode 方法时指定 XPathPrefixMapping 参数。 若要使用此示例,请从 ThisDocument 文档级项目中的 类运行它。

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.");
    }
}
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

注解

FastSearchSkippingTextNodes将 参数设置为 true 会降低性能,因为 Microsoft Office Word根据节点中包含的文本搜索文档中的所有节点。

可选参数

有关可选参数的信息,请参阅 Office 解决方案中的可选参数

适用于