Share via


XML Şemaları Derleme

Ad alanı içindeki System.Xml.Schema sınıflar, World Wide Web Consortium (W3C) XML Şema Önerisi'nde tanımlanan yapılarla eşler ve bellek içinde XML şemaları oluşturmak için kullanılabilir.

XML Şeması Oluşturma

Aşağıdaki kod örneklerinde SOM API'si, bellek içi müşteri XML şeması oluşturmak için kullanılır.

Öğe ve Öznitelik oluşturma

Kod örnekleri müşteri şemasını aşağıdan yukarıya doğru oluşturur, önce alt öğeleri, öznitelikleri ve bunlara karşılık gelen türleri, sonra da en üst düzey öğeleri oluşturur.

Aşağıdaki kod örneğinde, ve öğelerinin FirstName yanı CustomerId sıra müşteri şemasının özniteliği, SOM'nin XmlSchemaElement ve XmlSchemaAttribute sınıfları kullanılarak oluşturulur.LastName bir XML şemasındaki NameXmlSchemaElement ve öğelerinin "name" özniteliğine karşılık gelen ve XmlSchemaAttribute<xs:attribute /> sınıflarının <xs:element /> özellikleri dışında, şema (defaultValue, fixedValue, formvb.) tarafından izin verilen diğer tüm öznitelikler ve XmlSchemaAttribute sınıflarında ilgili özelliklere XmlSchemaElement sahiptir.

// Create the FirstName and LastName elements.
XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
firstNameElement->Name = "FirstName";
XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
lastNameElement->Name = "LastName";

// Create CustomerId attribute.
XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
idAttribute->Name = "CustomerId";
idAttribute->Use = XmlSchemaUse::Required;
// Create the FirstName and LastName elements.
XmlSchemaElement firstNameElement = new XmlSchemaElement();
firstNameElement.Name = "FirstName";
XmlSchemaElement lastNameElement = new XmlSchemaElement();
lastNameElement.Name = "LastName";

// Create CustomerId attribute.
XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
idAttribute.Name = "CustomerId";
idAttribute.Use = XmlSchemaUse.Required;
' Create the FirstName and LastName elements.
Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
firstNameElement.Name = "FirstName"
Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
lastNameElement.Name = "LastName"

' Create CustomerId attribute.
Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
idAttribute.Name = "CustomerId"
idAttribute.Use = XmlSchemaUse.Required

Şema Türleri Oluşturma

Öğelerin ve özniteliklerin içeriği türlerine göre tanımlanır. Türleri yerleşik şema türlerinden biri olan öğeler ve öznitelikler oluşturmak için, SchemaTypeName veya sınıflarının XmlSchemaElement özelliği, sınıfı kullanılarak XmlQualifiedName yerleşik türün ilgili tam XmlSchemaAttribute adıyla ayarlanır. Öğeler ve öznitelikler için kullanıcı tanımlı bir tür oluşturmak için, veya XmlSchemaComplexType sınıfı kullanılarak XmlSchemaSimpleType yeni bir basit veya karmaşık tür oluşturulur.

Not

Bir öğenin veya özniteliğin anonim alt öğeleri olan adsız basit veya karmaşık türler oluşturmak için (öznitelikler için yalnızca basit türler geçerlidir), veya XmlSchemaAttribute sınıflarının XmlSchemaElement özelliğini veya XmlSchemaAttribute sınıflarının özelliği yerine SchemaTypeName adsız basit veya karmaşık tür olarak XmlSchemaElement ayarlayınSchemaType.

XML şemaları hem anonim hem de adlandırılmış basit türlerin diğer basit türlerden (yerleşik veya kullanıcı tanımlı) kısıtlamayla türetilmesine veya diğer basit türlerin listesi veya birleşimi olarak oluşturmasına olanak tanır. sınıfı XmlSchemaSimpleTypeRestriction , yerleşik xs:string türü kısıtlayarak basit bir tür oluşturmak için kullanılır. Liste veya birleşim türleri oluşturmak için veya XmlSchemaSimpleTypeUnion sınıflarını da kullanabilirsinizXmlSchemaSimpleTypeList. özelliği bunun XmlSchemaSimpleType.Content basit bir tür kısıtlaması mı, liste mi yoksa birleşim mi olduğunu belirtir.

Aşağıdaki kod örneğinde, FirstName öğenin türü yerleşik türüdürxs:string, LastName öğenin türü yerleşik türü kısıtlaması olan adlandırılmış bir basit türdür xs:stringMaxLength ve model değeri 20 ve özniteliğin CustomerId türü yerleşik türüdürxs:positiveInteger. Customer öğesi, parçacığı ve LastName öğelerinin FirstName dizisi olan ve öznitelikleri özniteliğini içeren CustomerId anonim bir karmaşık türdür.

Not

Veya sınıflarını XmlSchemaChoiceXmlSchemaAll , çoğaltmak <xs:choice /> veya <xs:all /> semantik yapmak için karmaşık türün parçacığı olarak da kullanabilirsiniz.

// Create the simple type for the LastName element.
XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
lastNameType->Name = "LastNameType";
XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
    gcnew XmlSchemaSimpleTypeRestriction();
lastNameRestriction->BaseTypeName =
    gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
maxLength->Value = "20";
lastNameRestriction->Facets->Add(maxLength);
lastNameType->Content = lastNameRestriction;

// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement->SchemaTypeName =
    gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement->SchemaTypeName =
    gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
    "http://www.w3.org/2001/XMLSchema");

// Create the top-level Customer element.
XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
customerElement->Name = "Customer";

// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
sequence->Items->Add(firstNameElement);
sequence->Items->Add(lastNameElement);
customerType->Particle = sequence;

// Add the CustomerId attribute to the complex type.
customerType->Attributes->Add(idAttribute);

// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement->SchemaType = customerType;
// Create the simple type for the LastName element.
XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
lastNameType.Name = "LastNameType";
XmlSchemaSimpleTypeRestriction lastNameRestriction =
    new XmlSchemaSimpleTypeRestriction();
lastNameRestriction.BaseTypeName =
    new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
maxLength.Value = "20";
lastNameRestriction.Facets.Add(maxLength);
lastNameType.Content = lastNameRestriction;

// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement.SchemaTypeName =
    new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement.SchemaTypeName =
    new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
    "http://www.w3.org/2001/XMLSchema");

// Create the top-level Customer element.
XmlSchemaElement customerElement = new XmlSchemaElement();
customerElement.Name = "Customer";

// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType customerType = new XmlSchemaComplexType();
XmlSchemaSequence sequence = new XmlSchemaSequence();
sequence.Items.Add(firstNameElement);
sequence.Items.Add(lastNameElement);
customerType.Particle = sequence;

// Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute);

// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement.SchemaType = customerType;
' Create the simple type for the LastName element.
Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
lastNameType.Name = "LastNameType"
Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
    New XmlSchemaSimpleTypeRestriction()
lastNameRestriction.BaseTypeName = _
    New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
maxLength.Value = "20"
lastNameRestriction.Facets.Add(maxLength)
lastNameType.Content = lastNameRestriction

' Associate the elements and attributes with their types.
' Built-in type.
firstNameElement.SchemaTypeName = _
    New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' User-defined type.
lastNameElement.SchemaTypeName = _
    New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
' Built-in type.
idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
    "http://www.w3.org/2001/XMLSchema")

' Create the top-level Customer element.
Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
customerElement.Name = "Customer"

' Create an anonymous complex type for the Customer element.
Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
sequence.Items.Add(firstNameElement)
sequence.Items.Add(lastNameElement)
customerType.Particle = sequence

' Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute)

' Set the SchemaType of the Customer element to
' the anonymous complex type created above.
customerElement.SchemaType = customerType

Şema Oluşturma ve Derleme

Bu noktada, alt öğeler ve öznitelikler, bunlara karşılık gelen türler ve en üst düzey Customer öğe SOM API'si kullanılarak bellek içinde oluşturulmuştur. Aşağıdaki kod örneğinde şema öğesi sınıfı kullanılarak XmlSchema oluşturulur, en üst düzey öğeler ve türler özelliği kullanılarak XmlSchema.Items bu öğeye eklenir ve tüm şema sınıfı kullanılarak XmlSchemaSet derlenir ve konsola yazılır.

// Create an empty schema.
XmlSchema^ customerSchema = gcnew XmlSchema();
customerSchema->TargetNamespace = "http://www.tempuri.org";

// Add all top-level element and types to the schema
customerSchema->Items->Add(customerElement);
customerSchema->Items->Add(lastNameType);

// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
schemaSet->Add(customerSchema);
schemaSet->Compile();

for each (XmlSchema^ schema in schemaSet->Schemas())
{
    customerSchema = schema;
}

// Write the complete schema to the Console.
customerSchema->Write(Console::Out);
// Create an empty schema.
XmlSchema customerSchema = new XmlSchema();
customerSchema.TargetNamespace = "http://www.tempuri.org";

// Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement);
customerSchema.Items.Add(lastNameType);

// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add(customerSchema);
schemaSet.Compile();

foreach (XmlSchema schema in schemaSet.Schemas())
{
    customerSchema = schema;
}

// Write the complete schema to the Console.
customerSchema.Write(Console.Out);
' Create an empty schema.
Dim customerSchema As XmlSchema = New XmlSchema()
customerSchema.TargetNamespace = "http://www.tempuri.org"

' Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement)
customerSchema.Items.Add(lastNameType)

' Create an XmlSchemaSet to compile the customer schema.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add(customerSchema)
schemaSet.Compile()

For Each schema As XmlSchema In schemaSet.Schemas()
    customerSchema = schema
Next

' Write the complete schema to the Console.
customerSchema.Write(Console.Out)

yöntemi, XmlSchemaSet.Compile müşteri şemasını bir XML şemasının kurallarına göre doğrular ve şema derleme sonrası özellikleri kullanılabilir hale getirir.

Not

SOM API'sindeki tüm şema sonrası derleme özellikleri, Post-Schema-Validation-Infoset'ten farklıdır.

ValidationEventHandler öğesine XmlSchemaSet eklenen, şema doğrulama uyarılarını ve hatalarını işlemek için geri çağırma yöntemini ValidationCallback çağıran bir temsilcidir.

Aşağıda tam kod örneği ve konsola yazılmış müşteri şeması verilmiştir.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class XmlSchemaCreateExample
{
public:

    static void Main()
    {
        // Create the FirstName and LastName elements.
        XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
        firstNameElement->Name = "FirstName";
        XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
        lastNameElement->Name = "LastName";

        // Create CustomerId attribute.
        XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
        idAttribute->Name = "CustomerId";
        idAttribute->Use = XmlSchemaUse::Required;

        // Create the simple type for the LastName element.
        XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
        lastNameType->Name = "LastNameType";
        XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
            gcnew XmlSchemaSimpleTypeRestriction();
        lastNameRestriction->BaseTypeName =
            gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
        maxLength->Value = "20";
        lastNameRestriction->Facets->Add(maxLength);
        lastNameType->Content = lastNameRestriction;

        // Associate the elements and attributes with their types.
        // Built-in type.
        firstNameElement->SchemaTypeName =
            gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        // User-defined type.
        lastNameElement->SchemaTypeName =
            gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
        // Built-in type.
        idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
            "http://www.w3.org/2001/XMLSchema");
        
        // Create the top-level Customer element.
        XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
        customerElement->Name = "Customer";

        // Create an anonymous complex type for the Customer element.
        XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
        XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
        sequence->Items->Add(firstNameElement);
        sequence->Items->Add(lastNameElement);
        customerType->Particle = sequence;
        
        // Add the CustomerId attribute to the complex type.
        customerType->Attributes->Add(idAttribute);

        // Set the SchemaType of the Customer element to
        // the anonymous complex type created above.
        customerElement->SchemaType = customerType;
        
        // Create an empty schema.
        XmlSchema^ customerSchema = gcnew XmlSchema();
        customerSchema->TargetNamespace = "http://www.tempuri.org";

        // Add all top-level element and types to the schema
        customerSchema->Items->Add(customerElement);
        customerSchema->Items->Add(lastNameType);

        // Create an XmlSchemaSet to compile the customer schema.
        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
        schemaSet->Add(customerSchema);
        schemaSet->Compile();
        
        for each (XmlSchema^ schema in schemaSet->Schemas())
        {
            customerSchema = schema;
        }

        // Write the complete schema to the Console.
        customerSchema->Write(Console::Out);
    }

    static void ValidationCallback(Object^ sender, ValidationEventArgs^ args)
    {
        if (args->Severity == XmlSeverityType::Warning)
            Console::Write("WARNING: ");
        else if (args->Severity == XmlSeverityType::Error)
            Console::Write("ERROR: ");

        Console::WriteLine(args->Message);
    }
};

int main()
{
    XmlSchemaCreateExample::Main();
    return 0;
}
using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaCreateExample
{
    static void Main(string[] args)
    {
        // Create the FirstName and LastName elements.
        XmlSchemaElement firstNameElement = new XmlSchemaElement();
        firstNameElement.Name = "FirstName";
        XmlSchemaElement lastNameElement = new XmlSchemaElement();
        lastNameElement.Name = "LastName";

        // Create CustomerId attribute.
        XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
        idAttribute.Name = "CustomerId";
        idAttribute.Use = XmlSchemaUse.Required;

        // Create the simple type for the LastName element.
        XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
        lastNameType.Name = "LastNameType";
        XmlSchemaSimpleTypeRestriction lastNameRestriction =
            new XmlSchemaSimpleTypeRestriction();
        lastNameRestriction.BaseTypeName =
            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
        maxLength.Value = "20";
        lastNameRestriction.Facets.Add(maxLength);
        lastNameType.Content = lastNameRestriction;

        // Associate the elements and attributes with their types.
        // Built-in type.
        firstNameElement.SchemaTypeName =
            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        // User-defined type.
        lastNameElement.SchemaTypeName =
            new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
        // Built-in type.
        idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
            "http://www.w3.org/2001/XMLSchema");

        // Create the top-level Customer element.
        XmlSchemaElement customerElement = new XmlSchemaElement();
        customerElement.Name = "Customer";

        // Create an anonymous complex type for the Customer element.
        XmlSchemaComplexType customerType = new XmlSchemaComplexType();
        XmlSchemaSequence sequence = new XmlSchemaSequence();
        sequence.Items.Add(firstNameElement);
        sequence.Items.Add(lastNameElement);
        customerType.Particle = sequence;

        // Add the CustomerId attribute to the complex type.
        customerType.Attributes.Add(idAttribute);

        // Set the SchemaType of the Customer element to
        // the anonymous complex type created above.
        customerElement.SchemaType = customerType;

        // Create an empty schema.
        XmlSchema customerSchema = new XmlSchema();
        customerSchema.TargetNamespace = "http://www.tempuri.org";

        // Add all top-level element and types to the schema
        customerSchema.Items.Add(customerElement);
        customerSchema.Items.Add(lastNameType);

        // Create an XmlSchemaSet to compile the customer schema.
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
        schemaSet.Add(customerSchema);
        schemaSet.Compile();

        foreach (XmlSchema schema in schemaSet.Schemas())
        {
            customerSchema = schema;
        }

        // Write the complete schema to the Console.
        customerSchema.Write(Console.Out);
    }

    static void ValidationCallback(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.Write("WARNING: ");
        else if (args.Severity == XmlSeverityType.Error)
            Console.Write("ERROR: ");

        Console.WriteLine(args.Message);
    }
}
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaCreateExample

    Shared Sub Main()

        ' Create the FirstName and LastName elements.
        Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
        firstNameElement.Name = "FirstName"
        Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
        lastNameElement.Name = "LastName"

        ' Create CustomerId attribute.
        Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
        idAttribute.Name = "CustomerId"
        idAttribute.Use = XmlSchemaUse.Required

        ' Create the simple type for the LastName element.
        Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
        lastNameType.Name = "LastNameType"
        Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
            New XmlSchemaSimpleTypeRestriction()
        lastNameRestriction.BaseTypeName = _
            New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
        Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
        maxLength.Value = "20"
        lastNameRestriction.Facets.Add(maxLength)
        lastNameType.Content = lastNameRestriction

        ' Associate the elements and attributes with their types.
        ' Built-in type.
        firstNameElement.SchemaTypeName = _
            New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
        ' User-defined type.
        lastNameElement.SchemaTypeName = _
            New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
        ' Built-in type.
        idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
            "http://www.w3.org/2001/XMLSchema")

        ' Create the top-level Customer element.
        Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
        customerElement.Name = "Customer"

        ' Create an anonymous complex type for the Customer element.
        Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
        Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
        sequence.Items.Add(firstNameElement)
        sequence.Items.Add(lastNameElement)
        customerType.Particle = sequence

        ' Add the CustomerId attribute to the complex type.
        customerType.Attributes.Add(idAttribute)

        ' Set the SchemaType of the Customer element to
        ' the anonymous complex type created above.
        customerElement.SchemaType = customerType

        ' Create an empty schema.
        Dim customerSchema As XmlSchema = New XmlSchema()
        customerSchema.TargetNamespace = "http://www.tempuri.org"

        ' Add all top-level element and types to the schema
        customerSchema.Items.Add(customerElement)
        customerSchema.Items.Add(lastNameType)

        ' Create an XmlSchemaSet to compile the customer schema.
        Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
        schemaSet.Add(customerSchema)
        schemaSet.Compile()

        For Each schema As XmlSchema In schemaSet.Schemas()
            customerSchema = schema
        Next

        ' Write the complete schema to the Console.
        customerSchema.Write(Console.Out)
    End Sub

    Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
        If args.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
        Else
            If args.Severity = XmlSeverityType.Error Then
                Console.Write("ERROR: ")
            End If
        End If
        Console.WriteLine(args.Message)
    End Sub
End Class
<?xml version="1.0" encoding="utf-8"?>  
<xs:schema xmlns:tns="http://www.tempuri.org" targetNamespace="http://www.tempuri.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
   <xs:element name="Customer">  
      <xs:complexType>  
         <xs:sequence>  
            <xs:element name="FirstName" type="xs:string" />  
            <xs:element name="LastName" type="tns:LastNameType" />  
         </xs:sequence>  
         <xs:attribute name="CustomerId" type="xs:positiveInteger" use="required" />  
      </xs:complexType>  
   </xs:element>  
   <xs:simpleType name="LastNameType">  
      <xs:restriction base="xs:string">  
         <xs:maxLength value="20" />  
      </xs:restriction>  
   </xs:simpleType>  
</xs:schema>  

Ayrıca bkz.