Udostępnij za pośrednictwem


The xsi:nil attribute

Have you ever tried to programmatically set a value on a field only to get a “schema validation” error? Many times, this error is caused by the “nillable” attribute being present on the node. The nillable attribute is a special attribute that can appear on an xsd:element within an XML schema. If an element has the xsi:nil attribute specified, it indicates that the element is present but has no value, and therefore no content is associated with it.

However, if you attempt to programmatically set a value on this node and the nillable attribute is present, you will get an error similar to: “Schema validation found non-data type errors.” You will find the nillable attribute is typically present on the following data types:

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

To resolve this error, your code will simply need to test if the nil attribute is present and if so, remove that attribute before setting the value on the node. The following sample procedure takes an XpathNavigator object, checks that node for the nil attribute and if it exists deletes the attribute:

public void DeleteNil(XPathNavigator node)

{

if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))

      node.DeleteSelf();

}

The above procedure is generic - you can easily call this procedure as needed before programmatically trying to set the value of a field. As an example, this code is called from the click event of a button:

//Create a Navigator object for the main data source

XPathNavigator xn = this.MainDataSource.CreateNavigator();

//Create a navigator object for the field (node)

//where we want to set the current date value

XPathNavigator xnfield1 = xn.SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);

//Check if the "nil" attribute exists on this node

DeleteNil(xnfield1);

//Create a new dateTime object for the current date

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

xnfield1.SetValue(curDate.GetDateTimeFormats().GetValue(5).ToString());

Scott Heim
Support Engineer

Comments

  • Anonymous
    November 29, 2006
    PingBack from http://attribute.newstack.com/the-xsinil-attribute/

  • Anonymous
    June 02, 2007
    I have been facing this error everytime I start an InfoPath project, and I keep forgetting the reason

  • Anonymous
    July 20, 2007
    I have been facing this error everytime I start an InfoPath project, and I keep forgetting the reason

  • Anonymous
    March 04, 2008
    Why would you get this error during an AppendChild?  Is there any way to debug the problem?  If I take the same xml and manually append it to an existing form, then open the form, Infopath doesn't complain.  Only in code. Please help MSFT!

  • Anonymous
    March 04, 2008
    Hi BobC, What is the error you are getting with the xsi:nil attribute as in this sample? Scott

  • Anonymous
    March 04, 2008
    I was trying to append a row to a repeating section, and got the error on the Append action.  Found that If I restructured the schema to have the repeating group within a group, and used the AddRow code from http://blogs.msdn.com/infopath/archive/2006/12/15/another-way-of-sorting-repeating-data.aspx#8036694 Thanks.

  • Anonymous
    June 23, 2008
    最近一直和infopath表单打交道,碰到的问题也比较多,刚刚就碰到一个在程序中修改infopath表单中域的内容时出错的问题,写出来与大家共享一下,我想这个问题,可能玩infopath的话,迟早会碰...

  • Anonymous
    November 07, 2008
    If you try to programaticaly set/remove values of fields in InfoPath, you might have come across the

  • Anonymous
    November 15, 2008
    I tried to add a new row at the end of a repeating table programmatically, and received the error after all. The elements of the row are not of any of the data types mentioned in this post, and none of them resulted with attribute of xsl:nil after modified by my program, so I don't even know where I can apply the DeleteNil method to...How to fix this issue? Any idea is appreciated, thank you!

  • Anonymous
    November 17, 2008
    Hi samhuang, This error may be cause by the way in which you are appending the child via code. Can you provide me with an overview of your data structure and what code you are using to append the child row? (I know you are using the AppendChild method but what exactly are you passing to this method?) Scott

  • Anonymous
    March 05, 2012
    Wow ... thanks much .. I thought I was going nutz!!! Much appreciated.

  • Anonymous
    November 27, 2012
    Thank you for sharing. Great Post.