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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSpecify your language, please !!memberAceWizardly1 Nov '05 - 1:22 
GeneralRe: Specify your language, please !!memberJason Henderson1 Nov '05 - 2:57 
GeneralRe: Specify your language, please !!staffNishant Sivakumar21 Apr '06 - 11:42 
Okay, the zip URLs are wrong. I am trying to fix this now!
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there!
 
-- modified at 17:44 Friday 21st April, 2006
GeneralRe: Specify your language, please !!staffNishant Sivakumar21 Apr '06 - 11:49 
GeneralRe: Save Application Settings to XMLmemberJason Henderson20 Dec '06 - 2:40 
GeneralRe: Save Application Settings to XMLeditorNishant Sivakumar20 Dec '06 - 2:43 
GeneralRe: Save Application Settings to XMLmemberJason Henderson20 Dec '06 - 2:48 
GeneralRe: Specify your language, please !!memberziopino7013 Apr '10 - 21:06 
GeneralNo More CXMLSettings ClassmemberNicolas DUFAUT12 Feb '05 - 22:15 
GeneralRe: No More CXMLSettings ClassmemberJason Henderson13 Feb '05 - 18:13 
GeneralRe: No More CXMLSettings ClassmemberNicolas DUFAUT13 Feb '05 - 19:17 
GeneralRe: No More CXMLSettings Classmemberprcarp23 Aug '05 - 3:01 
GeneralRe: No More CXMLSettings ClassmemberJason Henderson23 Aug '05 - 3:20 
GeneralRe: No More CXMLSettings ClassmemberJason Henderson20 Dec '06 - 2:39 
GeneralRe: No More CXMLSettings Classmemberprcarp20 Dec '06 - 2:54 
GeneralRe: No More CXMLSettings ClassmemberJason Henderson20 Dec '06 - 3:06 
GeneralCoInitialize(NULL)memberSlawa6 May '03 - 5:52 
GeneralRe: CoInitialize(NULL)memberJason Henderson6 May '03 - 7:03 
GeneralI love it for MY needs!memberPaul Kissel4 Apr '03 - 9:46 
GeneralRe: I love it for MY needs!memberJason Henderson4 Apr '03 - 9:57 
GeneralattributesmemberJesper Odgaard Nielsen26 Mar '03 - 2:38 
GeneralRe: attributesmemberJason Henderson4 Apr '03 - 9:56 
GeneralIsolated Storage - Out of the RegistrymemberNeville Franks20 Sep '02 - 14:38 
GeneralNice ArticlesussAnonymous20 Sep '02 - 3:19 
GeneralRe: Nice ArticlememberJohn Mautari18 Oct '02 - 5:39 
GeneralCString in interface methodssussAnonymous18 Sep '02 - 16:20 
GeneralRe: CString in interface methodsmemberJason Henderson19 Sep '02 - 3:11 
GeneralRe: CString in interface methodssussAnonymous19 Sep '02 - 10:47 
GeneralRe: CString in interface methodsmemberJason Henderson19 Sep '02 - 10:51 
GeneralRe: CString in interface methodssussAnonymous19 Sep '02 - 13:54 
GeneralRe: CString in interface methodssussAnonymous16 Oct '02 - 12:17 
GeneralRe: CString in interface methodsmemberGD_Patrick10 Oct '06 - 23:05 
GeneralRe: CString in interface methodsmemberCodeProjectSQ19 Sep '02 - 15:26 
GeneralRe: CString in interface methodssussAnonymous11 Oct '02 - 19:57 
GeneralRe: CString in interface methodsmemberNeville Franks20 Sep '02 - 14:33 
GeneralXML vs Registry...sussigor196017 Sep '02 - 19:15 
QuestionWhy not use both?memberGary Wheeler17 Sep '02 - 6:14 
AnswerRe: Why not use both?memberJason Henderson17 Sep '02 - 8:09 
GeneralMissing the resource file (*.rc)memberSøren Madsen16 Sep '02 - 19:34 
GeneralRe: Missing the resource file (*.rc)memberJason Henderson17 Sep '02 - 3:36 
GeneralIt's okmemberJason King16 Sep '02 - 6:55 
GeneralWhy DOMmemberYoSilver16 Sep '02 - 5:02 
GeneralRe: Why DOMmemberJason Henderson16 Sep '02 - 8:21 
GeneralXML vs. RegistrymemberPaul A. Howes16 Sep '02 - 3:38 
GeneralRe: XML vs. RegistrymemberJason Henderson16 Sep '02 - 3:49 
GeneralRe: XML vs. RegistrymemberPaul A. Howes16 Sep '02 - 6:50 
GeneralRe: XML vs. RegistrymemberJason Henderson16 Sep '02 - 8:25 
GeneralRe: XML vs. RegistrymemberRui Jiang13 Dec '02 - 9:20 
GeneralRe: XML vs. RegistrymemberDaniel Desormeaux17 Sep '02 - 3:59 
GeneralRe: XML vs. RegistrymemberJason Henderson17 Sep '02 - 4:07 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 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