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 方法,并为节点的数据类型传递一个正确格式的字符串值。

转载于:https://www.cnblogs.com/annpaul/archive/2010/12/21/1912747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值