Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 56,104 times

Contents

Related Categories

XSD Schemas for VB Developers - Validating with Schema

shohoudy

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.

Yasser started programming at the age of 12 when he wrote his first text-based game on a Commodore PET. He's since moved to IBM mainframes then to Microsoft technologies and has worked as Systems Engineer for IBM and a Chief Architect for Best Software (now Sage Software). Now an independent consultant and trainer, Yasser specializes in XML and Web services and has authored a book on Web Services for Addison Wesley. A Microsoft Most Valuable Professional (MVP) for ASP.NET, Yasser speaks at industry conferences including VS Live and VS Connections and his articles appear regularly in VSM, XML Magazine, and MSDN Magazine. Yasser also publishes the monthly .NET Web Services newsletter.

Comments