XmlSchemaInference.InferSchema Method (XmlReader)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public XmlSchemaSet InferSchema(
	XmlReader instanceDocument


Type: System.Xml.XmlReader

An XmlReader object containing the XML document to infer a schema from.

Return Value

Type: System.Xml.Schema.XmlSchemaSet

An XmlSchemaSet object containing the inferred schemas.

Exception Condition

The XML document is not well-formed.


The XmlReader object is not positioned on the root node or on an element. An error occurs during the schema inference process.

The InferSchema method infers one or more W3C XML Schema Definition Language (XSD) schemas from the XML instance document contained in the XmlReader object specified. If the XML document contains elements and attributes from multiple namespaces, then multiple schemas are generated: one for each namespace used in the document. The primary schema is the schema that can validate the entire XML document, and its target namespace is the same as the namespace of the document element of the XML document.

The following are important notes to consider when using the InferSchema method.

  • The InferSchema method ignores any xsi:type, xsi:schemaLocation, or xsi:noNamespaceSchemaLocation attributes in the XML document.

  • If the XmlReader object is typed, the type information it contains is ignored.

  • If the XmlReader object is positioned on an element that is not the root element of the XML document, a schema is inferred for only that element. If the XmlReader object is not positioned on an element, the Read method is called on the XmlReader parameter until an element is encountered (for example, when NodeType is Element). At this point, the inference process starts from that element. If no element is encountered until the end of the document, an ArgumentException is thrown.

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 = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();

schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())

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>
  <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
    <title>The Gorgias</title>

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:element maxOccurs="unbounded" name="book">
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                        <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:element name="price" type="xs:decimal" />
                        <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" />

.NET Framework
Available since 2.0
Return to top