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.
You have an InfoPath form and would like to create a (C# or Visual Basic) .NET class for it.
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.
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:cs
This 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:vb
This 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.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- How to deserialize an InfoPath form into a C# or Visual Basic object
- Programmatically retrieve an InfoPath form from a SharePoint library
- Programmatically create an InfoPath form from scratch using a Console application and C# code
- How to loop through items in a repeating table in InfoPath 2007
- 4 Ways to programmatically add a row to a repeating table in InfoPath