You receive an error message if you try to programmatically set the text value of an XML node in InfoPath 2003 (826998)



The information in this article applies to:

  • 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, you might receive 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="true" 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="true" attribute and then removes the attribute (if the attribute is found) at run time before the code sets the text value of the node. For example, replace the following code:
{
	// Receive a reference to the element to be filled.
	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
	
	//Set the value of the element.
	objDataElement.text = "10.0";
}
with:
{
	// Receive a reference to the element to be filled.
	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
	
	//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("xsi:nil"))
          objDataElement.removeAttribute("xsi:nil");
	
	//Set the value of the element.
	objDataElement.text = "10.0";
}

MORE INFORMATION

Steps to Reproduce the Behavior

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

    The Field or Group Properties dialog box appears.
  7. Set the Data Type property for the field to Decimal (double), and then click OK.
  8. Add script for the OnClick event of the button as follows:
    1. Right-click the button, and then click Button Properties.

      The Button Properties dialog box appears.
    2. Click the Microsoft Script Editor button.

      Microsoft Script Editor appears.
    3. In the OnClick event of the button, insert the following code:
      {
      	// Receive a reference to the element to be filled.
      	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
      	
      	//Set the value of the element.
      	objDataElement.text = "10.0";
      }
    4. Save your script, and then close Microsoft Script Editor.
  9. Preview the form, and then click the button.

    You receive the error message that is mentioned in the "Symptoms" section of this article.
  10. Click No, and then click OK to dismiss the error message. Close the Preview window.
  11. Modify the OnClick event of the button as follows:
    1. Right-click the button, and then click Button Properties.

      The Button Properties dialog box appears.
    2. Click the Microsoft Script Editor button.

      Microsoft Script Editor appears.
    3. Modify the code for the OnClick event of the button as follows:
      {
      	// Receive a reference to the element to be filled.
      	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
      	
      	//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("xsi:nil"))
                objDataElement.removeAttribute("xsi:nil");
      	
      	//Set the value of the element.
      	objDataElement.text = "10.0";
      }
  12. Preview the form again, and then click the button.

    No error message is returned. The TextBox control displays a value of 10.0.

REFERENCES

For additional information about how to debug script, click the following article number to view the article in the Microsoft Knowledge Base:

827002 How to debug a script for a Microsoft Office InfoPath 2003 form


Modification Type:MinorLast Reviewed:8/19/2004
Keywords:kbprb kberrmsg KB826998 kbAudDeveloper