Export (0) Print
Expand All

XmlSchemaObject Class

Represents the root class for the Xml schema object model hierarchy and serves as a base class for classes such as the XmlSchema class.

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

[PermissionSetAttribute(SecurityAction::InheritanceDemand, Name = L"FullTrust")]
public ref class XmlSchemaObject abstract

The following example displays each schema object.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Reflection;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class ValidXSD
	static void DisplayObjects(Object^ o)
        DisplayObjects(o, "");

    static void DisplayObjects(Object^ o, String^ indent)
        Console::WriteLine("{0}{1}", indent, o);

        for each (PropertyInfo^ property1 in o->GetType()->GetProperties())
            if (property1->PropertyType->FullName == "System.Xml.Schema.XmlSchemaObjectCollection")
                XmlSchemaObjectCollection^ childObjectCollection = dynamic_cast<XmlSchemaObjectCollection^>(property1->GetValue(o, nullptr));

                for each (XmlSchemaObject^ schemaObject in childObjectCollection)
                    DisplayObjects(schemaObject, indent + "\t");

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

	static int Main()
        String^ xsd = "example.xsd";

        FileStream^ fs;
        XmlSchema^ schema;
			fs = gcnew FileStream(xsd, FileMode::Open);
			schema = XmlSchema::Read(fs, gcnew ValidationEventHandler(ShowCompileError));

            XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
            schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ShowCompileError);

            XmlSchema^ compiledSchema = nullptr;

            for each (XmlSchema^ schema1 in schemaSet->Schemas())
                compiledSchema = schema1;

            schema = compiledSchema;

            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;

int main()
	return 0;

The example uses the example.xsd file as input.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="testGroup">
    <xs:any namespace="##any"/>
  <xs:element name="myElement" >
    <xs:choice maxOccurs="unbounded">
      <xs:group ref="testGroup" />

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2015 Microsoft