C-C++ Code (noNamespace.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.
    MSXML2::IXMLDOMDocument2Ptr pXMLDoc;
    MSXML2::IXMLDOMParseErrorPtr pError;
    _bstr_t bstrResult = L"";
    HRESULT hr = S_OK;

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

    pXMLDoc->async = VARIANT_FALSE;
    pXMLDoc->validateOnParse = VARIANT_TRUE;
    pXMLDoc->resolveExternals = 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"nn-valid.xml");
            bstrOutput += validateFile(L"nn-notValid.xml");
            MessageBoxW(NULL, bstrOutput, L"noNamespace",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 "noNamespaceProj" 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 noNamespaceProj files. From the File menu, select New.

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

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

  7. Repeat steps 5-6 for the second XML data file (nn-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 XSD schema file (nn.xsd).

  9. Repeat steps 5-6 for the C++ file above (noNamespace.cpp).

  10. Build the sample by selecting Build noNamespaceProj.exe from the Build menu.

  11. Execute the sample application by selecting !Execute validateNodeProj.exe from the Build menu.

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