Monday, April 25, 2011

MSXML: How does one programmatically get the error text for failed transforms?

XMLNotepad provides the following text (for example) when a transform fails:

Error Transforming XML
The variable or parameter 'saturated-background-color' was duplicated with the same import precedence.

How would I go about getting this error text programmatically? My code looks like this:

CComPtr<IXSLTemplate> tmpl;
HRESULT hr = CoCreateInstance(CLSID_XSLTemplate, NULL, CLSCTX_INPROC_SERVER, IID_IXSLTemplate, (void**)&tmpl);
if (SUCCEEDED(hr)) {
    hr = tmpl->putref_stylesheet(xslt_doc);
    if (SUCCEEDED(hr)) {
 // Huzzah; do stuff.
    } else {
 // How do I get the error text?  I want to log it!
    }
}
From stackoverflow
  • If IXSLTemplate supports IErrorInfo (AFAIK, it does), then you can query that for additional information.

    (jeffamaphone clued me in on the proper way to get this - using the GetErrorInfo() API:)

    CComPtr<IErrorInfo> error;
    if (SUCCEEDED( GetErrorInfo(0, &error) ) && error)
    {
       // call IErrorInfo::GetDescription(), etc.
    }
    
    jeffamaphone : Nope, it doesn't seem to support that interface. QueryInterface() returns E_NOINTERFACE.
    Shog9 : That's a shame. You could try IXMLDOMParseError, but i'm pretty sure that's only used by the DOMDocument classes.
    jeffamaphone : Yeah, I had already looked at that. :(
    jeffamaphone : I suspect the right way to get the IErrorInfo pointer is not through QueryInterface, but through the GetErrorInfo() COM API itself. I've implemented that, and will see if it works.
    Shog9 : Interesting... I didn't even know about that. Post an answer if it works!
    jeffamaphone : Marking as answer... you were pretty close. Instead the solution is to call the GetErrorInfo() API directly. http://msdn.microsoft.com/en-us/library/ms221032.aspx
    Shog9 : @jeffamaphone: excellent work, i've updated the example to reflect this.

0 comments:

Post a Comment