Click here to Skip to main content
Licence 
First Posted 9 May 2003
Views 92,747
Bookmarked 23 times

Serializing Your Structs Using XML

By | 9 May 2003 | Article
This article shows how to save a structure into an XML file (using STL), then load the file back using XML

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 <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. 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.

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



United States United States

Member

Follow on Twitter Follow on Twitter
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, astronomy, technology, and genealogy.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generallol Pinmembervcshaman0:12 16 Jan '10  
GeneralCompilation Errors Pinmemberimadkk114:07 22 May '06  
GeneralRe: Compilation Errors PinmemberBassam Abdul-Baki16:49 25 May '06  
GeneralFailure to load the XML file PinmemberFox Ray22:24 14 Dec '03  
GeneralRe: Failure to load the XML file PinmemberBassam Abdul-Baki8:20 15 Dec '03  
Generalerror C2872: 'CLSID_DOMDocument' Pinmemberexceler14:18 11 May '03  
GeneralRe: error C2872: 'CLSID_DOMDocument' PinmemberBassam Abdul-Baki14:59 11 May '03  
QuestionSTL ? Pinmemberdog_spawn2:53 10 May '03  
AnswerRe: STL ? PinmemberBassam Abdul-Baki15:00 11 May '03  
GeneralRe: STL ? Pinmemberdog_spawn16:00 11 May '03  
GeneralNot a reusable code Pinmember.S.Rod.19:58 9 May '03  
GeneralRe: Not a reusable code Pinmemberdog_spawn2:59 10 May '03  
GeneralRe: Not a reusable code Pinmember.S.Rod.3:13 10 May '03  
GeneralRe: Not a reusable code Pinmemberdog_spawn3:27 10 May '03  
GeneralRe: Not a reusable code Pinmember.S.Rod.3:41 10 May '03  
GeneralRe: Not a reusable code Pinmemberdshah3:43 11 May '03  
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.4:07 11 May '03  
GeneralRe: Not a reusable code Pinmemberdog_spawn5:26 11 May '03  
GeneralRe: Not a reusable code PinmemberAnna-Jayne Metcalfe23:49 12 May '03  
GeneralRe: Not a reusable code PinmemberBassam Abdul-Baki15:03 11 May '03  
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.19:52 21 May '03  
GeneralRe: Not a reusable code PinmemberSam Hobbs6:43 20 Sep '05  
GeneralRe: Not a reusable code PinmemberStephane Rodriguez.5:42 23 Sep '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 May 2003
Article Copyright 2003 by Bassam Abdul-Baki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid