XmlSchema Class
Contains the definition of a schema. All XML Schema definition language (XSD) elements are children of the schema element. Represents the World Wide Web Consortium (W3C) schema element.
For a list of all members of this type, see XmlSchema Members.
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchema
[Visual Basic] Public Class XmlSchema Inherits XmlSchemaObject [C#] public class XmlSchema : XmlSchemaObject [C++] public __gc class XmlSchema : public XmlSchemaObject [JScript] public class XmlSchema extends 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.
Remarks
All XML Schema definition language (XSD) elements are children of the schema element.
Example
[Visual Basic, C#, C++] The following example creates a schema definition.
[Visual Basic] Option Explicit Option Strict Imports System Imports System.Xml Imports System.Xml.Schema Class XMLSchemaExamples Public Shared Sub Main() Dim schema As New XmlSchema() ' <xs:element name="cat" type="xs:string"/> Dim elementCat As New XmlSchemaElement() schema.Items.Add(elementCat) elementCat.Name = "cat" elementCat.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") ' <xs:element name="dog" type="xs:string"/> Dim elementDog As New XmlSchemaElement() schema.Items.Add(elementDog) elementDog.Name = "dog" elementDog.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema") ' <xs:element name="redDog" substitutionGroup="dog" /> Dim elementRedDog As New XmlSchemaElement() schema.Items.Add(elementRedDog) elementRedDog.Name = "redDog" elementRedDog.SubstitutionGroup = New XmlQualifiedName("dog") ' <xs:element name="brownDog" substitutionGroup ="dog" /> Dim elementBrownDog As New XmlSchemaElement() schema.Items.Add(elementBrownDog) elementBrownDog.Name = "brownDog" elementBrownDog.SubstitutionGroup = New XmlQualifiedName("dog") ' <xs:element name="pets"> Dim elementPets As New XmlSchemaElement() schema.Items.Add(elementPets) elementPets.Name = "pets" ' <xs:complexType> Dim complexType As New XmlSchemaComplexType() elementPets.SchemaType = complexType ' <xs:choice minOccurs="0" maxOccurs="unbounded"> Dim choice As New XmlSchemaChoice() complexType.Particle = choice choice.MinOccurs = 0 choice.MaxOccursString = "unbounded" ' <xs:element ref="cat"/> Dim catRef As New XmlSchemaElement() choice.Items.Add(catRef) catRef.RefName = New XmlQualifiedName("cat") ' <xs:element ref="dog"/> Dim dogRef As New XmlSchemaElement() choice.Items.Add(dogRef) dogRef.RefName = New XmlQualifiedName("dog") schema.Compile(AddressOf ValidationCallbackOne) Dim nsmgr As New XmlNamespaceManager(New NameTable()) nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema") schema.Write(Console.Out, nsmgr) End Sub 'Main Public Shared Sub ValidationCallbackOne(sender As Object, args As ValidationEventArgs) Console.WriteLine(args.Message) End Sub 'ValidationCallbackOne End Class 'XMLSchemaExamples [C#] using System; using System.Xml; using System.Xml.Schema; class XMLSchemaExamples { public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:element name="cat" type="xs:string"/> XmlSchemaElement elementCat = new XmlSchemaElement(); schema.Items.Add(elementCat); elementCat.Name = "cat"; elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:element name="dog" type="xs:string"/> XmlSchemaElement elementDog = new XmlSchemaElement(); schema.Items.Add(elementDog); elementDog.Name = "dog"; elementDog.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:element name="redDog" substitutionGroup="dog" /> XmlSchemaElement elementRedDog = new XmlSchemaElement(); schema.Items.Add(elementRedDog); elementRedDog.Name = "redDog"; elementRedDog.SubstitutionGroup = new XmlQualifiedName("dog"); // <xs:element name="brownDog" substitutionGroup ="dog" /> XmlSchemaElement elementBrownDog = new XmlSchemaElement(); schema.Items.Add(elementBrownDog); elementBrownDog.Name = "brownDog"; elementBrownDog.SubstitutionGroup = new XmlQualifiedName("dog"); // <xs:element name="pets"> XmlSchemaElement elementPets = new XmlSchemaElement(); schema.Items.Add(elementPets); elementPets.Name = "pets"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); elementPets.SchemaType = complexType; // <xs:choice minOccurs="0" maxOccurs="unbounded"> XmlSchemaChoice choice = new XmlSchemaChoice(); complexType.Particle = choice; choice.MinOccurs = 0; choice.MaxOccursString = "unbounded"; // <xs:element ref="cat"/> XmlSchemaElement catRef = new XmlSchemaElement(); choice.Items.Add(catRef); catRef.RefName = new XmlQualifiedName("cat"); // <xs:element ref="dog"/> XmlSchemaElement dogRef = new XmlSchemaElement(); choice.Items.Add(dogRef); dogRef.RefName = new XmlQualifiedName("dog"); schema.Compile(new ValidationEventHandler(ValidationCallbackOne)); XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); schema.Write(Console.Out, nsmgr); } public static void ValidationCallbackOne(object sender, ValidationEventArgs args) { Console.WriteLine(args.Message); } } [C++] #using <mscorlib.dll> #using <System.Xml.dll> using namespace System; using namespace System::Xml; using namespace System::Xml::Schema; __gc class XMLSchemaExamples { public: static void main() { XmlSchema* schema = new XmlSchema(); // <xs:element name="cat" type="xs:string"/> XmlSchemaElement* elementCat = new XmlSchemaElement(); schema->Items->Add(elementCat); elementCat->Name = S"cat"; elementCat->SchemaTypeName = new XmlQualifiedName(S"string", S"http://www.w3.org/2001/XMLSchema"); // <xs:element name="dog" type="xs:string"/> XmlSchemaElement* elementDog = new XmlSchemaElement(); schema->Items->Add(elementDog); elementDog->Name = S"dog"; elementDog->SchemaTypeName = new XmlQualifiedName(S"string", S"http://www.w3.org/2001/XMLSchema"); // <xs:element name="redDog" substitutionGroup="dog" /> XmlSchemaElement* elementRedDog = new XmlSchemaElement(); schema->Items->Add(elementRedDog); elementRedDog->Name = S"redDog"; elementRedDog->SubstitutionGroup = new XmlQualifiedName(S"dog"); // <xs:element name="brownDog" substitutionGroup ="dog" /> XmlSchemaElement* elementBrownDog = new XmlSchemaElement(); schema->Items->Add(elementBrownDog); elementBrownDog->Name = S"brownDog"; elementBrownDog->SubstitutionGroup = new XmlQualifiedName(S"dog"); // <xs:element name="pets"> XmlSchemaElement* elementPets = new XmlSchemaElement(); schema->Items->Add(elementPets); elementPets->Name = S"pets"; // <xs:complexType> XmlSchemaComplexType* complexType = new XmlSchemaComplexType(); elementPets->SchemaType = complexType; // <xs:choice minOccurs="0" maxOccurs="unbounded"> XmlSchemaChoice* choice = new XmlSchemaChoice(); complexType->Particle = choice; choice->MinOccurs = 0; choice->MaxOccursString = S"unbounded"; // <xs:element ref="cat"/> XmlSchemaElement* catRef = new XmlSchemaElement(); choice->Items->Add(catRef); catRef->RefName = new XmlQualifiedName(S"cat"); // <xs:element ref="dog"/> XmlSchemaElement* dogRef = new XmlSchemaElement(); choice->Items->Add(dogRef); dogRef->RefName = new XmlQualifiedName(S"dog"); schema->Compile(new ValidationEventHandler(0, ValidationCallbackOne)); XmlNamespaceManager* nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr->AddNamespace(S"xs", S"http://www.w3.org/2001/XMLSchema"); schema->Write(Console::Out,nsmgr); } static void ValidationCallbackOne(Object* /*sender*/, ValidationEventArgs* args) { Console::WriteLine(args->Message); } }; int main() { XMLSchemaExamples::main(); }
[Visual Basic, C#, C++] The following XML file is generated for the preceding code example.
<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cat" type="xs:string"/>
<xs:element name="dog" type="xs:string"/>
<xs:element name="redDog" type="xs:string" substitutionGroup="dog"/>
<xs:element name="brownDog" type="xs:string" substitutionGroup ="dog" />
<xs:element name="pets">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="cat"/>
<xs:element ref="dog"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
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)