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!
- Copy the XML data (validateNode.xml), and paste it into a text file. Save the file as validateNode.xml.
- 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.
- 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.
- Create a reference to MSXML 5.0. To do this, select References... from the Project menu, then check the box for Microsoft XML, v50.
- Copy the Visual Basic code listing above, and paste it into the form_load subroutine.
- Execute the code by selecting Start from the Run menu.
- Verify that the output is the same as that listed in Output for the validateNode Example.