【问题标题】:Problem with DataSet schema interpreted from xsd从 xsd 解释的 DataSet 架构的问题
【发布时间】:2011-02-18 03:06:31
【问题描述】:

我有一个 xsd 文件,它描述了一个 DataSet 架构,我用它来将我的 DataSet 作为 xml 文件读/写到磁盘。我不是手写schema,而是手写xml文件,从xml文件推断schema,然后写出xsd schema。 (我对此很陌生......)

不管怎样,这里是架构(这里有一些 amazon.com 的东西):

<?xml version="1.0" standalone="yes"?>
<xs:schema id="Items" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Items" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="Item">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="ASIN" type="xs:string" />
          <xs:element name="Title" type="xs:string" />
          <xs:element name="Offer" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="MerchantName" type="xs:string" />
                <xs:element name="Price" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

当 DataSet 读取架构时,它会创建两个表,通过它创建的名为 Item_Id 的即席键链接,该键对于每个 Item 都是唯一的,并映射到 Offer。所以我得到一个带有列(ASIN、Title、Item_Id)的 Item 表和一个带有列(MerchantName、Price、Item_Id)的 Offer 表。

这里的问题是 ASIN 已经是商品的唯一标识符,因此架构导入过程引入了一些冗余,使代码比它需要的更尴尬。如何更改此架构以最终得到 2 个表(ASIN、Title)和(ASIN、MerchantName、Price)?

谢谢!

【问题讨论】:

  • 没有 C#.NET 这样的东西。只有 C#。

标签: c# xml xsd schema dataset


【解决方案1】:

XSD 架构会自动为标识单元创建一个随机 Item_Id,除非您声明了主键。

在您的 xsd 架构中分配主键丢失。

<xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//Item" />            
      <xs:field xpath="ASIN"/>
</xs:unique>

这样,您的数据集中将没有 Item_Id,并且您的架构必须如下所示:

    <?xml version="1.0" standalone="yes"?>
    <xs:schema id="Items" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"       xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Items" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Item">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ASIN" type="xs:string"  />
              <xs:element name="Title" type="xs:string" />
              <xs:element name="Offer" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="MerchantName" type="xs:string" />
                    <xs:element name="Price" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
                <xs:unique name="Constraint1" msdata:PrimaryKey="true">
                  <xs:selector xpath=".//Item" />            
                  <xs:field xpath="ASIN"/>
                </xs:unique>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint2" msdata:PrimaryKey="true">
      <xs:selector xpath=".//Item" />
      <xs:field xpath="ASIN" />
    </xs:unique>
  </xs:element>
</xs:schema>

【讨论】:

    猜你喜欢
    • 2015-04-20
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2018-12-04
    • 2012-02-06
    相关资源
    最近更新 更多