ReadXmlSchema不带列

时间:2015-05-06 13:20:31

标签: c# sql-server xml ado.net

我有一个导出/导入工具,可以将存储过程中的记录写入和读取.xml文件。它工作正常,直到我更改了proc:我在proc结果集中添加了一个新列,将原始表连接到一个新列。

到目前为止,导出工作正常,但是当我尝试导入生成的文件时,它会引发datatable does not match to any datatable in source错误。

这是我尝试导入的真实.xml文件:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="RISKS">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="RiskID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />
                <xs:element name="ProjectID" type="xs:int" minOccurs="0" />
                <xs:element name="ProjectPhase" type="xs:int" minOccurs="0" />
                <xs:element name="CompanyID" type="xs:int" />
                <xs:element name="SchemaID" type="xs:int" />
                <xs:element name="Sequence" type="xs:int" />
                <xs:element name="Probability" type="xs:unsignedByte" />
                <xs:element name="Impact" type="xs:unsignedByte" />
                <xs:element name="RiskScore" msdata:ReadOnly="true" type="xs:unsignedByte" minOccurs="0" />
                <xs:element name="RiskStatus" type="xs:unsignedByte" />
                <xs:element name="RBSNodeID" type="xs:int" />
                <xs:element name="ActionCount" type="xs:short" />
                <xs:element name="Created" type="xs:dateTime" />
                <xs:element name="LimitDate" type="xs:dateTime" minOccurs="0" />
                <xs:element name="Changed" type="xs:dateTime" />
                <xs:element name="ChangedBy" type="xs:int" />
                <xs:element name="PublishedBy" type="xs:int" minOccurs="0" />
                <xs:element name="ExpectedCostValue" type="xs:decimal" minOccurs="0" />
                <xs:element name="PlannedCostPendingActions" type="xs:decimal" minOccurs="0" />
                <xs:element name="PlannedCostMonitoringActions" type="xs:decimal" minOccurs="0" />
                <xs:element name="PlannedCostFinishedActions" type="xs:decimal" minOccurs="0" />
                <xs:element name="RealCostFinishedActions" type="xs:decimal" minOccurs="0" />
                <xs:element name="PlannedCostSum" msdata:ReadOnly="true" type="xs:decimal" minOccurs="0" />
                <xs:element name="ProjectedCostSum" msdata:ReadOnly="true" type="xs:decimal" minOccurs="0" />
                <xs:element name="IsImported" type="xs:boolean" />
                <xs:element name="IsRealized" type="xs:boolean" />
                <xs:element name="CustomCode" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="100" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="ExternalCustomCode" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="100" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="Description">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="2147483647" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="Description2" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="2147483647" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="AdditionalInformation" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="2147483647" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="WorkPackage" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="2147483647" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="ExpectedCostReason" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="2147483647" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="ProjectNickname">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="50" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <RISKS>
    <RiskID>1001</RiskID>
    <ProjectID>1</ProjectID>
    <ProjectPhase>1</ProjectPhase>
    <CompanyID>1</CompanyID>
    <SchemaID>2</SchemaID>
    <Sequence>126</Sequence>
    <Probability>1</Probability>
    <Impact>1</Impact>
    <RiskScore>1</RiskScore>
    <RiskStatus>0</RiskStatus>
    <RBSNodeID>45</RBSNodeID>
    <ActionCount>0</ActionCount>
    <Created>2015-04-23T22:11:08.723-03:00</Created>
    <LimitDate>2015-04-23T00:00:00-03:00</LimitDate>
    <Changed>2015-04-23T22:11:08.807-03:00</Changed>
    <ChangedBy>1073741823</ChangedBy>
    <ExpectedCostValue>0.0000</ExpectedCostValue>
    <PlannedCostPendingActions>0.0000</PlannedCostPendingActions>
    <PlannedCostMonitoringActions>0.0000</PlannedCostMonitoringActions>
    <PlannedCostFinishedActions>0.0000</PlannedCostFinishedActions>
    <RealCostFinishedActions>0.0000</RealCostFinishedActions>
    <PlannedCostSum>0.0000</PlannedCostSum>
    <ProjectedCostSum>0.0000</ProjectedCostSum>
    <IsImported>false</IsImported>
    <IsRealized>false</IsRealized>
    <CustomCode>RR.PROJ_A.0126</CustomCode>
    <Description>asd</Description>
    <Description2>asd</Description2>
    <AdditionalInformation />
    <WorkPackage />
    <ExpectedCostReason />
    <ProjectNickname>PROJ_AA</ProjectNickname>
  </RISKS>
</NewDataSet>

这是我用来导入.xml文件的代码:

_tbl = new DataTable();
_tbl.ReadXmlSchema("MyFile.xml");
_tbl.TableName = "RISKS";
_tbl.ReadXml("MyFile.xml");

在我阅读架构之后,在尝试加载文件内容之前,我可以看到表格列为空,因此ReadXmlSchema没有正确读取架构,但我没有&#39 ; t知道原因,因为ReadXmlSchema没有引起任何错误。

新列是ProjectNickname,最后一列。

修改

伙计们,我在这里发现了真正的问题,它既不是ReadXmlSchema也不是XML文件:实际上这个代码是在循环中调用的,它从.zip文件中提取5个条目。这些条目应按特定条件排序,崩溃程序的.zip条目的顺序不同。所以上面的代码从表RISKCOMMENTS中读取模式,将表名设置为RISK,当它尝试读取xml时,它显然会崩溃......

1 个答案:

答案 0 :(得分:0)

尝试在ReadXml()上使用重载:

DataSet dataSet = new DataSet();
dataTable.BeginLoadData();
dataSet.ReadXml("MyFile.xml", XmlReadMode.ReadSchema);
dataTable.EndLoadData();

欲了解更多信息: https://msdn.microsoft.com/en-us/library/fx29c3yd%28v=vs.110%29.aspx

还要确保所有必需的元素都在XML XSD中。