【问题标题】:Why is a DataSet loaded in C# with ReadXml empty?为什么在 C# 中加载的 DataSet 中 ReadXml 为空?
【发布时间】:2013-10-01 12:53:13
【问题描述】:

我正在使用 C# 4.5/Visual Studio 2012 尝试将 XML 文件加载到数据集。我在这里阅读了说明:

http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx

我正在加载一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>

<UnitTestDataSet xmlns="https://rebuildinghomes.codeplex.com/" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="https://rebuildinghomes.codeplex.com/ UnitTestLoadData.xsd">
  <Person>
    <PersonId>1111</PersonId>
    <FirstName>Unit</FirstName>
    <LastName>Tester</LastName>
  </Person>
</UnitTestDataSet>

使用这样的架构:

<?xml version="1.0" encoding="utf-8" ?>

<xs:schema id="RebuildingUnitTestData"
            targetNamespace="https://rebuildinghomes.codeplex.com/"
            xmlns:u="https://rebuildinghomes.codeplex.com/"
            elementFormDefault="qualified"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:complexType name="PersonType">
    <xs:sequence>
      <xs:element name="PersonId" type="xs:int"
                   minOccurs="1" maxOccurs="1" />
      <xs:element name="FirstName" type="xs:string"
                   minOccurs="1" maxOccurs="1" />
      <xs:element name="LastName" type="xs:string"
                   minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

使用以下 C# 代码:

        //Load init/expected
        DataSet init = new DataSet();

        init.ReadXmlSchema("RebuildingModel/Data/UnitTestLoadData.xsd");

        init.ReadXml("RebuildingModel/Data/PersonUnitTest.xml", XmlReadMode.ReadSchema);

但是,当我在调试器中查看“init”时,DataSet 是空的。我做错了什么?

【问题讨论】:

  • 我运行了你的代码,它似乎可以工作。数据集是空的,数据集中没有表是什么意思?
  • 有趣,这对我也不起作用。只有当我在您的示例中将 XmlReadMode 更改为 XmlReadMode.Auto 时,该表才会填充到数据集中。

标签: c# xml dataset


【解决方案1】:

可能是下面的空格:

xsi:schemaLocation="https://rebuildinghomes.codeplex.com/ UnitTestLoadData.xsd">

来自您的 xml 文件?

【讨论】:

【解决方案2】:

遗憾的是,您的 sn-p 代码似乎可以在我的机器上运行。您确定要检查 init AFTER init.ReadXml() 语句执行吗?

在下面的 Visual Studio 即时窗口中查看我的输出:

ds.Tables[0]
{Person}
    base {System.ComponentModel.MarshalByValueComponent}: {Person}
    CaseSensitive: false
    ChildRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Columns: {System.Data.DataColumnCollection}
    Constraints: {System.Data.ConstraintCollection}
    DataSet: {System.Data.DataSet}
    DefaultView: {System.Data.DataView}
    DisplayExpression: ""
    ExtendedProperties: Count = 0
    HasErrors: false
    IsInitialized: true
    Locale: {en-US}
    MinimumCapacity: 50
    Namespace: "https://rebuildinghomes.codeplex.com/"
    ParentRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Prefix: ""
    PrimaryKey: {System.Data.DataColumn[0]}
    RemotingFormat: Xml
    Rows: {System.Data.DataRowCollection}
    Site: null
    TableName: "Person"
ds.Tables[0].Rows
{System.Data.DataRowCollection}
    base {System.Data.InternalDataCollectionBase}: {System.Data.DataRowCollection}
    Count: 1

我的数据集根据您的 XML 填充了一个包含单行的表。

【讨论】:

  • 我很确定。我有一个异常要检查它,它总是被抛出:init = new DataSet(); init.ReadXmlSchema("RebuildingModel/Data/UnitTestLoadData.xsd"); init.ReadXml("RebuildingModel/Data/PersonUnitTest.xml", XmlReadMode.ReadSchema); if (init.Tables.Count &lt; 1) throw new Exception("Didn't load the unit test data!");
  • 有趣的事实....如果我删除 XmlReadMode.ReadSchema 参数,DataSet 会被填充,但其中唯一的表名为“Table1”。
【解决方案3】:

我终于明白了。

架构现在如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UnitTestDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections />
        <Tables />
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="UnitTestDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="UnitTestDataSet" msprop:Generator_UserDSName="UnitTestDataSet">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Person" msprop:Generator_TableClassName="PersonDataTable" msprop:Generator_TableVarName="tablePerson" msprop:Generator_TablePropName="Person" msprop:Generator_RowDeletingName="PersonRowDeleting" msprop:Generator_RowChangingName="PersonRowChanging" msprop:Generator_RowEvHandlerName="PersonRowChangeEventHandler" msprop:Generator_RowDeletedName="PersonRowDeleted" msprop:Generator_UserTableName="Person" msprop:Generator_RowChangedName="PersonRowChanged" msprop:Generator_RowEvArgName="PersonRowChangeEvent" msprop:Generator_RowClassName="PersonRow">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PersonId" msprop:Generator_ColumnVarNameInTable="columnPersonId" msprop:Generator_ColumnPropNameInRow="PersonId" msprop:Generator_ColumnPropNameInTable="PersonIdColumn" msprop:Generator_UserColumnName="PersonId" type="xs:int" />
              <xs:element name="FirstName" msprop:Generator_ColumnVarNameInTable="columnFirstName" msprop:Generator_ColumnPropNameInRow="FirstName" msprop:Generator_ColumnPropNameInTable="FirstNameColumn" msprop:Generator_UserColumnName="FirstName" type="xs:string" />
              <xs:element name="LastName" msprop:Generator_ColumnVarNameInTable="columnLastName" msprop:Generator_ColumnPropNameInRow="LastName" msprop:Generator_ColumnPropNameInTable="LastNameColumn" msprop:Generator_UserColumnName="LastName" type="xs:string" />
              <xs:element name="HomePhoneNbr" msprop:Generator_ColumnVarNameInTable="columnHomePhoneNbr" msprop:Generator_ColumnPropNameInRow="HomePhoneNbr" msprop:Generator_ColumnPropNameInTable="HomePhoneNbrColumn" msprop:Generator_UserColumnName="HomePhoneNbr" type="xs:string" minOccurs="0" />
              <xs:element name="CellPhoneNbr" msprop:Generator_ColumnVarNameInTable="columnCellPhoneNbr" msprop:Generator_ColumnPropNameInRow="CellPhoneNbr" msprop:Generator_ColumnPropNameInTable="CellPhoneNbrColumn" msprop:Generator_UserColumnName="CellPhoneNbr" type="xs:string" minOccurs="0" />
              <xs:element name="StreetAddress" msprop:Generator_ColumnVarNameInTable="columnStreetAddress" msprop:Generator_ColumnPropNameInRow="StreetAddress" msprop:Generator_ColumnPropNameInTable="StreetAddressColumn" msprop:Generator_UserColumnName="StreetAddress" type="xs:string" minOccurs="0" />
              <xs:element name="AptNbr" msprop:Generator_ColumnVarNameInTable="columnAptNbr" msprop:Generator_ColumnPropNameInRow="AptNbr" msprop:Generator_ColumnPropNameInTable="AptNbrColumn" msprop:Generator_UserColumnName="AptNbr" type="xs:string" minOccurs="0" />
              <xs:element name="City" msprop:Generator_ColumnVarNameInTable="columnCity" msprop:Generator_ColumnPropNameInRow="City" msprop:Generator_ColumnPropNameInTable="CityColumn" msprop:Generator_UserColumnName="City" type="xs:string" minOccurs="0" />
              <xs:element name="County" msprop:Generator_ColumnVarNameInTable="columnCounty" msprop:Generator_ColumnPropNameInRow="County" msprop:Generator_ColumnPropNameInTable="CountyColumn" msprop:Generator_UserColumnName="County" type="xs:string" minOccurs="0" />
              <xs:element name="StateProvCd" msprop:Generator_ColumnVarNameInTable="columnStateProvCd" msprop:Generator_ColumnPropNameInRow="StateProvCd" msprop:Generator_ColumnPropNameInTable="StateProvCdColumn" msprop:Generator_UserColumnName="StateProvCd" type="xs:string" minOccurs="0" />
              <xs:element name="PostalCode" msprop:Generator_ColumnVarNameInTable="columnPostalCode" msprop:Generator_ColumnPropNameInRow="PostalCode" msprop:Generator_ColumnPropNameInTable="PostalCodeColumn" msprop:Generator_UserColumnName="PostalCode" type="xs:string" minOccurs="0" />
              <xs:element name="CountryCode" msprop:Generator_ColumnVarNameInTable="columnCountryCode" msprop:Generator_ColumnPropNameInRow="CountryCode" msprop:Generator_ColumnPropNameInTable="CountryCodeColumn" msprop:Generator_UserColumnName="CountryCode" type="xs:string" minOccurs="0" />
              <xs:element name="NumberOfYearsAtAddr" msprop:Generator_ColumnVarNameInTable="columnNumberOfYearsAtAddr" msprop:Generator_ColumnPropNameInRow="NumberOfYearsAtAddr" msprop:Generator_ColumnPropNameInTable="NumberOfYearsAtAddrColumn" msprop:Generator_UserColumnName="NumberOfYearsAtAddr" type="xs:int" minOccurs="0" />
              <xs:element name="EmailAddress" msprop:Generator_ColumnVarNameInTable="columnEmailAddress" msprop:Generator_ColumnPropNameInRow="EmailAddress" msprop:Generator_ColumnPropNameInTable="EmailAddressColumn" msprop:Generator_UserColumnName="EmailAddress" type="xs:string" minOccurs="0" />
              <xs:element name="GenderCode" msprop:Generator_ColumnVarNameInTable="columnGenderCode" msprop:Generator_ColumnPropNameInRow="GenderCode" msprop:Generator_ColumnPropNameInTable="GenderCodeColumn" msprop:Generator_UserColumnName="GenderCode" type="xs:string" minOccurs="0" />
              <xs:element name="EthnicityCode" msprop:Generator_ColumnVarNameInTable="columnEthnicityCode" msprop:Generator_ColumnPropNameInRow="EthnicityCode" msprop:Generator_ColumnPropNameInTable="EthnicityCodeColumn" msprop:Generator_UserColumnName="EthnicityCode" type="xs:int" minOccurs="0" />
              <xs:element name="BirthDate" msprop:Generator_ColumnVarNameInTable="columnBirthDate" msprop:Generator_ColumnPropNameInRow="BirthDate" msprop:Generator_ColumnPropNameInTable="BirthDateColumn" msprop:Generator_UserColumnName="BirthDate" type="xs:date" minOccurs="0" />
              <xs:element name="ResidentStatus" msprop:Generator_ColumnVarNameInTable="columnResidentStatus" msprop:Generator_ColumnPropNameInRow="ResidentStatus" msprop:Generator_ColumnPropNameInTable="ResidentStatusColumn" msprop:Generator_UserColumnName="ResidentStatus" type="xs:int" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//Person" />
      <xs:field xpath="PersonId" />
    </xs:unique>
  </xs:element>
</xs:schema>

您会注意到,最大的变化是 Visual Studio 添加的所有无关垃圾。显然,.NET 需要其中的一些才能正确加载数据,最显着的是主键注释。所以这个故事的寓意:如果你要从 XML 加载一个数据集,通过 Visual Studio 创建一个模式,或者准备通过 XSLT 运行它来添加所有这些东西,否则结果将是不可预测的,充其量。

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 2020-08-24
    • 2013-06-28
    • 2013-01-02
    • 1970-01-01
    • 2013-04-17
    • 2013-11-24
    • 2022-01-26
    • 2014-08-06
    相关资源
    最近更新 更多