Validate an XML Document Against an XML Schema Using Visual Basic

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Schemas

Validate an XML Document Against an XML Schema Using Visual Basic

You can use Visual Basic to validate an XML document against an XML Schema definition language (XSD) schema. To validate a document, add the schema to the XMLSchemaCache object, set the schemas property of a DOMDocument object to reference the schema in the XMLSchemaCache object, and then load the XML file you want to validate into the DOMDocument object. Validation is performed when the document is loaded into the DOMDocument object. Validation errors are handled using the parseError property of the DOMDocument object. This topic provides a quick example of how to validate a document against a schema and consists of the following.

books.xsd
The XML Schema that is used to validate the books.xml file.
books.xml
The file that is loaded and validated against the books.xsd schema.
Microsoft Visual Basic validation code
Creates an XMLSchemaCache object, adds the schema to it, and sets the schemas property of the DOMDocument object to reference the schema in the XMLSchemaCache object. In addition, the code loads the document into the DOMDocument object and returns any validation errors that occur.

To get started

  1. Open Visual Basic 6.0, and in the New Project dialog box, double-click Standard EXE.
  2. On the Project menu, click References.
  3. In the Available References list, select Microsoft XML,v5.0, and then click OK.
  4. Add a Command button to Form1.
  5. Save the project to a folder on your hard drive.

The XML Document (books.xml)

The books.xml file used in this example, shown below, is a modified version of the books.xml file used throughout the MSXML SDK. Notice that "urn:books" is declared as the default namespace for this document.

If you want to run the example in this topic, copy the following code to a text editor, such as Notepad, and then save the file as books.xml.

<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </book>
</x:catalog>

The XML Schema (books.xsd)

The following document shows the books.xsd schema used in this example.

If you want to run the example in this topic, copy the following code to a text editor, such as Notepad, and then save the file as books.xsd.

<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="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>

Visual Basic Validation Code

The following code snippet shows how you can perform validation on the books.xml document. This code creates an XMLSchemaCache50 object, adds the namespace URI declaration ("urn:books") and the schema (books.xsd) to the object, and then references the schema using the schemas property of the DOMDocument object. Validation is performed when the books.xml file is loaded into the DOMDocument object. Validation errors are returned using the parseError property of the DOMDocument object.

To run the example

  1. Copy the code shown below to the Command1_Click procedure.
    Private Sub Command1_Click()
    
      'Create a schema cache and add books.xsd to it.
      Dim xmlschema As MSXML2.XMLSchemaCache50
      Set xmlschema = New MSXML2.XMLSchemaCache50
      xmlschema.Add "urn:books", App.Path & "\books.xsd"
    
      'Create an XML DOMDocument object.
      Dim xmldom As MSXML2.DOMDocument50
      Set xmldom = New MSXML2.DOMDocument50
    
      'Assign the schema cache to the DOM document.
      'schemas collection.
      Set xmldom.schemas = xmlschema
    
      'Load books.xml as the DOM document.
      xmldom.async = False
      xmldom.Load App.Path & "\books.xml"
    
      'Return validation results in message to the user.
      If xmldom.parseError.errorCode <> 0 Then
         MsgBox xmldom.parseError.errorCode & " " & _
         xmldom.parseError.reason
      Else
         MsgBox "No Error"
      End If
    
    End Sub
  2. On the Visual Basic toolbar, click Start, and then click the Command1 button on form1.

    When you run the example, it returns a "No Errors" message box.

Testing the Validation Code

To generate an error message for the validation code, change the books.xml document so the title element appears directly after the catalog element, as shown in bold in the following example. This makes the document invalid according to the books.xsd schema. Save the books.xml file and then run the Visual Basic code. The code returns the following error message:

"1072898028 Element content is invalid according to DTD/Schema. Expecting: book."

<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
 <title>2000-10-01</title>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with 
      XML.</description>
   </book>
</x:catalog>

See Also

XML Schema Examples | XML Schema Element Map | XML Schema Elements | XML Data Types Reference | Primitive XML Data Types | Derived XML Data Types | Data Type Facets | IXMLDOMSchemaCollection/XMLSchemaCache

Other Resources Other Resources

World Wide Web Consortium (W3C) XML Schema Part 2: Datatypes | World Wide Web Consortium (W3C) XML Schema Part 1: Structures