Visual Basic Code (enumErrors.frm)

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

Visual Basic Code (enumErrors.frm)

   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"
   
   ' Validate the 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:"
             
      ' Enumerate the error collection, invoking _newEnum implicitly.
      i = 0
      For Each e In errs
        msg = msg & vbCrLf & vbCrLf & "ErrorItem[" & i & "]" _
                  & vbCrLf & "    reason:" & vbCrLf & e.reason _
                  & vbCrLf & "    location:" & vbCrLf & e.errorXPath
        i = i + 1
      Next
   Else
      msg = msg & "DOM is valid:" & vbCrLf & xd.xml & vbCrLf
   End If

   MsgBox msg

Try It!

  1. Copy the XML data (books.xml), and paste it into a text file. Save the file as books.xml.
  2. Copy the XSD listing (books.xsd), and paste it into a text file. Save the file as books.xsd, in the same directory where you saved books.xml.
  3. Create a Standard EXE project in Visual Basic. Save the empty project as enumErrors.vbp to the same directory where you saved books.xml and books.xsd. Name the form file enumErrors.frm.
  4. Create a reference to MSXML 5.0. To do this, select References... from the Project menu, then check the box for Microsoft XML, v50.
  5. Copy the Visual Basic code above, and paste it into the form_load subroutine.
  6. Execute the code by selecting Start from the Run menu.
  7. Verify that your output is the same as that listed in Output for the enumErrors Example.