Share via


How to: Work with the XPathNavigator and XPathNodeIterator Classes

To access and manipulate the XML data in form template data sources, many members of the managed code object model provided by the Microsoft.Office.InfoPath namespace either create or can be passed an instance of the XPathNavigator class of the System.Xml.XPath namespace. After you have access to an XPathNavigator object returned by an InfoPath object model member, you can use the properties and methods of the XPathNavigator class to work with the data.

The most commonly used member of the Microsoft.Office.InfoPath namespace that utilizes the XPathNavigator class is the CreateNavigator method of the DataSource class, which allows you to work with the stored data represented by a DataSource object. The CreateNavigator method creates an XPathNavigator object positioned at the root of the data source represented by the DataSource object.

Note

If you are familiar with using MSXML5 from script to work with data in Microsoft InfoPath 2003, you can think of the CreateNavigator method as the replacement for the DOM property of the DataObject.

For example, the following code sample shows how to create an XPathNavigator object positioned at the root of a data source named "CityList" using the CreateNavigator method, and then use the OuterXml property of the XPathNavigator class to display the XML returned in a message box.

XPathNavigator myNavigator = 
   this.DataSources["CityList"].CreateNavigator();
MessageBox.Show("Data source XML: " + myNavigator.OuterXml.ToString());
Dim myNavigator As XPathNavigator  = 
   Me.DataSources("CityList").CreateNavigator()
MessageBox.Show("Data source XML: " & myNavigator.OuterXml.ToString())

InfoPath Object Model Members That Use the XPathNavigator and XPathNodeIterator Classes

The following table provides a summary of all of the members of the Microsoft.Office.InfoPath namespace that utilize the XPathNavigator class to access, manipulate, or submit XML data.

Parent Class Member

AdoQueryConnection

BuildSqlFromXmlNodes method

AdoSubmitConnection

BuildSqlFromXmlNodes method

ClickedEventArgs

Source property

ContextChangedEventArgs

Context property

DataSource

CreateNavigator method

GetNamedNodeProperty method

SetNamedNodeProperty method

EmailSubmitConnection

Execute method

FileQueryConnection

Execute method

FileSubmitConnection

Execute method

FormError

Site property

FormErrorCollection

Add methods

FormTemplate

Manifest property

MergeEventArgs

Xml property

SharepointListQueryConnection

Execute method

Signature

SignatureBlockXmlNode property

SignedDataBlock

SignatureContainer property

View

GetContextNodes methods

SelectNodes methods

SelectText methods

WebServiceConnection

Execute method

GenerateDataSetDiffGram method

XmlEventArgs

OldParent property

Site property

XmlForm

MainDataSource property, which returns a DataSource object that in turn provides the CreateNavigator method for creating an XPathNavigator object positioned at the root of the form's underlying XML document (main data source).

MergeForm method

XmlFormCollection

NewFromFormTemplate method

XmlValidatingEventArgs

ReportError methods

In addition to the InfoPath object model members that return or accept an XPathNavigator object, the following methods return an instance of the XPathNodeIterator class of the System.Xml.XPath namespace for iterating over the XML nodes of items that are specified or selected in a view.

Parent Class Member

View

GetContextNodes methods

GetSelectedNodes method

For information on the properties and methods of the XPathNavigator and XPathNodeIterator classes, search the .NET Framework Reference Documentation.

Using the XPathNavigator and XPathNodeIterator Classes to Work with Data Selected in a View

The following example uses members of both the XPathNavigator and XPathNodeIterator classes to work with form data in the following sequence:

  1. The CreateNavigator method of the DataSource class is used to create an XPathNavigator object variable named repeatingTableRow1, which by default is positioned at the root of the underlying XML document of the form (the main data source).

  2. The SelectSingleNode method of the XPathNavigator class is used to move the position of the XPathNavigator object to the first row of a Repeating Table control bound to group2 in the data source.

  3. The repeatingTableRow1 object variable is passed to the SelectNodes method of the View class to select the nodes in that row.

  4. An XPathNodeIterator object variable named selectedNodes is declared and the GetSelectedNodes method of the View class is used populate the XPathNodeIterator object with the selected nodes.

  5. The Count property of the XPathNodeIterator class is used to display the number of nodes contained in the selectedNodes object variable.

  6. A For/Each loop is used to iterate over the nodes in the selectedNodes object variable and display information about each node using the Name, InnerXml, and Value properties of the XPathNavigator class.

// 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);
}
' Create XPathNavigator and specify XPath for nodes.
Dim repeatingTableRow1 As XPathNavigator  = _
   CreateNavigator().SelectSingleNode( _
   "/my:myFields/my:group1/my:group2[1]", NamespaceManager)

' Select nodes in specified XPathNavigator.
CurrentView.SelectNodes(repeatingTableRow1)

' Get selected nodes.
Dim selectedNodes As XPathNodeIterator = _
   CurrentView.GetSelectedNodes()

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

' Loop through collection and display information.
Dim selectedNode As XPathNavigator
For Each selectedNode In selectedNodes
   MessageBox.Show(selectedNode.Name)
   MessageBox.Show(selectedNode.InnerXml)
   MessageBox.Show(selectedNode.Value)
Next