Visual Basic Code (next.frm)
Private Sub Form_Load() Dim er As IXMLDOMParseError2 Dim errs As IXMLDOMParseErrorCollection Dim xd As New DOMDocument50 xd.async = False xd.validateOnParse = False xd.setProperty "MultipleErrorMessages", True ' Load XML to xd xd.Load App.Path & "\books.xml" ' Validate the XML, relying on the xsi:schemaLocation attribute ' of the XML document element to resolve where to read the schema ' definition. Set er = xd.Validate If er.errorCode <> 0 Then Set errs = er.allErrors i = 0 msg = "" Set er = errs.Next While Not (er Is Nothing) msg = msg + "errorItem[" + CStr(i) + "]: " + er.reason msg = msg + vbNewLine Set er = errs.Next i = i + 1 Wend Else msg = "DOM is valid:" & vbNewLine & xd.xml End If MsgBox msg End Sub
Try It!
- Copy the XML data (books.xml), and paste it into a text file. Save the file as books.xml.
- 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.
- Create a Standard EXE project in Visual Basic. Save the empty project as next.vbp to the same directory where you saved books.xml and books.xsd. Name the form file next.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 your output is the same as that listed in Output for the next Example.