This documentation is archived and is not being maintained.

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

Office 2007

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.

NameDescription
Close methodCloses the specified form
New methodCreates a new form based on an existing form
NewFromSolution methodCreates a new form based on an existing form template
NewFromSolutionWithData methodCreates a new Microsoft Office InfoPath 2003 form using the specified XML data and form template.
Open methodOpens the specified form
Count propertyReturns a count of the number of XDocument objects contained in the collection
Item propertyReturns 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.

NameDescription
GetDataVariable method Returns the string value of a specified data variable
GetDOM methodReturns a reference to the XML Document Object Model (DOM) associated with the specified DataObject object
ImportFile methodImports (or merges) the specified form with the currently open form
PrintOut methodPrints the current view of a form
Query methodRetrieves data from a form's associated data adapter
Save methodSaves the currently open form
SaveAs methodSaves the currently open form with the specified name
SetDataVariable methodSets the value of a specified data variable
Submit methodSubmits a form according to the submit operation established in design mode
DataObjects propertyReturns a reference to the DataObjects collection
DOM propertyReturns a reference to the XML DOM that is populated with the source XML data of a form
Errors propertyReturns a reference to the Errors collection
Extension propertyReturns a reference to an object representing all of the functions and variables contained in a form's scripting file
IsDirty propertyReturns a Boolean value indicating whether the data in the form has been changed
IsDOMReadOnly propertyReturns a Boolean value indicating whether the XML DOM is set as read-only
IsNew propertyReturns a Boolean value indicating whether the form was saved after it was created
IsReadOnly propertyReturns a Boolean value indicating whether the form is in read-only mode
IsSigned propertyReturns a Boolean value indicating whether the form is digitally signed
Language propertySpecifies or returns the string value of the language used for the form
QueryAdapter propertyReturns a reference to the data adapter object
Solution propertyReturns a reference to the Solution object
UI propertyReturns a reference to the UI object
URI propertyReturns a string value containing the Uniform Resource Identifier (URI) of the form
View propertyReturns a reference to the View object
ViewInfos propertyReturns 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.


Show: