編集

次の方法で共有


XmlSchemaInference.InferSchema Method

Definition

Infers an XML Schema Definition Language (XSD) schema from the XML document specified.

Overloads

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.

InferSchema(XmlReader)

Source:
Infer.cs
Source:
Infer.cs
Source:
Infer.cs

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

public:
 System::Xml::Schema::XmlSchemaSet ^ InferSchema(System::Xml::XmlReader ^ instanceDocument);
public System.Xml.Schema.XmlSchemaSet InferSchema (System.Xml.XmlReader instanceDocument);
member this.InferSchema : System.Xml.XmlReader -> System.Xml.Schema.XmlSchemaSet
Public Function InferSchema (instanceDocument As XmlReader) As XmlSchemaSet

Parameters

instanceDocument
XmlReader

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

Returns

An XmlSchemaSet object containing the inferred schemas.

Exceptions

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.

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

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.

Applies to

InferSchema(XmlReader, XmlSchemaSet)

Source:
Infer.cs
Source:
Infer.cs
Source:
Infer.cs

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.

public:
 System::Xml::Schema::XmlSchemaSet ^ InferSchema(System::Xml::XmlReader ^ instanceDocument, System::Xml::Schema::XmlSchemaSet ^ schemas);
public System.Xml.Schema.XmlSchemaSet InferSchema (System.Xml.XmlReader instanceDocument, System.Xml.Schema.XmlSchemaSet schemas);
member this.InferSchema : System.Xml.XmlReader * System.Xml.Schema.XmlSchemaSet -> System.Xml.Schema.XmlSchemaSet
Public Function InferSchema (instanceDocument As XmlReader, schemas As XmlSchemaSet) As XmlSchemaSet

Parameters

instanceDocument
XmlReader

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

schemas
XmlSchemaSet

An XmlSchemaSet object containing an existing schema used to refine the inferred schema.

Returns

An XmlSchemaSet object containing the inferred schemas.

Exceptions

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.

Examples

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 = gcnew XmlSchemaSet();
XmlSchemaInference^ inference = gcnew XmlSchemaInference();
schemaSet = inference->InferSchema(reader);

// Display the inferred schema.
Console::WriteLine("Original schema:\n");
for each (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");
for each (XmlSchema^ schema in schemaSet->Schemas("http://www.contoso.com/items"))
{
    schema->Write(Console::Out);
}
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);
}
Dim reader As XmlReader = XmlReader.Create("item1.xml")
Dim reader1 As XmlReader = XmlReader.Create("item2.xml")
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
Dim inference As XmlSchemaInference = New XmlSchemaInference()
schemaSet = inference.InferSchema(reader)

' Display the inferred schema.
Console.WriteLine("Original schema:\n")
For Each schema As XmlSchema In schemaSet.Schemas("http://www.contoso.com/items")
    schema.Write(Console.Out)
Next

' 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")
For Each schema As XmlSchema In schemaSet.Schemas("http://www.contoso.com/items")
    schema.Write(Console.Out)
Next

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>

Remarks

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.

Applies to