Condividi tramite


XmlSchema Classe

Definizione

Rappresentazione in memoria di un XML Schema, come specificato in World Wide Web Consortium (W3C) XML Schema Part 1: Strutture e XML Schema Part 2: Datatypes].

public ref class XmlSchema
public ref class XmlSchema : System::Xml::Schema::XmlSchemaObject
public class XmlSchema
public class XmlSchema : System.Xml.Schema.XmlSchemaObject
type XmlSchema = class
type XmlSchema = class
    inherit XmlSchemaObject
Public Class XmlSchema
Public Class XmlSchema
Inherits XmlSchemaObject
Ereditarietà
XmlSchema
Ereditarietà

Esempio

Nell'esempio seguente viene creata una definizione dello schema.

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");

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
        schemaSet.Add(schema);
        schemaSet.Compile();

        XmlSchema compiledSchema = null;

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);
    }

    public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
    {
        Console.WriteLine(args.Message);
    }
}
Option Explicit On
Option Strict On

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")

        Dim schemaSet As New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallbackOne

        schemaSet.Add(schema)
        schemaSet.Compile()

        Dim compiledSchema As XmlSchema = Nothing

        For Each schema1 As XmlSchema In schemaSet.Schemas()
            compiledSchema = schema1
        Next

        Dim nsmgr As New XmlNamespaceManager(New NameTable())
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
        compiledSchema.Write(Console.Out, nsmgr)


    End Sub


    Public Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Console.WriteLine(args.Message)
    End Sub
End Class

Il file XML seguente viene generato per l'esempio di codice precedente.


<?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>

Commenti

Importante

  • Non usare schemi da origini o percorsi sconosciuti o non attendibili. In questo modo verrà compromessa la sicurezza del codice.
  • Gli XML Schema (inclusi gli schemi inline) sono intrinsecamente vulnerabili agli attacchi Denial of Service; non accettarli in scenari non attendibili.
  • Le eccezioni generate in seguito all'uso della classe , ad esempio la XmlSchemaXmlSchemaException classe , possono contenere informazioni riservate che non devono essere esposte in scenari non attendibili. Ad esempio, la SourceUri proprietà di un XmlSchemaException oggetto restituisce il percorso dell'URI al file di schema che ha causato l'eccezione. La SourceUri proprietà non deve essere esposta in scenari non attendibili. Le eccezioni devono essere gestite correttamente in modo che queste informazioni riservate non vengano esposte in scenari non attendibili.

Costruttori

Nome Descrizione
XmlSchema()

Inizializza una nuova istanza della classe XmlSchema.

Campi

Nome Descrizione
InstanceNamespace

Spazio dei nomi dell'istanza di XML Schema. Questo campo è costante.

Namespace

Spazio dei nomi XML Schema. Questo campo è costante.

Proprietà

Nome Descrizione
AttributeFormDefault

Ottiene o imposta il form per gli attributi dichiarati nello spazio dei nomi di destinazione dello schema.

AttributeGroups

Ottiene il valore di post-compilazione dello schema di tutti i gruppi di attributi globali nello schema.

Attributes

Ottiene il valore di post-compilazione dello schema per tutti gli attributi nello schema.

BlockDefault

Ottiene o imposta l'attributo blockDefault , che imposta il valore predefinito dell'attributo block sui tipi di elemento e complessi nello targetNamespace schema.

ElementFormDefault

Ottiene o imposta il form per gli elementi dichiarati nello spazio dei nomi di destinazione dello schema.

Elements

Ottiene il valore di post-compilazione dello schema per tutti gli elementi dello schema.

FinalDefault

Ottiene o imposta l'attributo finalDefault , che imposta il valore predefinito dell'attributo final sugli elementi e sui tipi complessi nello spazio dei nomi di destinazione dello schema.

Groups

Ottiene il valore di post-compilazione dello schema di tutti i gruppi nello schema.

Id

Ottiene o imposta l'ID stringa.

Includes

Ottiene la raccolta di schemi inclusi e importati.

IsCompiled

Indica se lo schema è stato compilato.

Items

Ottiene la raccolta di elementi dello schema nello schema e viene utilizzata per aggiungere nuovi tipi di elemento a schema livello di elemento.

LineNumber

Ottiene o imposta il numero di riga nel file a cui fa riferimento l'elemento schema .

(Ereditato da XmlSchemaObject)
LinePosition

Ottiene o imposta la posizione della riga nel file a cui fa riferimento l'elemento schema .

(Ereditato da XmlSchemaObject)
Namespaces

Ottiene o imposta l'oggetto XmlSerializerNamespaces da utilizzare con questo oggetto schema.

(Ereditato da XmlSchemaObject)
Notations

Ottiene il valore di post-compilazione dello schema per tutte le notazioni nello schema.

Parent

Ottiene o imposta l'elemento padre dell'oggetto XmlSchemaObject.

(Ereditato da XmlSchemaObject)
SchemaTypes

Ottiene il valore di post-compilazione dello schema di tutti i tipi di schema nello schema.

SourceUri

Ottiene o imposta il percorso di origine per il file che ha caricato lo schema.

(Ereditato da XmlSchemaObject)
TargetNamespace

Ottiene o imposta l'URI (Uniform Resource Identifier) dello spazio dei nomi di destinazione dello schema.

UnhandledAttributes

Ottiene o imposta gli attributi qualificati che non appartengono allo spazio dei nomi di destinazione dello schema.

Version

Ottiene o imposta la versione dello schema.

Metodi

Nome Descrizione
Compile(ValidationEventHandler, XmlResolver)
Obsoleti.
Obsoleti.
Obsoleti.

Compila xml Schema Object Model (SOM) in informazioni sullo schema per la convalida. Usato per controllare la struttura sintattica e semantica del SOM creato a livello di codice. Il controllo della convalida semantica viene eseguito durante la compilazione.

Compile(ValidationEventHandler)
Obsoleti.
Obsoleti.
Obsoleti.

Compila xml Schema Object Model (SOM) in informazioni sullo schema per la convalida. Usato per controllare la struttura sintattica e semantica del SOM creato a livello di codice. Il controllo della convalida semantica viene eseguito durante la compilazione.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
Read(Stream, ValidationEventHandler)

Legge un XML Schema dal flusso fornito.

Read(TextReader, ValidationEventHandler)

Legge un XML Schema dall'oggetto fornito TextReader.

Read(XmlReader, ValidationEventHandler)

Legge un XML Schema dall'oggetto fornito XmlReader.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)
Write(Stream, XmlNamespaceManager)

Scrive l'XML Schema nell'oggetto fornito Stream utilizzando l'oggetto XmlNamespaceManager specificato.

Write(Stream)

Scrive l'XML Schema nel flusso di dati fornito.

Write(TextWriter, XmlNamespaceManager)

Scrive l'XML Schema nell'oggetto fornito TextWriter.

Write(TextWriter)

Scrive l'XML Schema nell'oggetto fornito TextWriter.

Write(XmlWriter, XmlNamespaceManager)

Scrive l'XML Schema nell'oggetto fornito XmlWriter.

Write(XmlWriter)

Scrive l'XML Schema nell'oggetto fornito XmlWriter.

Si applica a

Vedi anche