风中的猪儿

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
最近一直和infopath表单打交道,碰到的问题也比较多,刚刚就碰到一个在程序中修改infopath表单中域的内容时出错的问题,写出来与大家共享一下,我想这个问题,可能玩infopath的话,迟早会碰上的吧。
具体表现就是在代码中对一些值类型的域进行更改的时候,代码报错,出错信息:"Schema validation found non-data type errors."
小弟对xml schema真的不是很了解,所以找了半天,最后总算在infopath team blog上找到标准答案
文章地址:http://blogs.msdn.com/infopath/archive/2006/11/28/the-xsi-nil-attribute.aspx
 

xsi:nil属性

你是否曾经在程序中设置域值时碰到schema valiadation错误?很多时候这个错误的原因是节点定义了“nillable”属性. Nillable属性是一个特殊的属性,它能出现在一个XML schemaxsd:elment.如果一个元素定义了xsi:nil属性,意味着这个元素被定义了,但是没有值,因此它是没有内容的。

然而,如果你试图在程序中给一个定义了nillable属性的节点设置值,你会得到一个错误信息:“Schema validation found non-data type errors.”。你可能会发现nillable属性常被定义在下面几种数据类型上:

  • Whole Number (integer)
  • Decimal (double)
  • Date (date)
  • Time (time)
  • Date and Time (dateTime)

要解决这个问题,你的代码需要先检测nil属性是否被定义了。如果定义了,那需要在给节点设置值之前移除这个属性。下面这段简单的程序使用一个XpathNavigator类,检查节点的node是否有nil属性。如果存在则删除掉。

public void DeleteNil(XPathNavigator node)
{
if (node.MoveToAttribute("nil""http://www.w3.org/2001/XMLSchema-instance"))
      node.DeleteSelf();
}


上面这段代码是通用的,你能很简单的调用这段代码在你需要编程改变一个域的值之前。下面是一个例子,这段代码被一个按钮的click事件调用。

//Create a Navigator object for the main data source
// 为主数据源创建一个Navigator对象
XPathNavigator xn = this.MainDataSource.CreateNavigator();

//Create a navigator object for the field (node) 
//where we want to set the current date value
// 为你要设置当前日期值的一个域(节点)创建一个navigator对象
XPathNavigator xnfield1 = xn.SelectSingleNode("/my:myFields/my:field1"this.NamespaceManager);

//Check if the "nil" attribute exists on this node
// 检查节点是否存在 “nil”属性
DeleteNil(xnfield1);

//Create a new dateTime object for the current date
// 创建一个包含当前日期的新的dateTime对象
DateTime curDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);

//Set the value of field1 to the current date in the 
//correct format: yyyy-mm-dd
// 设置field1的值为使用”yyyy-mm-dd”格式的当前日期
xnfield1.SetValue(curDate.GetDateTimeFormats().GetValue(5).ToString());


posted on 2008-06-24 14:20  风中的猪儿  阅读(810)  评论(1编辑  收藏  举报