我使用以下代码将数据保存到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;
}
代码本身很好。在程序的其他地方,我保存了文件,然后转到显示报告。它在CHtmlView
浏览器控件中对XML文件使用XSL转换:
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);
}
}
我发现:
有没有安全的方法可以保存到HDD,然后在文件完全写入光盘之前不使用它?