XmlForm Class (Microsoft.Office.InfoPath)
Assembly: Microsoft.Office.InfoPath (in microsoft.office.infopath.dll)
The XmlForm class is a key object in the InfoPath managed code object model that provides properties, methods, and events that can be used to programmatically interact with and manipulate the XML data in the underlying XML document of a form. The XmlForm class is the equivalent in most respects of the XDocument object of the InfoPath scripting (COM) and InfoPath 2003-compatible (Microsoft.Office.InfoPath.SemiTrust) object models.
While XmlForm objects can be accessed from the XmlFormCollection class using the XmlForms property of the Application class, in most cases, when working with form template business logic you will access the XmlForm class and its members using the this (in C#) or Me (in Visual Basic) keywords without going through the collection.
For example, the following code samples of event handlers for the Loading event use the this or Me keywords to access the MainDataSource and NamespaceManager properties of the XmlForm class. These properties are used to work with an instance of the System.Xml.XPath.XPathNavigator class to navigate to the employee field (resolving namespace prefixes using the NamespaceManager property) and set the field to the current user's username.
Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As LoadingEventArgs) ' Create XPathNavigator positioned at the root of the main data ' source. Dim myNav As XPathNavigator = Me.MainDataSource.CreateNavigator() ' Select employee field and set value to username. myNav.SelectSingleNode("//my:employee", Me.NamespaceManager). _ SetValue(System.Environment.UserName) End Sub
It is not necessary to explicitly reference the this or Me keywords to access the members of the XmlForm class in form code. For example, the lines of code to access the MainDataSource property in the previous C# and Visual Basic examples can be written as follows:
XPathNavigator myNav = MainDataSource.CreateNavigator() Dim myNav As XPathNavigator = MainDataSource.CreateNavigator()
However, using the this or Me keywords when writing code does facilitate IntelliSense statement completion, which lists the members of the XmlForm class when writing business logic code in a form template's FormCode module.
When working with managed code, the source XML data of a form is accessed through the MainDataSource property which returns an instance of the DataSource class that represents the main data source of the form. The CreateNavigator method of the DataSource class is then used to create an instance of the System.Xml.XPath.XPathNavigator class which is positioned at the root of the form's underlying XML document. The members of the XPathNavigator class can then be used to navigate, read from and write to the form data. For more information on using the XPathNavigator class in InfoPath form code, see How to: Work with System.Xml from Managed-code Form Templates.
The XmlForm class also provides additional properties that can be used to get information about the form and its underlying XML document. It also provides methods that can be used to perform various actions with the form, such as printing, saving, and submitting. While in the scripting (COM) object model the XDocument object implements form-level events, in the managed code object model, form-level events, such as loading a form, switching views, or a merge operation, are implemented by the FormEvents class. Additionally, events raised by changes to the underlying XML document itself are implemented by the XmlEvents class
The XmlForm object can be accessed from the XmlFormCollection and Window classes in the InfoPath object model. The following table summarizes these locations where the XmlForm object is available.
Provides the XmlForm property for accessing the XmlForm object associated with the window.
In the InfoPath 2003-compatible managed code object model (the types and members of the Microsoft.Office.Interop.InfoPath.SemiTrust namespace), which was first introduced in InfoPath 2003 Service Pack 1 in conjunction with the Microsoft Office InfoPath 2003 Toolkit for Visual Studio .NET, the following classes provided an XDocument property for accessing the underlying XML document of a form during events: DataDOMEventObject, DocActionEventObject, DocEventObject, DocReturnEventObject, and VersionUpgradeEventObject classes.
In the new InfoPath managed code object model (the types and members of the Microsoft.Office.InfoPath namespace), you use the this (in C#) or Me (in Visual Basic) keywords to access the XmlForm class and its members from event handlers and from other code in your form template's business logic.