bool GetInlineXmlDoc(LPCTSTR pcszInlineXml,CComPtr<ixmldomdocument> & inlineXmlDoc)
{
if (SUCCEEDED(::CoCreateInstance(CLSID_DOMDocument2, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&inlineXmlDoc)))
{
VARIANT_BOOL isSuccessful = FALSE;
if(SUCCEEDED(inlineXmlDoc->loadXML(_bstr_t(pcszInlineXml), &isSuccessful)))
{
return true;
}
}
return false;
}</ixmldomdocument>
bool GetDocumentRootChild(CComPtr<ixmldomdocument> xmlDoc,CComPtr<ixmldomnode>& rootNode)
{
if (SUCCEEDED(xmlDoc->get_firstChild(&rootNode)))
{
CComBSTR bstr;
if (SUCCEEDED(rootNode->get_nodeName(&bstr)))
{
}
return true;
}
return false;
}</ixmldomnode></ixmldomdocument>
int _tmain(int argc, _TCHAR* argv[])
{ LPCTSTR pcszInlineXml = _T("<a><c>xmlContent</c></a>");
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
CComPtr<ixmldomdocument> XMLDoc;
HRESULT hr2 = ::CoCreateInstance(CLSID_DOMDocument2, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&XMLDoc);
if (SUCCEEDED(hr2))
{
VARIANT varFileName;
varFileName.bstrVal = _bstr_t(_T("rootXmlFile.xml"));
varFileName.vt = VT_BSTR;
VARIANT_BOOL isSuccess = FALSE;
if (SUCCEEDED(XMLDoc->load(varFileName, &isSuccess)))
{
CComPtr<ixmldomdocument> inlineXmlDoc;
if (GetInlineXmlDoc(pcszInlineXml,inlineXmlDoc))
{
CComPtr<ixmldomnode> rootNodeofInline;
if (GetDocumentRootChild(inlineXmlDoc, rootNodeofInline))
{
CComPtr<ixmldomnode> node;
if (SUCCEEDED(XMLDoc->selectSingleNode(_bstr_t(_T("/root/root1[0]")), &node)))
{
CComPtr<ixmldomnode> newChild;
if (SUCCEEDED(node->appendChild(rootNodeofInline, &newChild)))
{
if (SUCCEEDED(XMLDoc->save(varFileName)))
{
::MessageBox(NULL, _T("Worked"), _T("Worked"), MB_OK);
}
}
}
}
}
}
::VariantClear(&varFileName);
}
}
::CoUninitialize();
return 0;
}</ixmldomnode></ixmldomnode></ixmldomnode></ixmldomdocument></ixmldomdocument>