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.

ADVERTISEMENTS

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:

  1. 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.
  2. 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.
  3. Close InfoPath.
  4. Open a Visual Studio 2005 Command Prompt which you can access via Start > Programs > Microsoft Visual Studio 2005 > Visual Studio Tools.
  5. At the command-line prompt, use the cd command to navigate to the folder where you saved the form files.
  6. 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.

 
 Subscribe for updates via RSS or Email

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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services