Pergunta

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.

Foi útil?

Solução

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>

Outras dicas

On great possibility is to do with Linq. E.g. you have a list of objects, in which you put different things, but you only iterate over the strings:

var fields = new List<object>();
fields.Add(42);
fields.Add("one");
fields.Add(84);
fields.Add("two");
foreach (string s in fields.OfType<string>())
{
    Debug.Print(s);
}

You will get in the output window:

one
two
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top