How to deserialize an InfoPath form into a C# or Visual Basic object
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.
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.
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.
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:
- Use an existing InfoPath form or create a new one. Here we will use the Sample - Expense Report form template that comes with InfoPath.
- 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.
- After you have created the class, open the form template in InfoPath.
- Add a button control to the form template.
- Double-click the button control to open its Properties dialog box.
- 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.
- 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.
- Add a using or Imports statement for the System.Xml.Serialization namespace to the form's code.
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
XmlReader reader = domNav.ReadSubtree();
// 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
Dim reader As XmlReader = domNav.ReadSubtree()
' Deserialize the InfoPath form into an expenseReport object.
Dim xser As XmlSerializer = _
Dim expRep As 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.
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.
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 create a class for an InfoPath form
- Programmatically retrieve the value of an InfoPath form field using .NET code
- Programmatically retrieve an InfoPath form from a SharePoint library
- Programmatically create an InfoPath form from scratch using a Console application and C# code
- 4 Ways to programmatically add a row to a repeating table in InfoPath