Search code examples
c#infopath2010

Iterate over InfoPath fields by Type


Is there anyway to iterate over infopath fields by type?

As in, iterate over all date fields, or all integer fields, etc. I am trying to write a class that performs field validation in code behind the form, as the OOTB field validation doesnt work when submitting the form from code behind. I would like to write a generic class that can be used for any infopath form. Thanks.


Solution

  • You cannot do this by the XML of the InfoPath form only.
    You have to depend on myschema.xsd (you can get this file by renaming you xsn file to cab and extract it or by choosing "Export Source Files" from the publish menu). Now that you have the xsd file and given that the names of the fields are unique in the InfoPath form, you can do the following:
    1- Get the name of the filed from the InfoPath form
    2- Check the data type of that field in myschema.xsd file
    3- Do the validation.

    In short, myschema.xsd file is storing the data types
    <xsd:element name="CurrentAccount" nillable="true" type="xsd:boolean"/>
    But the InfoPath form is just storing the value which may translate to several data types (I just treat it as a string)
    <my:CurrentAccount>false</my:CurrentAccount>