---
title: Serializing objects to XML using the API
related:
  - https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration.md
  - https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration/continuous-integration-repository-structure.md
  - https://docs.kentico.com/13/custom-development/database-table-api.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

The Xperience API provides a way to [serialize](http://en.wikipedia.org/wiki/Serialization) many types of objects from the database into XML data. You can use the serialized data to synchronize objects between multiple development instances, in combination with a custom source control system or continuous integration solution. The system ensures that the XML data of matching objects is always identical and consistent (including element and attribute order), even when serialized on different instances of Xperience.

Serialization is supported for the object types listed on the [Object types supported by continuous integration](https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration/object-types-supported-by-continuous-integration.md) page.

> **Note:** **Note**: Only use the serialization API when implementing a custom synchronization solution. If you are using the default [Xperience continuous integration solution](https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration.md), you do not need to write any custom code.

## Getting the serialized XML data for objects

To get the serialized XML data of Xperience objects:

1. Prepare a _BaseInfo (UserInfo, PageTemplateInfo_ etc.) instance representing the object.
2. Call the **Serialize** extension method of the _BaseInfo_ object (provided within the **CMS.DataEngine.Serialization** library).

The _Serialize_ method returns an [XmlElement](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlelement).

```csharp title="Example - Serializing a user"

using System.Xml;
using CMS.DataEngine.Serialization;
using CMS.Membership;

...

// Gets a user object
UserInfo userObject = UserInfo.Provider.Get("admin");

// Creates an XmlElement containing the serialized data of the object
XmlElement objectElement = userObject .Serialize();

// Gets the serialized object data as an XmlDocument
XmlDocument xmlDoc = objectElement.OwnerDocument;

```

## Deserializing XML data into objects

The API provides a way to deserialize XML data back into Xperience objects:

1. Read the XML data as an [XmlDocument](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocument) from the file system (or another source).
2. Get the [XmlElement](https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlelement) representing the given object from the _XmlDocument_ (typically the root element).
3. Call the **Deserialize** extension method of the _XmlElement_ (provided within the **CMS.DataEngine.Serialization** library).

The _Deserialize_ method returns a **DeserializationResult** object, which you can convert to a specific Info object type _(UserInfo, PageTemplateInfo,_ etc.) or a general _BaseInfo._ You can then work with the _Info_ object in any way. For example, use the corresponding _IInfoProvider_ service or _InfoProvider_ class to save the deserialized object into the application's database.

```csharp title="Example - Deserializing a user"

using System.Xml;
using CMS.DataEngine;
using CMS.DataEngine.Serialization;
using CMS.Membership;

...

// Prepare an XmlDocument containing the serialized data of a user
XmlDocument xmlDoc = ... ;

// Gets the root XmlElement of the XmlDocument
XmlElement xmlElement = xmlDoc.DocumentElement;

// Deserializes the XML data into a UserInfo object
DeserializationResult result = xmlElement.Deserialize();
UserInfo user = (UserInfo)result;

// Saves the deserialized user into the database
UserInfo.Provider.Set(user);

```

> **Info:** **IDs of deserialized objects**
>
> When deserializing XML data, the system automatically checks whether the given object already exists in the database (based on the code name or GUID values). If yes, the _Deserialize_ method sets the appropriate object ID when creating the _DeserializationResult_. Saving the deserialized object into the database then updates the existing object instead of creating a new one.

## Serializing all objects to the file system

You can call the API of the [Xperience continuous integration solution](https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration.md) to serialize all objects of the supported types directly to the file system.

Call the **StoreAll** static method of the **CMS.ContinuousIntegration.FileSystemRepositoryManager** class. The method returns a **RepositoryActionResult** object, which you can use to verify the success of the operation and process any errors that occurred.

> **Info:** **Note**: The _StoreAll_ method may have a long run time, depending on the number of objects in your database.

```csharp

using CMS.ContinuousIntegration;

...

// Serializes all supported objects to the file system
RepositoryActionResult result = FileSystemRepositoryManager.StoreAll((logItem) =>
{
    // Optionally create a progress log by processing the 'logItem.Message' strings 
});

// Checks whether the serialization process was successful
if (!result.Success)
{
    foreach (string error in result.Errors)
    {
        // Display or log the errors
    }
}

```

The _StoreAll_ method creates XML files containing the serialized data of objects in the administration project's **CMS\App\_Data\CIRepository** folder (or another location set by the _CMSCIRepositoryPath_ web.config key). You need to ensure that the folder is included in your source control system.

For details about the file system structure, see [Continuous integration repository structure](https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration/continuous-integration-repository-structure.md).

### Restoring objects from the file system

When developing websites in a team, you may often need to load the serialized XML data of objects from a source control system or another developer.

You can restore the data from the XML files in the project's **CIRepository** folder (or another location set by the _CMSCIRepositoryPath_ web.config key) using the **ContinuousIntegration.exe** command line utility available in the Xperience administration project's **CMS\bin** folder. The utility deserializes the objects stored in the project's _CIRepository_ folder and creates, overwrites or removes corresponding data in the given project's database. See [Restoring continuous integration files to the database](https://docs.kentico.com/13/developing-websites/setting-up-continuous-integration/restoring-continuous-integration-files-to-the-database.md) for details.

> **Note:** **Warning**: The restore operation _**deletes**_ all objects of the supported types that do not exist as files in the repository. Only restore if you are sure that the _CIRepository_ folder contains the required state of your object data. We also recommend creating regular backups of your database.

If you wish to implement a custom solution for restoring objects to the database, use the Xperience API.

Call the **RestoreAll** static method of the **CMS.ContinuousIntegration.FileSystemRepositoryManager** class. The method returns a **RepositoryActionResult** object, which you can use to verify the success of the operation and process any errors that occurred.

> **Info:** **Notes**
>
> - To avoid potential collisions in the _CIRepository_ folder, we strongly recommend calling the restore API from an external application while the main Xperience application is not running. For example, you can create a custom console application for this purpose (see [Using the Xperience API externally](https://docs.kentico.com/13/integrating-3rd-party-systems/using-the-xperience-api-externally.md)).
> - The _RestoreAll_ method may have a long run time, depending on the number of objects in your file system repository.

```csharp

using CMS.Base;
using CMS.ContinuousIntegration;

...

RepositoryActionResult result;

// Disables automatic processing of new smart search indexing tasks during the restore operation to prevent potential conflicts
// Indexing tasks are still created, but processed after the restore is finished
using (new CMSActionContext { EnableSmartSearchIndexer = false })
{
    // Deserializes all supported objects from the file system repository and saves them to the database
    // Deletes objects from the database if their representation does not exist in the file system repository
    result = FileSystemRepositoryManager.RestoreAll((logItem) => 
    { 
        // Optionally create a progress log by processing the 'logItem.Message' strings 
    });
}

// Checks whether the restoring process was successful        
if (!result.Success)
{
    foreach (string error in result.Errors)
    {
        // Display or log the errors
    }
}

```
