Click here to Skip to main content
Click here to Skip to main content

Serializing Your Structs Using XML

, 9 May 2003
Rate this:
Please Sign up or sign in to vote.
This article shows how to save a structure into an XML file (using STL), then load the file back using XML
<!-- Add the rest of your HTML here -->

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();

   // Since 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 <span class="code-keyword"><atlbase.h>         // Needed for CComVariant.
</span>

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. Example code follows:

   // Create a structure of type "first".
   {
      FirstStruct first;
      FirstSub1Struct firstSub1;
      FirstSub2Struct firstSub2;
      unsigned short i;
 
      first.someName = _T("Struct 1");
      first.someNumber = 1;

      for (i = 0; i < 16; ++i)
      {
         firstSub1.someNumber = i;
         _TCHAR buff[100];
         _stprintf(buff, _T("Struct 1 - %d"), i);
         firstSub1.someName = buff;
         first.firstSub1.push_back(firstSub1);
      }

      for (i = 0; i < 4; ++i)
      {
         firstSub2.someNumber = i;
         _TCHAR buff[100];
         _stprintf(buff, _T("Struct 1 - %d"), i);
         firstSub2.someName = buff;
         first.firstSub2.push_back(firstSub2);
      }

      mainStruct.first.push_back(first);
   }

   // Create another structure of type "first".
   {
      FirstStruct first;
      FirstSub1Struct firstSub1;
      FirstSub2Struct firstSub2;
      unsigned short i;
 
      first.someName = _T("Struct 2");
      first.someNumber = 1;

      for(i = 0;i < 16; ++i)
      {
         firstSub1.someNumber = i;
         _TCHAR buff[100];
         _stprintf(buff, _T("Struct 2 - %d"), i);
         firstSub1.someName = buff;
         first.firstSub1.push_back(firstSub1);
      }

      for(i = 0;i < 4; ++i)
      {
         firstSub2.someNumber = i;
         _TCHAR buff[100];
         _stprintf(buff, _T("Struct 2 - %d"), i);
         firstSub2.someName = buff;
         first.firstSub2.push_back(firstSub2);
      }

      mainStruct.first.push_back(first);
   }

   // Add "second" struct to main structure.
   SecondStruct secondStruct;
   secondStruct.someNumber1 = 1;
   secondStruct.someNumber2 = 2;
   mainStruct.second.push_back(secondStruct);
   secondStruct.someNumber1 = 2;
   secondStruct.someNumber2 = 4;
   mainStruct.second.push_back(secondStruct);
   secondStruct.someNumber1 = 3;
   secondStruct.someNumber2 = 6;
   mainStruct.second.push_back(secondStruct);

   // We have a complete structure ready for serialization.
   CSerializeXML serializeXML;

   serializeXML.SaveConfigurationToDisk("Test.xml", &mainStruct);
   serializeXML.LoadConfigurationFromDisk("Test.xml", &mainStruct2);
   serializeXML.SaveConfigurationToDisk("Test2.xml", &mainStruct2);

 

Points of Interest

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

History

May 5, 2003 - article created. <!------------------------------- That's it! --------------------------->

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Bassam Abdul-Baki
Systems Engineer
United States United States
Bassam Abdul-Baki has a Bachelor of Science (BS) degree and a Master of Science (MS) degree in Mathematics and another MS in Technology Management. He's an analyst by trade. He started out in Quality Assurance (QA) and analysis, then dabbled in Visual C++ and Visual C# programming for a while, and then came back to QA and analysis again. He's not sure where he'll be five years from now, but is looking into data analytics.
 
Bassam is into mathematics, technology, astronomy, archaeology, technology, and genealogy.
Follow on   Twitter

Comments and Discussions

 
QuestionLicense PinmemberMember 896106528-May-12 23:22 
Generallol Pinmembervcshaman16-Jan-10 0:12 
GeneralCompilation Errors Pinmemberimadkk122-May-06 14:07 
GeneralRe: Compilation Errors PinmemberBassam Abdul-Baki25-May-06 16:49 
GeneralFailure to load the XML file PinmemberFox Ray14-Dec-03 22:24 
GeneralRe: Failure to load the XML file PinmemberBassam Abdul-Baki15-Dec-03 8:20 
Generalerror C2872: 'CLSID_DOMDocument' Pinmemberexceler11-May-03 14:18 
GeneralRe: error C2872: 'CLSID_DOMDocument' PinmemberBassam Abdul-Baki11-May-03 14:59 
QuestionSTL ? Pinmemberdog_spawn10-May-03 2:53 
AnswerRe: STL ? PinmemberBassam Abdul-Baki11-May-03 15:00 
GeneralRe: STL ? Pinmemberdog_spawn11-May-03 16:00 
GeneralNot a reusable code Pinmember.S.Rod.9-May-03 19:58 
GeneralRe: Not a reusable code Pinmemberdog_spawn10-May-03 2:59 
GeneralRe: Not a reusable code Pinmember.S.Rod.10-May-03 3:13 
GeneralRe: Not a reusable code Pinmemberdog_spawn10-May-03 3:27 
GeneralRe: Not a reusable code Pinmember.S.Rod.10-May-03 3:41 
GeneralRe: Not a reusable code Pinmemberdshah11-May-03 3:43 
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.11-May-03 4:07 
GeneralRe: Not a reusable code Pinmemberdog_spawn11-May-03 5:26 
GeneralRe: Not a reusable code PinmemberAnna-Jayne Metcalfe12-May-03 23:49 
GeneralRe: Not a reusable code PinmemberBassam Abdul-Baki11-May-03 15:03 
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.21-May-03 19:52 
GeneralRe: Not a reusable code PinmemberSam Hobbs20-Sep-05 6:43 
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.23-Sep-05 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 10 May 2003
Article Copyright 2003 by Bassam Abdul-Baki
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid