Infopath 2010 设置具有 xsi:nil 属性的节点的值

对于某些数据类型,尝试以编程方式设置空白域的值时将引发"架构验证过程发现非数据类型错误"错误。导致出现此错误的原因通常是,元素的 xsi:nil(该链接可能指向英文页面) 属性设置为 true。如果您检查表单中空白域的基础 XML 元素,您会看到此设置。例如,以下空白日期域的 XML 段的 xsi:nil 属性设置为 true

XML  复制代码
<my:myDate xsi:nil="true"></my:myDate>

如果 xsi:nil 属性设置为 true,则表示相应元素存在但没有值,或者换句话说就是元素为 空。如果您尝试以编程方式设置这样一个节点的值,InfoPath 将显示"架构验证过程发现非数据类型错误"消息,原因是元素当前标记为 空。InfoPath 将以下数据类型的 空 域的 xsi:nil 属性设置为 true

  • Whole Number (integer)

  • Decimal (double)

  • Date (date)

  • Time (time)

  • Date and Time (dateTime)

为了防止发生此错误,您的代码必须测试 xsi:nil 属性。如果该属性存在,则在设置节点值前删除该属性。下面的子例程采用一个位于您要设置的节点上的 XpathNavigator 对象,检查 nil 属性,然后将其删除(如果它存在)。

C#  复制代码
public void DeleteNil(XPathNavigator node)
{
   if (node.MoveToAttribute(
      "nil", "http://www.w3.org/2001/XMLSchema-instance"))
      node.DeleteSelf();
}
Visual Basic  复制代码
Public Sub DeleteNil(ByVal node As XPathNavigator)
   If (node.MoveToAttribute( _
      "nil", "http://www.w3.org/2001/XMLSchema-instance")) Then
      node.DeleteSelf()
   End If
End Sub

在尝试设置可能具有 xsi:nil 属性的某数据类型的域之前,您可以调用此子例程,如以下设置日期域的示例所示。

C#  复制代码
// Access the main data source.
XPathNavigator myForm = this.CreateNavigator();

// Select the field.
XPathNavigator myDate = myForm.SelectSingleNode("/my:myFields/my:myDate", NamespaceManager);

// Check for and remove the "nil" attribute.
DeleteNil(myDate);

// Build the current date in the proper format. (yyyy-mm-dd)
string curDate = DateTime.Today.Year + "-" + DateTime.Today.Month + 
   "-" + DateTime.Today.Day;

// Set the value of the myDate field.
myDate.SetValue(strCurDate);
Visual Basic  复制代码
' Access the main data source.
Dim myForm As XPathNavigator = Me.CreateNavigator()

' Select the field.
Dim myDate As XPathNavigator = _
   myForm.SelectSingleNode("/my:myFields/my:myDate", NamespaceManager)

' Check for and remove the "nil" attribute.
DeleteNil(myDate)

' Build the current date in the proper format. (yyyy-mm-dd)
Dim curDate As String = DateTime.Today.Year + "-" + _
   DateTime.Today.Month + "-" + DateTime.Today.Day

' Set the value of the myDate field.
myDate.SetValue(strCurDate)
注释

尽管 InfoPath 中的 XPathNavigator 对象实现可公开 SetTypedValue 方法(用于设置使用特定类型值的节点),但是 InfoPath 不会实现该方法。您必须改用 SetValue 方法,并为节点的数据类型传递一个正确格式的字符串值。

posted @ 2010-12-21 15:26  annpaul  阅读(1142)  评论(0编辑  收藏  举报