Saturday, April 5, 2008

XMLDOM tricks

Add \r\n to the document when saving. Original info from Line breaks and IXMLDOMDocument

static LPCWSTR s_szFormatConfig = L""\
L" edit window is not XML friendly, see original post for details";

static HRESULT FormatConfig(IXMLDOMDocument * pInDoc, CComPtr & spOutDoc)
{
HRESULT hr = S_OK;
CComPtr spSS;
VARIANT_BOOL vbSuccess;

IFC(CoCreateInstance(__uuidof(DOMDocument60),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&spSS));

IFC(CoCreateInstance(__uuidof(DOMDocument60),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&spOutDoc));

spSS->put_resolveExternals(VARIANT_FALSE);
spSS->put_preserveWhiteSpace(VARIANT_TRUE);

spOutDoc->put_resolveExternals(VARIANT_FALSE);
spOutDoc->put_preserveWhiteSpace(VARIANT_TRUE);

//
// load SS from string
//
IFC(spSS->loadXML(CComBSTR(s_szFormatConfig), &vbSuccess));
if(vbSuccess != VARIANT_TRUE)
{
hr = E_FAIL;
goto Cleanup;
}

//
// convert
//
IFC(pInDoc->transformNodeToObject(spSS, CComVariant(spOutDoc)));

Cleanup:

return hr;
}

Convert document to UTF8

static HRESULT ForceUTF8(IXMLDOMDocument * pDoc)
{
HRESULT hr = S_OK;
CComPtr spPI;
CComPtr spOldPI;
CComPtr spOutOldPI;
CComPtr spChildren;

IFC(pDoc->createProcessingInstruction(L"xml", L"version=\"1.0\" encoding=\"UTF-8\"", &spPI));

IFC(pDoc->get_childNodes(&spChildren));

IFC(spChildren->get_item(0, &spOldPI));

IFC(pDoc->replaceChild( spPI, spOldPI, &spOutOldPI));

Cleanup:

return hr;
}

1 comment:

Anonymous said...

PINDOC?
Like in JKR - 66?
This is the GI- JOES of JO BIDEN with DICK CHENEY- SCHAR- LEARS?
This is your NEVADA WHAMBANGO?
And all the SKY ROCK with ACE?
DISCO 3 and MP MIG SEMPOU got a COWSCULA - URAMU and it's a huge THUMMU - SORAM and made
MANDES - BEDISMAN .
And y'all know what that means?
YONNOB 113....with KILLA WATT....and MASTESPANKER 69....being JOHN KERR.