Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Save Application Settings to XML

, 16 Sep 2002
An easy to use class that reads, writes and deletes app settings to an XML file.
xmlsettings_demo.zip
XmlSettingsTest.clw
XmlSettingsTest.dsp
XmlSettingsTest.dsw
res
XmlSettingsTest.ico
mainpic.bmp
bitmap2.bmp
Release
XmlSettingsTest.exe
xmlsettings_src.zip
// XmlSettingsTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XmlSettingsTest.h"
#include "XmlSettingsTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXmlSettingsTestDlg dialog

CXmlSettingsTestDlg::CXmlSettingsTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CXmlSettingsTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CXmlSettingsTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXmlSettingsTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CXmlSettingsTestDlg)
	DDX_Control(pDX, IDC_DELETERADIO, m_radioDelete);
	DDX_Control(pDX, IDC_VALUESTATIC, m_ValueStatic);
	DDX_Control(pDX, IDC_VALUENAME, m_edValueName);
	DDX_Control(pDX, IDC_VALUE, m_edValue);
	DDX_Control(pDX, IDC_KEYPATH, m_edKeyPath);
	DDX_Control(pDX, IDC_FILEPATH, m_edFilePath);
	DDX_Control(pDX, IDC_STRINGRADIO, m_radioString);
	DDX_Control(pDX, IDC_LONGRADIO, m_radioLong);
	DDX_Control(pDX, IDC_READRADIO, m_radioRead);
	DDX_Control(pDX, IDC_WRITERADIO, m_radioWrite);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXmlSettingsTestDlg, CDialog)
	//{{AFX_MSG_MAP(CXmlSettingsTestDlg)
	ON_BN_CLICKED(IDC_STRINGRADIO, OnStringradio)
	ON_BN_CLICKED(IDC_LONGRADIO, OnLongradio)
	ON_BN_CLICKED(IDC_READRADIO, OnReadradio)
	ON_BN_CLICKED(IDC_WRITERADIO, OnWriteradio)
	ON_BN_CLICKED(IDC_VIEW, OnView)
	ON_BN_CLICKED(IDC_DELETERADIO, OnDeleteradio)
	ON_BN_CLICKED(IDC_QUESTION, OnQuestion)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXmlSettingsTestDlg message handlers

BOOL CXmlSettingsTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// initialize controls
	m_edFilePath.SetWindowText("C:\\test.config.xml");
	m_edKeyPath.SetWindowText("MySettings\\Setting_1\\Test");
	m_edValueName.SetWindowText("Value");
	m_edValue.SetWindowText("Test String");

	m_radioString.SetCheck(1);
	m_radioLong.SetCheck(0);

	m_radioRead.SetCheck(0);
	m_radioWrite.SetCheck(1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// get all of the radio buttons working correctly
void CXmlSettingsTestDlg::OnStringradio() 
{
	m_radioString.SetCheck(1);
	m_radioLong.SetCheck(0);
}

void CXmlSettingsTestDlg::OnLongradio() 
{
	m_radioString.SetCheck(0);
	m_radioLong.SetCheck(1);
}

void CXmlSettingsTestDlg::OnReadradio() 
{
	m_radioRead.SetCheck(1);
	m_radioWrite.SetCheck(0);
	m_radioDelete.SetCheck(0);
}

void CXmlSettingsTestDlg::OnWriteradio() 
{
	m_radioRead.SetCheck(0);
	m_radioWrite.SetCheck(1);
	m_radioDelete.SetCheck(0);
}

void CXmlSettingsTestDlg::OnDeleteradio() 
{
	m_radioRead.SetCheck(0);
	m_radioWrite.SetCheck(0);
	m_radioDelete.SetCheck(1);
}


void CXmlSettingsTestDlg::OnOK() 
{
	long lRetVal = 0;
	CString cstrRetVal = "";
	CString cstrFile, cstrKey, cstrValueName, cstrValue;
	
	m_edFilePath.GetWindowText(cstrFile);
	m_edKeyPath.GetWindowText(cstrKey);
	m_edValueName.GetWindowText(cstrValueName);
	m_edValue.GetWindowText(cstrValue);

	if (!cstrFile.IsEmpty() && !cstrKey.IsEmpty() && 
		(!cstrValueName.IsEmpty()) || m_radioDelete.GetCheck() == 1)
	{
		xmlSettings.SetSettingsFile(cstrFile);

		// read, write or delete?
		if (m_radioRead.GetCheck() == 1)
		{
			// long or string?
			if (m_radioString.GetCheck() == 1)
				cstrRetVal = xmlSettings.GetSettingString(cstrKey, cstrValueName, cstrValue);
			else
			{
				lRetVal = xmlSettings.GetSettingLong(cstrKey, cstrValueName, atol(cstrValue));
				cstrRetVal.Format("%d", lRetVal);
			}
		}
		else if (m_radioWrite.GetCheck() == 1)
		{
			// long or string
			if (m_radioString.GetCheck() == 1)
				lRetVal = xmlSettings.SetSettingString(cstrKey, cstrValueName, cstrValue);
			else
				lRetVal = xmlSettings.SetSettingLong(cstrKey, cstrValueName, atol(cstrValue));

			cstrRetVal.Format("Returned %d", lRetVal);
		}
		else
		{
			// delete
			cstrRetVal = "Not Deleted";

			if (xmlSettings.DeleteSetting(cstrKey, cstrValueName))
				cstrRetVal = "Deleted";
		}

		m_edValue.SetWindowText(cstrRetVal);
	}
	else
	{
		AfxMessageBox("Please fill in the necessary fields.");
	}
}

// view the XML
void CXmlSettingsTestDlg::OnView() 
{
	CString cstrFile;
	
	m_edFilePath.GetWindowText(cstrFile);

	if (!cstrFile.IsEmpty())
	{
		CString cstrMsg = "";
		int iRetVal = 0;
			
		iRetVal = (int)ShellExecute(NULL, NULL, cstrFile, NULL, NULL, 1);

		if (iRetVal > -1 && iRetVal < 33)
		{
			switch (iRetVal)
			{
			case ERROR_FILE_NOT_FOUND:
			case ERROR_PATH_NOT_FOUND:
				cstrMsg = "File or path not found.";
				break;
			case SE_ERR_NOASSOC:
				cstrMsg = "There is no application associated with the given file name extension.";
				break;
			default:
				cstrMsg.Format("ShellExecute returned %d.  Look it up.", iRetVal);
				break;
			}

			::MessageBox(this->m_hWnd, cstrMsg, "View XML File Failed!", MB_OK);
		}
	}
}

// a little help
void CXmlSettingsTestDlg::OnQuestion() 
{
	::MessageBox(NULL, "Example Key Path:  MySettings\\Setting_1\\Test \r\n\r\n"
		"Tips: \r\n"
		"  - Install MSXML 4.0 (but if you're reading this, its probably installed). \r\n"
		"  - If you haven't already guessed, keys are separated by \\. \r\n"
		"  - Spaces in keys and value names are converted to _ automatically. \r\n"
		"  - If the XML file does not exist, it is created on the fly. \r\n"
		"  - To delete an entire key, leave the Value Name blank.",
		"XML Settings Test HELP", MB_OK | MB_ICONINFORMATION);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 17 Sep 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid