Click here to Skip to main content
15,616,585 members
Articles / Desktop Programming / ATL
Article
Posted 9 May 2003

Stats

136K views
1.9K downloads
23 bookmarked

Serializing Your Structs Using XML

Rate me:
Please Sign up or sign in to vote.
3.40/5 (4 votes)
9 May 20031 min read
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.

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

C++
#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:

C++
#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:

C++
// 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

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.


Written By
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, and genealogy.

Comments and Discussions

 
QuestionLicense Pin
Member 896106528-May-12 23:22
Member 896106528-May-12 23:22 
Generallol Pin
vcshaman16-Jan-10 0:12
vcshaman16-Jan-10 0:12 
GeneralCompilation Errors Pin
imadkk122-May-06 14:07
imadkk122-May-06 14:07 
GeneralRe: Compilation Errors Pin
Bassam Abdul-Baki25-May-06 16:49
professionalBassam Abdul-Baki25-May-06 16:49 
GeneralFailure to load the XML file Pin
Fox Ray14-Dec-03 22:24
Fox Ray14-Dec-03 22:24 
GeneralRe: Failure to load the XML file Pin
Bassam Abdul-Baki15-Dec-03 8:20
professionalBassam Abdul-Baki15-Dec-03 8:20 
Generalerror C2872: 'CLSID_DOMDocument' Pin
exceler11-May-03 14:18
exceler11-May-03 14:18 
GeneralRe: error C2872: 'CLSID_DOMDocument' Pin
Bassam Abdul-Baki11-May-03 14:59
professionalBassam Abdul-Baki11-May-03 14:59 
QuestionSTL ? Pin
dog_spawn10-May-03 2:53
dog_spawn10-May-03 2:53 
AnswerRe: STL ? Pin
Bassam Abdul-Baki11-May-03 15:00
professionalBassam Abdul-Baki11-May-03 15:00 
GeneralRe: STL ? Pin
dog_spawn11-May-03 16:00
dog_spawn11-May-03 16:00 
GeneralNot a reusable code Pin
Stephane Rodriguez.9-May-03 19:58
Stephane Rodriguez.9-May-03 19:58 
GeneralRe: Not a reusable code Pin
dog_spawn10-May-03 2:59
dog_spawn10-May-03 2:59 
GeneralRe: Not a reusable code Pin
Stephane Rodriguez.10-May-03 3:13
Stephane Rodriguez.10-May-03 3:13 
GeneralRe: Not a reusable code Pin
dog_spawn10-May-03 3:27
dog_spawn10-May-03 3:27 
GeneralRe: Not a reusable code Pin
Stephane Rodriguez.10-May-03 3:41
Stephane Rodriguez.10-May-03 3:41 
GeneralRe: Not a reusable code Pin
dshah11-May-03 3:43
dshah11-May-03 3:43 
GeneralRe: Not a reusable code Pin
Stephane Rodriguez.11-May-03 4:07
Stephane Rodriguez.11-May-03 4:07 
GeneralRe: Not a reusable code Pin
dog_spawn11-May-03 5:26
dog_spawn11-May-03 5:26 
GeneralRe: Not a reusable code Pin
Anna-Jayne Metcalfe12-May-03 23:49
Anna-Jayne Metcalfe12-May-03 23:49 
GeneralRe: Not a reusable code Pin
Bassam Abdul-Baki11-May-03 15:03
professionalBassam Abdul-Baki11-May-03 15:03 
GeneralRe: Not a reusable code Pin
Stephane Rodriguez.21-May-03 19:52
Stephane Rodriguez.21-May-03 19:52 
GeneralRe: Not a reusable code Pin
Sam Hobbs20-Sep-05 6:43
Sam Hobbs20-Sep-05 6:43 
GeneralRe: Not a reusable code Pin
Stephane Rodriguez.23-Sep-05 5:42
Stephane Rodriguez.23-Sep-05 5:42 

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

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