Export (0) Print
Expand All

XmlForm.MainDataSource property

Gets a DataSource object that represents the main data source of the form.

Namespace:  Microsoft.Office.InfoPath
Assembly:  Microsoft.Office.InfoPath (in Microsoft.Office.InfoPath.dll)

public abstract DataSource MainDataSource { get; }

Property value

Type: Microsoft.Office.InfoPath.DataSource
A DataSource that represents the main data source of the form.

The MainDataSource property gets a reference to a DataSource object that represents the underlying XML document of the form. This object can then be used to access the properties and methods of the DataSource class to work with the data in the underlying XML document of the form.

This member can be accessed only by forms running in the same domain as the currently open form, or by forms that have been granted cross-domain permissions.

This type or member can be accessed from code running in forms opened in Microsoft InfoPath Filler or in a Web browser.

In the following code example, an event handler for the Loading event first uses the MainDataSource property to call the CreateNavigator method of the DataSource class to create an instance of the System.Xml.XPath.XPathNavigator class positioned at the root node of the underlying XML document of the form. Then, it uses the SelectSingleNode method of the XPathNavigator object to navigate to the employee field (resolving namespace prefixes using the NamespaceManager property) and set the field to the current user's username.

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
   // Create XPathNavigator positioned at the root of the main data 
   // source.
   XPathNavigator myNav = this.MainDataSource.CreateNavigator()

   // Select employee field and set value to username.
   myNav.SelectSingleNode("//my:employee",this.NamespaceManager).
      SetValue(System.Environment.UserName);
}
Show:
© 2014 Microsoft