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   
GeneralNice workmemberDonsw8 May '09 - 16:18 
Good article
 
cheers,
Donsw
My Recent Article : Ajax Calendar Control

GeneralThanksmemberislobodan6 Dec '08 - 15:58 
Class works excellent, you got my 5.
This class is the only acceptable solution for saving xml settings from MFC app.
GeneralRe: Thanksmemberelo12312 Dec '08 - 12:56 
Agree, this is just a simple solution that works! Need some small changes to work with UNICODE and to store files in app_local_data. The rest is just perfect.
QuestionA suggestionmemberstokos20 Mar '07 - 2: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 onlymemberPrajakta Patil16 Jan '07 - 18:04 
Hi
 
I am able to wite diffrent type of shapes using xmlwitter.
 
But not able to read that shapes using xmlreader.
 
please help me out.

 
Prajakta

GeneralRe: Read and save Shapes using xml in vb.net onlymemberJason Henderson17 Jan '07 - 2:29 
I've never worked with shapes in xml before. I assume its still just text? If so, I wouldn't know why you couldn't read it. Does it give you an error code?
 
"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

QuestionRe: Read and save Shapes using xml in vb.net onlymemberPrajakta Patil22 Jan '07 - 19:46 
I have finished with saving shapes. But I'm not able to save images and text font. I have two separate controls one is Bitmap and another one is Textbox. Using Bitmap user can add image to current drawing and using Textbox Text.
I'm not able to save the image file and font related information to xml.
Can you help me out??
 
Prajakta

GeneralFailure on Closemember123qwertz19 Dec '06 - 21:38 

void Release() throw()
{
ATLASSERT( nRefs != 0 );
 
if( _InterlockedDecrement( &nRefs ) <= 0 )
00438A42 lea eax,[ecx+0Ch]
00438A45 or edx,0FFFFFFFFh
00438A48 lock xadd dword ptr [eax],edx /*<< HERE*/
00438A4C dec edx
00438A4D test edx,edx
00438A4F jg ATL::CStringData::Release+19h (438A5Bh)
{
pStringMgr->Free( this );
00438A51 mov eax,dword ptr [ecx]
00438A53 mov edx,dword ptr [eax]
00438A55 push ecx
00438A56 mov ecx,eax
00438A58 call dword ptr [edx+4]
}
}
00438A5B ret

The Parser Works fine, but if my app end a [debug][cancel][send to ms] Erro occurres. I just have to do:

CXMLSettings *xmlSettings;
xmlSettings = new CXMLSettings();

anywhere to cause this.
 

-- modified at 3:35 Wednesday 20th December, 2006
GeneralRe: Failure on ClosememberJason Henderson20 Dec '06 - 2:42 
Check the obvious first - are you deleting the pointer you allocated?
 
delete xmlSettings;
 
"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

GeneralKill this article.memberTomM21 Apr '06 - 10:34 
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.
 

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
GeneralRe: Nice ArticlememberJohn Mautari18 Oct '02 - 5:39 
Very usefull class! Big Grin | :-D
 
Thanks.

GeneralCString in interface methodssussAnonymous18 Sep '02 - 16:20 
How lame
GeneralRe: CString in interface methodsmemberJason Henderson19 Sep '02 - 3:11 
Get a life.
 

Jason Henderson
start page
articles
"If you are going through hell, keep going." - Sir Winston Churchill


GeneralRe: CString in interface methodssussAnonymous19 Sep '02 - 10:47 
I have a life, and in my life I don't put CString in my headers. I am so tired of seeing this. By putting CString in your interface, you force your clients to to use MFC or WTL or something with CString. Use LPCTSTR in your interface and if you must, use CString in your implementation.
GeneralRe: CString in interface methodsmemberJason Henderson19 Sep '02 - 10:51 
I gave out the source code so if you don't use MFC go change it yourself!
 

Jason Henderson
start page
articles
"If you are going through hell, keep going." - Sir Winston Churchill


GeneralRe: CString in interface methodssussAnonymous19 Sep '02 - 13:54 
I knew it would be pointless to even say anything.
GeneralRe: CString in interface methodssussAnonymous16 Oct '02 - 12:17 
Not to mention that this forces you to use VC6 if CString are compiled in library's compiled with VC6. I also realy hate it. havent we learned to use 'const char*' ?WTF | :WTF:
GeneralRe: CString in interface methodsmemberGD_Patrick10 Oct '06 - 23:05 
Anonymous wrote:
I knew it would be pointless to even say anything.

 
Why did you say something then? Instead of being grateful, cry on.
GeneralRe: CString in interface methodsmemberCodeProjectSQ19 Sep '02 - 15:26 
Personally, I use MFC and use CString everywhere. If you do not use MFC then simply modify the code yourself. What is the problem?
GeneralRe: CString in interface methodssussAnonymous11 Oct '02 - 19:57 
You're smart
GeneralRe: CString in interface methodsmemberNeville Franks20 Sep '02 - 14:33 
Anonymous wrote:
How lame
 
It is hard to beat an Anonymous poster who has probably never written an article for CP nor contibuted in any usefull way who can't even go to the trouble to spell out what his problem is. Talk about lame.Hmmm | :|
 
Neville Franks, Author of ED for Windows. www.getsoft.com
GeneralXML vs Registry...sussigor196017 Sep '02 - 19:15 
I think, when you were discussing XML vs Registry issue you all forgot that Registry as it implemented is Direct access Database -- meaning you can just change or read just one record without rewriting the whole file...
How would you do it in XML may I ask???...
So, I would say: when efficiency and replication is an issue -- Use Registry... When code portability is of great concern -- use INI file and XML if you want...
However, I didn't encounter any portability reasons in my own work however...
GL
I
QuestionWhy not use both?memberGary Wheeler17 Sep '02 - 6:14 
My applications use both the registry and XML, as follows:
 
HKEY_LOCAL_MACHINE\SOFTWARE\...: User-agnostic application settings
HKEY_CURRENT_USER\Software\...: User-specific application settings
XML: User 'documents'
 
Admittedly, our documents are relatively small (< 256K), which means the DOM is perfectly adequate for loading/saving them.
 
As far as support issues go, why not provide a little application that can export the relevant registry contents for the user? If you wanted, you could make it smart enough to export the registry stuff, accumulate system information, accumulate VERSIONINFO stuff from your application, and package it all up and e-mail it to your support folks. If your product is big enough, something like this would pay for itself in short order by providing 99.7% of the info the support person needs.
 

"Think of it as evolution in action." - 'Oath of Fealty' by Larry Niven and Jerry Pournelle
AnswerRe: Why not use both?memberJason Henderson17 Sep '02 - 8:09 
Gary Wheeler wrote:
As far as support issues go, why not provide a little application that can export the relevant registry contents for the user? If you wanted, you could make it smart enough to export the registry stuff, accumulate system information, accumulate VERSIONINFO stuff from your application, and package it all up and e-mail it to your support folks. If your product is big enough, something like this would pay for itself in short order by providing 99.7% of the info the support person needs.
 
Nos you're thinking. There are several application that do just that but may not use XML.
 

Jason Henderson
start page
articles
"If you are going through hell, keep going." - Sir Winston Churchill


GeneralMissing the resource file (*.rc)memberSøren Madsen16 Sep '02 - 19:34 
Hi There
 
I became extreme happy this morning, when I "felt" over this article, but I can't compile the demo project, because I missing XmlSettingsTest.rc.
 
Thanks in advance,
 
Best Regards
Søren Madsen
Denmark

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.130516.1 | Last Updated 17 Sep 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid