tags:

views:

547

answers:

1

I want to validate an XML file against an XML Schema file.

It is a simple xml file. does not include namespace etc.

I want to do this in c++, using MSXML 6.0.

Can anyone provide me the guidance to do this

Thanks

+2  A: 

You can validate as you load. This is sample code from the Windows/MSXML SDK:

   IXMLDOMSchemaCollectionPtr   pXS;
   IXMLDOMDocument2Ptr          pXD = NULL;
   IXMLDOMParseErrorPtr         pErr = NULL;
   _bstr_t                      strResult = "";

   HRESULT hr = pXS.CreateInstance(__uuidof(XMLSchemaCache50));
   hr = pXS->add("urn:namespace", "myschema.xsd");

   // Create a DOMDocument and set its properties.
   hr = pXD.CreateInstance(__uuidof(DOMDocument50));

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

   // Load books.xml as the DOM document.
   pXD->async = VARIANT_FALSE;
   pXD->validateOnParse = VARIANT_TRUE;
   pXD->resolveExternals = VARIANT_TRUE;
   hr = pXD->load("TheXmlDocument.xml");

   // check hr and pXD->errorCode here

You can download the MSXML6 SDK to get this sample and lots of others. Note: It will not install on Vista. If you run Vista, then get the Windows SDK.

Cheeso