Click here to Skip to main content
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

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

 
QuestionWhy not use both? PinmemberGary Wheeler17-Sep-02 6:14 
AnswerRe: Why not use both? PinmemberJason Henderson17-Sep-02 8:09 
GeneralMissing the resource file (*.rc) PinmemberSøren Madsen16-Sep-02 19:34 
GeneralRe: Missing the resource file (*.rc) PinmemberJason Henderson17-Sep-02 3:36 
GeneralIt's ok PinmemberJason King16-Sep-02 6:55 
GeneralWhy DOM PinmemberYoSilver16-Sep-02 5:02 
GeneralRe: Why DOM PinmemberJason Henderson16-Sep-02 8:21 
GeneralXML vs. Registry PinmemberPaul A. Howes16-Sep-02 3:38 
One of the many reasons Microsoft implemented the registry was to eliminate all of the INI files that applications created in system- and program-specific areas. As one comment mentioned, anyone who has ever tried to write software that will function properly in a multi-user environment can attest to how well the registry works.
 
The registry contains two general areas that you should save program settings into: HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. The local machine area, as the name implies, is for storing program information that all users of the system will need. The current user area is for storing user-specific data. In a multi-user and multi-system environment, the user's registry generally follows the user from one machine to another. This used to be called a "roaming profile" in the Windows 95 days.
 
To mimic this using XML, you would need to save one file in a system-specific area, and another in a user-specific area. Both of these do exist, in the form of the C:\Documents and Settings\All Users\Application Data\ and C:\Documents and Settings\username\Application Data\ directories, but there is a problem: replication.
 
Many Windows shops will replicate a user's registry settings when the user logs in and out of various workstations. Replicating files is less common. Why? Simple: Most users create an inordinate amount of files in their "My documents" folder. This is good, because it provides a backup server with a single place to look when making a backup from the system. However, if full replication is used, all of these files are transferred between the Domain Controller and the workstation at every log on and log off. This is a huge waste of bandwidth! On my LAN at home, I set the systems to replicate all user profile data except for the "My Documents" folder. On 100 Mbps, it takes 15 seconds to get on or off a workstation because of the amount of data being transferred.
 
So, to end my rant with my opinion on this subject as both a software developer and a systems administrator: Follow Microsoft's recommendation. Software developers should save program settings in the proper area of the registry. By creating subkeys, one can have heirarchical storage of the data with very little additional effort. Plus, it enables program settings to be carried from one system to the next using replication. In the business environment, these are all important things to keep in mind.
 
XML was originally intended as a data description and transfer mechanism, not as an actual storage format. The registry may not be human-readable without a tool like RegEdit, but it was intended to be efficient, and store many different types of data in a heirarchical fashion. Duplicating this with text (XML) files doesn't accomplish anything beneficial.
 
--
Paul
 
"I drank... WHAT?"
GeneralRe: XML vs. Registry PinmemberJason Henderson16-Sep-02 3:49 
GeneralRe: XML vs. Registry PinmemberPaul A. Howes16-Sep-02 6:50 
GeneralRe: XML vs. Registry PinmemberJason Henderson16-Sep-02 8:25 
GeneralRe: XML vs. Registry PinmemberRui Jiang13-Dec-02 9:20 
GeneralRe: XML vs. Registry PinmemberDaniel Desormeaux17-Sep-02 3:59 
GeneralRe: XML vs. Registry PinmemberJason Henderson17-Sep-02 4:07 
GeneralRe: XML vs. Registry PinmemberTim Smith16-Sep-02 4:02 
GeneralRe: XML vs. Registry PinmemberJason Henderson16-Sep-02 4:06 
GeneralRe: XML vs. Registry PinmemberPaul A. Howes16-Sep-02 6:57 
GeneralRe: XML vs. Registry PinmemberJason Henderson16-Sep-02 8:38 
GeneralRe: XML vs. Registry PinsussAnonymous16-Sep-02 9:56 
GeneralRe: XML vs. Registry PinmemberHoudini17-Sep-02 5:16 
GeneralRe: XML vs. Registry PinmemberPaul A. Howes17-Sep-02 6:43 
GeneralRe: XML vs. Registry PinmemberHoudini17-Sep-02 7:39 
GeneralWhy XML PinsussChris Holt15-Sep-02 14:06 
GeneralRe: Why XML PinmemberMaximilien15-Sep-02 14:17 
GeneralRe: Why XML PinmemberTim Kosse15-Sep-02 20:41 

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 | Mobile
Web02 | 2.8.140709.1 | Last Updated 17 Sep 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid