' Check "Microsoft XML, v50" from Project->References...
' in Visual Studio.
Dim er, e As IXMLDOMParseError2
Dim errs As IXMLDOMParseErrorCollection
Dim xd As New DOMDocument50
Dim xs As New XMLSchemaCache50
' Add xs to xd.
xs.Add "urn:books", App.Path & "\books.xsd"
Set xd.schemas = xs
xd.async = False
xd.validateOnParse = False
xd.setProperty "MultipleErrorMessages", True
' Load the XML file to xd.
xd.Load App.Path & "\books.xml"
Set er = xd.Validate
If er.errorCode <> 0 Then
Set errs = er.allErrors
errsCount = errs.length
msg = "Error as returned from validate():" & vbCrLf _
& vbCrLf & " Error Code:" & vbCrLf & er.errorCode _
& vbCrLf & " Error reason:" & vbCrLf & er.reason _
& vbCrLf & " Error location:" & vbCrLf & er.errorXPath _
& vbCrLf & " Error Count:" & vbCrLf & errsCount & vbCrLf _
& vbCrLf & "Error items from the allErrors collection:"
For i = 0 To errsCount - 1
Set e = errs.Item(i)
msg = msg & vbCrLf & vbCrLf & "ErrorItem[" & i & "]" _
& vbCrLf & " reason:" & vbCrLf & e.reason _
& vbCrLf & " localtion:" & vbCrLf & e.errorXPath
Next
Else
msg = msg & "DOM is valid:" & vbCrLf & xd.xml & vbCrLf
End If
MsgBox msg
Try It!