View.GetContextNodes 方法
根據目前的內容,取得填入「XML 文件物件模型」(DOM) 節點的 XMLNodesCollection 集合參考。
**命名空間:**Microsoft.Office.Interop.InfoPath
**組件:**Microsoft.Office.Interop.InfoPath (於 microsoft.office.interop.infopath.dll 中)
語法
'宣告
<DispIdAttribute(11)> _
Function GetContextNodes ( _
<InAttribute> <OptionalAttribute> varNode As Object, _
<InAttribute> <OptionalAttribute> varViewContext As Object _
) As XMLNodesCollection
'用途
Dim instance As View
Dim varNode As Object
Dim varViewContext As Object
Dim returnValue As XMLNodesCollection
returnValue = instance.GetContextNodes(varNode, varViewContext)
[DispIdAttribute(11)]
XMLNodesCollection GetContextNodes (
[OptionalAttribute] [InAttribute] Object varNode,
[OptionalAttribute] [InAttribute] Object varViewContext
)
參數
- varNode
XML DOM 節點。
- varViewContext
用於內容的控制項識別碼。
傳回值
XMLNodesCollection 集合的參考。
備註
GetContextNodes 方法傳回的集合含有從檢視對應的 XML DOM 節點順序,與目前的「XSL 轉換」(XSLT) 節點對應,從目前的選取範圍開始,並向上經過檢視祖系到達 BODY 標籤。
如果未使用參數,內容節點會以目前的選取範圍為根據;如果使用了參數,則傳回的內容節點會以透過呼叫 SelectNodes 方法而取得之選取範圍所傳回的節點為根據。
注意
如果在檢視中按鈕的 OnClick 事件中使用 GetContextNodes 方法,則該方法不會根據目前的選取範圍傳回節點,這是因為本來應該留在內容中的控制項失去了焦點。若要避免這種行為,請使用自訂工作窗格、功能表或工具列的 GetContextNodes 方法。
範例
在下列範例中,ViewObject 物件的 GetContextNodes 方法是用於根據目前的內容傳回 XML DOM 節點的集合;接著程式碼會在 XML DOM 節點的集合中執行迴圈,以尋找特定節點;找到時就會更新 field1 的文字。這個範例需要名為 group1 (其中包含名為 field1 的欄位) 的重複區段結構。
XMLNodesCollection contextNodes = thisXDocument.View.GetContextNodes(Type.Missing, Type.Missing);
// Scan the list of context nodes for an field1 node and if one is found
// update its text.
foreach (IXMLDOMNode contextNode in contextNodes)
{
if (contextNode.nodeName == "my:group1")
{
contextNode.selectSingleNode("my:field1").text = "found node";
break;
}
}