2012-08-24 63 views
1

我使用ATL和但是CComPtr,像這樣:保存XML頭與但是CComPtr <的IXMLDOMDocument>(C++)

CComPtr<IXMLDOMDocument> xml_doc; 
HRESULT hr = xml_doc.CoCreateInstance(__uuidof(DOMDocument)) 
... 
HRESULT hr = xml_doc->createElement(CComBSTR(element_name.c_str()), &element); 
... 
etc 

當我調用xml_doc->保存它保存XML沒有報頭。什麼最簡單的方法來添加標題?

回答

2

解決:

HRESULT hr; 

CComPtr<IXMLDOMProcessingInstruction> proc_instr; 
hr = xml_out->createProcessingInstruction(L"xml", L"version=\"1.0\" encoding=\"UTF-8\"", &proc_instr); 
if (FAILED(hr)) 
    throw std::exception("Failed to create IXMLDOMDocument processing instruction"); 


CComPtr<IXMLDOMNode> first_child; 
hr = xml_out->get_firstChild(&first_child); 
if (FAILED(hr)) 
    throw std::exception("Failed to get first child of IXMLDOMDocument"); 

hr = xml_out->insertBefore(proc_instr, CComVariant(first_child), NULL); 
if (FAILED(hr)) 
    throw std::exception("Failed to insert IXMLDOMDocument processing instruction"); 


hr = xml_out->save(CComVariant(header->output_file.c_str())); 
if (FAILED(hr)) 
    throw std::exception("Failed to save response XML to '" + header->output_file + "'"); 

http://msdn.microsoft.com/en-us/library/windows/desktop/ms755439(v=vs.85).aspx