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();
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 struct
s 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:
{
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);
}
{
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);
}
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);
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.
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.