如何获取内联xml架构?

时间:2018-04-26 04:10:24

标签: c# xml

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
  <xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" id="NewDataSet">
    <xs:element msdata:IsDataSet="true" msdata:UseCurrentLocale="true" name="NewDataSet">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded" minOccurs="0">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" msdata:DateTimeMode="Unspecified" name="MAXTIME" type="xs:dateTime"/>
                <xs:element minOccurs="0" name="X" type="xs:string"/>
                <xs:element minOccurs="0" name="Y" type="xs:string"/>
                <xs:element minOccurs="0" name="Z" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table1>
    <MAXTIME/>
    <X>123</X>
    <Y>DDo</Y>
    <Z>Cri</Z>
  </Table1>
</NewDataSet>

我想从上面的xml文件中获取xml架构,但是当我使用

        XmlReader reader = XmlReader.Create(xmlPath);
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        XmlSchemaInference schema = new XmlSchemaInference();
        schemaSet = schema.InferSchema(reader);

失败了:

The supplied xml instance is a schema or contains an inline schema. This class cannot infer a schema for a schema.

那么我应该如何获得它来获取架构

1 个答案:

答案 0 :(得分:0)

使用此:

DataSet dset = new DataSet();
dset.ReadXml("xmlFile.xml",XmlReadMode.IgnoreSchema);
var _schema = dset.GetXmlSchema();