Microsoft KB Archive/309535

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

Article ID: 309535

Article Last Modified on 10/26/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q309535





IN THIS TASK

 * SUMMARY
 * Create the Sample XSD Schema Document
 * Create the Sample XML Document
 * Create the Visual Basic Application
 * Run the Visual Basic Application



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.

back to the top

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:   Use Notepad to create an XSD document named Books.xsd that contains the following code: 



    

   <xs:element name=&quot;State&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;genre&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;price&quot; type=&quot;xs:float&quot;/> <xs:element name=&quot;publish_date&quot; type=&quot;xs:date&quot;/> <xs:element name=&quot;description&quot; type=&quot;xs:string&quot;/> </xs:sequence>

<xs:attribute name=&quot;id&quot; type=&quot;xs:string&quot;/>

</xs:complexType>

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

back to the top

Create the Sample XML Document
<ol> <li> Use Notepad to create an XML document named Books.xml that contains the following code: <?xml version=&quot;1.0&quot;?> <x:catalog xmlns:x=&quot;urn:books&quot;> Adams <State>NC</State> XML Developer's Guide Computer No Price data available <publish_date>2000-10-01</publish_date> </x:catalog> </li> <li>Save Books.xml in the root folder of drive C.</li></ol>

back to the top

Create the Visual Basic Application
<ol> <li>In Visual Basic, create new Standard EXE project.</li> <li>Add a project reference to Microsoft XML version 4.0.</li> <li>Drag a command button onto Form1 and set its Name property to cmdValidate.</li> <li>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.</li> <li> 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 &quot;Error- validation error&quot;, 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 &quot;Fatal error or parsing error&quot;, 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 + &quot;: (&quot; + CStr(ErrCode) + &quot;) &quot; + _ strDescription & &quot;at &quot; + &quot;line &quot; + _ Str(Line) + &quot;, column &quot; + _ 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.

</li> <li> 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 &quot;urn:books&quot;, &quot;c:\books.xsd&quot; 'Configure the SAX reader to validate the XML document. oReader.putFeature &quot;schema-validation&quot;, True oReader.putFeature &quot;exhaustive-errors&quot;, True oReader.putProperty &quot;schemas&quot;, 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 &quot;c:\books.xml&quot; Debug.Print &quot;End of parsing and validation&quot; </li> <li>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.</li></ol>

back to the top

Run the Visual Basic Application
<ol> <li>Save and run the Visual Basic project.</li> <li>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.</li></ol>

back to the top

Keywords: kbhowto kbhowtomaster KB309535

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.