Microsoft KB Archive/815812

= How to serialize an object to XML by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815812

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 815813.



For a Microsoft Visual Basic .NET version of this article, see 315703.

IN THIS TASK

 * SUMMARY
 * Requirements
 * XML Serialization
 * Complete Code Listing
 * Verify That It Works
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article describes how to serialize an object to XML by using Visual C++ .NET or Visual C++ 2005. You can use this method to persist the state of an object. You can also use this method to clone an object by deserializing the XML back to the new object.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * General familiarity with XML
 * General familiarity with Visual C++ .NET or Visual C++ 2005

back to the top

XML Serialization
Serialization is the process of converting an object into a form that can be easily transported. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. You can do this with the System::Xml::Serialization namespace.

Create a console application that creates an object, and then serializes the state of an object to XML. To do this, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. In Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual Studio 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates. In the Name text box, type 815812 . Switch to Class View. To do this, click Class View on the View menu. Right-click 815812, point to Add, and then click Add Class.</li> In the Add Class dialog box, click Generic C++ Class under Templates, and then click Open.

Note In Visual Studio 2005, click C++ Class under Templates, and then click Add.</li>  In the Generic C++ Class Wizard, type Person in the Class name text box, and then click Finish. The Person class appears as follows:
 * 1) pragma once

class Person { public: Person(void); ~Person(void); }; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

To make the Person class a public managed C++ class, add the __gc public keyword before the Person class: __gc public class Person { public: Person(void); ~Person(void); }; </li> Open the Person.h file.</li>  Use the using directive on the System namespace so that you do not have to qualify declarations from these namespaces later in your code. Paste the following code in the Person.h file: using namespace System; // This shortcut avoids you having to type System::String </li>  Add the following three public member variables to the Person class: String* firstName; String* middleNI; String* lastName; </li> Switch to the Code window for the 815812.CPP file.</li>  Use System.dll and System.XML.dll as follows: </li>  Use the using directive on the System::XML and System::Xml::Serialization namespaces so that you do not have to qualify declarations from these namespaces later in your code: using namespace System; using namespace System::Xml; using namespace System::Xml::Serialization; </li>  In the void Main function, declare and create an instance of the Person class: Person* p = new Person; </li>  Set the properties of the Person object: p->firstName = S&quot;Jeff&quot;; p->middleNI = S&quot;A&quot;; p->lastName = S&quot;Price&quot;; </li>  The Xml::Serialization namespace contains an XmlSerializer class that serializes an object to XML. When you create an instance for the XmlSerializer class, you pass the type of the class that you want to serialize to its constructor: XmlSerializer* x = new XmlSerializer(p->GetType); </li>  Use the Serialize method to serialize an object to XML. Serialize is overloaded and can send the output to a TextWriter, a Stream, or an XMLWriter object. You can use the following code to send the output to the console: x->Serialize(Console::Out,p); Console::WriteLine; Console::ReadLine; </li></ol>
 * 1) pragma once
 * 1) include &quot;Person.h&quot;
 * 2) using <System.Dll>
 * 3) using <System.XML.Dll>

back to the top

815812.cpp
// This is the main project file for the Visual C++ application project // that is generated by using an Application Wizard.


 * 1) include <stdafx.h>
 * 2) using <mscorlib.dll>


 * 1) include &quot;Person.h&quot;

using namespace System; using namespace System::Xml; using namespace System::Xml::Serialization;
 * 1) using <System.Dll>
 * 2) using <System.XML.Dll>

int _tmain {   Person* p = new Person; p->firstName = S&quot;Jeff&quot;; p->middleNI = S&quot;A&quot;; p->lastName = S&quot;Price&quot;; XmlSerializer* x = new XmlSerializer(p->GetType);

x->Serialize(Console::Out,p); Console::WriteLine; Console::ReadLine; return 0; }

Person.h
using namespace System;
 * 1) pragma once

__gc public class Person { public: Person(void); ~Person(void); String* firstName; String* middleNI; String* lastName;

}; back to the top

Verify That It Works
To verify that your project works, press the CTRL+F5 key combination to run the project. A Person object is created and populated with the values that you typed. This state is serialized to XML. The Console window displays the following: <?xml version=&quot;1.0&quot; encoding=&quot;IBM437&quot;?> <Person xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3 .org/2001/XMLSchema-instance&quot;> <firstName>Jeff</firstName> <middleNI>A</middleNI> <lastName>Price</lastName> </Person> back to the top

Troubleshooting
The Xml::Serialization::XmlSerializer object performs only shallow serialization. If you want to serialize the private variables of an object or child objects, you must use deep serialization.

back to the top

<div class="references_section">