为什么在 C# 中加载的数据集和 ReadXml 为空

本文关键字:数据集 ReadXml 为空 加载 为什么 | 更新日期: 2023-09-27 18:32:44

我正在使用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"时,数据集是空的。 我做错了什么?

为什么在 C# 中加载的数据集和 ReadXml 为空

可悲的是,您的代码片段似乎可以在我的机器上运行。 你确定你在初始化之后签入初始化吗?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 的单行。

我终于想通了。

架构现在如下所示:

<?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运行它来添加所有这些东西,否则结果充其量是不可预测的。

可能是以下内容中的空格:

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

从您的 XML 文件?