Visual Basic Code (noNamespace.frm)

 

This topic provides Visual Basic code.

Try It!

  1. Copy the first XML data file from the resource files, and paste it into a text file. Save the file as nn-valid.xml.

  2. Copy the second XML data file from the resource files, and paste it into a text file. Save the file as nn-notValid.xml, in the same directory where you saved nn-valid.xml.

  3. Copy the XSD schema file from the resource files, and paste it into a text file. Save the file as nn.xsd, in the same directory you used in previous steps.

  4. Create a Standard EXE project in Visual Basic. Save the empty project as noNamespace.vbp to the same directory you used in previous steps. Name and save the form file as noNamespace.frm.

  5. Create a reference to MSXML 6.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v6.0.

  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 the output for this example.

noNamespace.frm

Private Sub Form_Load()
    Dim sOutput As String
    sOutput = ValidateFile("nn-valid.xml")
    sOutput = sOutput & ValidateFile("nn-notValid.xml")
    MsgBox sOutput
    Unload Me
End Sub

Function ValidateFile(strFile As String)
    'Create an XML DOMDocument object.
    Dim x As New DOMDocument60
    'Load and validate the specified file into the DOM.
    x.async = False
    x.validateOnParse = True
    x.resolveExternals = True
    x.Load App.Path & "\" & strFile
    'Return validation results in message to the user.
    If x.parseError.errorCode <> 0 Then
        ValidateFile = "Validation failed on " & _
                       strFile & vbCrLf & _
                       "=====================" & vbCrLf & _
                       "Reason: " & x.parseError.reason & _
                       vbCrLf & "Source: " & _
                       x.parseError.srcText & _
                       vbCrLf & "Line: " & _
                       x.parseError.Line & vbCrLf
    Else
        ValidateFile = "Validation succeeded for " & _
                       strFile & vbCrLf & _
                       "======================" & _
                       vbCrLf & x.xml & vbCrLf
    End If
End Function