Click here to Skip to main content
Licence 
First Posted 14 Sep 2002
Views 180,309
Bookmarked 50 times

Save Application Settings to XML

By | 16 Sep 2002 | Article
An easy to use class that reads, writes and deletes app settings to an XML file.

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 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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice work PinmemberDonsw16:18 8 May '09  
GeneralThanks Pinmemberislobodan15:58 6 Dec '08  
GeneralRe: Thanks Pinmemberelo12312:56 12 Dec '08  
QuestionA suggestion Pinmemberstokos2:48 20 Mar '07  
GeneralRead and save Shapes using xml in vb.net only PinmemberPrajakta Patil18:04 16 Jan '07  
GeneralRe: Read and save Shapes using xml in vb.net only PinmemberJason Henderson2:29 17 Jan '07  
QuestionRe: Read and save Shapes using xml in vb.net only PinmemberPrajakta Patil19:46 22 Jan '07  
GeneralFailure on Close Pinmember123qwertz21:38 19 Dec '06  
GeneralRe: Failure on Close PinmemberJason Henderson2:42 20 Dec '06  
GeneralKill this article. PinmemberTomM10:34 21 Apr '06  
GeneralRe: Kill this article. PinstaffNishant Sivakumar11:49 21 Apr '06  
GeneralSpecify your language, please !! PinmemberAceWizardly1:22 1 Nov '05  
GeneralRe: Specify your language, please !! PinmemberJason Henderson2:57 1 Nov '05  
GeneralRe: Specify your language, please !! PinstaffNishant Sivakumar11:42 21 Apr '06  
GeneralRe: Specify your language, please !! PinstaffNishant Sivakumar11:49 21 Apr '06  
GeneralRe: Save Application Settings to XML PinmemberJason Henderson2:40 20 Dec '06  
GeneralRe: Save Application Settings to XML PineditorNishant Sivakumar2:43 20 Dec '06  
GeneralRe: Save Application Settings to XML PinmemberJason Henderson2:48 20 Dec '06  
GeneralRe: Specify your language, please !! Pinmemberziopino7021:06 13 Apr '10  
GeneralNo More CXMLSettings Class PinmemberNicolas DUFAUT22:15 12 Feb '05  
GeneralRe: No More CXMLSettings Class PinmemberJason Henderson18:13 13 Feb '05  
GeneralRe: No More CXMLSettings Class PinmemberNicolas DUFAUT19:17 13 Feb '05  
GeneralRe: No More CXMLSettings Class Pinmemberprcarp3:01 23 Aug '05  
GeneralRe: No More CXMLSettings Class PinmemberJason Henderson3:20 23 Aug '05  
GeneralRe: No More CXMLSettings Class PinmemberJason Henderson2:39 20 Dec '06  

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.

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