InfoPath Forms Code Error: Schema validation found non-data type errors

As per Microsoft InfoPath blog this error occurs when we are trying to set a nodes element value and the node has an xsi:null attribute. So, before setting any value check if the node has the xsi:nil attribute and if so delete that attribute. Belowcode snippe is from the same blog which can be used as a generic method before setting any value.

public void DeleteNil(XPathNavigator node)

{

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

node.DeleteSelf();

}

I had similar problem while appending rows to a repeating table using the XmlWriter in the code behind for an InfoPath Form. Unfortunately, the code snippet didn’t solved my problem. Examining the xml contained in the node using the innerxml property, I didn’t find any xsi:nil anywhere. However after struggling for 2 hours I was able to find the solution and this problem is specific to the repeating table.
Solution:
The order in which we are appending the values in the code should be in the same order as is in the form design’s data source.

Below is a more detailed explanation. The repeating table in the InfoPath Form design was as follows:

-commentTable
–commentRow
—userID
—commentText
—occurrenceTime

commentTable being the root of the repeating table control. commentRow is the repeating table. The columns within a row are and its order is as show above: userID, commentText, occurrenceTime.

In the code behind of the form, I have my code as follows:


myRoot = MainDataSource.CreateNavigator();
node = myRoot.SelectSingleNode("/my:Main/my:commentTable", NamespaceManager);
DeleteNil(node);
using(XmlWriter writer = node.AppendChild())
{

writer.WriteStartElement("commentRow", myNamespace);
writer.WriteElementString("userID", myNamespace, "user id");
writer.WriteElementString("occurrenceTime", myNamespace, "12/20/08 10:00:00");
writer.WriteElementString("commentText", myNamespace, "test comment");
writer.WriteEndElement();
writer.Close();

}

Everything looks perfect! Err… No!, the order in which I am writing the xml into the node is not in the correct sequence as it is in the InfoPath form design. In my code i am trying to write the ‘occurrenceTime’ before the ‘commentText’ which is not as per the design. So swapping it solved my problem. Though it is silly its important for the xml schema and that is the reason the schema validation fails.

20 Responses

  1. r

  2. nice, this helped. makes perfect sense now that I understand what the problem actually is.

  3. Thank you!

  4. Finally the solution that works. Great!

  5. Thanks for info really helped me to understand where my error came from…

  6. I kept having the validation error problem until I moved the applicable group under another dedicated group with nothing else in it.

    The group for the repeating table was at the same level as many other elements which would have been the problem.

    I therefore now have… /my:ReferralContact/my:grpContactTable/my:ContactHistory with only the relevant three fields within my:ContactHistory

  7. I am still getting the error! What am I doing wrong? I am copying data from a repeating table in the secondary data source to 3 repeating tables in the main data source. Right now I’m only working on the first.

    My repeating table (main ds) structure is
    /my:COBInquiry/my:group1/my:group2/my:field1 and
    /my:COBInquiry/my:group1/my:group2/my:field2

    Code:

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

    private void AddItem(string title, string displayname, XPathNodeIterator current)
    {
    // Group1
    // Group2
    // Field1
    // Field2

    XPathNavigator main = MainDataSource.CreateNavigator();
    XPathNavigator nav = main.SelectSingleNode(“/my:COBInquiry/my:group1”, NamespaceManager);
    DeleteNil(nav);

    using (XmlWriter writer = nav.AppendChild())
    {
    DeleteNil(nav);
    writer.WriteStartElement(“group2”);
    writer.WriteElementString(“field1”, title);
    writer.WriteElementString(“field2”, displayname);
    writer.WriteEndElement();
    writer.Close();
    }

    • You may try the following;

      string myNamespace = NamespaceManager.LookupNamespace(“my”); // “my” or whatever ns: you used in your form xml

      using (XmlWriter writer = nav.AppendChild())
      {
      writer.WriteStartElement(”group2″);
      writer.WriteElementString(”field1″, myNamespace, title);
      writer.WriteElementString(”field2″, myNamespace ,displayname);
      writer.WriteEndElement();
      writer.Close();
      }

  8. hey, I am getting the same error but my scenario is different. I create the form with one template and then change the header of the actually xml to point to different template. This was working great till now, we had some major upgrade and added/removed few fields (we have done this in past and it worked) to the template. Is it the order of the field important inside the group (non-repeating)?

  9. Thank you, this fixed my problem.

  10. Thank you very much for this hint!

  11. hi, help
    i do that step by step but i got the error
    Schema validation found non-data type errors.
    tnx

  12. You are such a gr8 guy……………
    love it!!!

  13. Thanx .. This really help me.. Good job..

  14. Thanx a lot.. this really helped me.

  15. This solved my problem, thank you!

  16. Thanks man I was looking for hours what’s wrong with my code.

  17. Great post! This wasn’t the error I was seeing, but it is good information that I’ll use.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: