XmlSchemaInference Classe

Definizione

Deduce uno schema XSD (XML Schema Definition Language) da un documento XML. La XmlSchemaInference classe non può essere ereditata.

public ref class XmlSchemaInference sealed
public sealed class XmlSchemaInference
type XmlSchemaInference = class
Public NotInheritable Class XmlSchemaInference
Ereditarietà
XmlSchemaInference

Esempio

In questo esempio viene accettato un file XML come input e viene generato uno schema in grado di convalidare il codice XML di esempio.

XmlReader reader = XmlReader.Create("contosoBooks.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();

schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())
{
    s.Write(Console.Out);
}
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml")
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
Dim schema As XmlSchemaInference = New XmlSchemaInference()

schemaSet = schema.InferSchema(reader)

For Each s As XmlSchema In schemaSet.Schemas()
    s.Write(Console.Out)
Next

Di seguito è riportato il file XML di input.

<bookstore xmlns="http://www.contoso.com/books">
  <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

Di seguito è riportato lo schema dedotto dal documento XML.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Commenti

Important

  • 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.
  • I messaggi di errore di convalida dello schema e le eccezioni possono esporre informazioni riservate sul modello di contenuto o i percorsi URI al file di schema. Prestare attenzione a non esporre queste informazioni ai chiamanti non attendibili.

La XmlSchemaInference classe nello System.Xml.Schema spazio dei nomi consente di dedurre uno schema XSD (XML Schema Definition Language) dalla struttura di un documento XML. La XmlSchemaInference classe restituisce un XML Schema in grado di convalidare il documento XML.

La XmlSchemaInference classe supporta gli standard W3C XML e XML Schemas. Può essere usato per dedurre un nuovo schema o per perfezionare uno schema esistente.

Costruttori

Nome Descrizione
XmlSchemaInference()

Inizializza una nuova istanza della classe XmlSchemaInference.

Proprietà

Nome Descrizione
Occurrence

Ottiene o imposta il XmlSchemaInference.InferenceOption valore che influisce sulle dichiarazioni di occorrenza dello schema dedotte dal documento XML.

TypeInference

Ottiene o imposta il XmlSchemaInference.InferenceOption valore che influisce sui tipi dedotti dal documento XML.

Metodi

Nome Descrizione
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)
InferSchema(XmlReader, XmlSchemaSet)

Deduce uno schema XSD (XML Schema Definition Language) dal documento XML contenuto nell'oggetto XmlReader specificato e affina lo schema dedotto usando uno schema esistente nell'oggetto XmlSchemaSet specificato con lo stesso spazio dei nomi di destinazione.

InferSchema(XmlReader)

Deduce uno schema XSD (XML Schema Definition Language) dal documento XML contenuto nell'oggetto XmlReader specificato.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a