Visual Basic Code (errorParams.frm)

MSXML 5.0 SDK

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

Visual Basic Code (errorParams.frm)

Private Sub Form_Resize()
    ' Resize the text box to the size of the form
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750
End Sub

Private Sub Form_Load()
    ' Resize the text box to the size of the form
    Text1.Top = 100
    Text1.Left = 100
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750

   ' Check "Microsoft XML, v50" from Project->References... 
   ' in Visual Studio.
   Dim er, e As IXMLDOMParseError2
   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
   
   msg = "Validating DOM..." & vbCrLf
   
   ' Load XML to xd.
   xd.Load App.Path & "\books.xml"
   Set er = xd.Validate
   If er.errorCode <> 0 Then
      msg = msg & "invalid dom:" & vbCrLf
      msg = msg & "    reason:" & vbCrLf & er.reason
      msg = msg & "    errorXPath:" & vbCrLf & er.errorXPath & vbCrLf
      msg = msg & "parameters count: " & er.errorParametersCount
      msg = msg & vbCrLf
      For i = 0 To er.errorParametersCount - 1
         msg = msg & "     errorParameters(" & i & "): "
         msg = msg & er.errorParameters(i)
         msg = msg & vbCrLf
      Next
   Else
      msg = msg & "    DOM is valid:" & vbCrLf
      msg = msg & xd.xml
   End If
   
   msg = msg & vbCrLf
   msg = msg & "Validating nodes..." & vbCrLf
   Set pNodes = xd.selectNodes("//book")
   For Each pNode In pNodes
      Set er = xd.validateNode(pNode)
      If er.errorCode <> 0 Then
         msg = msg & vbCrLf
         msg = msg & "Node is invalid:" & vbCrLf
         msg = msg & "  reason:" & vbCrLf & er.reason
         msg = msg & "  errorXPath:" & vbCrLf & er.errorXPath
         msg = msg & vbCrLf
         msg = msg & "Parameters Count: " & er.errorParametersCount
         msg = msg & vbCrLf
         
         For i = 0 To er.errorParametersCount - 1
            msg = msg & "   errorParameter(" & i & "): "
            msg = msg & er.errorParameters(i)
            msg = msg & vbCrLf
         Next
      Else
         msg = msg & vbCrLf
         msg = msg & "Node is valid: " & vbCrLf
         msg = msg & pNode.xml & vbCrLf
      End If
   Next

   Text1 = msg
End Sub

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 errorParams.vbp to the same directory where you saved books.xml and books.xsd. Name the form file errorParams.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, v5.0.
  5. Double click on the TextBox icon from the tools menu. A TextBox control will appear on the project's form named "Text1". Select the Text1 control and and in the Properties window, modify the following settings: select "2 - Vertical" as the value for the ScrollBars property and set the value of the MultiLine property to True.
  6. Copy the Visual Basic code listing above, and paste it into the Visual Basic code editor to replace whatever code is already there.
  7. Execute the code by selecting Start from the Run menu.
  8. Verify that your output is the same as that listed in Output for the errorParams Example.