HOW TO: Use the MSXML 4.0 SOM in a Visual Basic Application to Locate Element Declarations in an XSD Schema (309616)



The information in this article applies to:

  • Microsoft XML 4.0

This article was previously published under Q309616

SUMMARY

The Schema Object Model (SOM) that is implemented in MSXML version 4.0 can be used to programmatically locate schema information that pertains to XML element declarations in an XSD schema document. This step-by-step article describes how to program the MSXML 4.0 SOM in a Visual Basic application to locate the schema declaration and display the corresponding type information of a specified element.

Back to the top

Create the Sample XSD Schema Document

To create an XSD schema to define the structure of an XML document that is used to store data about a book catalog, follow these steps:
  1. Use Notepad to create an XSD document named Books.xsd that contains the following code:
    <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="title" type="xs:string"/>
          <xs:element name="price" type="xs:float"/>
          <xs:element name="publish_date" type="xs:date"/>           
        </xs:sequence>
    
        <xs:attribute name="id" type="xs:string"/>
    
      </xs:complexType>
    
    </xs:schema> 
    					
  2. Save Books.xsd in the root folder of drive C.
Back to the top

Create the Sample XML Document

  1. Use Notepad to create an XML document named Books.xml that contains the following code:
    <?xml version="1.0"?>
    <x:catalog xmlns:x="urn:books">      
       <book>         
          <title>XML Step by Step</title>                          
          <price>50.00</price>
          <publish_date>2000-10-01</publish_date>            
       </book>
    </x:catalog>
  2. Save Books.xml in the root folder of drive C.
Back to the top

Create the Visual Basic Application

  1. In Visual Basic, create a new Standard EXE project. Form1 is created by default.
  2. Add a project reference to Microsoft XML version 4.0.
  3. Drag 2 text boxes and a command button onto Form1.
  4. Set the Name property of one of the text boxes to txtXPath, set the Name property of the other text box to txtResults, and set the Name property of the command button to cmdGetTypeInfo. The txtXPath text box specifies an XPath expression that is run to locate the element node in the XML document whose schema declaration information is to be extracted from the XSD schema. The txtResults text box displays the type information of the specified element as extracted from the schema.

  5. Set the MultiLine and Scrollbars properties of the txtResults text box to True and Both. Resize txtResults so that it can display up to 10 lines of text with approximately 30 columns in each line of text.
  6. Paste the following code in the Click event procedure of the cmdGetTypeInfo command button. See the inline comments for a description of how the code functions:
      On Error Resume Next
      
      'Declare a Schema cache object to cache Books.xsd.
      Dim sc As New MSXML2.XMLSchemaCache40
      
      'Declare a DOMDocument object to load Books.xml.
      Dim doc As New MSXML2.DOMDocument40
        
      'Add books.xsd to the schema cache.
      sc.Add "urn:books", "c:\books.xsd"
      
      'Configure the DOMDocument object to load and validate
      'Books.xml against Books.xsd.
      
      Set doc.schemas = sc
      doc.async = False
      doc.setProperty "SelectionNamespaces", "xmlns:x='urn:books'"
      doc.Load "c:\books.xml"
      
        
      'Call the selectSingleNode method of the DOM object to
      'locate the first node in Books.xml that matches the XPath
      'expression that is specified in the txtXPath text box.
      Dim node As MSXML2.IXMLDOMNode
      Set node = doc.selectSingleNode(txtXPath.Text)
      
      
      'Check to verify that the specified XPath expression
      'did not generate an error.
      If Err.Number <> 0 Then
        MsgBox "Invalid XPath expression", , "Error"
        Exit Sub
      End If
        
      
      'Declare and use a SchemaCache object to hold a reference
      'to the list of namespaces that are referenced by the DOMDocument.
      Dim docNamespaces As New MSXML2.XMLSchemaCache40
      Set docNamespaces = doc.namespaces
      
      'Run the getDeclaration method of the SchemaCache object
      'to obtain the schema declaration of the node that is selected by running
      'the specified XPath expression. Assign the return value to an ISchemaElement object.
      
      Dim oDecl As MSXML2.ISchemaElement
      Set oDecl = docNamespaces.getDeclaration(node)
      
      'Blank out the txtResults text box. This text box is
      'used to display the type information for the element that is
      'referenced by the node that is returned on running the specified
      'XPath expression.
      
      txtResults.Text = ""
      
      
      'Check the type property of the ISchema element that is returned by
      'running getDeclaration to determine whether it is a
      'ComplexType or a simpleType element.
      
      If TypeName(oDecl.Type) = "ISchemaComplexType" Then
       
           'If the element is a complexType element, obtain a
           'reference to its SchemaModelGroup by using its contentModel property.
           
           Dim otype As MSXML2.ISchemaComplexType
           Set otype = oDecl.Type
        
           Dim scModelGroup As MSXML2.ISchemaModelGroup
           Set scModelGroup = otype.contentModel
           
           
           'Display the complexType name of the element.
           txtResults.Text = "Type of " & txtXPath.Text & " is " & oDecl.Type.Name & vbCrLf & vbCrLf
                  
           'Loop through the particles of the element's SchemaModelGroup
           'to display the names and type names of its subelements.
           
           txtResults.Text = txtResults.Text & "Sub Elements of " & txtXPath.Text & vbCrLf
           Dim subElem As MSXML2.ISchemaElement
           For Each subElem In scModelGroup.particles
              Set stype = subElem.Type
              txtResults.Text = txtResults.Text & vbTab & subElem.Name & " : " & subElem.Type.Name & vbCrLf
           Next
           
           'Loop through the attributes of the element (if any) and
           'display the names and type information of each attribute.
           
           txtResults.Text = txtResults.Text & vbCrLf & "Attributes of " & txtXPath.Text & vbCrLf
           Dim att As MSXML2.ISchemaAttribute
           For Each att In otype.Attributes
             txtResults.Text = txtResults.Text & vbTab & att.Name & " : " & att.Type.Name & vbCrLf
           Next
           
           
      End If
      
      'If the element is a simpleType element, only display
      'its name and type information.
      
      If TypeName(oDecl.Type) = "ISchemaType" Then
          txtResults.Text = "Data type of " & txtXPath.Text & " is " & oDecl.Type.Name
      End If
Back to the top

Run the Visual Basic Application

  1. Save and run the Visual Basic project.
  2. To extract and display the schema declaration information for the book complexType element, type the following XPath expression in the txtXPath text box and click the cmdGetTypeInfo command button:
    /x:catalog/book
    The following output is displayed in the txtResults text box. The output displays the data type of the book element (custom type bookdata), the names and data types of its subelements (title, price, publish_date), and the name and data type of its single attribute (id):
    Type of /x:catalog/book is bookdata
    
    Sub Elements of /x:catalog/book
    	title : string
    	price : float
    	publish_date : date
    
    Attributes of /x:catalog/book
    	id : string
  3. To extract and display the schema declaration information for the price simpleType element, specify the following XPath expression in the txtXPath text box and click the cmdGetTypeInfo command button:
    /x:catalog/book/price
    The following output is displayed in the txtResults text box, indicating the data type of the price simpleType element:
    Data type of /x:catalog/book/price is float
Back to the top

REFERENCES

For additional information about how to validate an XML document with Visual Basic .NET, click the following article number to view the article in the Microsoft Knowledge Base:

315533 HOW TO: Validate an XML Document by Using DTD, XDR, or XSD in Visual Basic .NET

For comprehensive samples that demonstrate how to program the MSXML SOM to implement common application requirements, see the SOM Developer's Guide in the MSXML 4.0 Software Development Kit (SDK) documentation.

Modification Type:MinorLast Reviewed:4/24/2003
Keywords:kbHOWTOmaster KB309616 kbAudDeveloper