查看已经保存的.xml文件:用记事本打开。
bool CZWMateralCreateDlg::SaveDataToXML() ///CZWMateralCreateDlg类名
{
Zwc::ZXml xml;
pugi::xml_parse_result result = xml.loadFile(Z_SP::FP::DlgDefault());///DlgDefault为.xml文件名
//Z_SP::FP::DlgDefault()求出了.xml文件的路径
if (result.status == pugi::status_file_not_found)
Zwc::ZXmlHelper::AppendDeclarationNode(xml);
else if (result.status != pugi::status_ok)
return FALSE;
pugi::xml_node root = Zwc::ZXmlHelper::GetFirstOrCreateChild(xml.root(), _T("xmlRoot"));
if (root.empty())
return FALSE;
pugi::xml_node materalCreateDlgNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(root, _T("MateralCreateDlg"));///MateralCreateDlg、materalCreateDlgNode
if (materalCreateDlgNode.empty())
return FALSE;
///上面的几乎不需要修改
bool bIsCreateExcelTable = ((CButton*)GetDlgItem(IDC_CHECK_ZW_MATERIAL_CREATE_EXCEL))->GetCheck();///获取CheckBox的选中状态
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(materalCreateDlgNode, _T("IsCreateExcelTable")).set_value(bIsCreateExcelTable);///保存一条名为IsCreateExcelTable的记录
CString strExcleTemplatePath;
GetDlgItem(IDC_EDIT_ZW_MATERIAL_EXCEL_TEMPLATE)->GetWindowText(strExcleTemplatePath);
///获取Edit Control的文本、IDC_EDIT_ZW_MATERIAL_EXCEL_TEMPLATE为Edit Control的ID
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(materalCreateDlgNode, _T("ExcleTemplatePath")).set_value(strExcleTemplatePath);
xml.saveFile(Z_SP::FP::DlgDefault());
return TRUE;
}
bool CZWMateralCreateDlg::LoadDataFromXML(StructMatarialCreate &structMetariaCreateData)
{///StructMatarialCreate已定义的结构体,做项目时需要给结构体对象赋值,故设置结形参。也可以不加形参,具体问题具体分析
Zwc::ZXml xml;
pugi::xml_parse_result result = xml.loadFile(Z_SP::FP::DlgDefault());
if (result.status != pugi::status_ok)
return FALSE;
pugi::xml_node root = xml.root().child(_T("xmlRoot"));
if (root.empty())
return FALSE;
pugi::xml_node materalCreateDlgNode = root.child(_T("MateralCreateDlg"));
if (materalCreateDlgNode.empty())
return FALSE;
structMetariaCreateData.m_bIsCreateExcelFile = materalCreateDlgNode.attribute(_T("IsCreateExcelTable")).as_bool();///获取名为IsCreateExcelTable的记录,是bool类型的
structMetariaCreateData.m_strExcleTemplatePath = materalCreateDlgNode.attribute(_T("ExcleTemplatePath")).as_string();
return TRUE;
}
==============================================================
bool Z_ElbowMardinCalculationDlg::SaveDataToXML()
{
Zwc::ZXml xml;
pugi::xml_parse_result result = xml.loadFile(Z_SP::FP::DlgDefault());
if (result.status == pugi::status_file_not_found)
Zwc::ZXmlHelper::AppendDeclarationNode(xml);
else if (result.status != pugi::status_ok)
return false;
pugi::xml_node root = Zwc::ZXmlHelper::GetFirstOrCreateChild(xml.root(), _T("xmlRoot"));
if (root.empty())
return false;
pugi::xml_node elbowMardinCalculationDlgNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(root, _T("为保存的所有数据起个响亮的名字"));
if (elbowMardinCalculationDlgNode.empty())
return false;
pugi::xml_node elbowNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("数据类型1"));
elbowNode.remove_children();
for (int i = 1; i < m_pGridCtrl.GetRowCount(); i++)
{
CString strNodeName;
strNodeName.Format(_T("data%d"), i);
pugi::xml_node dataNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowNode, strNodeName.GetString());
dataNode.remove_attributes();
CString strSourceElbowAngle = m_pGridCtrl.GetItemText(i, 0);
CString strGoalElbowAngle = m_pGridCtrl.GetItemText(i, 1);
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(dataNode, _T("SourceElbowAngle")).set_value(strSourceElbowAngle);
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(dataNode, _T("GoalElbowAngle")).set_value(strGoalElbowAngle);
}
pugi::xml_node conditionAndRulerNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("数据类型2"));
conditionAndRulerNode.remove_children();
for (int i = 1; i < m_pGridCtrlRuler.GetRowCount(); i++)
{
CString strNodeName;
strNodeName.Format(_T("data%d"), i);
pugi::xml_node dataNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(conditionAndRulerNode, strNodeName.GetString());
dataNode.remove_attributes();
CString strCondition = m_pGridCtrlRuler.GetItemText(i, 0);
CString strRuler = m_pGridCtrlRuler.GetItemText(i, 1);
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(dataNode, _T("Contion")).set_value(strCondition);
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(dataNode, _T("Ruler")).set_value(strRuler);
}
pugi::xml_node filterKeysNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("FilterKeys"));
filterKeysNode.remove_children();
for (int i = 0; i < m_List.GetItemCount(); i++)
{
CString strNodeName;
strNodeName.Format(_T("data%d"), i);
pugi::xml_node dataNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(filterKeysNode, strNodeName.GetString());
dataNode.remove_attributes();
CString strKeyWords = m_List.GetItemText(i, 0);
Zwc::ZXmlHelper::GetFirstOrCreateAttribute(dataNode, _T("FilterKeyWords")).set_value(strKeyWords);
}
xml.saveFile(Z_SP::FP::DlgDefault());
return true;
}
bool Z_ElbowMardinCalculationDlg::LoadDataFromXML()
{
Zwc::ZXml xml;
pugi::xml_parse_result result = xml.loadFile(Z_SP::FP::DlgDefault());
if (result.status != pugi::status_ok)
return false;
pugi::xml_node root = xml.root().child(_T("xmlRoot"));
if (root.empty())
return false;
pugi::xml_node elbowMardinCalculationDlgNode = root.child(_T("为保存的所有数据起个响亮的名字"));
if (elbowMardinCalculationDlgNode.empty())
return false;
std::vector<std::pair<CString, CString>> vecPair;
pugi::xml_node elbowNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("数据类型1"));
std::for_each(ZWS_B2E(elbowNode), [&](const auto& child) {
std::pair<CString, CString> pair(child.attribute(_T("SourceElbowAngle")).as_string(), child.attribute(_T("GoalElbowAngle")).as_string());
vecPair.push_back(pair);
});
m_pGridCtrl.SetRowCount(vecPair.size() + 1);
for (int i = 1; i <= vecPair.size(); i++)
{
InitGridCellStyle(i);
m_pGridCtrl.SetItemText(i, 0, vecPair[i - 1].first);
m_pGridCtrl.SetItemText(i, 1, vecPair[i - 1].second);
}
vecPair.clear();
pugi::xml_node conditionAndRulerNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("数据类型2"));
std::for_each(ZWS_B2E(conditionAndRulerNode), [&](const auto& child) {
std::pair<CString, CString> pair(child.attribute(_T("Contion")).as_string(), child.attribute(_T("Ruler")).as_string());
vecPair.push_back(pair);
});
m_pGridCtrlRuler.SetRowCount(vecPair.size() + 1);
for (int i = 1; i <= vecPair.size(); i++)
{
m_pGridCtrlRuler.SetItemText(i, 0, vecPair[i - 1].first);
m_pGridCtrlRuler.SetItemText(i, 1, vecPair[i - 1].second);
}
pugi::xml_node filterKeysNode = Zwc::ZXmlHelper::GetFirstOrCreateChild(elbowMardinCalculationDlgNode, _T("FilterKeys"));
std::for_each(ZWS_B2E(filterKeysNode), [&](const auto& child) {
m_List.InsertItem(m_List.GetItemCount(), child.attribute(_T("FilterKeyWords")).as_string());
});
return true;
}