HOW TO: Modify an XML Schema by Using System.Xml.Schema Classes in Visual Basic .NET (317903)



The information in this article applies to:

  • Microsoft XML Classes (included with the .NET Framework 1.0)
  • Microsoft XML Classes (included with the .NET Framework 1.1)

This article was previously published under Q317903

SUMMARY

This step-by-step article shows how to modify an existing XML schema.

The System.Xml.Schema namespace contains the XML classes that provide standards-based support for XML schema definition language (XSD) schemas.

The XmlSchema class contains the definition of a schema. All XSD elements are children of the schema element; this represents the World Wide Web Consortium (W3C) schema element. You can use these classes to generate new XML schemas or modify existing ones.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that 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:
  • Visual Basic .NET language
  • XML standards
  • XSD schemas
back to the top

Create an XML Schema File

  1. Paste the following code in a text file, and then save the file as C:\Book.xsd:
    <xs:schema xmlns="urn:bookstore-schema"
             targetNamespace="urn:bookstore-schema"
             xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="book" type="booktype"/>
          <xs:complexType name="booktype">
             <xs:sequence>
                <xs:element name="author" type="xs:string"/>
                   <xs:element name="price" type="xs:decimal" />
                   </xs:sequence>
          </xs:complexType>
    </xs:schema>
    					
  2. Modify the author of the element name.
  3. Set the minOccurs and maxOccurs properties.
  4. Modify the data type of the element price.
  5. Add a new element publisher.
back to the top

Create a Visual Basic .NET Application to Modify the Schema

To add a new publisher child element to the book element of the schema, follow these steps:
  1. Create a new Visual Basic .NET Console application project.
  2. Replace the code in Module1.vb with the following:
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim xsd As String = "c:\book.xsd"
            Dim fs As FileStream
            Dim schema As XmlSchema
            Try
                fs = New FileStream(xsd, FileMode.Open)
                schema = XmlSchema.Read(fs, New ValidationEventHandler(AddressOf ShowCompileError))
    
                schema.Compile(AddressOf ShowCompileError)
    
                'Create a SchemaObjectTable with the elements of the schema.
                Dim schematable As XmlSchemaObjectTable
                schematable = schema.Elements
    
                Dim qname As New XmlQualifiedName("book", "urn:bookstore-schema")
                Dim bookelement As New XmlSchemaElement()
                Dim complexelement As New XmlSchemaComplexType()
                If schematable.Contains(qname) Then
                    bookelement = CType(schematable.Item(qname), XmlSchemaElement)
    
                    Dim simpletype As XmlSchemaSimpleType
    
                    If (bookelement.SchemaTypeName.ToString = "urn:bookstore-schema:booktype") Then
                        complexelement = schema.SchemaTypes.Item(bookelement.SchemaTypeName)
                        Dim seqelement As New XmlSchemaSequence()
                        seqelement = complexelement.Particle
                        'Modify author element to show maxOccurs and minOccurs.
                        Dim eleauthor As New XmlSchemaElement()
                        eleauthor = seqelement.Items(0)
                        'Modify the author element name.
                        eleauthor.Name = "authorname"
                        'Add min and max.
                        eleauthor.MaxOccurs = 1
                        eleauthor.MinOccurs = 1
    
                        Dim eleprice As New XmlSchemaElement()
                        eleprice = seqelement.Items(1)
                        'Modify the type of price element.
                        eleprice.SchemaTypeName = New XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema")
    
                        'Create a new element and add to the schema
                        Dim Newelement As New XmlSchemaElement()
                        Newelement.Name = "Publisher"
    
                        Newelement.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
    
                        seqelement.Items.Add(Newelement)
                    End If
    
                End If
    
    
                'Display the schema in the Output window.
                schema.Write(Console.Out)
                Console.Read()
                'Save to the hard disk.
                Dim Newschema As New StreamWriter("c:\Newbooks.xsd", False)
                schema.Write(Newschema)
    
            Catch exp As XmlSchemaException
                Console.WriteLine(exp.Message)
            Catch exp As XmlException
                Console.WriteLine(exp.Message)
            Catch exp As Exception
                Console.WriteLine(exp.Message)
            Finally
                Console.Read()
            End Try
    
    
        End Sub
        Public Sub ShowCompileError(ByVal sender As Object, ByVal e As ValidationEventArgs)
            Console.WriteLine("Validation Error: {0}", e.Message)
        End Sub
    End Module
    						
    XmlSchemaObjectTable is a collection class that provides read-only helpers for XmlSchemaObject objects. This class is used to provide the collections for contained elements that are in the schema as collections that are accessed from the XmlSchema class; for example, Attributes, AttributeGroups, and Elements.

    Because you are trying to add a child element publisher to a book element, add the new element to the XmlSchemaSequence. The XmlSchemaSequence class represents the World Wide Web Consortium SEQUENCE (compositor) element.
  3. Press F5 to build and run the application.

    The new schema that includes the publisher element appears in the Console output area. Also, the XSD file named Newbook.xsd is created in the C:\ folder.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

313826 INFO: Roadmap for XML Schemas in the .NET Framework

313651 INFO: Roadmap for XML in the .NET Framework

For additional information, see the following MSDN Web sites: back to the top

Modification Type:MajorLast Reviewed:9/4/2003
Keywords:kbHOWTOmaster kbinfo KB317903 kbAudDeveloper