Microsoft KB Archive/317903

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

Article ID: 317903

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q317903



For a Microsoft Visual C# .NET version of this article, see 318502.

IN THIS TASK
SUMMARY
 * Requirements
 * Create an XML Schema File
 * Create Visual Basic .NET Application Application to Modify the Schema

REFERENCES



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
  Paste the following code in a text file, and then save the file as C:\Book.xsd:           Modify the author of the element name.</li> Set the minOccurs and maxOccurs properties.</li> Modify the data type of the element price.</li> Add a new element publisher.</li></ol>

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: <ol> Create a new Visual Basic .NET Console application project.</li>  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 = &quot;c:\book.xsd&quot; 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(&quot;book&quot;, &quot;urn:bookstore-schema&quot;) 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 = &quot;urn:bookstore-schema:booktype&quot;) 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 = &quot;authorname&quot; '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(&quot;decimal&quot;, &quot;http://www.w3.org/2001/XMLSchema&quot;)

'Create a new element and add to the schema Dim Newelement As New XmlSchemaElement Newelement.Name = &quot;Publisher&quot;

Newelement.SchemaTypeName = New XmlQualifiedName(&quot;string&quot;, &quot;http://www.w3.org/2001/XMLSchema&quot;)

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(&quot;c:\Newbooks.xsd&quot;, 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(&quot;Validation Error: {0}&quot;, 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. </li> 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.</li></ol>

back to the top

<div class="references_section">