I have used CMarkup a lot in past projects. It has some great features and it also allows you to create documents without a single root element (can be used for log files where you just append entries to the end of the file), but this is not well-formed XML and other tools will typically fail to load it.
I will assume your goal is to create XML with a "Records" root element something like this:
<records>
<login>
....
</login>
<login>
....
</login>
<login>
....
</login>
</records>
I do not believe you will be able to load the document in append mode to do this, but CMarkup is pretty fast, so you can load the document and add new Login elements to the end of the existing Login elements without worrying too much about the performance.
To use CMarkup for maintaining an XML document laid out as I have shown, I have modified your code and removed some of the test ing you had.
CMarkup xmlWrite;
sPath = "D:\Login.xml";
xmlWrite.Load(sPath);
if (!xmlWrite.FindElem( _T("Records") ))
{
xmlWrite.AddElem( _T("Records") );
}
if (xmlWrite.IntoElem())
{
if (xmlWrite.AddElem( _T("Login") ) && xmlWrite.IntoElem())
{
xmlWrite.AddElem( _T("LoginID"), getLoginstr);
xmlWrite.AddElem( _T("Password"), getEncrypted);
xmlWrite.AddElem( _T("DateTime"), getDateTime);
xmlWrite.OutOfElem();
}
xmlWrite.OutOfElem();
}
xmlWrite.Save();
To retrieve stored values for a specific Login, you open the file, find the root element and navigate into it. Then you just use a while loop to go through all the Login elements. For every login element, you retrieve the value of the LoginID child element and compare it to the one you are looking for and once you find it, you retrieve the values of the Password and DateTime child elements.
I put this function together to show you how simple it is, but I did not test if it compiles or runs - it should be close to working.
bool GetLogin(LPCTSTR szFileName,
LPCTSTR szId,
CString& strPassword,
CString& strDateTime)
{
bool bReturn = false;
CMarkup markup;
if (markup.Load( szFileName ))
{
if (markup.FindElem( _T("Records") ) && markup.IntoElem())
{
while (markup.FindElem( _T("Login") ))
{
if (markup.FindChildElem( _T("LoginID") ))
{
CString strId = markup.GetChildData();
if (strId.CompareNoCase( szId ) == 0)
{
if (markup.FindChildElem( _T("Password") ))
{
strPassword = markup.GetChildData();
}
if (markup.FindChildElem( _T("DateTime") ))
{
strDateTime = markup.GetChildData();
}
bReturn = true;
break;
}
}
}
markup.OutOfElem();
}
}
return bReturn;
}
Soren Madsen