XmlSchemaInference Class

Definition

Infers an XML Schema Definition Language (XSD) schema from an XML document. The XmlSchemaInference class cannot be inherited.

public ref class XmlSchemaInference sealed
public sealed class XmlSchemaInference
type XmlSchemaInference = class
Public NotInheritable Class XmlSchemaInference
Inheritance
XmlSchemaInference

Examples

This example takes an XML file as input, and generates a schema that can validate the example XML.

XmlReader^ reader = XmlReader::Create("contosoBooks.xml");
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
XmlSchemaInference^ schema = gcnew XmlSchemaInference();

schemaSet = schema->InferSchema(reader);

for each (XmlSchema^ s in schemaSet->Schemas())
{
    s->Write(Console::Out);
}
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

The following is the input XML file.

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

The following is the schema inferred from the XML document.

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

Remarks

Important

  • Do not use schemas from unknown or untrusted sources or locations. Doing so will compromise the security of your code.
  • XML schemas (including inline schemas) are inherently vulnerable to denial of service attacks; do not accept them in untrusted scenarios.
  • Schema validation error messages and exceptions may expose sensitive information about the content model or URI paths to the schema file. Be careful not to expose this information to untrusted callers.

The XmlSchemaInference class in the System.Xml.Schema namespace allows you to infer an XML Schema Definition Language (XSD) schema from the structure of an XML document. The XmlSchemaInference class outputs an XML schema that can validate the XML document.

The XmlSchemaInference class supports the W3C XML and XML Schemas standards. It can be used to infer a new schema or to refine an existing schema.

Constructors

XmlSchemaInference()

Initializes a new instance of the XmlSchemaInference class.

Properties

Occurrence

Gets or sets the XmlSchemaInference.InferenceOption value that affects schema occurrence declarations inferred from the XML document.

TypeInference

Gets or sets the XmlSchemaInference.InferenceOption value that affects types inferred from the XML document.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InferSchema(XmlReader)

Infers an XML Schema Definition Language (XSD) schema from the XML document contained in the XmlReader object specified.

InferSchema(XmlReader, XmlSchemaSet)

Infers an XML Schema Definition Language (XSD) schema from the XML document contained in the XmlReader object specified, and refines the inferred schema using an existing schema in the XmlSchemaSet object specified with the same target namespace.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to