Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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.

↑ Back to the top


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.

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("/my:myFields/my:myDate", this.NamespaceManager);

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

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

InfoPath 2003

{
// 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";
}

Example of code that can prevent this error

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("/my:myFields/my:myDate", this.NamespaceManager);

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

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

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

InfoPath 2003

{
// 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";
}

↑ Back to the top


More Information

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

  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. Save the template.
  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. 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.
    3. 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("/my:myFields/my:myDate", this.NamespaceManager);

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

      //Clean up.
      xnDoc = null;
      xnmyDate = null;
      InfoPath 2003
      {
      // 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. 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.
  10. In the Save As dialog box, type xmlnode, and then click Save.
  11. Click File, and then click Close.

Test the form

  1. Start InfoPath.
  2. In InfoPath 2007, double-click xmlnode under Recently used Forms.

    In InfoPath 2003, click xmlnode under Fill out a Form.
  3. Click the button.

    You receive the error message that is mentioned in the "Symptoms" section.
  4. In InfoPath 2007, click OK to close the error message.

    In InfoPath 2003, click No, and then click OK to close the error message.
  5. Open the form in design mode.
  6. Modify the OnClick event of the button. To do this, follow these steps:
    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.

      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("/my:myFields/my:myDate", this.NamespaceManager);

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

      //Clean up
      xnDoc = null;
      xnmyDate = null;
      InfoPath 2003
      {
      // 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";
      }
  7. Preview the form again, and then click the button.

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

↑ Back to the top


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

↑ Back to the top


Keywords: kboffice12yes, kbfreshness2006, kbsweptinfopath2003sp1, kbsweptsoltax, kbprb, kberrmsg, kb

↑ Back to the top

Article Info
Article ID : 826998
Revision : 3
Created on : 4/17/2018
Published on : 4/19/2018
Exists online : False
Views : 440