How to deserialize an InfoPath form into a C# or Visual Basic object

Applies to: InfoPath 2007

Learn how to use the Deserialize method of the XmlSerializer class to convert the XML of an InfoPath form into a C# or Visual Basic object.

ADVERTISEMENTS

Problem

You have an InfoPath form and you would like to convert the InfoPath form with all of the data it contains into a C# or Visual Basic object, so that you can easily access the InfoPath form data via properties of the object.

Solution

Use the technique described in How to create a class for an InfoPath form. to create a C# or Visual Basic class for the InfoPath form, and then use the Deserialize method of the XmlSerializer class to deserialize the XML of the InfoPath form into an object.

Discussion

XML deserialization means converting XML into and object. The opposite of XML deserialization is XML serialization, which means converting and object into XML.

You can deserialize an XML form as follows:

  1. Use an existing InfoPath form or create a new one. Here we will use the Sample - Expense Report form template that comes with InfoPath.
  2. Follow the steps in How to create a class for an InfoPath form to create a C# or Visual Basic class for the InfoPath form.
  3. After you have created the class, open the form template in InfoPath.
  4. Add a button control to the form template.
  5. Double-click the button control to open its Properties dialog box.
  6. On the Button Properties dialog box, click Edit Form Code to open Visual Studio Tools for Applications and add a Clicked event handler for the button.
  7. From the Project Explorer window in Visual Studio Tools for Applications, right-click the root node, choose Add and then Existing Item from the context menu, and select and add the class you created in step 2.
  8. Add a using or Imports statement for the System.Xml.Serialization namespace to the form's code.
  9. Add the following C# XML deserialization code to the Clicked event handler of the button:

    // Get a reference to the root node of the InfoPath form.
    XPathNavigator domNav = MainDataSource.CreateNavigator();

    // Read the entire XML of the InfoPath form into an XMLReader
    // object.
    XmlReader reader = domNav.ReadSubtree();
    reader.MoveToContent();

    // Deserialize the InfoPath form into an expenseReport object.
    XmlSerializer xser = new XmlSerializer(typeof(expenseReport));
    expenseReport expRep = (expenseReport)xser.Deserialize(reader);

    Or add the following Visual Basic XML deserialization code to the Clicked event handler of the button:

    ' Get a reference to the root node of the InfoPath form.
    Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()

    ' Read the entire XML of the InfoPath form into an XMLReader
    ' object.
    Dim reader As XmlReader = domNav.ReadSubtree()
    reader.MoveToContent()

    ' Deserialize the InfoPath form into an expenseReport object.
    Dim xser As XmlSerializer = _
    New XmlSerializer(GetType(expenseReport))
    Dim expRep As expenseReport = _
    DirectCast(xser.Deserialize(reader), expenseReport)

The expRep object of type expenseReport should now contain all of the data that the XML of the InfoPath form contained and you should be able to access this data via the properties that are available on the expenseReport class.

Note:
The MSDN documentation on Custom Serialization says:

Note that serialization can allow other code to see or modify object instance data that is otherwise inaccessible. Therefore, code that performs serialization requires the SecurityPermission with the SerializationFormatter flag specified. Under default policy, this permission is not given to Internet-downloaded or intranet code; only code on the local computer is granted this permission.

This means that you will have to give the form template Full Trust to be able to run the XML deserialization code from within the InfoPath form.

 


Related InfoPath Articles:

 

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. 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.

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