EzhNotes

April 5, 2008

XMLDOM tricks

Filed under: Uncategorized — alexezh @ 6:11 pm

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;
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: