C-C++ Code (next.cpp)


#include <stdio.h>
#import <msxml6.dll>
using namespace MSXML2;

int main(int argc, char* argv[])
    HRESULT hr;


    IXMLDOMDocument3Ptr pXMLDoc;
    hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
    if (FAILED(hr)) 
        printf("Failed to CreateInstance on an XML DOM");
        return -1;
    pXMLDoc->async = VARIANT_FALSE; 
    pXMLDoc->validateOnParse = VARIANT_FALSE;    
    pXMLDoc->resolveExternals = VARIANT_TRUE;
    hr = pXMLDoc->setProperty("MultipleErrorMessages", VARIANT_TRUE);
    if (FAILED(hr))
        printf("Failed to enable mulitple validation errors\n");
        return -1;
        printf("Failed to load DOM from books.xml. %s\n",
        return -1;

    // Validate the document as a whole, relying on the 
    // xsi:schemalocation attribute to resolve where to 
    // to read the schema definition.
    IXMLDOMParseError2Ptr pError =pXMLDoc->validate();
    if (pError->errorCode != 0) {
        IXMLDOMParseErrorCollectionPtr pErrSet;
        pErrSet = pError->allErrors;
        IXMLDOMParseError2Ptr pErr=NULL;
        int i=0;
        while (NULL != (pErr = pErrSet->next)) 
            printf("errorItem[%d]: %s\n", i++, (LPCSTR)pErr->reason);
        printf("DOM is valid:\n%s\n",(LPCSTR)pXMLDoc->xml);

    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 "nextProj" 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 nextProj files. From the File menu, select New.

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

  6. Copy the XML data file (books.xml), and paste it into the text file you just created.

  7. Repeat steps 4-7 for the XSD listing (Resource Files (books.xml and books.xsd)).

    System_CAPS_ICON_note.jpg Note

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

  8. Repeat steps 4-7 for the C++ listing above (next.cpp).

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

  10. Execute the sample application by selecting !Execute nextProj.exe from the Build menu.

  11. Verify that your output is the same as that listed in Output for the next Example.