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.
VB XmlSchemaObjectModel.exe
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.
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.ConformanceLevel = ConformanceLevel.Fragment
writer = XmlWriter.Create(Console.Out, settings)
...
Dim xmlNameTable As NameTable = New NameTable()
Dim xmlSchemaSet As New XmlSchemaSet(xmlNameTable)
xmlSchemaSet.Add(Nothing, sampleSchema1)
xmlSchemaSet.Add(Nothing, sampleSchema2)
xmlSchemaSet.Compile()
...
Dim tempXmlSchema As XmlSchema
For Each tempXmlSchema In xmlSchemaSet.Schemas()
' Write out the various schema parts
WriteXSDSchema()
Next tempXmlSchema
VB
The WriteXSDSchema function loops through each item in the XmlSchema and, after determining its type, formats the item for output to the screen.
Summary
- The Schema Object Model (SOM) loads and saves valid XML Schemas.
- The SOM provides an easy way to create in-memory schemas using strongly typed classes.
- 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.
|