How to Serialize a Profile as XML

Commerce Server 2007
For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

Serializing a profile as XML will retrieve a user profile and create a single XML string that represents the data or schema representation of the profile, or both. This can be a useful mechanism when Commerce Server must communicate with a back-end system or an off-line system. Because serialized XML is merely a string, it can be used to put data onto a queue or transmit data through a Web service.

Bb219176.alert_caution(en-US,CS.70).gifImportant Note:

Serializing a profile as XML is not a high-performance operation. Therefore, be careful if you are using it in your Commerce Server site code.

To serialize a profile

  1. Create a ProfileContext object.

  2. Retrieve the profile by calling the GetProfile method of the ProfileContext object. You can use any valid key, such as eMail address, to retrieve the profile.

  3. Call the GetXml method of the profile object to retrieve the serialized XML representation of the profile. Make sure that you specify the ProfileXmlRetrieve and ProfileXmlFormat values.

The following code example shows how to serialize a profile as XML. It creates a function called GetSerializedProfile and accepts a single string parameter, which is the eMail address to retrieve from the Profiles System. The code then calls the GetXml method to serialize the profile as XML and returns the serialized string from the function.

private string GetSerializedProfile(string eMail)
        // Get the Profiles run-time object.
        ProfileContext ctxt = CommerceContext.Current.ProfileSystem;

        // Get the profile using the eMail variable.
        Profile prof = ctxt.GetProfile("email_address", eMail, "UserObject");

        // Serialize as XML.
        string profXML = prof.GetXml(ProfileXmlRetrieve.SchemaAndData, ProfileXmlFormat.ElementCentric);

        // Return an XML string.
        return profXML;
    catch (CommerceProfileSystemException ex)
        Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message.ToString());
        return null;