Serializing Your Structs Using XML

Environment: VC++ 5.0-6.0, Win95/98/NT 4.0/2K/XP

Introduction

This article shows how to save a struct into an XML file (using STL), and load it back using Microsoft's MSXML 3.0 parser. If you have Microsoft's MSXML 4.0 parser installed, modify the stdafx.h file to use MSXML4 instead of MSXML3. This code can be modified to use a class instead of a struct.

Using the Code

To implement this code in your project, add a call to initialize OLE support by inserting a call to ::AfxOleInit in the application class' InitInstance function. In my demo, this class is called CSerializeApp.

BOOL CSerializeApp::InitInstance()
{
   AfxEnableControlContainer();

   .
   .
   .

   ::AfxOleInit();

   // Because the dialog has been closed, return FALSE so
   // that we exit the application, rather than start the
   // application's message pump.
   return FALSE;
}

To import the Microsoft XML Parser typelib (OLE type library), add the following lines before the stdafx.h file's closing #endif. directive.

#import <msxml3.dll> named_guids
using namespace MSXML2;

Replace the 3 in msxml3.dll above with 4 if you have Microsoft's MSXML 4.0 parser installed. In addition, include the following to the stdafx.h file, right before the #import above:

#include <atlbase.h>         // Needed for CComVariant.

Add the files SerializeXML.cpp/h to your project and modify the structs and function names to suit your needs. Finally, call the save/load functions you added to save/load your structures to XML files. In my demo application, I call everything from BOOL CSerializeDlg::OnInitDialog(), just to test it out.

Points of Interest

Introduction to Using the XML DOM from Visual C++ by Tom Archer.

Downloads

Download demo project - 15 Kb
Download source - 3 Kb


Comments

  • NEVER place 'using namespace ...' in a header!!!

    Posted by Legacy on 05/08/2003 12:00am

    Originally posted by: Jeff Flinn

    NEVER, NEVER, NEVER place 'using namespace ...' in a header!!!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds