Click here to Skip to main content
11,412,833 members (72,558 online)
Click here to Skip to main content

Save Application Settings to XML

, 16 Sep 2002
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Jason Henderson
Software Developer (Senior)
United States United States
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.

Comments and Discussions

 
GeneralNice work Pin
Donsw at 8-May-09 17:18
memberDonsw8-May-09 17:18 
GeneralThanks Pin
islobodan at 6-Dec-08 16:58
memberislobodan6-Dec-08 16:58 
GeneralRe: Thanks Pin
elo123 at 12-Dec-08 13:56
memberelo12312-Dec-08 13:56 
QuestionA suggestion Pin
stokos at 20-Mar-07 3:48
memberstokos20-Mar-07 3: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 only Pin
Prajakta Patil at 16-Jan-07 19:04
memberPrajakta Patil16-Jan-07 19:04 
GeneralRe: Read and save Shapes using xml in vb.net only Pin
Jason Henderson at 17-Jan-07 3:29
memberJason Henderson17-Jan-07 3:29 
QuestionRe: Read and save Shapes using xml in vb.net only Pin
Prajakta Patil at 22-Jan-07 20:46
memberPrajakta Patil22-Jan-07 20:46 
GeneralFailure on Close Pin
123qwertz at 19-Dec-06 22:38
member123qwertz19-Dec-06 22:38 
GeneralRe: Failure on Close Pin
Jason Henderson at 20-Dec-06 3:42
memberJason Henderson20-Dec-06 3:42 
GeneralKill this article. Pin
TomM at 21-Apr-06 11:34
memberTomM21-Apr-06 11:34 
GeneralRe: Kill this article. Pin
Nishant Sivakumar at 21-Apr-06 12:49
staffNishant Sivakumar21-Apr-06 12:49 
GeneralSpecify your language, please !! Pin
AceWizardly at 1-Nov-05 2:22
memberAceWizardly1-Nov-05 2:22 
GeneralRe: Specify your language, please !! Pin
Jason Henderson at 1-Nov-05 3:57
memberJason Henderson1-Nov-05 3:57 
GeneralRe: Specify your language, please !! Pin
Nishant Sivakumar at 21-Apr-06 12:42
staffNishant Sivakumar21-Apr-06 12:42 
GeneralRe: Specify your language, please !! Pin
Nishant Sivakumar at 21-Apr-06 12:49
staffNishant Sivakumar21-Apr-06 12:49 
GeneralRe: Save Application Settings to XML Pin
Jason Henderson at 20-Dec-06 3:40
memberJason Henderson20-Dec-06 3:40 
GeneralRe: Save Application Settings to XML Pin
Nishant Sivakumar at 20-Dec-06 3:43
editorNishant Sivakumar20-Dec-06 3:43 
GeneralRe: Save Application Settings to XML Pin
Jason Henderson at 20-Dec-06 3:48
memberJason Henderson20-Dec-06 3:48 
GeneralRe: Specify your language, please !! Pin
ziopino70 at 13-Apr-10 22:06
memberziopino7013-Apr-10 22:06 
GeneralNo More CXMLSettings Class Pin
Nicolas DUFAUT at 12-Feb-05 23:15
memberNicolas DUFAUT12-Feb-05 23:15 
GeneralRe: No More CXMLSettings Class Pin
Jason Henderson at 13-Feb-05 19:13
memberJason Henderson13-Feb-05 19:13 
GeneralRe: No More CXMLSettings Class Pin
Nicolas DUFAUT at 13-Feb-05 20:17
memberNicolas DUFAUT13-Feb-05 20:17 
GeneralRe: No More CXMLSettings Class Pin
prcarp at 23-Aug-05 4:01
memberprcarp23-Aug-05 4:01 
GeneralRe: No More CXMLSettings Class Pin
Jason Henderson at 23-Aug-05 4:20
memberJason Henderson23-Aug-05 4:20 
GeneralRe: No More CXMLSettings Class Pin
Jason Henderson at 20-Dec-06 3:39
memberJason Henderson20-Dec-06 3:39 
GeneralRe: No More CXMLSettings Class Pin
prcarp at 20-Dec-06 3:54
memberprcarp20-Dec-06 3:54 
GeneralRe: No More CXMLSettings Class Pin
Jason Henderson at 20-Dec-06 4:06
memberJason Henderson20-Dec-06 4:06 
GeneralCoInitialize(NULL) Pin
Slawa at 6-May-03 6:52
memberSlawa6-May-03 6:52 
GeneralRe: CoInitialize(NULL) Pin
Jason Henderson at 6-May-03 8:03
memberJason Henderson6-May-03 8:03 
GeneralI love it for MY needs! Pin
Paul Kissel at 4-Apr-03 10:46
memberPaul Kissel4-Apr-03 10:46 
GeneralRe: I love it for MY needs! Pin
Jason Henderson at 4-Apr-03 10:57
memberJason Henderson4-Apr-03 10:57 
Generalattributes Pin
Jesper Odgaard Nielsen at 26-Mar-03 3:38
memberJesper Odgaard Nielsen26-Mar-03 3:38 
GeneralRe: attributes Pin
Jason Henderson at 4-Apr-03 10:56
memberJason Henderson4-Apr-03 10:56 
GeneralIsolated Storage - Out of the Registry Pin
Neville Franks at 20-Sep-02 15:38
memberNeville Franks20-Sep-02 15:38 
GeneralNice Article Pin
Anonymous at 20-Sep-02 4:19
sussAnonymous20-Sep-02 4:19 
GeneralRe: Nice Article Pin
John Mautari at 18-Oct-02 6:39
memberJohn Mautari18-Oct-02 6:39 
GeneralCString in interface methods Pin
Anonymous at 18-Sep-02 17:20
sussAnonymous18-Sep-02 17:20 
GeneralRe: CString in interface methods Pin
Jason Henderson at 19-Sep-02 4:11
memberJason Henderson19-Sep-02 4:11 
GeneralRe: CString in interface methods Pin
Anonymous at 19-Sep-02 11:47
sussAnonymous19-Sep-02 11:47 
GeneralRe: CString in interface methods Pin
Jason Henderson at 19-Sep-02 11:51
memberJason Henderson19-Sep-02 11:51 
GeneralRe: CString in interface methods Pin
Anonymous at 19-Sep-02 14:54
sussAnonymous19-Sep-02 14:54 
GeneralRe: CString in interface methods Pin
Anonymous at 16-Oct-02 13:17
sussAnonymous16-Oct-02 13:17 
GeneralRe: CString in interface methods Pin
GD_Patrick at 11-Oct-06 0:05
memberGD_Patrick11-Oct-06 0:05 
GeneralRe: CString in interface methods Pin
CodeProjectSQ at 19-Sep-02 16:26
memberCodeProjectSQ19-Sep-02 16:26 
GeneralRe: CString in interface methods Pin
Anonymous at 11-Oct-02 20:57
sussAnonymous11-Oct-02 20:57 
GeneralRe: CString in interface methods Pin
Neville Franks at 20-Sep-02 15:33
memberNeville Franks20-Sep-02 15:33 
GeneralXML vs Registry... Pin
igor1960 at 17-Sep-02 20:15
sussigor196017-Sep-02 20:15 
QuestionWhy not use both? Pin
Gary Wheeler at 17-Sep-02 7:14
memberGary Wheeler17-Sep-02 7:14 
AnswerRe: Why not use both? Pin
Jason Henderson at 17-Sep-02 9:09
memberJason Henderson17-Sep-02 9:09 
GeneralMissing the resource file (*.rc) Pin
Søren Madsen at 16-Sep-02 20:34
memberSøren Madsen16-Sep-02 20:34 

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
Web03 | 2.8.150427.1 | Last Updated 17 Sep 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid