Click here to Skip to main content
11,581,982 members (75,013 online)
Click here to Skip to main content

CXMLProfile - Easy XML Profiles for applications

, 26 Feb 2004 111.1K 1.2K 42
Rate this:
Please Sign up or sign in to vote.
A MFC class to store an application's profile in an XML file.

Introduction

Storing and loading configuration data for application is a very common task for programmers, and there are many ways to do it: store information in a file, in the registry, in the Windows common initialization files (system.ini, win.ini, etc...). But information saving and sharing has a new standard called XML.

Windows APIs and MFC have a set of functions which simplify these procedures a lot, storing and retrieving information from win.ini, the registry or any file based on the Windows-INI structure. Currently, handling XML files under Visual C++ is a bit complex, due to the COM nature of the Microsoft XML services.

CXMLProfile is a class which simplifies this work, encapsulating the standard configuration input/output operations in a really simple set of functions.

Using CXMLProfile

When you instantiate a CXMLProfile class and call the loadProfile function, it tries to open the XML file containing the profile specified by the first param of the constructor; if it is not found, a blank profile is created. The data is stored in the file every time you call the saveProfile function.

Now, let's have a look on the class public declarations:

CXMLProfile(LPCTSTR lpszProfileName);

bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
bool writeProfileString(LPCTSTR lpszSection, 
    LPCTSTR lpszEntry, LPCTSTR lpszData);

int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, 
    LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);

bool saveProfile();
bool loadProfile();

~CXMLProfile();

The function prototypes are self-explaining, but I will give a short description:

  • CXMLProfile(LPCTSTR lpszProfileName)

    The only and one constructor, lpszProfileName contains the profile name (your application's name, for example).

  • virtual ~CXMLProfile();

    The only and one destructor; performs a class cleanup.

  • bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);

    Writes an integer value into the specified section and entry.

  • bool writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszData);

    Writes a string value into the specified section and entry.

  • int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);

    Retrieves an integer value from the specified section and entry.

  • LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);

    Retrieves a string value from the specified section and entry.

  • bool saveProfile();

    Saves the current profile to disk.

  • bool loadProfile();

    Loads the current profile from disk. It must be called before reading or writing from the profile.

Comments

To use this code, just include the xmlprofile.h and xmlprofile.cpp into your project and add comsupp.lib to the linker dependencies.

Example code:

#include "xmlprofile.h"

int main()
{
    CXMLProfile xmlProfile("MyApplication");

    xmlProfile.loadProfile();

    // write some stuff in the profile
    xmlProfile.writeProfileString("Owner", "Name", "John");
    xmlProfile.writeProfileInt("Owner", "Age", 20);

    // now let´s retrieve it
    CHAR szName[255];

    xmlProfile.getProfileString("Owner", "Name", "None", szName, 255);
    int nAge = xmlProfile.getProfileInt("Owner", "Age", 0);

    // Warning! the destructor will not save to file any data... 
    // we have to use saveProfile()
    xmlProfile.saveProfile();
}

Updates

  • 21/August/2002 - Now uses the "Documents and Settings\Username" directory to store the profile if the current OS allows it (NT/2000/XP).
  • 18/February/2004 - Now this class is not MFC-dependent and some functions have been added/improved.

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

Share

About the Author

Emilio Guijarro
Instructor/Trainer
Netherlands Netherlands
Emilio is a Computer Engineer currently working as software engineer in embedded systems.

Main interests are C/C++ programming, algorithmics, compilers, embedded systems, cryptography, and operating systems.

You may also be interested in...

Comments and Discussions

 
Questionsame code, one solution works, other doesent Pin
xcvsdfsdf8-Feb-09 15:11
memberxcvsdfsdf8-Feb-09 15:11 
GeneralJust what I needed... Pin
hitesh_sharma29-Apr-07 3:26
memberhitesh_sharma29-Apr-07 3:26 
General Pin
martinjang6-Feb-07 19:13
membermartinjang6-Feb-07 19:13 
Questionhow to do add xml version [modified] Pin
farlongchen23-May-06 2:11
memberfarlongchen23-May-06 2:11 
GeneralI optimized your code. Pin
LamaTJ20-Dec-04 19:57
memberLamaTJ20-Dec-04 19:57 
GeneralgetAddress not found Pin
kim199919-Oct-04 18:53
memberkim199919-Oct-04 18:53 
GeneralRe: getAddress not found Pin
Emilio Guijarro20-Oct-04 3:10
memberEmilio Guijarro20-Oct-04 3:10 
GeneralMemory Leaks... Pin
Humble Programmer27-Feb-04 3:16
memberHumble Programmer27-Feb-04 3:16 
GeneralPotential Buffer Error Pin
Albert Weinert27-Feb-04 1:18
memberAlbert Weinert27-Feb-04 1:18 
GeneralDon't need MFC Pin
Coruscant17-Feb-04 1:01
memberCoruscant17-Feb-04 1:01 
GeneralUnicode Pin
quzi17-Nov-03 22:48
memberquzi17-Nov-03 22:48 
GeneralUnhandled exception Pin
quzi17-Nov-03 4:02
memberquzi17-Nov-03 4:02 
GeneralRe: Unhandled exception Pin
quzi17-Nov-03 5:14
memberquzi17-Nov-03 5:14 
GeneralRe: Unhandled exception Pin
Sven Schäfer3-Mar-04 3:20
memberSven Schäfer3-Mar-04 3:20 
GeneralWhich header files Pin
Joseph M. Newcomer27-Jul-03 14:49
memberJoseph M. Newcomer27-Jul-03 14:49 
GeneralRe: Which header files Pin
Joseph M. Newcomer27-Jul-03 18:29
memberJoseph M. Newcomer27-Jul-03 18:29 
GeneralRe: Which header files Pin
Emilio Guijarro28-Jul-03 9:27
memberEmilio Guijarro28-Jul-03 9:27 
GeneralRe: Which header files Pin
alerma8-Dec-03 2:02
memberalerma8-Dec-03 2:02 
GeneralRe: Which header files Pin
Joseph M. Newcomer8-Dec-03 6:27
memberJoseph M. Newcomer8-Dec-03 6:27 
GeneralRe: Which header files Pin
alerma8-Dec-03 21:35
memberalerma8-Dec-03 21:35 
GeneralAwesome. :D Pin
Djof23-May-03 19:35
memberDjof23-May-03 19:35 
GeneralRe: Awesome. :D Pin
Emilio Guijarro25-May-03 5:14
memberEmilio Guijarro25-May-03 5:14 
Generalexception error Pin
Jazzy Jeff13-Feb-03 9:25
memberJazzy Jeff13-Feb-03 9:25 
GeneralRe: exception error Pin
Emilio Guijarro15-Feb-03 4:02
memberEmilio Guijarro15-Feb-03 4:02 
GeneralRe: exception error Pin
LJ Whitmire30-Jun-03 5:56
sussLJ Whitmire30-Jun-03 5:56 
GeneralGood idea.. Pin
Ian Leslie29-Aug-02 8:44
memberIan Leslie29-Aug-02 8:44 
GeneralRe: Good idea.. Pin
Emilio Guijarro30-Aug-02 6:36
memberEmilio Guijarro30-Aug-02 6:36 
Yes, I forgot those things, I will try to fix them in my free time.

Thanks.

"nobody knows it, but you´ve got a secret smile, and you use it only for me"
Generalc:\Documens and settings\ Pin
huggeh21-Aug-02 1:30
memberhuggeh21-Aug-02 1:30 
GeneralRe: c:\Documens and settings\ Pin
huggeh21-Aug-02 1:34
memberhuggeh21-Aug-02 1:34 
GeneralRe: c:\Documens and settings\ Pin
Emilio Guijarro21-Aug-02 6:24
memberEmilio Guijarro21-Aug-02 6:24 
QuestionUnmanaged code? Pin
Miguel Lopes19-Aug-02 9:00
memberMiguel Lopes19-Aug-02 9:00 
AnswerRe: Unmanaged code? Pin
Giles19-Aug-02 11:16
memberGiles19-Aug-02 11:16 
AnswerRe: Unmanaged code? Pin
Chris Maunder19-Aug-02 12:40
adminChris Maunder19-Aug-02 12:40 
AnswerRe: Unmanaged code? Pin
Emilio Guijarro20-Aug-02 7:37
memberEmilio Guijarro20-Aug-02 7:37 

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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 27 Feb 2004
Article Copyright 2002 by Emilio Guijarro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid