Library tutorials & articles

XSD Schemas for VB Developers

Validating with Schema

Before we dive into the details of creating XSD schemas, lets understand how to use them for validating XML documents. You’ll need to get a copy of MSXML 4.0 and install it. Assuming you already have the schema and the XML document, all you need now is a few lines of code to validate. I wrote a simple app that validates and XML document using an XSD schema just to show you how to write the code. You can download it here.

You’ll first instantiate an XMLSchema object and add to it the XSD schema file that you already have.

Dim schemaCache As MSXML2.XMLSchemaCache40
Set schemaCache = New MSXML2.XMLSchemaCache40
schemaCache.Add "","D:\schemas\mySchema.xsd"

When you call the Add method, the first parameter is the namespace that your XML document uses. If your XML document does not use a namespace, you can just pass an empty string like you see here (To learn more about XML namespaces, see the XML Namespaces tutorial.). The second parameter is the URL or file path pointing to the XSD schema document. Next, you create a DOM document and add to it the schemaCache:

Dim doc As MSXML2.DOMDocument40
Set doc = New MSXML2.DOMDocument40
Set doc.schemas = schemaCache

Now you are ready to load the XML document and do the validation:

doc.async = False

If Not doc.Load("D:\docs\myDoc.xml") Then
    MsgBox "Error loading XML document: " & doc.parseError.reason
End If

After loading the document, you must check the parseError property for validation errors. As you can see, validating is quite easy, once you have the XSD schema. Now I’ll show you the basics for creating XSD schemas.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of XSD Schemas for VB Developers.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • Java Posse #202 - Newscast for Aug 22nd 2008

    Newcast for August 22nd, 2008Fully formatted shownotes can always be found at http://javaposse.com Sadly, Java does not run on the Mars LanderThe Golden Gate Project http://research.sun.com/projects/goldengate/Space surveillance radar http://www.sun.com/aboutsun/pr/2008-04/sunflash Google has r...

Related jobs