View.GetSelectedNodes method

Office 2013 and later

Gets a reference to an XPathNodeIterator object for iterating over all XML nodes in the current selection of items in a view.

Namespace:  Microsoft.Office.InfoPath
Assembly:  Microsoft.Office.InfoPath (in Microsoft.Office.InfoPath.dll)

public abstract XPathNodeIterator GetSelectedNodes()

Return value

Type: System.Xml.XPath.XPathNodeIterator
An XPathNodeIterator for iterating all over nodes in the current selection.

ExceptionCondition
InvalidOperationException

The GetSelectedNodes method was called from an event handler for the Loading event.

If no items are selected in a view, or if only text is selected, then the XPathNodeIterator object returned by the GetSelectedNodes method has nothing to iterate over.

NoteNote

The GetSelectedNodes method will not return nodes based on the current selection if used in the event handler for the Clicked event of a button in the view, because the focus is lost from the control that is intended to be in context. To avoid this behavior, use GetSelectedNodes from a custom task pane, menu or toolbar.

This member can be accessed only by forms running in the same domain as the currently open form, or by forms that have been granted cross-domain permissions.

This type or member can be accessed only from code running in forms opened in Microsoft InfoPath Filler.

In the following example, the SelectNodes method is used to select the nodes in the first row of a Repeating Table control bound to group2. The GetSelectedNodes method is then used to return a collection of nodes based on the current selection. Finally, the code loops through the collection of nodes displays the name, inner XML, and value of each node.

// Create XPathNavigator and specify XPath for nodes.
XPathNavigator repeatingTableRow1 = 
   MainDataSource.CreateNavigator().SelectSingleNode(
   "/my:myFields/my:group1/my:group2[1]", NamespaceManager);

// Select nodes in specified XPathNavigator.
CurrentView.SelectNodes(repeatingTableRow1);

// Get selected nodes.
XPathNodeIterator selectedNodes = 
   CurrentView.GetSelectedNodes();

// Display the count of selected nodes.
MessageBox.Show(selectedNodes.Count.ToString());

// Loop through collection and display information.
foreach (XPathNavigator selectedNode in selectedNodes)
{
   MessageBox.Show(selectedNode.Name);
   MessageBox.Show(selectedNode.InnerXml);
   MessageBox.Show(selectedNode.Value);
}
Show: