Sample: Serialize and deserialize an entity Instance

This sample shows how to serialize early-bound and late-bound entity instances into an XML format, and how to de-serialize from an XML format to an early-bound entity instance.

You can download the sample from here.

Note

This sample applies to both Dynamics 365 Customer Engagement (on-premises) and Dataverse.

How to run this sample

To obtain a local copy of all samples and build them, follow these steps:

  1. Download or clone the Samples repo so that you have a local copy.
  2. (Optional) Edit the dataverse/App.config file to define a connection string specifying the instance/organization you want to connect to.
  3. Open the sample solution in Visual Studio and press F5 to run the sample. After you specify a connection string in dataverse/App.config, any sample you run will use that connection information.

If you don't specify a connection string in dataverse/App.config file, a dialog opens each time you run the sample, and you'll need to enter information about which instance or org you want to connect to and which credentials you want to use. This dialog caches previous connections so that you can choose a previously used connection.

What this sample does

The DataContractSerializer message is intended to be used in a scenario where it Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.

How this sample works

In order to simulate the scenario described in What this sample does, the sample will do the following:

Setup

  1. Checks for the current version of the org.
  2. The CreateRequiredRecords method creates required sample data for the sample.

Demonstrate

  1. The DataContractSerializer method serializes the contact records into XML and write it to the hard drive.
  2. The earlyBoundSerializer method deserializes the entity instance.

Clean up

Display an option to delete the records created in the Setup. The deletion is optional in case you want to examine the entities and data created by the sample. You can manually delete the records to achieve the same result.