How to create a class for an InfoPath form
Learn how to use the XML Schema Defintion Tool to generate a class for an InfoPath form.
Problem
You have an InfoPath form and would like to create a (C# or Visual Basic) .NET class for it.
Solution
Use the XML Schema Definition Tool (Xsd.exe) that is part of the .NET Framework Tools to create a C# or Visual Basic class for the InfoPath form.
Discussion
You can achieve this as follows:
- In InfoPath, create a form template or use an existing one. Here we will make use of one of the sample form templates that comes with InfoPath, the Sample - Expense Report form template.
- On the File menu, click Save As Source Files and save the form files in a folder on your local computer. There will be a file called myschema.xsd among the files you just saved.
- Close InfoPath.
- Open a Visual Studio 2005 Command Prompt which you can access via Start > Programs > Microsoft Visual Studio 2005 > Visual Studio Tools.
- At the command-line prompt, use the cd command to navigate to the folder where you saved the form files.
To generate a C# class for the form, at the command-line prompt type:
xsd myschema.xsd /c /l:csThis will create a C# file named myschema.cs for the InfoPath form.
To generate a Visual Basic class for the form, at the command-line prompt type:
xsd myschema.xsd /c /l:vbThis will create a Visual Basic file name myschema.vb for the InfoPath form.
Note: Once you have created the class and examine it, you will notice that the name of the class is the same as the name of the root node of the InfoPath form. So if you want the generated class to have a meaningful name other than myFields, which is the default name assigned to the root node of an InfoPath form, you must change the name of the root node in InfoPath to the name you want the generated class to have, before you create the .NET class.
You should now be able to add the .NET class file to your project and use it for example for serializing and deserializing data stored within InfoPath forms that are based on the InfoPath form template you used to generate the class file.
Related InfoPath Articles:
