Access Form Data Using the InfoPath 2003 Object Model

When you want to extend the functionality of an InfoPath form, it is often necessary to programmatically access information about the form's underlying XML document, access the data that the XML document contains, or perform some action on the XML document. The InfoPath object model supports accessing and manipulating a form's underlying XML document through the use of the XDocument interface in association with the XDocumentsCollection interface.

The XDocument interface is one of the most useful types within the InfoPath object model because it provides a variety of properties, methods, and events that not only interact with a form's underlying XML document, but also perform many of the actions that are available in the InfoPath user interface. In a managed-code project created using the InfoPath 2003-compatible object model, a variable of type XDocument that is named thisXDocument is automatically defined in the _StartUp method of the class that contains event handlers in your project's form code. You can use the thisXDocument variable in your form's code to access the XDocument interface and its members.

Overview of the XDocumentsCollection Interface

The XDocumentsCollection interface provides the following methods and properties that form developers can use to manage the XDocument objects that the collection contains.

Name Description
Close method
Closes the specified form.
New method
Creates a new form based on an existing form.
NewFromSolution method
Creates a new form based on an existing form template.
NewFromSolutionWithData method
Creates a new InfoPath form using the specified XML data and form template.
Open method
Opens the specified form.
Count property
Returns a count of the number of XDocument objects contained in the collection.
Item property
Returns a reference to the specified XDocument object.

Overview of the XDocument Interface

The XDocument interface provides the following methods and properties, which form developers can use to interact with and perform actions on a form's underlying XML document.

Name Description
GetDataVariable method
Returns the string value of a specified data variable.
GetDOM method
Returns a reference to the XML Document Object Model (DOM) associated with the specified DataObject object.
ImportFile method
Imports (or merges) the specified form with the currently open form.
PrintOut method
Prints the current view of a form.
Query method
Retrieves data from a form's associated data adapter.
Save method
Saves the currently open form.
SaveAs method
Saves the currently open form with the specified name.
SetDataVariable method
Sets the value of a specified data variable.
Submit method
Submits a form according to the submit operation established in design mode.
DataObjects property
Returns a reference to the DataObjects collection.
DOM property
Returns a reference to the XML DOM that is populated with the source XML data of a form.
Errors property
Returns a reference to the Errors collection.
Extension property
Returns a reference to an object representing all of the functions and variables contained in a form code file.
IsDirty property
Returns a Boolean value indicating whether the data in the form has been changed.
IsDOMReadOnly property
Returns a Boolean value indicating whether the XML DOM is set as read-only.
IsNew property
Returns a Boolean value indicating whether the form was saved after it was created.
IsReadOnly property
Returns a Boolean value indicating whether the form is in read-only mode.
IsSigned property
Returns a Boolean value indicating whether the form is digitally signed.
Language property
Specifies or returns the string value of the language used for the form.
QueryAdapter property
Returns a reference to the data adapter object.
Solution property
Returns a reference to the Solution object.
UI property
Returns a reference to the UI object.
URI property
Returns a string value containing the Uniform Resource Identifier (URI) of the form.
View property
Returns a reference to the View object.
ViewInfos property
Returns a reference to the ViewInfos collection.

Using the XDocuments Collection and the XDocument Interfaces

The XDocumentsCollection interface is accessed through the XDocuments property of the Application interface. In a managed-code project created using the InfoPath 2003-compatible object model, you can access the XDocumentsCollection interface by using the thisApplication variable that is instantiated in the _StartUp method of your project's form code. The following lines of code create a variable that references the XDocumentsCollection interface of the current project.

XDocumentsCollection xdocs;
xdocs = thisApplication.XDocuments;
// Write code here to work with the XDocumentsCollection.
Dim xdocs As XDocumentsCollection
xdocs = thisApplication.XDocuments
' Write code here to work with the XDocumentsCollection.

In a managed-code project created using the InfoPath 2003-compatible object model, you can access the XDocument interface by using the thisXDocument variable that is instantiated in the StartUp method of your project's form code. The following line of code uses the thisXDocument variable to access the XDocument interface of the current project to display the URI of the currently open form in an alert message.

thisXDocument.UI.Alert(thisXDocument.URI);
thisXDocument.UI.Alert(thisXDocument.URI)

Note

When you use the XDocument interface to access a form's underlying XML document, you are accessing the XML document that is associated with the currently open form.

A key property of the XDocument interface is the DOM property. This property returns a reference to the XML DOM that is populated with the source XML data of a form. When using the DOM property, you can use any of the properties and methods that are supported by the XML DOM. For example, the following code uses the xml property of the XML DOM to return and display all of the contents of a form's underlying XML document.

string xmldoc;
xmldoc = thisXDocument.DOM.xml;
// Display xml.
thisXDocument.UI.Alert(xmldoc);
Dim xmldoc As String
xmldoc = thisXDocument.DOM.xml
' Display xml.
thisXDocument.UI.Alert(xmldoc)

Note

To learn more about the XML DOM and all of the properties and methods that it supports, see the MSXML SDK on MSDN.