Microsoft KB Archive/826998

= You receive an error message if you try to programmatically set the text value of an XML node in InfoPath =

Article ID: 826998

Article Last Modified on 5/2/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



SYMPTOMS
If you try to programmatically set the text value of an XML node in Microsoft Office InfoPath 2007 or in Microsoft Office InfoPath 2003, you may receive an error message.

In InfoPath 2007, you may receive an error message that resembles the following:

InfoPath cannot complete this action, because of an error in the form's code.

In InfoPath 2003, you may receive an error message that resembles the following error message:

A run-time error has occurred.

Do you want to debug?

The following error occurred:

'#PCDATA' is in nil content.



CAUSE
This error is generated when you try to programmatically set the text value of an XML node that has the xsi:nil=&quot;true&quot; attribute. When this attribute is set to true, any text value of the XML node produces XML that is not valid. Therefore, Microsoft Office InfoPath does not accept the value, and you receive the error message.



WORKAROUND
To work around this problem, add code that checks for the xsi:nil=&quot;true&quot; attribute and then removes the attribute (if the attribute is found) at run time before the code sets the text value of the node.

Example of code that can cause this error
For example, this error can occur when you use code that resembles the following code.

InfoPath 2007
//Create a Navigator object to access the main DOM. XPathNavigator xnDoc = this.MainDataSource.CreateNavigator; //Create a Navigator object for the field that you want to set. XPathNavigator xnmyDate = xnDoc.SelectSingleNode(&quot;/my:myFields/my:myDate&quot;, this.NamespaceManager);

//Set the value of the myDate field. xnmyDate.SetValue(&quot;2006-10-12&quot;);

//Clean up. xnDoc = null; xnmyDate = null;

InfoPath 2003
{   // Receive a reference to the element to be filled. var objDataElement = XDocument.DOM.selectSingleNode(&quot;/my:myFields/my:field1&quot;); //Set the value of the element. objDataElement.text = &quot;10.0&quot;; }

InfoPath 2007
To prevent this error, you can use the following code example. //Create a Navigator object to access the main DOM. XPathNavigator xnDoc = this.MainDataSource.CreateNavigator;

//Create a Navigator object for the field that you want to set. XPathNavigator xnmyDate = xnDoc.SelectSingleNode(&quot;/my:myFields/my:myDate&quot;, this.NamespaceManager);

//Remove the &quot;nil&quot; attribute. if (xnmyDate.MoveToAttribute(&quot;nil&quot;, &quot;http://www.w3.org/2001/XMLSchema-instance&quot;)) xnmyDate.DeleteSelf;

//Set the value of the myDate field. xnmyDate.SetValue(&quot;2006-10-12&quot;);

//Clean up. xnDoc = null; xnmyDate = null;

InfoPath 2003
{   // Receive a reference to the element to be filled. var objDataElement = XDocument.DOM.selectSingleNode(&quot;/my:myFields/my:field1&quot;); //Determine whether the xsi:nil attribute is set for this //element. If so, remove the xsi:nil attributes so that //the value can be set. if (objDataElement.getAttribute(&quot;xsi:nil&quot;)) objDataElement.removeAttribute(&quot;xsi:nil&quot;); //Set the value of the element. objDataElement.text = &quot;10.0&quot;; }



Steps to reproduce the behavior
To reproduce this behavior, you must create the form and add the code in the example to the form. Then you must test the form. To create the form, follow these steps:

Create the form
 Start Microsoft Office InfoPath. Design a new blank form. In the task pane, click Controls. Verify that the Automatically create data source check box is selected. Add a TextBox control to the form, and then add a Button control to the form. In the task pane, click Data Source. In the Data Source list, right-click field1, and then click Properties.

The Field or Group Properties dialog box appears.</li> Set the Data Type property for the field to Decimal (double), and then click OK. Save the template.</li> Add script for the OnClick event of the button as follows: <ol style="list-style-type: lower-alpha;"> Right-click the button, and then click Button Properties.

The Button Properties dialog box appears.</li> In InfoPath 2007, click Edit Form code. Microsoft Visual Studio Tools for Applications starts.

In InfoPath 2003, click the Microsoft Script Editor button.

Microsoft Script Editor appears.</li>  In the OnClick event of the button, insert the following code example.

InfoPath 2007 //Create a Navigator object to access the main DOM. XPathNavigator xnDoc = this.MainDataSource.CreateNavigator;

//Create a Navigator object for the field that you want to set. XPathNavigator xnmyDate = xnDoc.SelectSingleNode(&quot;/my:myFields/my:myDate&quot;, this.NamespaceManager);

//Set the value of the myDate field. xnmyDate.SetValue(&quot;2006-10-12&quot;);

//Clean up. xnDoc = null; xnmyDate = null; InfoPath 2003 {   // Receive a reference to the element to be filled. var objDataElement = XDocument.DOM.selectSingleNode(&quot;/my:myFields/my:field1&quot;); //Set the value of the element. objDataElement.text = &quot;10.0&quot;; } </li> Save your script, and then close Microsoft Script Editor.</li></ol> </li> In InfoPath 2007, click Save on the File menu.

In InfoPath 2003, click Save on the File menu, and then click Save in the Microsoft Office InfoPath dialog box.</li> In the Save As dialog box, type xmlnode, and then click Save.</li> Click File, and then click Close.</li></ol>

Test the form
<ol> Start InfoPath.</li> In InfoPath 2007, double-click xmlnode under Recently used Forms.

In InfoPath 2003, click xmlnode under Fill out a Form.</li> Click the button.

You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li> In InfoPath 2007, click OK to close the error message.

In InfoPath 2003, click No, and then click OK to close the error message.</li> Open the form in design mode.</li> Modify the OnClick event of the button. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Right-click the button, and then click Button Properties.

The Button Properties dialog box appears.</li> <li>Click the Microsoft Script Editor button.

Microsoft Script Editor appears.</li> <li> Modify the code for the OnClick event of the button as follows.

InfoPath 2007 //Create a Navigator object to access the main DOM XPathNavigator xnDoc = this.MainDataSource.CreateNavigator;

//Create a Navigator object for the field we want to set XPathNavigator xnmyDate = xnDoc.SelectSingleNode(&quot;/my:myFields/my:myDate&quot;, this.NamespaceManager);

//Set the value of the myDate field xnmyDate.SetValue(&quot;2006-10-12&quot;);

//Clean up xnDoc = null; xnmyDate = null; InfoPath 2003 {   // Receive a reference to the element to be filled. var objDataElement = XDocument.DOM.selectSingleNode(&quot;/my:myFields/my:field1&quot;); //Determine whether the xsi:nil attribute is set for this //element. If so, remove the xsi:nil attributes so that //the value can be set. if (objDataElement.getAttribute(&quot;xsi:nil&quot;)) objDataElement.removeAttribute(&quot;xsi:nil&quot;); //Set the value of the element. objDataElement.text = &quot;10.0&quot;; } </li></ol> </li> <li>Preview the form again, and then click the button.

No error message is returned. The TextBox control displays a value of 10.0.</li></ol>

<div class="references_section">