XML内容
Test.xml
<?xml version="1.0" encoding="gb2312"?>
<Config><DevID>00001111</DevID>
</Config>
以下程序读取Test.xml中的DevID节点数值。
#include "stdafx.h"
#include <windows.h>
#include "iostream"
#import <msxml3.dll>
using namespace std;
//TCHAR lpFileName[MAX_PATH];
char m_szCfgFilePath[MAX_PATH];
char m_szCurExeDir[MAX_PATH];
DWORD m_dwDevID;
int _tmain(int argc, _TCHAR* argv[])
{
char szDriver[MAX_PATH];
char szDir[MAX_PATH];
::GetModuleFileNameA(NULL, m_szCfgFilePath, sizeof(m_szCfgFilePath)-1); //获取当前exe的完整路径
::_splitpath_s(m_szCfgFilePath, szDriver, sizeof(szDriver), szDir, sizeof(szDir), NULL, 0, NULL, 0); //得到盘符,目录
::_makepath_s(m_szCurExeDir, szDriver, szDir, NULL, NULL); //制作exe目录
::_makepath_s(m_szCfgFilePath, szDriver, szDir, "Test", "xml"); //制作与exe同一个目录下的xml路径
::CoInitialize(NULL);
printf("%s\n", m_szCfgFilePath);
MSXML2::IXMLDOMDocumentPtr m_xmlCfg;
HRESULT hr = m_xmlCfg.CreateInstance(__uuidof(MSXML2::DOMDocument30));
m_xmlCfg->async = false;
m_xmlCfg->load(m_szCfgFilePath); //加载xml文件
MSXML2::IXMLDOMNodePtr xmlNode;
xmlNode = m_xmlCfg->selectSingleNode("Config/DevID"); //读取节点
printf("%s\n", xmlNode->text.operator const char*()); //输出00001111
m_dwDevID = ::strtoul(xmlNode->text.operator const char*(), NULL, 10);
printf("%lu\n", m_dwDevID); //输出1111
while (1);
return 0;
}