This documentation is archived and is not being maintained.

XmlSchemaObject Class

Creates an empty schema.

For a list of all members of this type, see XmlSchemaObject Members.

      Derived classes

[Visual Basic]
MustInherit Public Class XmlSchemaObject
public abstract class XmlSchemaObject
public __gc __abstract class XmlSchemaObject
public abstract class XmlSchemaObject

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


[Visual Basic, C#, C++] The following example displays each schema object.

[Visual Basic] 
Option Strict
Option Explicit

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class ValidXSD
    Public Shared Sub Main()
        Dim xsd As String = "example.xsd"
        Dim fs As FileStream
        Dim schema As XmlSchema
            fs = New FileStream(xsd, FileMode.Open)
            schema = XmlSchema.Read(fs,new ValidationEventHandler(AddressOf ShowCompileError))
            schema.Compile(AddressOf ShowCompileError)
            If schema.IsCompiled Then
            End If

        Catch e As XmlSchemaException
            Console.WriteLine("LineNumber = {0}", e.LineNumber)
            Console.WriteLine("LinePosition = {0}", e.LinePosition)
            Console.WriteLine("Message = {0}", e.Message)
            Console.WriteLine("Source = {0}", e.Source)

        End Try
    End Sub 'Main
    Overloads Private Shared Sub DisplayObjects(o As Object)
        DisplayObjects(o, "")
    End Sub 'DisplayObjects
    Overloads Private Shared Sub DisplayObjects(o As Object, indent As String)
        Console.WriteLine("{0}{1}", indent, o)
        Dim property1 As PropertyInfo
        For Each property1 In  o.GetType().GetProperties()
            If property1.PropertyType.FullName = "System.Xml.Schema.XmlSchemaObjectCollection" Then
                Dim childObjectCollection As XmlSchemaObjectCollection = CType(property1.GetValue(o, Nothing), XmlSchemaObjectCollection)
                Dim schemaObject As XmlSchemaObject
                For Each schemaObject In  childObjectCollection
                    DisplayObjects(schemaObject, indent + ControlChars.Tab)
                Next schemaObject
            End If
        Next property1
    End Sub 'DisplayObjects
    Private Shared Sub ShowCompileError(sender As Object, e As ValidationEventArgs)
        Console.WriteLine("Validation Error: {0}", e.Message)
    End Sub 'ShowCompileError
End Class 'ValidXSD

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

public class ValidXSD {
    public static int Main() {
        string xsd = "example.xsd";

        FileStream fs;
        XmlSchema schema;
        try {
            fs = new FileStream(xsd, FileMode.Open);
            schema = XmlSchema.Read(fs, new ValidationEventHandler(ShowCompileError));

            schema.Compile(new ValidationEventHandler(ShowCompileError));
            if (schema.IsCompiled) {
            return 0;
        } catch (XmlSchemaException e) {
            Console.WriteLine("LineNumber = {0}", e.LineNumber);
            Console.WriteLine("LinePosition = {0}", e.LinePosition);
            Console.WriteLine("Message = {0}", e.Message);
            Console.WriteLine("Source = {0}", e.Source);
            return -1;
    private static void DisplayObjects(object o) {
        DisplayObjects(o, "");
    private static void DisplayObjects(object o, string indent) {
        Console.WriteLine("{0}{1}", indent, o);

        foreach (PropertyInfo property in o.GetType().GetProperties()) {
            if (property.PropertyType.FullName == "System.Xml.Schema.XmlSchemaObjectCollection") {
                XmlSchemaObjectCollection childObjectCollection = (XmlSchemaObjectCollection) property.GetValue(o, null);
                foreach (XmlSchemaObject schemaObject in childObjectCollection) {
                    DisplayObjects(schemaObject, indent + "\t");
    private static void ShowCompileError(object sender, ValidationEventArgs e) {
        Console.WriteLine("Validation Error: {0}", e.Message);

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::IO;
using namespace System::Reflection;

public __gc class ValidXSD {
    static int main() {
        String* xsd = S"example.xsd";

        FileStream* fs;
        XmlSchema* schema;
        try {
            fs = new FileStream(xsd, FileMode::Open);
            schema = XmlSchema::Read(fs, new ValidationEventHandler(0, ShowCompileError));

            schema->Compile(new ValidationEventHandler(0, ShowCompileError));
            if (schema->IsCompiled) {
            return 0;
        } catch (XmlSchemaException* e) {
            Console::WriteLine(S"LineNumber = {0}", __box(e->LineNumber));
            Console::WriteLine(S"LinePosition = {0}", __box(e->LinePosition));
            Console::WriteLine(S"Message = {0}",e->Message);
            Console::WriteLine(S"Source = {0}",e->Source);
            return -1;
    static void DisplayObjects(Object* o) {
        DisplayObjects(o, S"");

    static void DisplayObjects(Object* o, String* indent) {

        System::Collections::IEnumerator* myEnum = o->GetType()->GetProperties()->GetEnumerator();
        while (myEnum->MoveNext())
            PropertyInfo* property = __try_cast<PropertyInfo*>(myEnum->Current);
            if (property->PropertyType->FullName->Equals(S"System.Xml.Schema.XmlSchemaObjectCollection")) {
                XmlSchemaObjectCollection* childObjectCollection = dynamic_cast<XmlSchemaObjectCollection*> (property->GetValue(o, 0));
                System::Collections::IEnumerator* myEnum1 = childObjectCollection->GetEnumerator();
                while (myEnum1->MoveNext())
                    XmlSchemaObject* schemaObject = __try_cast<XmlSchemaObject*>(myEnum1->Current);
                    DisplayObjects(schemaObject, String::Concat( indent, S"\t" ));

    static void ShowCompileError(Object* /*sender*/, ValidationEventArgs* e) {
        Console::WriteLine(S"Validation Error: {0}",e->Message);

int main()

[Visual Basic, C#, C++] The example uses the example.xsd file as input.

<?xml version="1.0"?>
<xs:schema xmlns:xs="">
  <xs:group name="testGroup">
    <xs:any namespace="##any"/>
  <xs:element name="myElement" >
    <xs:choice maxOccurs="unbounded">
      <xs:group ref="testGroup" />

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Xml.Schema

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlSchemaObject Members | System.Xml.Schema Namespace