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

Save Application Settings to XML

By , 16 Sep 2002
 

Introduction

Why another application settings XML class?

  1. Because INI files are so Win16.
  2. The registry is too big and you could really screw something up messing with it.
  3. XML seems to be the popular choice.

See Read and Write application parameters in XML for better reasons.

This class should make it easy to read, write and delete application settings to an XML file using the familiar registry key/value nomenclature (ex. "MyApp\Appearance\Font\Face"). If it doesn't make it easy, um... I didn't write it.

The CXMLSettings Class

To use the class, there are only 6 methods you will need to worry about.

  • void SetSettingsFile(CString cstrFile)
    • Sets the path and filename for the XML settings file.
  • long GetSettingLong(CString cstrBaseKeyName, CString cstrValueName, long lDefaultValue)
    • Returns a long value extracted from the settings file given a key and value name.
  • long SetSettingLong(CString cstrBaseKeyName, CString cstrValueName, long lValue)
    • Sets a long value in the settings file given a key and value name.
  • CString GetSettingString(CString cstrBaseKeyName, CString cstrValueName, CString cstrDefaultValue)
    • Returns a string value extracted from the settings file given a key and value name.
  • long SetSettingString(CString cstrBaseKeyName, CString cstrValueName, CString cstrValue)
    • Sets a string value in the settings file given a key and value name.
  • BOOL DeleteSetting(CString cstrBaseKeyName, CString cstrValueName)
    • Deletes a key or value from the settings file given a key and value name.

Other methods in the class parse key/value "chains", and load, save, and traverse the settings file.

How to Use It

  1. Call SetSettingsFile to tell the class where the XML file will be saved.
  2. Call one of the Get, Set or Delete methods using the registry-like nomenclature for the first parameter (ex. "MyApp\Appearance\Font"). A default value may also be sent as the last parameter to the Get methods.
  3. The DeleteSetting method can be used to delete an entire key or a value under a key.

That's it!

If the XML file does not exist or if a key/value does not exist, the Set methods will create it and the Get methods will return the default value.

Conclusion

The class and the demo app use MFC and MSXML 4.0. Please let me know if you find any bugs are would like to see improvements.

Other articles lie this one:

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

Jason Henderson
Software Developer (Senior)
United States United States
Member
I have been a professional developer since 1996. I live in the middle of no where in Illinois, USA. I am married and have four children.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNice workmemberDonsw8 May '09 - 16:18 
Good article
 
cheers,
Donsw
My Recent Article : Ajax Calendar Control

GeneralThanksmemberislobodan6 Dec '08 - 15:58 
Class works excellent, you got my 5.
This class is the only acceptable solution for saving xml settings from MFC app.
GeneralRe: Thanksmemberelo12312 Dec '08 - 12:56 
Agree, this is just a simple solution that works! Need some small changes to work with UNICODE and to store files in app_local_data. The rest is just perfect.
QuestionA suggestionmemberstokos20 Mar '07 - 2:48 
Hi Jason,
 
Thank you very much for your article!
 
One question, I think already asked by another CP reader: How easy would it be to support XML Attributes?
 
For example:
<MySettings>
  <Users>
    <User ID="Fred">   <-------- ID is an attribute
      <Font>Courier</Font>
    </User>
    <User ID="Bob">
      <Font>Arial</Font>
    </User>
  </Users>
</MySettings>
Apart from this suggestion for improvement, your code is simple, yet very functional. A great piece of work, well done!
 
--
/stokos

GeneralRead and save Shapes using xml in vb.net onlymemberPrajakta Patil16 Jan '07 - 18:04 
Hi
 
I am able to wite diffrent type of shapes using xmlwitter.
 
But not able to read that shapes using xmlreader.
 
please help me out.

 
Prajakta

GeneralRe: Read and save Shapes using xml in vb.net onlymemberJason Henderson17 Jan '07 - 2:29 
I've never worked with shapes in xml before. I assume its still just text? If so, I wouldn't know why you couldn't read it. Does it give you an error code?
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

QuestionRe: Read and save Shapes using xml in vb.net onlymemberPrajakta Patil22 Jan '07 - 19:46 
I have finished with saving shapes. But I'm not able to save images and text font. I have two separate controls one is Bitmap and another one is Textbox. Using Bitmap user can add image to current drawing and using Textbox Text.
I'm not able to save the image file and font related information to xml.
Can you help me out??
 
Prajakta

GeneralFailure on Closemember123qwertz19 Dec '06 - 21:38 

void Release() throw()
{
ATLASSERT( nRefs != 0 );
 
if( _InterlockedDecrement( &nRefs ) <= 0 )
00438A42 lea eax,[ecx+0Ch]
00438A45 or edx,0FFFFFFFFh
00438A48 lock xadd dword ptr [eax],edx /*<< HERE*/
00438A4C dec edx
00438A4D test edx,edx
00438A4F jg ATL::CStringData::Release+19h (438A5Bh)
{
pStringMgr->Free( this );
00438A51 mov eax,dword ptr [ecx]
00438A53 mov edx,dword ptr [eax]
00438A55 push ecx
00438A56 mov ecx,eax
00438A58 call dword ptr [edx+4]
}
}
00438A5B ret

The Parser Works fine, but if my app end a [debug][cancel][send to ms] Erro occurres. I just have to do:

CXMLSettings *xmlSettings;
xmlSettings = new CXMLSettings();

anywhere to cause this.
 

-- modified at 3:35 Wednesday 20th December, 2006
GeneralRe: Failure on ClosememberJason Henderson20 Dec '06 - 2:42 
Check the obvious first - are you deleting the pointer you allocated?
 
delete xmlSettings;
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

GeneralKill this article.memberTomM21 Apr '06 - 10:34 
If the code is truly lost, this article should be removed.
 
Too bad, it sounds useful.
 
I'm guessing that there is another article with the same name.
 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 17 Sep 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid