Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Identify Multiple Validation Errors When Using the MSXML 4.0 SAX Parser in Visual Basic


Summary

This step-by-step article demonstrates how to identify and list multiple validation errors when you use the MSXML 4.0 SAX parser in a Microsoft Visual Basic application to parse an XML document.


Validation errors in a well-formed XML document indicate that its contents do not conform to the structure that is defined in its associated Document Type Definition (DTD) or schema. The MSXML 4.0 Simple API for XML (SAX) parser has the ability to identify all of the validation errors that it encounters while it parses an XML document, instead of stopping the parsing when the first validation error is encountered. This feature of the MSXML 4.0 SAX parser can be activated by using its put-feature method to set its schema-validation and exhaustive-errors properties to True.

Create the Sample XSD Schema Document

Create an XML Schema Definition (XSD) schema that you can use to define the structure of an XML document that is used to store data about a book catalog. To do this, follow these steps:
  1. Use Notepad to create an XSD document named Books.xsd that contains the following code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:b="urn:books">

    <xs:element name="catalog" type="b:CatalogData"/>

    <xs:complexType name="CatalogData">
    <xs:sequence>
    <xs:element name="book" type="b:bookdata" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>


    <xs:complexType name="bookdata">
    <xs:sequence>
    <xs:element name="author" type="xs:string"/>
    <xs:element name="State" type="xs:string"/>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="genre" type="xs:string"/>
    <xs:element name="price" type="xs:float"/>
    <xs:element name="publish_date" type="xs:date"/>
    <xs:element name="description" type="xs:string"/>
    </xs:sequence>

    <xs:attribute name="id" type="xs:string"/>

    </xs:complexType>

    </xs:schema>
  2. Save Books.xsd in the root folder of drive C.

Create the Sample XML Document

  1. Use Notepad to create an XML document named Books.xml that contains the following code:
    <?xml version="1.0"?>
    <x:catalog xmlns:x="urn:books">
    <book>
    <author>Adams</author>
    <State>NC</State>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>No Price data available</price>
    <publish_date>2000-10-01</publish_date>
    </book>
    </x:catalog>
  2. Save Books.xml in the root folder of drive C.

Create the Visual Basic Application

  1. In Visual Basic, create new Standard EXE project.
  2. Add a project reference to Microsoft XML version 4.0.
  3. Drag a command button onto Form1 and set its Name property to cmdValidate.
  4. Add a class module to the project and set its Name property to MyValidator.

    This class module implements the IVBSAXErrorHandler interface, which defines the methods that are invoked when errors and warnings are encountered as the SAX parser parses an XML document. An object instance of MyValidator is used as the errorHandler interface of the SAXXMLReader object that is used to parse Books.xml in this sample.
  5. Paste the following code in the MyValidator class module:
    Option Explicit

    Implements IVBSAXErrorHandler

    Private Sub IVBSAXErrorHandler_error(ByVal oLocator As MSXML2.IVBSAXLocator, _
    strErrorMessage As String, ByVal nErrorCode As Long)
    WriteErrorToDebugWindow "Error- validation error", strErrorMessage, _
    nErrorCode, oLocator.lineNumber, oLocator.columnNumber
    End Sub

    Private Sub IVBSAXErrorHandler_fatalError(ByVal oLocator As MSXML2.IVBSAXLocator, _
    strErrorMessage As String, ByVal nErrorCode As Long)

    WriteErrorToDebugWindow "Fatal error or parsing error", strErrorMessage, _
    nErrorCode, oLocator.lineNumber, oLocator.columnNumber
    End Sub

    Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal oLocator As MSXML2.IVBSAXLocator, _
    strErrorMessage As String, ByVal nErrorCode As Long)
    End Sub


    Private Sub WriteErrorToDebugWindow(strLabel As String, _
    strDescription As String, ByVal ErrCode As Long, _
    Line As Long, Column As Long)
    Debug.Print strLabel + ": (" + CStr(ErrCode) + ") " + _
    strDescription & "at " + "line " + _
    Str(Line) + ", column " + _
    Str(Column) + vbCrLf
    End Sub
    This code in the MyValidator class module implements the methods that are defined in the IVBSAXErrorHandler interface. The code in the error and fatalError methods calls the custom WriteErrorToDebugWindow subprocedure to write out information about the errors that are encountered while parsing the XML document to the Visual Basic Immediate window.

  6. Paste the following code in the Click event procedure of the cmdValidate command button to parse the Books.xml sample XML document by using the MSXML 4.0 SAXXMLReader object:
      'Create a SAX reader.
    Dim oReader As New MSXML2.SAXXMLReader40
    'Create an XML schema cache.
    Dim oSC As New MSXML2.XMLSchemaCache40

    'Create an instance of the class module.
    'that implements the IVBSAXErrorHandler interface
    Dim oValidator As New MyValidator

    'Add the schema file to the schema cache.
    oSC.Add "urn:books", "c:\books.xsd"

    'Configure the SAX reader to validate the XML document.
    oReader.putFeature "schema-validation", True
    oReader.putFeature "exhaustive-errors", True
    oReader.putProperty "schemas", oSC

    'Assign an instance of the MyValidator class to
    'the errorHandler property of the SAX reader.
    Set oReader.errorHandler = oValidator

    'Parse and validate the file.
    oReader.parseURL "c:\books.xml"
    Debug.Print "End of parsing and validation"

  7. Review the code that is used to configure the SAX reader to validate the XML document. The schema-validation and exhaustive-errors properties of the object are set to True to instruct the parser to validate the XML document against the specified XSD schema, and to continue parsing even after the first error is encountered so that the code in the associated errorHandler interface can identify and report all of the validation errors. The XMLSchemaCache object to which Books.xsd is added is assigned to the schemas property of the SAX reader object to specify the schema document against which the XML is to be validated.

Run the Visual Basic Application

  1. Save and run the Visual Basic project.
  2. Click the cmdValidate command button to parse and validate Books.xml by using the MSXML 4.0 SAXXMLReader object. The following validation errors are listed in the Visual Basic Immediate window:

    Error- validation error: (-2147467259) The element: 'price' has an invalid value according to its data type.
    at line 8, column 44


    Error- validation error: (-2147467259) Element content is incomplete according to the DTD/Schema.
    Expecting: description.
    at line 10, column 10
    The data type of the price element is defined as a float in the XSD schema. The data in the price element in Books.xml does not contain a float value. The XSD schema also specifies that the book element should contain a required description subelement that should follow the publish_date subelement. The book element in Books.xml does not contain a description subelement. These errors are identified and listed in the Visual Basic Immediate window.

↑ Back to the top


Keywords: kbdsupport, kbhowto, kbhowtomaster, kb

↑ Back to the top

Article Info
Article ID : 309535
Revision : 1
Created on : 1/7/2017
Published on : 6/22/2014
Exists online : False
Views : 355