DataTable ReadXmlSchema和ReadXml导致错误

时间:2010-04-07 23:37:53

标签: xml xsd datatable

我在使用DataTable的ReadXmlSchema和ReadXml方法时遇到了一些麻烦。我收到错误“DataTable不支持来自Xml的模式推断”。

代码段: 我试过了

Table.ReadXmlSchema(new StringReader(File.ReadAllText(XsdFilePath)));
Table.ReadXml(new StringReader(File.ReadAllText(XmlFilePath)));

Table.ReadXmlSchema(XsdFilePath);
Table.ReadXml(XmlFilePath);

Xml片段:

<ScreenSets>
  <ScreenSet id="Credit 1">
    <Screen xmlFile="sb-credit1.en.xml" tabText="Recommendation" isCached="false">
      <Buttons>
        <Button id="btnClosePresentation"/>
      </Buttons>
    </Screen>
  </ScreenSet>
  <ScreenSet id="Credit 2">
    <Screen xmlFile="sb-credit2.en.xml" tabText="Recommendation" isCached="false">
      <Buttons>
        <Button id="btnClosePresentation"/>
      </Buttons>
    </Screen>
  </ScreenSet>
  <ScreenSet id="Credit 3">
    <Screen xmlFile="sb-credit3.en.xml" tabText="Recommendation" isCached="false">
      <Buttons>
        <Button id="btnClosePresentation"/>
      </Buttons>
    </Screen>
  </ScreenSet>
</ScreenSets>

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ScreenSets">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="ScreenSet">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Screen">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Buttons">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element maxOccurs="unbounded" name="Button">
                            <xs:complexType>
                              <xs:attribute name="id" type="xs:string" use="required" />
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="xmlFile" type="xs:string" use="required" />
                  <xs:attribute name="tabText" type="xs:string" use="required" />
                  <xs:attribute name="isCached" type="xs:boolean" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

3 个答案:

答案 0 :(得分:1)

您的架构和数据不代表单个表,也不是.net数据对象(如DataSet和DataTable)可识别的格式。

最好的办法是在设计器中设计一个数据集,然后调用dataset.WriteXmlSchema / Data来获得你的示例.xsd。

答案 1 :(得分:1)

尝试使用DataSet,并从数据集中提取DataTable 例如:

XML = @"
<Records>
  <Record>
    <Col1>0</Col1>
    <Col2>ABC</Col2>
  </Record>
  <Record>
    <Col1>1</Col1>
    <Col2>CDE</Col2>
  </Record>
</Records>";

DataSet DS = new DataSet();
DS.ReadXml(new StringReader(XML));

DataTable DT=DS.Tables["Records"];

答案 2 :(得分:0)

如果你想要数据库中的xml文件你可以使用,dataset.WriteXml(“filepath”,XmlWriteMode.WriteSchema)

然后,将文件读入表中, dataset.ReadXml(“filepath”,XmlReadMode.ReadSchema) 表= dataset.tables(0)

如果您已将表的架构填充到xmlfileschema的架构中。