Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   |   Class Browser
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

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 and navigate through the schemas that they represent.

This sample shows how to use the XmlSchemaSet to cache and retrieve multiple schemas, and demonstrates that the Schema Object Model (SOM) loads and saves valid XML Schemas. You can also use the SOM to create in-memory schemas by 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.

C# XmlSchemaObjectModel.exe
View Source

[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Xml\XmlSchemaObjectModel\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]

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.

		
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
writer = XmlWriter.Create(Console.Out, settings);
...
XmlNameTable xmlNameTable = new NameTable();
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet(xmlNameTable);
xmlSchemaSet.Add(null, sampleSchema1);
xmlSchemaSet.Add(null, sampleSchema2);
xmlSchemaSet.Compile(); 
...
foreach (XmlSchema tempXmlSchema in xmlSchemaSet.Schemas())
{
    // Write out the various schema parts
    WriteXSDSchema();
}
C#

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

Summary

  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 � 2005 Microsoft Corporation. All rights reserved.


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2009  Mindcracker LLC. All Rights Reserved