191上位机VC MFC利用XML文件读取保存参数文件
191上位机VC MFC利用XML文件读取保存参数文件 功能展示 XML文件可以自定义标签,很多应用程序都会用这类型文件来保存配置或程序参数,我们当前例程通过软件提供的接口函数实现从XML文件读取数据与保存参数功能,点击保存数据到XML会将列表框的内容保存到XML文件中,在程序初始化时会从XML加载数据到程序,效果如图; 要点提示 XML文件接口函数使用前得添加其所在的头文件及动态库,既添加几行代码#import "msxml6.dll" #define MSXML MSXML2 ,using namespace MSXML2;,还得初始化接口函数才能正常调用函数;CoInitialize(NULL); 实现功能 1.新建基于对话框的应用程序 2.拖拽三编辑框ID为IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,及按钮<添加到列表框>用于向列表框输入内容,添加按钮<保存列表框数据到XML>用于保存列表框内容到XML文件; 3.添加 变量CString m_sXMLFilePath;//XML文件路径 及两自定义函数voidReadXMLData();//从XML文件读取数据到列表框 void SaveXMLData();//保存数据到XML文件 在主对话框的OnInitDialog()初始化 - CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_LIST1);
- pList->SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
- pList->ModifyStyle(0,LVS_REPORT,0);
- pList->InsertColumn(0,"网站名");
- pList->SetColumnWidth(0,100);
- pList->InsertColumn(1,"网址");
- pList->SetColumnWidth(1,100);
- pList->InsertColumn(2,"网站类型");
- pList->SetColumnWidth(2,150);
- //<>
- CHAR buf[MAX_PATH];
- GetModuleFileName(NULL,buf,MAX_PATH);
- m_sXMLFilePath=buf;
- m_sXMLFilePath = m_sXMLFilePath.Left(m_sXMLFilePath.ReverseFind('\\')+1)+"工控编程吧.XML";
- CoInitialize(NULL);//初始化接口函数
- if(_access(m_sXMLFilePath,0)!=-1)
- ReadXMLData();//读取数据到列表框
复制代码- 其他函数代码为
- void CGkbc8Dlg::SaveXMLData()
- {
- CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_LIST1);
- MSXML::IXMLDOMDocumentPtr xdoc;
- xdoc.CreateInstance(__uuidof(MSXML::DOMDocument));
- MSXML::IXMLDOMElementPtr Root = xdoc->createElement("XMLList");
- MSXML::IXMLDOMElementPtr Row;
- MSXML::IXMLDOMElementPtr Col;
- xdoc->appendChild(Root);
- for (int i = 0 ; i < pList->GetItemCount() ; i++)
- {
- Row = xdoc->createElement("Row");
- Root->appendChild(Row);
- for (int j = 0 ; j < pList->GetHeaderCtrl()->GetItemCount() ;j++)
- {
- LVCOLUMN column;
- column.mask = LVCF_TEXT;
- column.cchTextMax = 255;
- char str[255];
- column.pszText = str;
- pList->GetColumn(j,&column);
- Col = xdoc->createElement(column.pszText);
- Row->appendChild(Col);
- CString values;
- values = pList->GetItemText(i,j);
- Col->appendChild(xdoc->createTextNode((_bstr_t)values));
- }
- }
- xdoc->save((_bstr_t)m_sXMLFilePath);
- }
- void CGkbc8Dlg::OnAdd()
- {
- CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_LIST1);
- CString sNetSite,sNetAddr,sNetStyle;
- GetDlgItemText(IDC_EDIT1,sNetSite);
- GetDlgItemText(IDC_EDIT2,sNetAddr);
- GetDlgItemText(IDC_EDIT3,sNetStyle);
-
复制代码- int index = pList->InsertItem(pList->GetItemCount(),sNetSite);
- pList->SetItemText(index,1,sNetAddr);
- pList->SetItemText(index,2,sNetStyle);
- }
- void CGkbc8Dlg::OnSave() SaveXMLData();}
- void CGkbc8Dlg::ReadXMLData()
- {
- CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_LIST1);
- MSXML::IXMLDOMDocumentPtr xdoc;
- xdoc.CreateInstance(__uuidof(MSXML::DOMDocument));
- xdoc->load((_bstr_t)m_sXMLFilePath);
- IXMLDOMNodeListPtr nodelist=NULL;
- IXMLDOMNodeListPtr rows=NULL;
- IXMLDOMNodeListPtr cols=NULL;
- nodelist=xdoc->selectNodes("XMLList");
- MSXML::IXMLDOMNodePtr Root;
- MSXML::IXMLDOMNodePtr Row;
- MSXML::IXMLDOMNodePtr Col;
- Root = nodelist->nextNode();
- _bstr_t bstrname=Root->nodeName;
- rows = Root->childNodes;
- pList->DeleteAllItems();
- for (int i = 0 ; i < rows->length ; i++)
- {
- Row = rows->nextNode();
- cols = Row->childNodes;
- _bstr_t rowname = Row->nodeName;
- pList->InsertItem(pList->GetItemCount(),"");
- for (int j = 0 ; j < cols->length ; j++)
- {
- Col = cols->nextNode();
- IXMLDOMNodePtr node = Col->firstChild;
- _bstr_t value = node->nodeValue;
- pList->SetItemText(i,j,value);
- }
- }
- }
复制代码最后添加头文件#import"msxml6.dll" #define MSXML MSXML2 using namespace MSXML2; #include <io.h> 我们来演示下功能实现的整个过程
|