c++ - MSXML6 中的第一次机会异常

标签 c++ visual-studio-2010 xsd msxml6 first-chance-exception

我正在根据 XSD 架构验证 XML 文件,就像它们在 MSXML 文档中所做的那样 example .我有以下代码:

XMLSchemaValidation updateInfoSchema;
updateInfoSchema.DoInitialization(L"schema.xsd");

HRESULT hr = CoInitialize(NULL);
if(SUCCEEDED(hr))
{
    try
    {
        _bstr_t bstrOutput = updateInfoSchema.validateFile(L"valid.xml");
    }
    catch(_com_error &e)
    {
        updateInfoSchema.dump_com_error(e); 
    }
    CoUninitialize();
}


// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt)        do { hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)


_bstr_t XMLSchemaValidation::validateFile(_bstr_t bstrFile)
{
    // Declare and initialize variables
    MSXML2::IXMLDOMSchemaCollectionPtr   pXS;
    MSXML2::IXMLDOMDocument2Ptr          pXD;
    MSXML2::IXMLDOMParseErrorPtr         pErr;
    _bstr_t bstrResult = L"";
    HRESULT hr = S_OK;

    // Create a schema cache and add xsd schema to it.
    CHK_HR(pXS.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60), NULL, CLSCTX_INPROC_SERVER));
    CHK_HR(pXS->add(L"", (LPCSTR)(SchemaFileName.GetString())));

    // Create a DOMDocument and set its properties.
    CHK_HR(pXD.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER));

    // Assign the schema cache to the DOMDocument's schemas collection.
    pXD->schemas = pXS.GetInterfacePtr();

    // Load bstrFile into the DOM document.
    pXD->async = VARIANT_FALSE;
    pXD->validateOnParse = VARIANT_TRUE;
    pXD->resolveExternals = VARIANT_TRUE;

    if(pXD->load(bstrFile) != VARIANT_TRUE)
    {
        pErr = pXD->parseError;

        bstrResult = _bstr_t(L"Validation failed on ") + bstrFile +
        _bstr_t(L"\n=====================") +
        _bstr_t(L"\nReason: ") + _bstr_t(pErr->Getreason()) +
        _bstr_t(L"\nSource: ") + _bstr_t(pErr->GetsrcText()) +
        _bstr_t(L"\nLine: ") + _bstr_t(pErr->Getline()) +
        _bstr_t(L"\n");
    }
    else
    {
        bstrResult = _bstr_t(L"Validation succeeded for ") + bstrFile +
        _bstr_t(L"\n======================\n") +
        _bstr_t(pXD->xml) + _bstr_t(L"\n");
    }

CleanUp:
    return bstrResult;
}

XMLSchemaValidation::DoInitialization(CString XSDFileName) 将 XSD 模式文件名获取到 CString XMLSchemaValidation::SchemaFileName 中。

代码遵循 MSXML 示例中的代码,但我明白了

First-chance exception at 0x76f9c41f (KernelBase.dll) in CSW.exe: Microsoft C++ exception: _com_error at memory location 0x04a7f014..

当代码到达 CHK_HR(pXS->add(L"", (LPCSTR)(SchemaFileName.GetString()))); 时。 hr 有 -2146697210。

谁能告诉我为什么会这样?

最佳答案

MSXML 抛出 HRESULT OBJECT_NOT_FOUND (0x800C0006) 因为您使用的 xml 没有指定字符集,详见 this question .或者 MSXML 找不到该文件。

关于c++ - MSXML6 中的第一次机会异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812780/

相关文章:

wpf - VS2010 的 WPF 在 Windows7 虚拟机中呈现不佳?

c# - 学习使用数据库——从 Web 到桌面应用程序

jquery - Visual Studio 2010 附带 jQuery 支持吗?

xml - Spring Security XML 错误 - cvc-complex-type.2.4.a : Invalid content was found starting with element 'csrf'

c++ - 如何使 QChar.unicode() 报告组合字符的 utf-16 表示形式?

c++ - 了解 Makefile 的依赖关系 (C++)

c++ - 使用信号和槽更新指针

java - 使用 XSD、目录解析器和用于 XSLT 的 JAXP DOM 验证 XML

java - 使用 JAXB 隐藏带有 nil ="true"的 XML 元素

java - 计算几何 : find where the triangle is after rotation, 在镜子上的平移或反射