我最近一直在使用 Rapidxml 并且遇到了以下问题。当我尝试添加不是硬编码但在程序运行时生成的属性时,rapidxml 会插入错误的字符。
这是我的代码示例:
void ProcessInfo::retriveInfo()
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk = ProcessFirst( &pe, hSnapshot );
using namespace rapidxml;
xml_document<> doc;
xml_node<>* decl = doc.allocate_node(node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
xml_node<>* root = doc.allocate_node(node_element, "rootnode");
while(fOk)
{
std::string processFile = pe.szExeFile;
xml_node<>* processName = doc.allocate_node(node_element, PROCESS_NODE);
root->append_node( processName );
processName->append_attribute(doc.allocate_attribute( PROCESS_NAME, processFile.c_str() ) );
char szPID[PID_BUFFER];
memset(szPID, 0x00, sizeof(szPID) );
itoa(pe.th32ProcessID, szPID, 10 );
processName->append_attribute(doc.allocate_attribute( PROCESS_ID, szPID ));
char szParentPID[PID_BUFFER];
itoa( pe.th32ParentProcessID, szParentPID, 10 );
processName->append_attribute(doc.allocate_attribute( PROCESS_PARENT_ID, szParentPID ));
std::cout << processFile.c_str() << " " << szPID << " " << szParentPID << std::endl;
fOk = ProcessNext( &pe, hSnapshot );
}
doc.append_node( root );
std::cout << doc;
}
编码似乎有问题,但我无法弄清楚,为什么?有人可以帮帮我吗?