Share via


C-C++ Code (inline.cpp)

 

The following is the C/C++ code.

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll>

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

void dump_com_error(_com_error &e)
{
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());

    printf("Error\n");
    printf("\a\tCode = %08lx\n", e.Error());
    printf("\a\tCode meaning = %s", e.ErrorMessage());
    printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
    printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}

_bstr_t validateFile(_bstr_t bstrFile)
{
    // Initialize objects and variables.
    HRESULT hr = S_OK;
    _bstr_t bstrResult = L"";
    MSXML2::IXMLDOMDocument2Ptr pXMLDoc;
    MSXML2::IXMLDOMParseErrorPtr  pError;

    CHK_HR(pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER));

    // Set properties on DOMDocument object.
    pXMLDoc->async = VARIANT_FALSE;
    pXMLDoc->validateOnParse = VARIANT_TRUE;
    pXMLDoc->resolveExternals = VARIANT_TRUE;
    CHK_HR(pXMLDoc->setProperty(L"UseInlineSchema", VARIANT_TRUE));

    // Load and validate the specified file into the DOM.
    // And return validation results in message to the user.
    if(pXMLDoc->load(bstrFile) != VARIANT_TRUE)
    {
        pError = pXMLDoc->parseError;

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

CleanUp:
    return bstrResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        try
        {
            _bstr_t bstrOutput = validateFile(L"valid.xml");
            bstrOutput += validateFile(L"notValid.xml");
            MessageBox(NULL, bstrOutput, L"Inline XSD Schema sample", MB_OK);
        }
        catch(_com_error &e)
        {
            dump_com_error(e);
        }
        CoUninitialize();
    }
    return 0;
}

Try It!

  1. Start Visual C++.

  2. From the File menu, select New. On the Projects tab of the New dialog box that appears, select Win32 Console Application in the left pane. Then type "inlineProj" in the Project name field. For the project Location field, either accept the default setting or choose another location. Click OK.

  3. The Win32 Console Application property page will appear. For the type of Console Application, select An empty project and click Finish. When the New Project Information box displays, click OK.

  4. Select FileView on the project browser, and highlight inlineProj files. From the File menu, select New.

  5. On the Files tab of the New dialog box, highlight C++ Source File. Then type "valid.xml" in the File name text box, and click OK.

  6. Copy the first XML/XSD data file (valid.xml), and paste it into the text file you just created.

  7. Repeat steps 5-6 for the second XML/XSD data file (notValid.xml).

    Note

    You can also copy the file into the project's main directory using Windows Explorer (or a command prompt).

  8. Repeat steps 5-6 for the C++ file above (inline.cpp).

  9. Build the sample by selecting Build inlineProj.exe from the Build menu.

  10. Execute the sample application by selecting Start Debugging from the Debug menu.

  11. Verify that your output is the same as that listed in the output for this example.