Article ID: 317595 - Last Review: November 26, 2007 - Revision: 3.5

HOW TO: Validate an XML Document by Using Multiple Schemas in Visual Basic .NET

This article was previously published under Q317595

On This Page

Expand all | Collapse all

SUMMARY

This step-by-step article describes how to use the XmlValidatingReader object to validate an Extensible Markup Language (XML) file with multiple XML Schema Definition Language (XSD) schemas. The code sample uses the XmlSchemaCollection object to cache the schemas. For more information about the XmlValidatingReader and the XmlSchemaCollection classes, see the REFERENCES section.

If the namespace is already associated with another schema in the collection, the schema that you add replaces the original schema in the collection. For example, the following code removes the Authors.xsd file from the collection and adds the Names.xsd file to the collection:
schemaColl.Add("urn:author", "authors.xsd");
schemaColl.Add("urn:author", "names.xsd");
				
The XML file is always validated against the last schema.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you are required:
  • Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
This article assumes that you are familiar with the following topics:
  • Microsoft Visual Basic .NET
  • XML standards
  • XSD schemas

Create the Book.xsd File

  1. Open a text editor such as Notepad.
  2. Copy and paste the following code into Notepad:
    <xs:schema xmlns="urn:bookstore-schema"
             targetNamespace="urn:bookstore-schema"
             xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="book">
          <xs:complexType>
             <xs:simpleContent>
                <xs:extension base="xs:string">
                   <xs:attribute name="price" type="xs:decimal" />
                </xs:extension>
             </xs:simpleContent>
          </xs:complexType>
       </xs:element>
    </xs:schema>
    					
  3. Save the file as C:\Book.xsd.

Create the Tape.xsd File

  1. Open a text editor such as Notepad.
  2. Copy and paste the following code into Notepad:
    <xs:schema xmlns="urn:tapestore-schema"
             targetNamespace="urn:tapestore-schema"
             xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="tape" type="xs:string"/>
    </xs:schema>
    					
  3. Save the file as C:\Tape.xsd.

Create the Mixed.xml File

  1. Open a text editor such as Notepad.
  2. Copy and paste the following code into Notepad:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:dvdstore-schema" targetNamespace="urn:dvdstore-schema">
        <xs:element name="dvd" type="xs:string" />
    </xs:schema>
      <pb:book price="7.99" xmlns:pb="urn:bookstore-schema">The Autobiography of Benjamin Franklin</pb:book>
      <pd:dvd xmlns:pd="urn:dvdstore-schema">The Godfather</pd:dvd>
      <pt:tape xmlns:pt="urn:tapestore-schema" xsi:schemaLocation="urn:tapestore-schema tape.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Il Postino</pt:tape>
    					
  3. Save the file as C:\Mixed.xml.
NOTE: The XML file also has an inline schema; therefore, this XML file must validate against three schemas.

Create a Visual Basic .NET Project

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, click Windows Application under Templates, and then click OK. Form1 is added to the project by default.
  4. Drag a Button control to Form1.
  5. Add the following namespaces to the top of the form:
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.IO
    					
  6. Add the following sub procedure to create a ValidationEventHandler delegate that raises validation errors in the XmlValidatingReader object:
    Public Sub MyvalidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
           Console.WriteLine(args.Message)
        End Sub
    					
  7. Paste the following code in the Button1_Click event procedure:
     Dim myschemacoll As New XmlSchemaCollection()
            Dim vr As XmlValidatingReader
            Dim stream As FileStream
            Try
                stream = New FileStream("c:\Mixed.xml", FileMode.Open)<BR/>
                'Load the XmlValidatingReader.
                vr = New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
    
                'Add the schemas to the XmlSchemaCollection object.
                myschemacoll.Add("urn:bookstore-schema", "c:\Book.xsd")
                myschemacoll.Add("urn:tapestore-schema", "c:\tape.xsd")
                vr.Schemas.Add(myschemacoll)
                vr.ValidationType = ValidationType.Schema
                AddHandler vr.ValidationEventHandler, AddressOf MyvalidationEventHandle
    
                While vr.Read()
                End While
                Console.WriteLine("Validation completed")
            'This code catches any XML exceptions. 
            Catch XmlExp As XmlException
                Console.WriteLine(XmlExp.Message)
            'This code catches any XML schema exceptions.
            Catch XmlSchemaExp As XmlSchemaException
                Console.WriteLine(XmlSchemaExp.Message)
            'This code catches any standard exceptions.
            Catch GeneralExp As Exception
                Console.WriteLine(GeneralExp.Message)
            Finally
            'Clean up.
                vr = Nothing
                myschemacoll = Nothing
                stream = Nothing
            End Try
    					
  8. When the following message appears in the output window, the XML document is valid against all three of the schemas that it references:
    Validation completed
    						

REFERENCES

For more information, see the following Microsoft .NET Framework Class Library documentation:
XmlValidatingReader Class
http://msdn2.microsoft.com/en-us/library/system.xml.xmlvalidatingreader(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.xml.xmlvalidatingreader(vs.71).aspx)

XmlSchemaCollection Class
http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemacollection(vs.71).aspx (http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemacollection(vs.71).aspx)

System.Xml Namespace
http://msdn.microsoft.com/en-us/library/system.xml(VS.71).aspx (http://msdn.microsoft.com/en-us/library/system.xml(VS.71).aspx)
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
313826  (http://support.microsoft.com/kb/313826/EN-US/ ) INFO: Roadmap for XML Schemas in the .NET Framework
313651  (http://support.microsoft.com/kb/313651/EN-US/ ) INFO: Roadmap for XML in the .NET Framework

APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
Keywords: 
kbhowtomaster KB317595
 

Article Translations