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

Visual Basic Code (validateNode.frm)

Dim xd As New MSXML2.DOMDocument50
Dim xs As New MSXML2.XMLSchemaCache50
Dim er As IXMLDOMParseError
Dim nlist As IXMLDOMNodeList
Dim node As IXMLDOMNode

  xs.Add "urn:books", App.Path & "\validateNode.xsd"
  Set xd.schemas = xs
  xd.validateOnParse = False
  xd.async = False
  xd.Load App.Path & "\validateNode.xml"
  
  Set er = xd.validate
  If er.errorCode <> 0 Then
    MsgBox "invalid dom: " & vbCrLf & er.reason
  Else
    MsgBox "dom is valid: " & vbCrLf & xd.xml
  End If
  
  Set nlist = xd.selectNodes("//book")
  i = 0
  For Each node In nlist
    Set er = xd.validateNode(node)
    If er.errorCode <> 0 Then
      MsgBox "invalid node: " & vbCrLf & er.reason
    Else
      MsgBox node.xml
    End If
  Next

Try It!

  1. Copy the XML data (validateNode.xml), and paste it into a text file. Save the file as validateNode.xml.
  2. Copy the XSD listing (validateNode.xsd), and paste it into a text file. Save the file as validateNode.xsd, in the same directory where you saved validateNode.xml.
  3. Create a Standard EXE project in Visual Basic. Save the empty project as validateNode.vbp to the same directory where you saved validateNode.xml and validateNode.xsd. Name the form file validateNode.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 listing above, and paste it into the form_load subroutine.
  6. Execute the code by selecting Start from the Run menu.
  7. Verify that the output is the same as that listed in Output for the validateNode Example.