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

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="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.

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

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.

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

Propriedades

ID do Artigo: 826998 - Última Revisão: 22/09/2011 - Revisão: 1

Comentários