I use this code to save the data to XML:
bool CMeetingScheduleAssistantApp::SaveToXML(CString strFileXML, tinyxml2::XMLDocument& rDocXML)
{
FILE *fStream = nullptr;
CString strError, strErrorCode;
errno_t eResult;
bool bDisplayError = false;
int iErrorNo = -1;
using namespace tinyxml2;
// Does the file already exist?
if (PathFileExists(strFileXML))
{
// It does, so try to delete it
if (!::DeleteFile(strFileXML))
{
// Unable to delete!
AfxMessageBox(theApp.GetLastErrorAsString(), MB_OK | MB_ICONINFORMATION);
return false;
}
}
// Now try to create a FILE buffer (allows UNICODE filenames)
eResult = _tfopen_s(&fStream, strFileXML, _T("w"));
if (eResult != 0 || fStream == nullptr) // Error
{
bDisplayError = true;
_tcserror_s(strErrorCode.GetBufferSetLength(_MAX_PATH), _MAX_PATH, errno);
strErrorCode.ReleaseBuffer();
}
else // Success
{
// Now try to save the XML file
XMLError eXML = rDocXML.SaveFile(fStream);
const int fileCloseResult = fclose(fStream);
if (eXML != XMLError::XML_SUCCESS)
{
// Error saving
bDisplayError = true;
strErrorCode = rDocXML.ErrorName();
iErrorNo = rDocXML.GetErrorLineNum();
}
if (!bDisplayError)
{
if (fileCloseResult != 0)
{
// There was a problem closing the stream. We should tell the user
bDisplayError = true;
_tcserror_s(strErrorCode.GetBufferSetLength(_MAX_PATH), _MAX_PATH, errno);
strErrorCode.ReleaseBuffer();
}
}
}
if (bDisplayError)
{
if (iErrorNo == -1)
iErrorNo = errno;
strError.Format(IDS_TPL_ERROR_SAVE_XML, strFileXML, strErrorCode, iErrorNo);
AfxMessageBox(strError, MB_OK | MB_ICONINFORMATION);
return false;
}
return true;
}
The code in itself is fine. Elsewhere in my program I save the file and then go to display a report. It uses a XSL transformation with the XML file in a CHtmlView
browser control:
CMeetingScheduleAssistantApp::SaveToXML(strPreviewXML, myDoc);
if (ePreviewType == PreviewType::Editor)
{
if (m_pHtmlPreview != nullptr)
{
CString strURL = strPreviewXML;
if (iBookmarkId != -1)
strURL.Format(_T("%s#week%d"), (LPCTSTR)strPreviewXML, iBookmarkId);
m_pHtmlPreview->Navigate2(strURL, 0, nullptr);
}
}
else
{
if (m_pPrintHtmlPreview != nullptr)
m_pPrintHtmlPreview->Navigate2(strPreviewXML, 0, nullptr);
}
}
I am finding that:
- Sometimes my preview is white, and after a refresh it shows.
- At other times if I am working in what I refer to as "weekly mode" and it recreates the XML each week I do get a message about the file already being in use. This does not always happen.
Is there secure way that we can save to the HDD and then not use the file until it has fully been written to disc?