Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Use the Xml Schema Object Model?

This sample illustrates how to read two XML Schema Definition language (XSD) schemas into a SchemaSet, navigate through the schemas that they represent, and create a fictious XML output that is representative of the schema's.

This sample shows how to use the XmlSchemaSet to cache and retrieve multiple schemas, as well as demonstrates that the Schema Object Model (SOM) loads and saves valid XML Schemas. You can also use the SOM to create in-memory schemas using strongly typed classes.

To demonstrate how to navigate the SOM, this sample outputs a formatted version of the two XML Schemas that were loaded into the XmlSchemaSet. Then, the sample creates an XML stream to give an example of what the given schema usage might look like.

VB XmlSchemaObjectModel.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Xml\XmlSchemaObjectModel\]

This sample first creates an XmlWriter that eventually writes out to the screen. The sample does this to take advantage of the various methods of the XmlWriter that produce well-formed XML. Some examples of these methods are the WriteStartElement, WriteEndElement, and WriteAttributeString. Then, the sample creates an XmlNameTable and adds the name table to the XmlSchemaSet. The sample adds two unique XML Schemas into the XmlSchemaSet and compiles the schemas. Finally, the sample writes each schema in the XmlSchemaSet to the screen, followed by an example of that schema would be used against some example XML.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
myXmlWriter = XmlWriter.Create(Console.Out, settings);
XmlNameTable myXmlNameTable = new NameTable();
XmlSchemaSet myXmlSchemaSet = new XmlSchemaSet(myXmlNameTable);
myXmlSchemaSet.Add(null, args[0]);
myXmlSchemaSet.Add(null, args[1]);
foreach (XmlSchema myTempXmlSchema in myXmlSchemaSet.Schemas())
myXmlSchema = myTempXmlSchema;
// Write out the various schema parts

The WriteXSDSchema function loops through each item in the XmlSchema and, after determining its type, format the item for output to the screen.


  1. The Schema Object Model (SOM) loads and saves valid XML Schemas.
  2. The SOM provides an easy way to create in-memory schemas using strongly typed classes.
  3. You can use the XmlSchemaSet object to cache and retrieve multiple schemas.

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.