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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Kill this article.staffNishant Sivakumar21 Apr '06 - 11:49 
TomM wrote:
If the code is truly lost, this article should be removed.
 
Too bad, it sounds useful.
 
I'm guessing that there is another article with the same name.

 
The URLs to the zips were incorrect. It's been fixed now. Please try downloading the zips once more.
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there!

GeneralSpecify your language, please !!memberAceWizardly1 Nov '05 - 1:22 
I spent several hours yesterday looking through CodeProject for C++ code that I can use to read and write application settings into an XML file. This project *WAS* one of my prime candidates, the first one I opened of the .ZIP files I fetched. Imagine my "surprise" when, instead of finding .cpp and .h files in the .ZIP file, I found ones with .cs and .resx extensions - leaving me wondering "what is ??this??" (I've never done any "CSharp" programming before, and have no intention of doing any in the future, so these filename extensions meant nothing to me.) Fortunately, a quick look into the third file I tried opening with Notepad - the _project_ file - told me what was going on - and that this code is completely useless to me.
 
Do us all a favor: Any time you post code to CodeProject, SPECIFY THE LANGUAGE IT'S WRITTEN IN, IN THE BODY OF YOUR ARTICLE, AT LEAST, and, preferably, in the title line.
 
Of course, if the CodeProject search system would *exclude* C# articles when the C# checkbox is unchecked during a search, that would help also - but it won't work if the system doesn't know what language your code is written in, in the first place.
 
Just my $0.02 worth ...
GeneralRe: Specify your language, please !!memberJason Henderson1 Nov '05 - 2:57 
Well Ace, this was a C++ project but it has somehow gotten mixed up with another article. Perhaps an editor messed up or something. Anyway, I do not have the code for the article any longer so you will have to figure it out for yourself or find a different example. I might update it someday, but I'm pretty busy right now. Sorry.
 
"Live long and prosper." - Spock
Jason Henderson
blog

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 
There was an error in the download URLs. It's been fixed. Please try again.
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there!

GeneralRe: Save Application Settings to XMLmemberJason Henderson20 Dec '06 - 2:40 
Nishant Sivakumar wrote:
There was an error in the download URLs. It's been fixed. Please try again.

 
Thanks for fixing this Nish. I just now noticed.
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

GeneralRe: Save Application Settings to XMLeditorNishant Sivakumar20 Dec '06 - 2:43 
Jason Henderson wrote:
Thanks for fixing this Nish. I just now noticed.

 
You are welcome. I was wondering just when you'd thank me. It's been a while now you see. Big Grin | :-D
 
Regards,
Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*)

GeneralRe: Save Application Settings to XMLmemberJason Henderson20 Dec '06 - 2:48 
I see that. Poke tongue | ;-P
But thanks again.
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

GeneralRe: Specify your language, please !!memberziopino7013 Apr '10 - 21:06 
jason u noob! C# or C++? omg! wake up! jesus!
GeneralNo More CXMLSettings ClassmemberNicolas DUFAUT12 Feb '05 - 22:15 
Hi, I'd like to test your CXMLSettings Class but I can't find it in the sources you gave in Code Project ?
 
Do you have a version in Visual C++ 6.0 ?
 
Thank's in advance !
 
Nicolas
GeneralRe: No More CXMLSettings ClassmemberJason Henderson13 Feb '05 - 18:13 
This is very strange. That is not my source code or demo project.
 
I will try to find the original code and repost it.
 
"Live long and prosper." - Spock
Jason Henderson
blog

GeneralRe: No More CXMLSettings ClassmemberNicolas DUFAUT13 Feb '05 - 19:17 
Thanks !Smile | :)
GeneralRe: No More CXMLSettings Classmemberprcarp23 Aug '05 - 3:01 
Any chance you found it yet? The contents of the download still don't seem to match your article.
GeneralRe: No More CXMLSettings ClassmemberJason Henderson23 Aug '05 - 3:20 
No dice. Sorry.
It's not in Google's cache either.
 
"Live long and prosper." - Spock
Jason Henderson
blog

GeneralRe: No More CXMLSettings ClassmemberJason Henderson20 Dec '06 - 2:39 
prcarp wrote:
Any chance you found it yet? The contents of the download still don't seem to match your article.

 
The code is restored if you are still interested.
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

GeneralRe: No More CXMLSettings Classmemberprcarp20 Dec '06 - 2:54 
Thanks, Jason! Just in time for my next project....
GeneralRe: No More CXMLSettings ClassmemberJason Henderson20 Dec '06 - 3:06 
Nicolas DUFAUT wrote:
Hi, I'd like to test your CXMLSettings Class but I can't find it in the sources you gave in Code Project ?
 
Do you have a version in Visual C++ 6.0 ?
 
Thank's in advance !

 
The code is restored if you are still interested.
 
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav

Jason Henderson
space art blog

GeneralCoInitialize(NULL)memberSlawa6 May '03 - 5:52 
Hi,
 
I took me 3 hours to find out why the whole thing doesn't work for me. So if you're making a new projects yourself don't forget to put
CoInitialize(NULL);
in the beginning.
 
Slawa.
GeneralRe: CoInitialize(NULL)memberJason Henderson6 May '03 - 7:03 
Good point. The XML parser won't work without it.
 

Jason Henderson

My articles

"The best argument against democracy is a five-minute conversation with the average voter." - Winston Churchill


GeneralI love it for MY needs!memberPaul Kissel4 Apr '03 - 9:46 
Thanks for the submission!
GeneralRe: I love it for MY needs!memberJason Henderson4 Apr '03 - 9:57 
You're very welcome.
 

Jason Henderson
"You must be the change you wish to see in the world." - Gandhi

articles profile


GeneralattributesmemberJesper Odgaard Nielsen26 Mar '03 - 2:38 
Hi, Good work. I like your program a lot. I'm sorry to see the criticism - its completely out in the woods. I see CP as a place to get inspiration, and your program has given me that. Thanks. Do you know anything about adressing attributes in xml?Blush | :O
 
brgds
Jesper Odgaard Nielsen, DK
GeneralRe: attributesmemberJason Henderson4 Apr '03 - 9:56 
Jesper Odgaard Nielsen wrote:
Do you know anything about adressing attributes in xml?
 
Not sure what you mean.
 

Jason Henderson
"You must be the change you wish to see in the world." - Gandhi

articles profile


GeneralIsolated Storage - Out of the RegistrymemberNeville Franks20 Sep '02 - 14:38 
For those of you who have been discussing the merits of the registry it would seem MS has a new Isolated Storage mechanism which replaces the previous methods of storing data in Windows .ini files and the system Registry.
 
For more info see: Maintaining User-Specific Data Using Isolated Storage and C# at http://codeguru.earthweb.com/cs_data/IsolatedStorage.html[^] I know they are the enemy but ...;P
 
Neville Franks, Author of ED for Windows. www.getsoft.com
GeneralNice ArticlesussAnonymous20 Sep '02 - 3:19 
Hi,
 
It's a good idea, I like it, thanks for your article.
 
So long
Braulio

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