Working with Data in an InfoPath Form's Underlying XML Document

When you want to extend the functionality of a Microsoft Office InfoPath 2007 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 access and manipulation of a form's underlying XML document through the use of the XDocument object in association with the XDocuments collection.

The XDocument object is one of the most useful objects 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 within the InfoPath user interface.

In contrast to other forms solutions, updating the value of a control in InfoPath requires changing the underlying data in the XML document to which the control is bound instead of updating the control itself. If, for example, multiple controls are bound to the same node, their associated values are updated at the same time with a single change to the underlying XML document, even though they may appear in different views in the form template.

Overview of the XDocuments collection

The XDocuments collection 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 Microsoft Office InfoPath 2003 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 object

The XDocument object provides the following methods and properties that 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's scripting 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
Bb250993.vs_note(en-us,office.12).gif  Note
The XDocument object also implements events that are not shown in the preceding table. For information about these events, see the XDocument object language reference topic in the InfoPath Developer's Reference.

Using the XDocuments collection and the XDocument object

The XDocuments collection is accessed through the XDocuments property of the Application object. When using the XDocuments collection to access a form's underlying XML document, you pass a long integer or a string to the Item method to return a reference to an XDocument object. For example, if you have a form named MyForm, both of the following lines of code will return a reference to the same XDocument object if it is the first XDocument object in the XDocuments collection:

  var objXDocs;

objXDocs = Application.XDocuments(0); // is the same as using... objXDocs = Application.XDocuments("MyForm");

However, it is much simpler to access a form's underlying XML document by using the XDocument object directly, without using the XDocuments collection. This is possible because the XDocument object is embedded directly in the InfoPath script engine. For example, the following code performs the same operation as the previous example:

  var objXDoc;
objXDoc = XDocument;
Bb250993.vs_note(en-us,office.12).gif  Note
When you use the XDocument object 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 object 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 all of the contents of a form's underlying XML document:

  var strXML;
strXML = XDocument.DOM.xml;
Bb250993.vs_note(en-us,office.12).gif  Note
To learn more about the XML DOM and all of the properties and methods that it supports, see the MSXML 5.0 SDK documentation in the Microsoft Script Editor (MSE) Help system.