Click here to Skip to main content
15,887,267 members
Articles / Desktop Programming / MFC
Article

Save Application Settings to XML

Rate me:
Please Sign up or sign in to vote.
3.92/5 (5 votes)
16 Sep 20022 min read 259.3K   1.8K   54   77
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


Written By
Software Developer (Senior)
United States United States
I have been a professional developer since 1996. I live in Illinois, in the USA. I am married and have four children.

Comments and Discussions

 
GeneralWhy DOM Pin
YoSilver16-Sep-02 5:02
YoSilver16-Sep-02 5:02 
GeneralRe: Why DOM Pin
Jason Henderson16-Sep-02 8:21
Jason Henderson16-Sep-02 8:21 
GeneralXML vs. Registry Pin
Paul A. Howes16-Sep-02 3:38
Paul A. Howes16-Sep-02 3:38 
GeneralRe: XML vs. Registry Pin
Jason Henderson16-Sep-02 3:49
Jason Henderson16-Sep-02 3:49 
GeneralRe: XML vs. Registry Pin
Paul A. Howes16-Sep-02 6:50
Paul A. Howes16-Sep-02 6:50 
GeneralRe: XML vs. Registry Pin
Jason Henderson16-Sep-02 8:25
Jason Henderson16-Sep-02 8:25 
GeneralRe: XML vs. Registry Pin
Rui Jiang13-Dec-02 9:20
Rui Jiang13-Dec-02 9:20 
GeneralRe: XML vs. Registry Pin
dandy7217-Sep-02 3:59
dandy7217-Sep-02 3:59 
> The registry is too big, cumbersome and dangerous to have your clients
> playing with when they call in for tech support.

Maybe the solution to this is to store your settings in the registry as usual, but add a button somewhere in your program labeled "Export Program Settings" (part of, say, a dialog box called by a Tech Support button in the About box, like the Office apps).

Have this button create a simple .REG file that your users can email back to you... Doesn't RegEdit even have some command-line parameter to export a subkey in a non-interactive mode (no UI)? That's what I'm thinking but for some reason I can't find the docs right now...if that's the case, that'd be just a matter of spawning RegEdit from your own program, so that'd be, what, two lines to write? and (you'd assume!) bug-free from the start. Reimporting the file if necessary would be just as easy.

Just to add to the conversation, I'd have to agree with going against XML for storing program settings; for one thing, a *big* assumption is that the proper DLLs are already installed on the client machine...if the program you're writing isn't already using XML for some reason, why drag along the entire redistribution package just for storing the program settings? But if you already have another reason for using XML in your program, fine, but as others have pointed out, you may run into multi-user/multi-location problems. Using the <profile>\AppData directory is an idea, but then what are you going to fall back to on OSes that didn't even have that directory? Heck, even My Documents didn't exist on 95.

While I don't disagree that sometimes you need to have program settings forwarded to tech support, I don't believe XML is really the answer. I don't want to discredit Jason or undermine his efforts, so I'll say this much--the article is providing a very decent intro to XML manipulation in C++. So, thanks!

- DanielD
GeneralRe: XML vs. Registry Pin
Jason Henderson17-Sep-02 4:07
Jason Henderson17-Sep-02 4:07 
GeneralRe: XML vs. Registry Pin
Tim Smith16-Sep-02 4:02
Tim Smith16-Sep-02 4:02 
GeneralRe: XML vs. Registry Pin
Jason Henderson16-Sep-02 4:06
Jason Henderson16-Sep-02 4:06 
GeneralRe: XML vs. Registry Pin
Paul A. Howes16-Sep-02 6:57
Paul A. Howes16-Sep-02 6:57 
GeneralRe: XML vs. Registry Pin
Jason Henderson16-Sep-02 8:38
Jason Henderson16-Sep-02 8:38 
GeneralRe: XML vs. Registry Pin
Anonymous16-Sep-02 9:56
Anonymous16-Sep-02 9:56 
GeneralRe: XML vs. Registry Pin
Houdini17-Sep-02 5:16
Houdini17-Sep-02 5:16 
GeneralRe: XML vs. Registry Pin
Paul A. Howes17-Sep-02 6:43
Paul A. Howes17-Sep-02 6:43 
GeneralRe: XML vs. Registry Pin
Houdini17-Sep-02 7:39
Houdini17-Sep-02 7:39 
GeneralWhy XML Pin
Member 91815-Sep-02 14:06
Member 91815-Sep-02 14:06 
GeneralRe: Why XML Pin
Maximilien15-Sep-02 14:17
Maximilien15-Sep-02 14:17 
GeneralRe: Why XML Pin
Tim Kosse15-Sep-02 20:41
Tim Kosse15-Sep-02 20:41 
GeneralRe: Why XML Pin
mstephens15-Sep-02 23:31
mstephens15-Sep-02 23:31 
GeneralRe: Why XML Pin
Jiminy15-Sep-02 17:12
Jiminy15-Sep-02 17:12 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.