Microsoft KB Archive/318502

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

Article ID: 318502

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 Q318502



For a Microsoft Visual Basic .NET version of this article, see 317903.

This article references the following .NET Framework Class Library namespaces:
 * System.Xml
 * System.Xml.Schema

IN THIS TASK
SUMMARY
 * Requirements
 * Create an XML Schema File
 * Create a Sample Visual C# .NET 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 you need:
 * 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 C# .NET language.
 * XML standards.
 * XSD schemas.

back to the top

Create the XML Schema File
  In Microsoft Notepad or another text editor, type or paste the following code into a new file, and then name the file C:\Book.xsd:           Modify the author of the element name.</li> Set 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 Sample Visual C# .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 C# Console application project.</li>  Replace the code in the Class1.cs module with the following:

using System; using System.Xml; using System.Xml.Schema; using System.IO;

namespace ConsoleApplication1 {  class Class1 {     [STAThread] static void Main(string[] args) {        FileStream fs; XmlSchema schema; ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors);

try {           // Open the XSD file. fs = new FileStream(&quot;c:\\book.xsd&quot;, FileMode.Open);

// Read the schema into an XMLSchema object. schema = XmlSchema.Read(fs, eventHandler);

// Compile the schema. schema.Compile(eventHandler);

// Define an XMLSchemaObjectTable to read the schema elements. XmlSchemaObjectTable schematable; schematable = schema.Elements;

// Define a QualifiedName to identify the elements. XmlQualifiedName qname = new XmlQualifiedName(&quot;book&quot;, &quot;urn:bookstore-schema&quot;);

XmlSchemaElement bookelement = new XmlSchemaElement; XmlSchemaComplexType complexelement = new XmlSchemaComplexType; // See if the XmlSchemaObjectTable has the element. if (schematable.Contains(qname)) {              bookelement = (XmlSchemaElement) schematable[qname]; // See if the element type is booktype. // If it is, create a new element and add it to the // schema. if (bookelement.SchemaTypeName.ToString == &quot;urn:bookstore-schema:booktype&quot;) {                 complexelement = (XmlSchemaComplexType) schema.SchemaTypes[bookelement.SchemaTypeName]; XmlSchemaSequence seqelement = new XmlSchemaSequence; seqelement = (XmlSchemaSequence) complexelement.Particle; // Modify author element to show maxOccurs and minOccurs. XmlSchemaElement eleauthor = new XmlSchemaElement; eleauthor = (XmlSchemaElement) seqelement.Items[0]; // Modify the author element name. eleauthor.Name = &quot;authorname&quot;; // Add min and max. eleauthor.MaxOccurs = 1; eleauthor.MinOccurs = 1;

XmlSchemaElement eleprice = new XmlSchemaElement; eleprice = (XmlSchemaElement) 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 it to the schema. // XmlSchemaSequence seqelement = new XmlSchemaSequence; // seqelement = (XmlSchemaSequence) complexelement.Particle; XmlSchemaElement Newelement = 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); }                 }

// Display the schema in the Output window. schema.Write(Console.Out); Console.Read;

// Save the output to the hard disk. StreamWriter strmWrtr = new StreamWriter(&quot;c:\\newbook.xsd&quot;, false); schema.Write(strmWrtr); }         catch (XmlSchemaException schemaEx) {           Console.WriteLine(schemaEx.Message); }        catch (XmlException xmlEx) {           Console.WriteLine(xmlEx.Message); }        catch (Exception ex) {           Console.WriteLine(ex.Message); }        finally {              Console.Read; }     }

public static void ShowCompileErrors(object sender, ValidationEventArgs args) {        Console.WriteLine(&quot;Validation Error: {0}&quot;, args.Message); }  }

}

XmlSchemaObjectTable is a collection class that provides read-only helpers for XmlSchemaObject objects. This class provides the collections for contained elements 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">