This topic has not yet been rated - Rate this topic

XmlSchemaInference.InferSchema Method (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.

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)
public XmlSchemaSet InferSchema(
	XmlReader instanceDocument,
	XmlSchemaSet schemas
)

Parameters

instanceDocument
Type: System.Xml.XmlReader
An XmlReader object containing the XML document to infer a schema from.
schemas
Type: System.Xml.Schema.XmlSchemaSet
An XmlSchemaSet object containing an existing schema used to refine the inferred schema.

Return Value

Type: System.Xml.Schema.XmlSchemaSet
An XmlSchemaSet object containing the inferred schemas.
Exception Condition
XmlException

The XML document is not well-formed.

XmlSchemaInferenceException

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

  • If an XmlSchemaSet object is passed as a parameter and the element upon which the XmlReader object is positioned is defined in one of the schemas in the XmlSchemaSet, the inferred schema is used to refine an existing schema in the XmlSchemaSet parameter with the same target namespace; otherwise, a new schema is inferred for the namespace.

The following example code takes XML document 1 as an input and generates a schema that can validate XML document 1. The example code then takes XML document 2 and refines the schema generated from XML document 1, based on the changes found in XML document 2.

The following is XML document 1.


<?xml version="1.0" encoding="utf-8"?>
<item xmlns="http://www.contoso.com/items" productID="123456789">
	<name>Hammer</name>
	<price>9.95</price>
	<supplierID>1929</supplierID>
</item>


The following is XML document 2.


<?xml version="1.0" encoding="utf-8"?>
<item xmlns="http://www.contoso.com/items" productID="A53-246">
	<name>Paint</name>
	<price>12.50</price>
</item>


The following example code infers a schema from the first XML document contained in reader, and then refines the inferred schema with the changes found in the second XML document contained in reader1. The example code uses the first overloaded InferSchema method to infer the schema, and the second overloaded InferSchema method to refine the existing schema in the XmlSchemaSet object.


XmlReader reader = XmlReader.Create("item1.xml");
XmlReader reader1 = XmlReader.Create("item2.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference inference = new XmlSchemaInference();
schemaSet = inference.InferSchema(reader);

// Display the inferred schema.
Console.WriteLine("Original schema:\n");
foreach (XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/items"))
{
    schema.Write(Console.Out);
}

// Use the additional data in item2.xml to refine the original schema.
schemaSet = inference.InferSchema(reader1, schemaSet);

// Display the refined schema.
Console.WriteLine("\n\nRefined schema:\n");
foreach (XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/items"))
{
    schema.Write(Console.Out);
}


The following schema is the schema inferred from XML document 1.


<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/items" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="price" type="xs:decimal" />
        <xs:element name="supplierID" type="xs:unsignedShort" />
      </xs:sequence>
      <xs:attribute name="productID" type="xs:unsignedInt" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>


The following schema is the refined version of the schema above, based on XML document 2.


<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/items" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="item">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="price" type="xs:decimal" />
        <xs:element minOccurs="0" name="supplierID" type="xs:unsignedShort" />
      </xs:sequence>
      <xs:attribute name="productID" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ